From a9a8e14f895403b2bbdeeabd0255610c51d0166c Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 26 Dec 2013 02:09:17 +0100 Subject: add gas-control app --- examples/scripts/gas/script.py | 104 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 examples/scripts/gas/script.py (limited to 'examples/scripts/gas/script.py') diff --git a/examples/scripts/gas/script.py b/examples/scripts/gas/script.py new file mode 100644 index 0000000..4ac95d0 --- /dev/null +++ b/examples/scripts/gas/script.py @@ -0,0 +1,104 @@ +import webiopi + +webiopi.setDebug() + +GPIO = webiopi.GPIO + +ICMP_DEVICE = "icmp0" +GAS_PORT = 0 +SLEEP = 1 + +class State(object): + def __init__(self, name, checks, expr, func): + self.name = name + self.checks = checks + self.cnt = 0 + self.enabled = True + self.expr = expr + self.func = func + + def incr(self, duration): + if not self.expr(duration): + self.cnt = 0 + return + if self.cnt_reached(): + return + self.cnt += 1 + if self.cnt_reached() and self.enabled: + self.trigger() + + def trigger(self): + webiopi.debug("GAS script - %s triggered" % (self.name)) + self.func() + + def cnt_reached(self): + return self.cnt >= self.checks + + def __str__(self): + enabled = "enabled" if self.enabled else "disabled" + return "%s(%s) %d/%d" % (self.name, enabled, self.cnt, + self.checks) + +_online = State('Online', 5, lambda x: x > 0 and x < 300, + lambda: GPIO.digitalWrite(GAS_PORT, GPIO.LOW)) +_offline = State('Offline', 30, lambda x: not _online.expr(x), + lambda: GPIO.digitalWrite(GAS_PORT, GPIO.HIGH)) +STATES = [ _online, _offline ] + +def setup(): + webiopi.debug("GAS script - setup") + GPIO.setFunction(GAS_PORT, GPIO.OUT) + GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) + +def loop(): + icmp = webiopi.deviceInstance(ICMP_DEVICE) + if icmp is not None: + duration = icmp.getMilliseconds() + webiopi.debug("GAS script - %s: %dms" % (ICMP_DEVICE, duration)) + for s in STATES: + s.incr(duration) + webiopi.debug("GAS script - %s" % (s)) + webiopi.sleep(SLEEP) + +def destroy(): + webiopi.debug("GAS script - destroy") + GPIO.setFunction(GAS_PORT, GPIO.OUT) + GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) + +@webiopi.macro +def gas_getPort(): + global GAS_PORT + return str(GAS_PORT) + +_force = None + +@webiopi.macro +def gas_getForce(): + global _force + if _force is None: + return "" + return _force.name + +@webiopi.macro +def gas_setForce(state = None): + global _force + webiopi.debug("GAS script - forcing state %s" % (state)) + + if state is None: + _force = None + for s in STATES: + s.enabled = True + # trigger again if mode is automatic + if s.cnt_reached(): + s.trigger() + else: + for s in STATES: + if s.name == state: + _force = s + break + if _force is not None: + for s in STATES: + s.enabled = False + _force.trigger() + + return gas_getForce() -- cgit v1.2.3