Merge branch 'master' of rvb.name:openhab-process
[openhab-process.git] / mqtt-bt / scan-beacons
index 98c39937d8efedb096c89c9439ef68a29c297ba1..34de7dc8f93830f9bafd074226a3c6fb8d2fc1e3 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/env lua
+#!/usr/bin/lua
 
 function getConfig(configname)
 
@@ -72,11 +72,12 @@ end
 
 function open_dump()
 
-  f = assert(io.popen ("hcidump --raw"))
-  run_command("kill `pgrep hcitool`")
-  run_command("hciconfig hci0 down")
-  run_command("hciconfig hci0 up")
-  f_null = assert(io.popen ("hcitool lescan --duplicates"))
+  run_command("/bin/kill `/usr/bin/pgrep hcidump`")
+  run_command("/bin/kill `/usr/bin/pgrep hcitool`")
+  f = assert(io.popen ("/usr/bin/hcidump --raw"))
+  run_command("/usr/bin/hciconfig hci0 down")
+  run_command("/usr/bin/hciconfig hci0 up")
+  f_null = assert(io.popen ("/usr/bin/hcitool lescan --duplicates"))
 
   return f
 
@@ -87,8 +88,8 @@ function dump(o)
       local s = '{ '
       for k,v in pairs(o) do
          if type(k) ~= 'number' then k = '"'..k..'"' end
---         s = s .. '['..k..'] = ' .. dump(v) .. ','
-         s = s .. dump(v) .. ','
+         s = s .. '['..k..'] = ' .. dump(v) .. ','
+--         s = s .. dump(v) .. ','
       end
       return s .. '} '
    else
@@ -100,6 +101,11 @@ function trim(s)
   return (s:gsub("^%s*(.-)%s*$", "%1"))
 end
 
+function mqtt_pub(path,value)
+  printLog("Pub "..path.." "..value)
+  return mqtt_client:publish(path,value)
+end
+
 function process_packet(packet)
 
   local bytes={}
@@ -151,13 +157,13 @@ function process_packet(packet)
       end
       if type=="ibeacon" then
         printLog(string.format("{type:'ibeacon',mac:'%s',uuid:'%s',major:'%s',minor:'%s',power:%d,tx:%d}",mac,uuid,major,minor,power,tx))
-        details=uuid..'/'..major..'/'..minor..'/'
+        details=uuid..'/'..major..'/'..minor
       else
         type='unknown'
         details=dump(bytes)
-        printLog(details)
+--        printLog(details)
       end
-      if mqtt_client then
+      if not (type=="unknown") then
         mqtt_path=string.gsub(mqtt_topic,"{(.-)}", 
           function (name) 
             if name=="type" then
@@ -168,7 +174,12 @@ function process_packet(packet)
               return '{'..name..'}'
             end      
           end)
-        mqtt_client:publish(mqtt_path,tx)
+        
+        if not pcall(mqtt_pub,mqtt_path,tx) then
+          printLog('Reconnecting MQTT...')
+          mqtt_client:connect(mqtt_id)
+        end
+
       end        
     end
   end
@@ -219,4 +230,5 @@ if mqtt_host then
 end
 
 inp = open_dump()
+
 read_loop()