From e3394561c91920e6d04efb1a732363ba6700b560 Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 25 Dec 2013 21:22:39 +0100 Subject: add icmp script --- examples/scripts/icmp/script.py | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/scripts/icmp/script.py 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 @@ +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.cur = 0 + self.expr = expr + self.func = func + + def set(self, duration): + if not self.expr(duration): + self.cur = 0 + return + if (self.cur >= self.checks): + return + self.cur += 1 + if (self.cur == self.checks): + webiopi.debug("ICMP script - State(%s) triggered" % (self.name)) + self.func() + + def __str__(self): + return "State(%s) %d/%d" % (self.name, self.cur, self.checks) + +online = State('Online', 2, 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("ICMP script - setup") + GPIO.setFunction(GAS_PORT, GPIO.OUT) + GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) + +def loop(): + icmp0 = webiopi.deviceInstance(ICMP_DEVICE) + if icmp0 is None: + webiopi.sleep(SLEEP) + return + + duration = icmp0.getMilliseconds() + webiopi.debug("ICMP script - %s: %dms" % (ICMP_DEVICE, duration)) + for s in STATES: + s.set(duration) + webiopi.debug("ICMP script - %s" % (s)) + webiopi.sleep(SLEEP) + +def destroy(): + webiopi.debug("ICMP script - destroy") + GPIO.setFunction(GAS_PORT, GPIO.OUT) + GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) -- cgit v1.2.3