5   local uci=require("uci")
 
   7   local config="weathermon"
 
   9   web_url  = cur.get(config,"web","url")
 
  10   web_user = cur.get(config,"web","user")
 
  11   web_pass = cur.get(config,"web","password")
 
  12   web_devid = cur.get(config,"web","devid")
 
  14   web_iface = cur.get(config,"web","iface")
 
  18     command = '/sbin/ifconfig '..web_iface..' | grep \'\\<inet\\>\' | sed -n \'1p\' | tr -s \' \' | cut -d \' \' -f3 | cut -d \':\' -f2'
 
  19     f=io.popen(command,'r')
 
  27       io.input("/sys/class/net/"..web_iface.."/address")
 
  29       io.input("/sys/class/net/eth0/address")
 
  32     mac = io.read("*line")
 
  33     mac = mac:gsub(":","")
 
  40   logging = cur.get(config,"logging","enabled") 
 
  42   serial_port = cur.get(config,"serial","port")
 
  43   serial_baud = cur.get(config,"serial","baud")
 
  45   input_file = cur.get(config,"input","file")
 
  46   input_exec = cur.get(config,"input","exec")
 
  47   alarm_exec = cur.get(config,"alarm","exec")
 
  51     command = "stty -F  "..serial_port.." "..serial_baud
 
  56   mqtt_host = cur.get(config,"mqtt","host")
 
  57   mqtt_port = cur.get(config,"mqtt","port")
 
  58   mqtt_id = cur.get(config,"mqtt","id")
 
  59   mqtt_topic = cur.get(config,"mqtt","topic")
 
  60   mqtt_alarm_topic = cur.get(config,"mqtt","alarm_topic")
 
  62   if mqtt_host and not mqtt_id then
 
  63     mqtt_id="weather-"..web_devid
 
  66   if mqtt_host and not mqtt_port then
 
  70   if mqtt_host and not mqtt_topic then
 
  71     mqtt_topic = 'weathermon/'..web_devid
 
  74   if mqtt_host and not mqtt_alarm_topic then
 
  75     mqtt_alarm_topic = 'alarm/'..web_devid
 
  83   socket.select(nil, nil, sec)
 
  86 function splitStr(str,char)
 
  98       res[idx]=str:sub(1,pos-1)
 
 108 function printLog(str)
 
 110   if logging=="on" then
 
 111     os.execute("logger -t weathermon "..str)
 
 115 function submitValue(type,id,param,val)
 
 117   url = web_url.."?stype="..type.."&sid="..id.."¶m="..param.."&value="..val
 
 122     command = command.." --interface "..ip_addr
 
 126     command = command.." -u "..web_user..":"..web_pass
 
 129   command = command.." \""..url.."\""
 
 135 function processLine(str)
 
 137   msg=splitStr(line,':')
 
 138   msg_type=msg[1] or nil
 
 139   msg_body=msg[2] or nil
 
 140   if msg_type=="STATUS" then
 
 141     printLog("Status: "..msg_body)
 
 142   elseif msg_type=="ERROR" then
 
 143     printLog("Error: "..msg_body)  
 
 144   elseif msg_type=="SENSOR" then
 
 145     printLog("SENSOR: "..msg_body)  
 
 146     sens = splitStr(msg_body,",")
 
 150     sensor_id = web_devid
 
 151     for i,rec in ipairs(sens) do
 
 152       recrd=splitStr(rec,'=')
 
 154       value=recrd[2] or nil
 
 158         elseif key=="ID" then
 
 166     for k,v in pairs(sensor) do
 
 167       printLog("Type = "..sensor_type..", ID = "..sensor_id..", Param = "..k..", Value = "..v)
 
 168       submitValue(sensor_type,sensor_id,k,v)
 
 171     mqtt_msg = { type=sensor_type, id=sensor_id, data=mqtt_param }
 
 173       serializedString = json.encode(mqtt_msg)
 
 174       mqtt_client:publish(mqtt_topic,serializedString)
 
 176   elseif msg_type=="ALARM" then
 
 177     printLog("ALARM: "..msg_body)  
 
 178     sens = splitStr(msg_body,",")
 
 182     sensor_id = web_devid
 
 184     for i,rec in ipairs(sens) do
 
 185       recrd=splitStr(rec,'=')
 
 187       value=recrd[2] or nil
 
 191         elseif key=="ID" then
 
 194           mqtt_param[key]=value
 
 199       mqtt_msg = { type = alarm_type, id = alarm_id, data = mqtt_param }
 
 200       serializedString = json.encode(mqtt_msg)
 
 201       mqtt_client:publish(mqtt_alarm_topic,serializedString)
 
 205         " \""..string.gsub(alarm_type,"\"","\\\"")..
 
 206         "\" \""..string.gsub(alarm_id,"\"","\\\"")..
 
 207         "\" \""..string.gsub(msg_body,"\"","\\\"").."\""
 
 217   MQTT = require "paho.mqtt"
 
 218   mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
 
 219   mqtt_client:connect(mqtt_id)
 
 220   json = require( "json" )
 
 224   serialin=io.open(serial_port,"r")
 
 225 elseif input_file == "-" then
 
 227 elseif input_file then
 
 228   serialin=io.open(input_file,"r")
 
 229 elseif input_exec then
 
 230   serialin=io.popen(input_exec,"r")
 
 232   printLog("No input selected")