3 function getConfig(configname)
5 local uci=require("uci")
14 logging = cur.get(config,"logging","enabled")
16 mqtt_host = cur.get(config,"mqtt","host")
17 mqtt_port = cur.get(config,"mqtt","port")
18 mqtt_id = cur.get(config,"mqtt","id")
19 mqtt_topic = cur.get(config,"mqtt","topic")
21 mqtt_user = cur.get(config,"mqtt","user")
22 mqtt_passwd = cur.get(config,"mqtt","password")
24 if mqtt_host and not mqtt_id then
28 if mqtt_host and not mqtt_port then
32 if mqtt_host and not mqtt_topic then
33 mqtt_topic = 'beaconmon/{type}/{details}'
38 function capture(cmd, raw)
39 local f = assert(io.popen(cmd, 'r'))
40 local s = assert(f:read('*a'))
42 if raw then return s end
43 s = string.gsub(s, '^%s+', '')
44 s = string.gsub(s, '%s+$', '')
45 s = string.gsub(s, '[\n\r]+', ' ')
49 function mqtt_encode(str)
51 str = string.gsub (str, "\n", "")
52 str = string.gsub (str, "/", "-")
57 function printLog(str)
59 capture("logger -t beaconmon "..str)
65 function run_command(cmd)
67 local file = assert(io.popen(cmd, 'r'))
68 local output = file:read('*all')
75 run_command("/bin/kill `/usr/bin/pgrep hcidump`")
76 run_command("/bin/kill `/usr/bin/pgrep hcitool`")
77 f = assert(io.popen ("/usr/bin/hcidump --raw"))
78 run_command("/usr/bin/hciconfig hci0 down")
79 run_command("/usr/bin/hciconfig hci0 up")
80 f_null = assert(io.popen ("/usr/bin/hcitool lescan --duplicates"))
87 if type(o) == 'table' then
89 for k,v in pairs(o) do
90 if type(k) ~= 'number' then k = '"'..k..'"' end
91 s = s .. '['..k..'] = ' .. dump(v) .. ','
92 -- s = s .. dump(v) .. ','
101 return (s:gsub("^%s*(.-)%s*$", "%1"))
104 function mqtt_pub(path,value)
105 printLog("Pub "..path.." "..value)
106 return mqtt_client:publish(path,value)
109 function process_packet(packet)
125 if packet:len()>1 then
127 while packet:len()>2 do
128 bytes[idx]=trim(packet:sub(1,3))
134 if bytes[1]=='04' and bytes[2]=='3E' then
137 mac=bytes[13]..':'..bytes[12]..':'..bytes[11]..':'..bytes[10]..':'..bytes[9]..':'..bytes[8]
139 power=tonumber("0x"..bytes[len-1])-256
140 tx=tonumber("0x"..bytes[len])-256
143 paysublen=tonumber('0x'..bytes[j])
144 if bytes[j+1]=="FF" and bytes[j+2]=="4C" and bytes[j+3]=="00" and bytes[j+4]=="02" and bytes[j+5]=="15" then
145 -- Standard UUID iBeacon
147 local uuid1=bytes[j+6]..bytes[j+7]..bytes[j+8]..bytes[j+9]
148 local uuid2=bytes[j+10]..bytes[j+11]
149 local uuid3=bytes[j+12]..bytes[j+13]
150 local uuid4=bytes[j+14]..bytes[j+15]
151 local uuid5=bytes[j+16]..bytes[j+17]..bytes[j+18]..bytes[j+19]..bytes[j+20]..bytes[j+21]
152 uuid=string.lower(uuid1..'-'..uuid2..'-'..uuid3..'-'..uuid4..'-'..uuid5)
153 major=bytes[j+23]..bytes[j+22]
154 minor=bytes[j+25]..bytes[j+24]
158 if type=="ibeacon" then
159 printLog(string.format("{type:'ibeacon',mac:'%s',uuid:'%s',major:'%s',minor:'%s',power:%d,tx:%d}",mac,uuid,major,minor,power,tx))
160 details=uuid..'/'..major..'/'..minor
166 if not (type=="unknown") then
167 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
170 return mqtt_encode(type)
171 elseif name=="details" then
172 return mqtt_encode(details)
174 return '{'..name..'}'
178 if not pcall(mqtt_pub,mqtt_path,tx) then
179 printLog('Reconnecting MQTT...')
180 mqtt_client:connect(mqtt_id)
193 for line in inp:lines() do
198 if lchr=="<" or lchr==">" then
199 line = trim(line:sub(2))
202 if not (lchr == " ") then
203 process_packet(packet)
210 packet = packet .. " " .. line
211 process_packet(packet)
214 packet = packet .. " " .. line
224 MQTT = require "paho.mqtt"
225 mqtt_client = MQTT.client.create(mqtt_host, mqtt_port)
227 mqtt_client:auth(mqtt_user, mqtt_passwd)
229 mqtt_client:connect(mqtt_id)