- Убран отдельный скрипт инициализации (датчики инициализируются непосредственно...
authorRoman Bazalevsky <rvb@rvb.name>
Fri, 16 Nov 2018 18:37:58 +0000 (21:37 +0300)
committerRoman Bazalevsky <rvb@rvb.name>
Fri, 16 Nov 2018 18:37:58 +0000 (21:37 +0300)
- Почищен код weathermon (в т.ч. для случая, когда используется только MQTT).
- Скрипт запуска-остановки процесса для бэкапа использует штатные средства, а не копирование файла напрямую.
- Вынесен в библиотеку код определения ID устройства.

bin/dump-current
bin/dump-yesterday
bin/weather-init [deleted file]
bin/weathermon
init.d/weathermon
lib/wm_util.lua

index 5b3761f13f0f52037487a57aa05271f38f93bb4e..953d0c073dfe9fc403675d292e94dbd48e1a7670 100755 (executable)
@@ -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
 
index 457792f9313498e90b2b2b240cfbe4ab52e93cba..d96d3dd6f5a0e3317982560f8b6f0d09d1547b43 100755 (executable)
@@ -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<date('now','-1 day')"
+  /usr/bin/weather-filter $logdb dump-compacted $yesterday $dumpdir
+
+  sqlite3 $logdb "delete from log where time_stamp<date('now','-1 day')"
+
+fi
diff --git a/bin/weather-init b/bin/weather-init
deleted file mode 100755 (executable)
index d009c67..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-# Put your custom commands here that should be executed once
-# the system init finished. By default this file does nothing.
-
-I2C_BUS=0
-DS3221=68
-
-echo heartbeat > /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
index fb431a7485f2ac33534d6dd23e4ce1635d82c565..d16f871c18671c0d0e3bcb8e573889463132cdaf 100755 (executable)
@@ -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 \'\\<inet\\>\' | 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)
     
index b57d620cd633a2b8436c698685d583a7db90473e..bbf6f210bb607b32d2526622b217e8146f2698d4 100755 (executable)
@@ -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
 }
index bb5d8ee30c158afac1375594be0dc86d67bfd2a0..984f6aeebfbc873c84e08e99c7321dd09445acab 100644 (file)
@@ -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