DB "canary" added - with fail/restart when connection dropped
[weathermon.git] / bin / weathermon-iio
index d288edf935d599d63f236ca97835c663c85313fb..c5a8102f6f038fc74bb76571b1613e25f6e5c37a 100755 (executable)
@@ -116,6 +116,20 @@ function get_mhz(record)
    
 end
 
+function search_rs232_const(rs232,prefix,value)
+
+  for k,v in pairs(rs232) do
+  
+    if k == prefix..value:upper() then
+      return v
+    end  
+  
+  end
+
+  return nil
+
+end
+
 function init_serial_device(device,subsystem,parameters)
 
   rs232 = require("luars232")
@@ -123,24 +137,33 @@ function init_serial_device(device,subsystem,parameters)
   pcall(function ()
 
     local e, port = rs232.open(device["port"])
-  
-    if subsystem == "mhz" then
+
+    local baud = device["baud"]; if baud == nil then baud = 9600; end
+    local bits = device["bits"]; if bits == nil then bits = 8; end
+    local stop_bits = device["stop_bits"]; if stop_bits == nil then stop_bits = 1; end
+    local parity = device["parity"]; if parity == nil then parity = "NONE"; end 
+    local flowctl = device["flowctl"]; if flowctl == nil then flowctl = "OFF"; end
     
-      assert(port:set_baud_rate(rs232.RS232_BAUD_9600) == rs232.RS232_ERR_NOERROR)
-      assert(port:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
-      assert(port:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
-      assert(port:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
-      assert(port:set_flow_control(rs232.RS232_FLOW_OFF)  == rs232.RS232_ERR_NOERROR)
+    assert(port:set_baud_rate(search_rs232_const(rs232,"RS232_BAUD_",baud)) == rs232.RS232_ERR_NOERROR)
+    assert(port:set_data_bits(search_rs232_const(rs232,"RS232_DATA_",bits)) == rs232.RS232_ERR_NOERROR)
+    assert(port:set_parity(search_rs232_const(rs232,"RS232_PARITY_",parity)) == rs232.RS232_ERR_NOERROR)
+    assert(port:set_stop_bits(search_rs232_const(rs232,"RS232_STOP_",stop_bits)) == rs232.RS232_ERR_NOERROR)
+    assert(port:set_flow_control(search_rs232_const(rs232,"RS232_FLOW_",flowctl)) == rs232.RS232_ERR_NOERROR)
     
-      getparameter = {}
+    getparameter = {}
 
-      getparameter["rs232"] = port
+    getparameter["rs232"] = port
+  
+    if subsystem == "mhz" then
+    
       getparameter["function"] = get_mhz 
-      getparameter["name"] = "CO2_PPM"
+      getparameter["name"] = "CO2PPM"
       getparameter["sensor"] = "MHZ19"
       parameters[#parameters+1] = getparameter
 
     end
+
+    parameters[#parameters+1] = getparameter
     
   end)