diff options
| author | manuel <manuel@mausz.at> | 2013-12-25 21:22:39 +0100 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2013-12-25 21:22:39 +0100 |
| commit | e3394561c91920e6d04efb1a732363ba6700b560 (patch) | |
| tree | fe9a8a394608a0b78f89a3d3eaa98c7b207d24dc | |
| parent | bdd42c323cafb4ac054ce902c77d4a39bfe5c425 (diff) | |
| download | webiopi-e3394561c91920e6d04efb1a732363ba6700b560.tar.gz webiopi-e3394561c91920e6d04efb1a732363ba6700b560.tar.bz2 webiopi-e3394561c91920e6d04efb1a732363ba6700b560.zip | |
add icmp script
| -rw-r--r-- | examples/scripts/icmp/script.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/scripts/icmp/script.py b/examples/scripts/icmp/script.py new file mode 100644 index 0000000..41de666 --- /dev/null +++ b/examples/scripts/icmp/script.py | |||
| @@ -0,0 +1,60 @@ | |||
| 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) | ||
