summaryrefslogtreecommitdiffstats
path: root/examples/scripts/gas/script.py
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2013-12-26 02:09:17 +0100
committermanuel <manuel@mausz.at>2013-12-26 02:09:17 +0100
commita9a8e14f895403b2bbdeeabd0255610c51d0166c (patch)
treeb2aa2a6881d55cc10fdc605cd6f0f10b85940012 /examples/scripts/gas/script.py
parent5b3b6e88542d811a2799320c4d0da2e4ea040b77 (diff)
downloadwebiopi-a9a8e14f895403b2bbdeeabd0255610c51d0166c.tar.gz
webiopi-a9a8e14f895403b2bbdeeabd0255610c51d0166c.tar.bz2
webiopi-a9a8e14f895403b2bbdeeabd0255610c51d0166c.zip
add gas-control app
Diffstat (limited to 'examples/scripts/gas/script.py')
-rw-r--r--examples/scripts/gas/script.py104
1 files changed, 104 insertions, 0 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 @@
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.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))
46STATES = [ _online, _offline ]
47
48def setup():
49 webiopi.debug("GAS script - setup")
50 GPIO.setFunction(GAS_PORT, GPIO.OUT)
51 GPIO.digitalWrite(GAS_PORT, GPIO.HIGH)
52
53def 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
63def 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
69def gas_getPort():
70 global GAS_PORT
71 return str(GAS_PORT)
72
73_force = None
74
75@webiopi.macro
76def gas_getForce():
77 global _force
78 if _force is None:
79 return ""
80 return _force.name
81
82@webiopi.macro
83def 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()