From c58ad4fb6f2c4c350a6aae01b100ebb4564ad8e9 Mon Sep 17 00:00:00 2001 From: Roman Bazalevsky Date: Fri, 16 Nov 2018 21:37:58 +0300 Subject: [PATCH] =?utf8?q?-=20=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=20=D0=BE?= =?utf8?q?=D1=82=D0=B4=D0=B5=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20=D1=81=D0=BA?= =?utf8?q?=D1=80=D0=B8=D0=BF=D1=82=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0?= =?utf8?q?=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20(=D0=B4=D0=B0=D1=82?= =?utf8?q?=D1=87=D0=B8=D0=BA=D0=B8=20=D0=B8=D0=BD=D0=B8=D1=86=D0=B8=D0=B0?= =?utf8?q?=D0=BB=D0=B8=D0=B7=D0=B8=D1=80=D1=83=D1=8E=D1=82=D1=81=D1=8F=20?= =?utf8?q?=D0=BD=D0=B5=D0=BF=D0=BE=D1=81=D1=80=D0=B5=D0=B4=D1=81=D1=82?= =?utf8?q?=D0=B2=D0=B5=D0=BD=D0=BD=D0=BE=20=D0=BF=D1=80=D0=BE=D1=86=D0=B5?= =?utf8?q?=D1=81=D1=81=D0=BE=D0=BC,=20=D1=83=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?utf8?q?=D1=81=D1=82=D0=B2=D0=B0=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0?= =?utf8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20-=20/etc/modules.d).=20-=20?= =?utf8?q?=D0=9F=D0=BE=D1=87=D0=B8=D1=89=D0=B5=D0=BD=20=D0=BA=D0=BE=D0=B4?= =?utf8?q?=20weathermon=20(=D0=B2=20=D1=82.=D1=87.=20=D0=B4=D0=BB=D1=8F=20?= =?utf8?q?=D1=81=D0=BB=D1=83=D1=87=D0=B0=D1=8F,=20=D0=BA=D0=BE=D0=B3=D0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?utf8?q?=D1=8F=20ID=20=D1=83=D1=81=D1=82=D1=80=D0=BE=D0=B9=D1=81=D1=82?= =?utf8?q?=D0=B2=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- bin/dump-current | 11 +++++-- bin/dump-yesterday | 12 +++++--- bin/weather-init | 33 --------------------- bin/weathermon | 73 ++++++++++++++++++---------------------------- init.d/weathermon | 7 +---- lib/wm_util.lua | 23 +++++++++++++++ 6 files changed, 68 insertions(+), 91 deletions(-) delete mode 100755 bin/weather-init diff --git a/bin/dump-current b/bin/dump-current index 5b3761f..953d0c0 100755 --- a/bin/dump-current +++ b/bin/dump-current @@ -1,7 +1,12 @@ #!/bin/sh logdb=`uci get weathermon.process.logdb` -dumpdir=`uci get weathermon.process.working_dir`/www -mkdir -p $dumpdir -/usr/bin/weather-filter $logdb dump-compacted - $dumpdir + +if [ -f "$logdb" ]; then + + dumpdir=`uci get weathermon.process.working_dir`/www + mkdir -p $dumpdir + /usr/bin/weather-filter $logdb dump-compacted - $dumpdir + +fi diff --git a/bin/dump-yesterday b/bin/dump-yesterday index 457792f..d96d3dd 100755 --- a/bin/dump-yesterday +++ b/bin/dump-yesterday @@ -4,10 +4,14 @@ yesterday=`echo "print(os.date(\"%Y-%m-%d\",os.time()-24*60*60))" | /usr/bin/lua logdb=`uci get weathermon.process.logdb` -dumpdir=`uci get weathermon.process.archive_dir`/`echo "print(os.date(\"%Y/%m/%d\",os.time()-22*60*60))" | /usr/bin/lua` +if [ -f "$logdb" ]; then -mkdir -p $dumpdir + dumpdir=`uci get weathermon.process.archive_dir`/`echo "print(os.date(\"%Y/%m/%d\",os.time()-22*60*60))" | /usr/bin/lua` -/usr/bin/weather-filter $logdb dump-compacted $yesterday $dumpdir + mkdir -p $dumpdir -sqlite3 $logdb "delete from log where time_stamp /sys/class/leds/linkit-smart-7688:orange\:wifi/trigger - -if [ ! -e /dev/lcdi2c ]; then - insmod lcdi2c busno=0 address=0x27 cursor=0 blink=0 topo=1 -fi - -echo 1 > /sys/class/alphalcd/lcdi2c/clear -echo \00\00 > /sys/class/alphalcd/lcdi2c/position - -if [ ! -e /dev/rtc0 ]; then - - modprobe rtc-ds1307 - echo ds1307 0x$DS3221 > /sys/class/i2c-dev/i2c-$I2C_BUS/device/new_device - sleep 1 - if [ -e /dev/rtc0 ]; then - ln -sf /dev/rtc0 /dev/rtc - logger -t hardware RTC initialized - fi - -fi - -if [ -e /dev/rtc0 ]; then - /sbin/hwclock -u --rtc=/dev/rtc0 --hctosys - echo "Hardware RTC OK" > /dev/lcdi2c -else - echo "Hardware RTC FAIL" > /dev/lcdi2c -fi diff --git a/bin/weathermon b/bin/weathermon index fb431a7..d16f871 100755 --- a/bin/weathermon +++ b/bin/weathermon @@ -1,9 +1,6 @@ #!/usr/bin/lua local json = require("json") -local socket = require("socket") - -local http = require("socket.http") require "wm_util" @@ -24,37 +21,12 @@ function getConfig(configname) web_user = cur.get(config,"web","user") web_timeout = cur.get(config,"web","timeout") web_pass = cur.get(config,"web","password") - web_devid = cur.get(config,"web","devid") - - web_iface = cur.get(config,"web","iface") if not web_timeout then web_timeout = 10 end - if web_iface then - - command = '/sbin/ifconfig '..web_iface..' | grep \'\\\' | sed -n \'1p\' | tr -s \' \' | cut -d \' \' -f3 | cut -d \':\' -f2' - f=io.popen(command,'r') - ip_addr=f:read() - - end - - if not web_devid then - - if web_iface then - io.input("/sys/class/net/"..web_iface.."/address") - else - io.input("/sys/class/net/eth0/address") - end - - local mac = io.read("*line") - mac = mac:gsub(":","") - mac = mac:upper() - - web_devid = mac - - end + web_devid = get_devid(config) logging = cur.get(config,"logging","enabled") touch_file = cur.get(config,"logging","touch_file") @@ -146,10 +118,6 @@ function submitValue(type,id,param,val) log_con:execute(string.format("INSERT INTO log(time_stamp,sensor_id,sensor,param,value) VALUES (datetime('now','localtime'),'%s','%s','%s',%f)",id,type,param,val)) end - if touch_file then - touch(touch_file) - end - end function storeRecord(id,sensor,param,value) @@ -200,7 +168,9 @@ function processJson(str) for k,v in pairs(sensor) do storeRecord(sensor_id,sensor_type,k,v) printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = \""..v.."\"") - submitValue(sensor_type,sensor_id,k,v) + if web_url then + submitValue(sensor_type,sensor_id,k,v) + end if mqtt_client then mqtt_path=string.gsub(mqtt_topic,"{(.-)}", function (name) @@ -216,9 +186,8 @@ function processJson(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) mqtt_client:publish(mqtt_path,v) - mqtt_client:disconnect() + mqtt_client:loop() end end else @@ -262,7 +231,9 @@ function processLine(str) for k,v in pairs(sensor) do storeRecord(sensor_id,sensor_type,k,v) printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v) - submitValue(sensor_type,sensor_id,k,v) + if web_url then + submitValue(sensor_type,sensor_id,k,v) + end if mqtt_client then mqtt_path=string.gsub(mqtt_topic,"{(.-)}", function (name) @@ -278,9 +249,8 @@ function processLine(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) mqtt_client:publish(mqtt_path,v) - mqtt_client:disconnect() + mqtt_client:loop() end end else @@ -320,9 +290,8 @@ function processLine(str) return '{'..name..'}' end end) - mqtt_client:connect(mqtt_host,mqtt_port) mqtt_client:publish(mqtt_path,msg_body) - mqtt_client:disconnect() + mqtt_client:loop() end if alarm_exec then command=alarm_exec.. @@ -362,12 +331,18 @@ if logdb then log_con:execute("CREATE INDEX log_idx ON log(sensor_id,sensor,param,time_stamp)") end +if web_url then + http = require("socket.http") + socket = require("socket") +end + if mqtt_host then MQTT = require "mosquitto" mqtt_client = MQTT.new(mqtt_id) if mqtt_user then mqtt_client:login_set(mqtt_user, mqtt_passwd) end + mqtt_client:connect(mqtt_host,mqtt_port) end if serial_port then @@ -404,11 +379,19 @@ while 1 do processLine(line) end + if touch_file then + pcall(function () + touch(touch_file) + end) + end + if dump_file then - local f = io.open(dump_file,"w") - io.output(f) - io.write(json.encode(records)) - io.close(f) + pcall(function () + local f = io.open(dump_file,"w") + io.output(f) + io.write(json.encode(records)) + io.close(f) + end) end end) diff --git a/init.d/weathermon b/init.d/weathermon index b57d620..bbf6f21 100755 --- a/init.d/weathermon +++ b/init.d/weathermon @@ -50,10 +50,5 @@ start_service() { stop_service() { killall weathermon killall weathermon-iio - if [ ! -z "$BACKLOGDB" ] && [ -f "$BACKLOGDB" ]; then - cp "$BACKLOGDB" "$BACKUP_DIR"/ - fi - if [ ! -z "$LOGDB" ] && [ -f "$BACKLOGDB" ]; then - cp "$LOGDB" "$BACKUP_DIR"/ - fi + /usr/bin/weather-backup } diff --git a/lib/wm_util.lua b/lib/wm_util.lua index bb5d8ee..984f6ae 100644 --- a/lib/wm_util.lua +++ b/lib/wm_util.lua @@ -104,3 +104,26 @@ function list_dir(name) end return result end + +function get_devid(config) + + local uci = require "uci" + + local web_devid = uci.get(config,"web","devid") + + if web_devid then + return web_devid + end + + web_iface = uci.get(config,"web","iface") + + if not web_iface then + web_iface = list_dir('/sys/class/net/')[1] + end + + io.input("/sys/class/net/"..web_iface.."/address") + + local web_devid = io.read("*line") + return web_devid:gsub(":",""):upper() + +end \ No newline at end of file -- 2.34.1