DB "canary" added - with fail/restart when connection dropped master
authorRoman Bazalevskiy <rvb@rvb.name>
Wed, 1 Nov 2023 15:57:33 +0000 (18:57 +0300)
committerRoman Bazalevskiy <rvb@rvb.name>
Wed, 1 Nov 2023 15:59:52 +0000 (18:59 +0300)
server/weathermon-mqtt

index 809b4e9521e85354b6fcc10e6945184122a717c2..d591abe30ee11dea85cf3ee4420408dab43cde6c 100755 (executable)
@@ -1,4 +1,4 @@
-#!/usr/bin/lua
+#!/usr/bin/lua5.1
 
 local random = math.random
 local json = require "json"
@@ -13,30 +13,36 @@ local function uuid()
 end
 
 function process_MSG(mid, topic, payload)
-  print(topic, payload)
   pcall(function(topic,payload)
-    print(payload)
     payload = json.decode(payload)
     local time = os.date(payload['Time'])
     if not time then time = os.date(payload['time']); end 
     local model = payload['model']
     local id = payload['id']
+    if not model then
+      device = payload['device']
+      if device then
+        model = device['model']
+        id = device['ieeeAddr']
+      end
+    end
     for sensor_type,sensor_data in pairs(payload) do
-      if sensor_type ~= "Time" and sensor_type ~= "TempUnit" and sensor_type ~= "model" and sensor_type ~="id" and sensor_type ~= "time" then
+      if sensor_type ~= "Time" and sensor_type ~= "TempUnit" and sensor_type ~= "model" and sensor_type ~="id" and sensor_type ~= "time" and sensor_type ~= "device" and sensor_type ~="linkquality" and sensor_type ~= "battery" and sensor_type ~= "last_seen" and sensor_type ~= "voltage" then
         if model then
           conn:execute(string.format("CALL meteo.submit_mqtt('%s','%s','%s','%s',NULL)", topic,model,sensor_type,sensor_data))
           conn:commit()
-          print(topic,model,sensor_type,sensor_data)
+          print(string.format("%s/%s (%s) -> %s", topic, model, sensor_type, sensor_data))
         else 
           for param,value in pairs(sensor_data) do
             conn:execute(string.format("CALL meteo.submit_mqtt('%s','%s','%s','%s',NULL)", topic,sensor_type,param,value))
             conn:commit()
-            print(topic,sensor_type,param,value)
+            print(string.format("%s/%s (%s) -> %s", topic, sensor_type, param, value))
           end
         end  
       end
     end
   end, topic, payload) 
+  conn:execute('select 1') -- will fail if connection to DB broken
 end
 
 uci = require "uci"