summaryrefslogtreecommitdiffstats
path: root/examples/scripts
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2013-12-25 21:22:39 +0100
committermanuel <manuel@mausz.at>2013-12-25 21:22:39 +0100
commite3394561c91920e6d04efb1a732363ba6700b560 (patch)
treefe9a8a394608a0b78f89a3d3eaa98c7b207d24dc /examples/scripts
parentbdd42c323cafb4ac054ce902c77d4a39bfe5c425 (diff)
downloadwebiopi-e3394561c91920e6d04efb1a732363ba6700b560.tar.gz
webiopi-e3394561c91920e6d04efb1a732363ba6700b560.tar.bz2
webiopi-e3394561c91920e6d04efb1a732363ba6700b560.zip
add icmp script
Diffstat (limited to 'examples/scripts')
-rw-r--r--examples/scripts/icmp/script.py60
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 @@
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)