From: Andrew Andrianov Date: Fri, 13 Feb 2015 19:22:18 +0000 (+0300) Subject: Add lua serverd X-Git-Tag: v0.1~19 X-Git-Url: http://git.linex4red.de/pub/pl2303-ft232-gpio.git/commitdiff_plain/0818f328fed2c98d09f2007fc32ee0293586bb18 Add lua serverd Signed-off-by: Andrew Andrianov --- diff --git a/extra/serverd.lua b/extra/serverd.lua new file mode 100644 index 0000000..5d61408 --- /dev/null +++ b/extra/serverd.lua @@ -0,0 +1,88 @@ +#!/usr/bin/lua + + +--TODO: host.conf +hosts = { + {"192.168.0.1", "windblade"}, + {"192.168.0.20", "iceblade"}, +} +sleep_interval=1 + +--dofile("/etc/serverd.conf") + +function gpio(host, n,o) + local str="cp2103gpio --serial="..host.." --gpio="..n.." --out="..o + os.execute(str) + --print(str) +end + +function green(host, state) + if (state) then + gpio(host, 0, 0); + else + gpio(host, 0, 1); + end +end + +function ylw(host, state) + if (state) then + gpio(host, 1, 0); + else + gpio(host, 1, 1); + end +end + +function red(host, state) + if (state) then + gpio(host, 2, 0); + else + gpio(host, 2, 1); + end +end + +function psu(host, state) + if (state) then + gpio(host, 2, 1); + else + gpio(host, 2, 0); + end +end + + +function is_alive(host, name) + ylw(name, true) + result = os.execute("ping -w 1 -c 1 "..host.." > /dev/null") + ylw(name, false) + if (0==result) then + return true; + else + return false; + end + +end + +function sleep(n) + ret = os.execute("sleep "..n); + if (ret > 0) then + error("Interrupted!"); + end +end + + +function check_host_status(host, name) + if (is_alive(host, name)) then + green(name, true) + red(name, false) + else + green(name, false) + red(name, true) + end +end + +while true do + for i,j in pairs(hosts) do + check_host_status(j[1],j[2]) + sleep(sleep_interval) + end + +end \ No newline at end of file