From: Roman Bazalevsky Date: Fri, 16 Nov 2018 18:37:58 +0000 (+0300) Subject: - Убран отдельный скрипт инициализации (датчики инициализируются непосредственно... X-Git-Url: https://git.rvb.name/weathermon.git/commitdiff_plain/c58ad4fb6f2c4c350a6aae01b100ebb4564ad8e9 - Убран отдельный скрипт инициализации (датчики инициализируются непосредственно процессом, устройства отображения - /etc/modules.d). - Почищен код weathermon (в т.ч. для случая, когда используется только MQTT). - Скрипт запуска-остановки процесса для бэкапа использует штатные средства, а не копирование файла напрямую. - Вынесен в библиотеку код определения ID устройства. --- 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