summaryrefslogtreecommitdiffstats
path: root/examples/scripts/icmp/script.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/scripts/icmp/script.py')
-rw-r--r--examples/scripts/icmp/script.py60
1 files changed, 0 insertions, 60 deletions
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 @@
1import webiopi
2
3webiopi.setDebug()
4
5GPIO = webiopi.GPIO
6
7ICMP_DEVICE = "icmp0"
8GAS_PORT = 0
9SLEEP = 1
10
11class 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
33online = State('Online', 2, lambda x: x > 0 and x < 300,
34 lambda: GPIO.digitalWrite(GAS_PORT, GPIO.LOW))
35offline = State('Offline', 30, lambda x: not online.expr(x),
36 lambda: GPIO.digitalWrite(GAS_PORT, GPIO.HIGH))
37STATES = [ online, offline ]
38
39def setup():
40 webiopi.debug("ICMP script - setup")
41 GPIO.setFunction(GAS_PORT, GPIO.OUT)
42 GPIO.digitalWrite(GAS_PORT, GPIO.HIGH)
43
44def 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
57def destroy():
58 webiopi.debug("ICMP script - destroy")
59 GPIO.setFunction(GAS_PORT, GPIO.OUT)
60 GPIO.digitalWrite(GAS_PORT, GPIO.HIGH)