3 local random = math.random
4 local json = require "json"
5 math.randomseed(os.time())
8 local template ='xxxx-xxxx'
9 return string.gsub(template, '[x]', function (c)
10 local v = random(0, 0xf)
11 return string.format('%x', v)
15 function process_MSG(mid, topic, payload)
17 pcall(function(topic,payload)
19 payload = json.decode(payload)
20 local time = os.date(payload['Time'])
21 if not time then time = os.date(payload['time']); end
22 local model = payload['model']
23 local id = payload['id']
24 for sensor_type,sensor_data in pairs(payload) do
25 if sensor_type ~= "Time" and sensor_type ~= "TempUnit" and sensor_type ~= "model" and sensor_type ~="id" and sensor_type ~= "time" then
27 conn:execute(string.format("CALL meteo.submit_mqtt('%s','%s','%s','%s',NULL)", topic,model,sensor_type,sensor_data))
29 print(topic,model,sensor_type,sensor_data)
31 for param,value in pairs(sensor_data) do
32 conn:execute(string.format("CALL meteo.submit_mqtt('%s','%s','%s','%s',NULL)", topic,sensor_type,param,value))
34 print(topic,sensor_type,param,value)
45 if not config_name then
46 config_name = "weathermon_mqtt"
49 mqtt_host = uci.get(config_name,"mqtt","server")
54 mqtt_port = uci.get(config_name,"mqtt","port")
59 mqtt_user = uci.get(config_name,"mqtt","username")
60 mqtt_pwd = uci.get(config_name,"mqtt","password")
62 mqtt_id = "wm-mqtt-"..uuid()
64 db_server = uci.get(config_name,"db","server")
65 db_name = uci.get(config_name,"db","db")
66 db_user = uci.get(config_name,"db","username")
67 db_pwd = uci.get(config_name,"db","password")
69 env = require("luasql.mysql").mysql()
70 conn = env:connect(db_name,db_user,db_pwd,db_server)
71 conn:execute("SET NAMES utf8")
73 MQTT = require "mosquitto"
74 mqtt_client = MQTT.new(mqtt_id)
76 mqtt_client:login_set(mqtt_user, mqtt_pwd)
79 mqtt_client:connect("estia.rvb-home.lan",1883)
80 mqtt_client.ON_MESSAGE = process_MSG
82 cur = conn:execute("SELECT DISTINCT topic FROM mqtt_topics WHERE topic<>''")
85 mqtt_client:subscribe(rec)
89 mqtt_client:loop_forever()