From 279e0f8652682c10387ba75326d463d801a4c22a Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 6 Mar 2016 13:33:50 +0100 Subject: [PATCH 1/8] Added suport for in valve --- app/client/state.html | 1 + app/server/api.js | 27 +++++++++++++++++--- controller/config/__init__.py | 3 ++- controller/state/changer.py | 47 +++++++++++++++++++++++++---------- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/app/client/state.html b/app/client/state.html index 2aacea9..2c32d6e 100644 --- a/app/client/state.html +++ b/app/client/state.html @@ -11,6 +11,7 @@ {{/with}}
Otpusni ventil: {{pretty_valve state.out_valve }}
+
Ulazni ventil/pumpa: {{pretty_valve state.in_valve }}
Zadnja komunikacija: {{ last_communication_time }}
{{/with}}
diff --git a/app/server/api.js b/app/server/api.js index 2c0cf3d..d1fc589 100644 --- a/app/server/api.js +++ b/app/server/api.js @@ -9,7 +9,7 @@ Api.addRoute('sensorData', { authRequired: false }, { post: function() { - console.log("Body params", this.bodyParams); + reactToSensorData(this.bodyParams); SensorData.insert({ temperatureValue: parseFloat(this.bodyParams.temperatureValue), humidityValue: parseFloat(this.bodyParams.humidityValue), @@ -23,16 +23,34 @@ Api.addRoute('sensorData', { }); +function reactToSensorData(nextSensorReading) { + var controllerId = nextSensorReading.controllerId; + var state = stateOrDefault(controllerId).state; + var shouldStartPumping = (parseInt(nextSensorReading.tankFull) === 0 && ( !state.in_valve || state.in_valve === 'closed' ) ); + + if(shouldStartPumping) { + ControllerState.update({ + controller_id: controllerId + }, { + '$set': { + 'state.in_valve': 'opening', + 'time': new Date(), + 'set_by': 'server' + } + }); + } +} + Api.addRoute('state/:id', { authRequired: false }, { post: function() { - console.log("Body params", this.bodyParams); return ControllerState.update({ controller_id: this.urlParams.id }, { '$set': { 'state.out_valve': this.bodyParams.out_valve, + 'state.in_valve': this.bodyParams.in_valve, 'time': new Date(), 'set_by': 'client' } @@ -43,6 +61,7 @@ Api.addRoute('state/:id', { } }); + function stateOrDefault(id) { var stateEntry = ControllerState.findOne({ controller_id: id, @@ -52,13 +71,13 @@ function stateOrDefault(id) { stateEntry = ControllerState.insert({ controller_id: id, state: { - out_valve: 'closed' + out_valve: 'closed', + in_valve: 'closed' }, time: new Date(), config: { draining_period_amount: 5, draining_period_unit: 'minutes' - }, set_by: 'server' }); diff --git a/controller/config/__init__.py b/controller/config/__init__.py index 6a6c197..e018b03 100644 --- a/controller/config/__init__.py +++ b/controller/config/__init__.py @@ -3,7 +3,8 @@ GPIO_PIN_DHT = 4 # BCM SENSORDATA_URL = 'http://tfm.meteor.com/api/v1.0/sensorData' GPIO_PIN_TANKFULL = 20 # BCM -GPIO_PIN_VALVE = 21 # BCM +GPIO_PIN_OUT_VALVE = 21 # BCM +GPIO_PIN_IN_VALVE = 18 # BCM API_BASE_URL = 'http://tfm.meteor.com/api/v1.0' CONTROLLER_ID = '120' # every controller must have a different one STATE_FILE = '/var/run/controller_state' diff --git a/controller/state/changer.py b/controller/state/changer.py index 45e3e5f..956b1f9 100644 --- a/controller/state/changer.py +++ b/controller/state/changer.py @@ -7,29 +7,50 @@ class Changer(object): self.local_state = local_state self.remote_state = remote_state GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme - GPIO.setup(config.GPIO_PIN_VALVE, GPIO.OUT) + GPIO.setup(config.GPIO_PIN_OUT_VALVE, GPIO.OUT) + GPIO.setup(config.GPIO_PIN_IN_VALVE, GPIO.OUT) - self.states = { - 'opening': self.open_valve, - 'closing': self.close_valve, - 'open': self.open_valve, - 'closed': self.close_valve + self.out_valve_states = { + 'opening': self.open_out_valve, + 'closing': self.close_out_valve, + 'open': self.open_out_valve, + 'closed': self.close_out_valve + } + self.in_valve_states = { + 'opening': self.open_in_valve, + 'closing': self.close_in_valve, + 'open': self.open_in_valve, + 'closed': self.close_in_valve } def process_change(self): self.validate_states() - change = self.states.get(self.remote_state['out_valve'], None ) - if change is not None: - change() + + out_valve_change = self.out_valve_states.get(self.remote_state['out_valve'], None ) + if out_valve_change is not None: + out_valve_change() + + in_valve_change = self.in_valve_states.get(self.remote_state['in_valve'], None ) + if in_valve_change is not None: + in_valve_change() + return self.local_state - def open_valve(self): - GPIO.output(config.GPIO_PIN_VALVE, GPIO.HIGH) + def open_in_valve(self): + GPIO.output(config.GPIO_PIN_IN_VALVE, GPIO.HIGH) + self.local_state['in_valve'] = 'open' + + def close_in_valve(self): + GPIO.output(config.GPIO_PIN_IN_VALVE, GPIO.LOW) + self.local_state['in_valve'] = 'closed' + + def open_out_valve(self): + GPIO.output(config.GPIO_PIN_OUT_VALVE, GPIO.HIGH) self.local_state['out_valve'] = 'open' - def close_valve(self): - GPIO.output(config.GPIO_PIN_VALVE, GPIO.LOW) + def close_out_valve(self): + GPIO.output(config.GPIO_PIN_OUT_VALVE, GPIO.LOW) self.local_state['out_valve'] = 'closed' def validate_states(self): -- 2.47.3 From 8d9e42c14782112d9d95be8afac5fcb721322d08 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 6 Mar 2016 13:40:07 +0100 Subject: [PATCH 2/8] reformatted code --- app/server/api.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/server/api.js b/app/server/api.js index d1fc589..dc51e30 100644 --- a/app/server/api.js +++ b/app/server/api.js @@ -26,9 +26,9 @@ Api.addRoute('sensorData', { function reactToSensorData(nextSensorReading) { var controllerId = nextSensorReading.controllerId; var state = stateOrDefault(controllerId).state; - var shouldStartPumping = (parseInt(nextSensorReading.tankFull) === 0 && ( !state.in_valve || state.in_valve === 'closed' ) ); - - if(shouldStartPumping) { + var shouldStartPumping = (parseInt(nextSensorReading.tankFull) === 0 && (!state.in_valve || state.in_valve === 'closed')); + + if (shouldStartPumping) { ControllerState.update({ controller_id: controllerId }, { -- 2.47.3 From c47e1be364cba0ce39e09774f405090ece92da9b Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 6 Mar 2016 14:07:33 +0100 Subject: [PATCH 3/8] Finished in valve support --- app/client/state.js | 1 - app/server/api.js | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/client/state.js b/app/client/state.js index 6264cd0..ed7e21a 100644 --- a/app/client/state.js +++ b/app/client/state.js @@ -1,7 +1,6 @@ function controller_state() { var controllerId = Session.get('controller_id'); result = ControllerState.findOne({}); - console.log("jupiii", result); if (!result) { result = {} }; diff --git a/app/server/api.js b/app/server/api.js index dc51e30..1bdcd08 100644 --- a/app/server/api.js +++ b/app/server/api.js @@ -24,6 +24,7 @@ Api.addRoute('sensorData', { function reactToSensorData(nextSensorReading) { + console.log("reacting to sensor"); var controllerId = nextSensorReading.controllerId; var state = stateOrDefault(controllerId).state; var shouldStartPumping = (parseInt(nextSensorReading.tankFull) === 0 && (!state.in_valve || state.in_valve === 'closed')); @@ -39,12 +40,26 @@ function reactToSensorData(nextSensorReading) { } }); } + var shouldStopPumping = parseInt(nextSensorReading.tankFull) === 1 && (state.in_valve === 'open' || state.in_valve === 'opening'); + + if (shouldStopPumping) { + ControllerState.update({ + controller_id: controllerId + }, { + '$set': { + 'state.in_valve': 'closing', + 'time': new Date(), + 'set_by': 'server' + } + }); + } } Api.addRoute('state/:id', { authRequired: false }, { post: function() { + console.log("setting state", this.bodyParams); return ControllerState.update({ controller_id: this.urlParams.id }, { -- 2.47.3 From 99fdc768fc847d2c7407eea490f8a712d7c3692a Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 6 Mar 2016 14:13:25 +0100 Subject: [PATCH 4/8] config/init.py removed --- controller/config/{__init__.py => copy__init__.py.example} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename controller/config/{__init__.py => copy__init__.py.example} (100%) diff --git a/controller/config/__init__.py b/controller/config/copy__init__.py.example similarity index 100% rename from controller/config/__init__.py rename to controller/config/copy__init__.py.example -- 2.47.3 From ed8267b6ab2d69c483bd323cf73fb35c086c011f Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 6 Mar 2016 14:14:26 +0100 Subject: [PATCH 5/8] ignore existing config --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index ba74660..8fde707 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# configuration +controller/config/__init__.py + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] -- 2.47.3 From 28e5f7f1f0ba0ac7059705c13a8c2b946ba5b9d7 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 6 Mar 2016 14:46:42 +0100 Subject: [PATCH 6/8] safe key getting from the response --- controller/state/changer.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/controller/state/changer.py b/controller/state/changer.py index 956b1f9..550f41e 100644 --- a/controller/state/changer.py +++ b/controller/state/changer.py @@ -24,14 +24,20 @@ class Changer(object): 'closed': self.close_in_valve } + def safe_remote_state(key): + if key in ['out_valve', 'in_valve']: + return self.remote_state.get(key, 'closed') + else + return self.remote_state.get(key,''); + def process_change(self): self.validate_states() - out_valve_change = self.out_valve_states.get(self.remote_state['out_valve'], None ) + out_valve_change = self.out_valve_states.get(self.safe_remote_state('out_valve'), None ) if out_valve_change is not None: out_valve_change() - in_valve_change = self.in_valve_states.get(self.remote_state['in_valve'], None ) + in_valve_change = self.in_valve_states.get(self.safe_remote_state('in_valve'), None ) if in_valve_change is not None: in_valve_change() -- 2.47.3 From ee828c544e92d0b81f26abd4e96b25355225a0ea Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 6 Mar 2016 14:47:42 +0100 Subject: [PATCH 7/8] syntax error fix --- controller/state/changer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/state/changer.py b/controller/state/changer.py index 550f41e..2896315 100644 --- a/controller/state/changer.py +++ b/controller/state/changer.py @@ -27,7 +27,7 @@ class Changer(object): def safe_remote_state(key): if key in ['out_valve', 'in_valve']: return self.remote_state.get(key, 'closed') - else + else: return self.remote_state.get(key,''); def process_change(self): -- 2.47.3 From d15bc0e5a69759c0f2c4b78a78b12df7aed371cd Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Sun, 6 Mar 2016 14:49:26 +0100 Subject: [PATCH 8/8] problem with python syntax --- controller/state/changer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controller/state/changer.py b/controller/state/changer.py index 2896315..2e2c1c8 100644 --- a/controller/state/changer.py +++ b/controller/state/changer.py @@ -24,7 +24,7 @@ class Changer(object): 'closed': self.close_in_valve } - def safe_remote_state(key): + def safe_remote_state(self, key): if key in ['out_valve', 'in_valve']: return self.remote_state.get(key, 'closed') else: -- 2.47.3