diff options
| author | manuel <manuel@mausz.at> | 2013-12-26 02:09:17 +0100 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2013-12-26 02:09:17 +0100 |
| commit | a9a8e14f895403b2bbdeeabd0255610c51d0166c (patch) | |
| tree | b2aa2a6881d55cc10fdc605cd6f0f10b85940012 /examples | |
| parent | 5b3b6e88542d811a2799320c4d0da2e4ea040b77 (diff) | |
| download | webiopi-a9a8e14f895403b2bbdeeabd0255610c51d0166c.tar.gz webiopi-a9a8e14f895403b2bbdeeabd0255610c51d0166c.tar.bz2 webiopi-a9a8e14f895403b2bbdeeabd0255610c51d0166c.zip | |
add gas-control app
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/scripts/gas/script.py | 104 | ||||
| -rw-r--r-- | examples/scripts/icmp/script.py | 60 |
2 files changed, 104 insertions, 60 deletions
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 @@ | |||
| 1 | import webiopi | ||
| 2 | |||
| 3 | webiopi.setDebug() | ||
| 4 | |||
| 5 | GPIO = webiopi.GPIO | ||
| 6 | |||
| 7 | ICMP_DEVICE = "icmp0" | ||
| 8 | GAS_PORT = 0 | ||
| 9 | SLEEP = 1 | ||
| 10 | |||
| 11 | class State(object): | ||
| 12 | def __init__(self, name, checks, expr, func): | ||
| 13 | self.name = name | ||
| 14 | self.checks = checks | ||
| 15 | self.cnt = 0 | ||
| 16 | self.enabled = True | ||
| 17 | self.expr = expr | ||
| 18 | self.func = func | ||
| 19 | |||
| 20 | def incr(self, duration): | ||
| 21 | if not self.expr(duration): | ||
| 22 | self.cnt = 0 | ||
| 23 | return | ||
| 24 | if self.cnt_reached(): | ||
| 25 | return | ||
| 26 | self.cnt += 1 | ||
| 27 | if self.cnt_reached() and self.enabled: | ||
| 28 | self.trigger() | ||
| 29 | |||
| 30 | def trigger(self): | ||
| 31 | webiopi.debug("GAS script - %s triggered" % (self.name)) | ||
| 32 | self.func() | ||
| 33 | |||
| 34 | def cnt_reached(self): | ||
| 35 | return self.cnt >= self.checks | ||
| 36 | |||
| 37 | def __str__(self): | ||
| 38 | enabled = "enabled" if self.enabled else "disabled" | ||
| 39 | return "%s(%s) %d/%d" % (self.name, enabled, self.cnt, | ||
| 40 | self.checks) | ||
| 41 | |||
| 42 | _online = State('Online', 5, lambda x: x > 0 and x < 300, | ||
| 43 | lambda: GPIO.digitalWrite(GAS_PORT, GPIO.LOW)) | ||
| 44 | _offline = State('Offline', 30, lambda x: not _online.expr(x), | ||
| 45 | lambda: GPIO.digitalWrite(GAS_PORT, GPIO.HIGH)) | ||
| 46 | STATES = [ _online, _offline ] | ||
| 47 | |||
| 48 | def setup(): | ||
| 49 | webiopi.debug("GAS script - setup") | ||
| 50 | GPIO.setFunction(GAS_PORT, GPIO.OUT) | ||
| 51 | GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) | ||
| 52 | |||
| 53 | def loop(): | ||
| 54 | icmp = webiopi.deviceInstance(ICMP_DEVICE) | ||
| 55 | if icmp is not None: | ||
| 56 | duration = icmp.getMilliseconds() | ||
| 57 | webiopi.debug("GAS script - %s: %dms" % (ICMP_DEVICE, duration)) | ||
| 58 | for s in STATES: | ||
| 59 | s.incr(duration) | ||
| 60 | webiopi.debug("GAS script - %s" % (s)) | ||
| 61 | webiopi.sleep(SLEEP) | ||
| 62 | |||
| 63 | def destroy(): | ||
| 64 | webiopi.debug("GAS script - destroy") | ||
| 65 | GPIO.setFunction(GAS_PORT, GPIO.OUT) | ||
| 66 | GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) | ||
| 67 | |||
| 68 | @webiopi.macro | ||
| 69 | def gas_getPort(): | ||
| 70 | global GAS_PORT | ||
| 71 | return str(GAS_PORT) | ||
| 72 | |||
| 73 | _force = None | ||
| 74 | |||
| 75 | @webiopi.macro | ||
| 76 | def gas_getForce(): | ||
| 77 | global _force | ||
| 78 | if _force is None: | ||
| 79 | return "" | ||
| 80 | return _force.name | ||
| 81 | |||
| 82 | @webiopi.macro | ||
| 83 | def gas_setForce(state = None): | ||
| 84 | global _force | ||
| 85 | webiopi.debug("GAS script - forcing state %s" % (state)) | ||
| 86 | |||
| 87 | if state is None: | ||
| 88 | _force = None | ||
| 89 | for s in STATES: | ||
| 90 | s.enabled = True | ||
| 91 | # trigger again if mode is automatic | ||
| 92 | if s.cnt_reached(): | ||
| 93 | s.trigger() | ||
| 94 | else: | ||
| 95 | for s in STATES: | ||
| 96 | if s.name == state: | ||
| 97 | _force = s | ||
| 98 | break | ||
| 99 | if _force is not None: | ||
| 100 | for s in STATES: | ||
| 101 | s.enabled = False | ||
| 102 | _force.trigger() | ||
| 103 | |||
| 104 | return gas_getForce() | ||
diff --git a/examples/scripts/icmp/script.py b/examples/scripts/icmp/script.py deleted file mode 100644 index 41de666..0000000 --- a/examples/scripts/icmp/script.py +++ /dev/null | |||
| @@ -1,60 +0,0 @@ | |||
| 1 | import webiopi | ||
| 2 | |||
| 3 | webiopi.setDebug() | ||
| 4 | |||
| 5 | GPIO = webiopi.GPIO | ||
| 6 | |||
| 7 | ICMP_DEVICE = "icmp0" | ||
| 8 | GAS_PORT = 0 | ||
| 9 | SLEEP = 1 | ||
| 10 | |||
| 11 | class State(object): | ||
| 12 | def __init__(self, name, checks, expr, func): | ||
| 13 | self.name = name | ||
| 14 | self.checks = checks | ||
| 15 | self.cur = 0 | ||
| 16 | self.expr = expr | ||
| 17 | self.func = func | ||
| 18 | |||
| 19 | def set(self, duration): | ||
| 20 | if not self.expr(duration): | ||
| 21 | self.cur = 0 | ||
| 22 | return | ||
| 23 | if (self.cur >= self.checks): | ||
| 24 | return | ||
| 25 | self.cur += 1 | ||
| 26 | if (self.cur == self.checks): | ||
| 27 | webiopi.debug("ICMP script - State(%s) triggered" % (self.name)) | ||
| 28 | self.func() | ||
| 29 | |||
| 30 | def __str__(self): | ||
| 31 | return "State(%s) %d/%d" % (self.name, self.cur, self.checks) | ||
| 32 | |||
| 33 | online = State('Online', 2, lambda x: x > 0 and x < 300, | ||
| 34 | lambda: GPIO.digitalWrite(GAS_PORT, GPIO.LOW)) | ||
| 35 | offline = State('Offline', 30, lambda x: not online.expr(x), | ||
| 36 | lambda: GPIO.digitalWrite(GAS_PORT, GPIO.HIGH)) | ||
| 37 | STATES = [ online, offline ] | ||
| 38 | |||
| 39 | def setup(): | ||
| 40 | webiopi.debug("ICMP script - setup") | ||
| 41 | GPIO.setFunction(GAS_PORT, GPIO.OUT) | ||
| 42 | GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) | ||
| 43 | |||
| 44 | def loop(): | ||
| 45 | icmp0 = webiopi.deviceInstance(ICMP_DEVICE) | ||
| 46 | if icmp0 is None: | ||
| 47 | webiopi.sleep(SLEEP) | ||
| 48 | return | ||
| 49 | |||
| 50 | duration = icmp0.getMilliseconds() | ||
| 51 | webiopi.debug("ICMP script - %s: %dms" % (ICMP_DEVICE, duration)) | ||
| 52 | for s in STATES: | ||
| 53 | s.set(duration) | ||
| 54 | webiopi.debug("ICMP script - %s" % (s)) | ||
| 55 | webiopi.sleep(SLEEP) | ||
| 56 | |||
| 57 | def destroy(): | ||
| 58 | webiopi.debug("ICMP script - destroy") | ||
| 59 | GPIO.setFunction(GAS_PORT, GPIO.OUT) | ||
| 60 | GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) | ||
