From a9a8e14f895403b2bbdeeabd0255610c51d0166c Mon Sep 17 00:00:00 2001 From: manuel Date: Thu, 26 Dec 2013 02:09:17 +0100 Subject: add gas-control app --- examples/scripts/gas/script.py | 104 +++++++++++++++++++++++++++++++++ examples/scripts/icmp/script.py | 60 ------------------- htdocs/app/gas-control/index.html | 120 ++++++++++++++++++++++++++++++++++++++ htdocs/dev.html | 25 ++++++++ htdocs/index.html | 15 ++--- 5 files changed, 254 insertions(+), 70 deletions(-) create mode 100644 examples/scripts/gas/script.py delete mode 100644 examples/scripts/icmp/script.py create mode 100644 htdocs/app/gas-control/index.html create mode 100644 htdocs/dev.html 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 @@ +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.cnt = 0 + self.enabled = True + self.expr = expr + self.func = func + + def incr(self, duration): + if not self.expr(duration): + self.cnt = 0 + return + if self.cnt_reached(): + return + self.cnt += 1 + if self.cnt_reached() and self.enabled: + self.trigger() + + def trigger(self): + webiopi.debug("GAS script - %s triggered" % (self.name)) + self.func() + + def cnt_reached(self): + return self.cnt >= self.checks + + def __str__(self): + enabled = "enabled" if self.enabled else "disabled" + return "%s(%s) %d/%d" % (self.name, enabled, self.cnt, + self.checks) + +_online = State('Online', 5, 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("GAS script - setup") + GPIO.setFunction(GAS_PORT, GPIO.OUT) + GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) + +def loop(): + icmp = webiopi.deviceInstance(ICMP_DEVICE) + if icmp is not None: + duration = icmp.getMilliseconds() + webiopi.debug("GAS script - %s: %dms" % (ICMP_DEVICE, duration)) + for s in STATES: + s.incr(duration) + webiopi.debug("GAS script - %s" % (s)) + webiopi.sleep(SLEEP) + +def destroy(): + webiopi.debug("GAS script - destroy") + GPIO.setFunction(GAS_PORT, GPIO.OUT) + GPIO.digitalWrite(GAS_PORT, GPIO.HIGH) + +@webiopi.macro +def gas_getPort(): + global GAS_PORT + return str(GAS_PORT) + +_force = None + +@webiopi.macro +def gas_getForce(): + global _force + if _force is None: + return "" + return _force.name + +@webiopi.macro +def gas_setForce(state = None): + global _force + webiopi.debug("GAS script - forcing state %s" % (state)) + + if state is None: + _force = None + for s in STATES: + s.enabled = True + # trigger again if mode is automatic + if s.cnt_reached(): + s.trigger() + else: + for s in STATES: + if s.name == state: + _force = s + break + if _force is not None: + for s in STATES: + s.enabled = False + _force.trigger() + + return gas_getForce() 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 @@ -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) diff --git a/htdocs/app/gas-control/index.html b/htdocs/app/gas-control/index.html new file mode 100644 index 0000000..0e9b3a1 --- /dev/null +++ b/htdocs/app/gas-control/index.html @@ -0,0 +1,120 @@ + + + + + + WebIOPi | Gas Control + + + + + +
+ Ventil:
+ Automatik: +
+ + diff --git a/htdocs/dev.html b/htdocs/dev.html new file mode 100644 index 0000000..68c49c9 --- /dev/null +++ b/htdocs/dev.html @@ -0,0 +1,25 @@ + + + + + + WebIOPi | Raspberry Pi IoT Framework + + + +

WebIOPi Main Menu

+ +

GPIO Header

+

Control and Debug the Raspberry Pi GPIO with a display which looks like the physical header.

+ +

GPIO List

+

Control and Debug the Raspberry Pi GPIO ordered in a single column.

+ +

Serial Monitor

+

Use the browser to play with Serial interfaces configured in WebIOPi.

+ +

Devices Monitor

+

Control and Debug devices and circuits wired to your Pi and configured in WebIOPi.

+ + + diff --git a/htdocs/index.html b/htdocs/index.html index 68c49c9..a1671bb 100644 --- a/htdocs/index.html +++ b/htdocs/index.html @@ -4,22 +4,17 @@ WebIOPi | Raspberry Pi IoT Framework +

WebIOPi Main Menu

-

GPIO Header

-

Control and Debug the Raspberry Pi GPIO with a display which looks like the physical header.

+

Gas Control

+

Gas Control.

-

GPIO List

-

Control and Debug the Raspberry Pi GPIO ordered in a single column.

- -

Serial Monitor

-

Use the browser to play with Serial interfaces configured in WebIOPi.

- -

Devices Monitor

-

Control and Debug devices and circuits wired to your Pi and configured in WebIOPi.

+

Developer tools

+

Developer tools.

-- cgit v1.2.3