5 function getConfig(configname)
7 local uci=require("uci")
16 logging = cur.get(config,"logging","enabled")
18 mqtt_host = cur.get(config,"mqtt","host")
19 mqtt_port = cur.get(config,"mqtt","port")
20 mqtt_id = cur.get(config,"mqtt","id")
21 mqtt_topic = cur.get(config,"mqtt","topic")
23 mqtt_user = cur.get(config,"mqtt","user")
24 mqtt_passwd = cur.get(config,"mqtt","password")
26 if mqtt_host and not mqtt_id then
27 socket = require("socket")
28 posix = require("posix")
29 hostname = socket.dns.gethostname()
31 mqtt_id="beaconmon-"..hostname.."-"..pid
34 if mqtt_host and not mqtt_port then
38 if mqtt_host and not mqtt_topic then
39 mqtt_topic = 'beaconmon/{type}/{details}'
44 function capture(cmd, raw)
45 local f = assert(io.popen(cmd, 'r'))
46 local s = assert(f:read('*a'))
48 if raw then return s end
49 s = string.gsub(s, '^%s+', '')
50 s = string.gsub(s, '%s+$', '')
51 s = string.gsub(s, '[\n\r]+', ' ')
55 function mqtt_encode(str)
57 str = string.gsub (str, "\n", "")
58 str = string.gsub (str, ":", "-")
59 str = string.gsub (str, "/", "-")
60 str = string.gsub (str, " ", "_")
65 function printLog(str)
66 if logging=="yes" then
67 capture("logger -t beaconmon \""..str.."\"")
73 function run_command(cmd)
75 local file = assert(io.popen(cmd, 'r'))
76 local output = file:read('*all')
83 run_command("/bin/kill `/usr/bin/pgrep btmon`")
84 run_command("/bin/kill `/usr/bin/pgrep hcitool`")
85 f = assert(io.popen ("/usr/bin/stdbuf -o0 /usr/bin/btmon"))
86 run_command("/usr/bin/hciconfig hci0 down")
87 run_command("/usr/bin/hciconfig hci0 up")
88 f_null = assert(io.popen ("/usr/bin/hcitool lescan --duplicates --passive"))
99 return (s:gsub("^%s*(.-)%s*$", "%1"))
102 local function starts_with(str, start)
103 return str:sub(1, #start) == start
106 function mqtt_pub(path,value)
107 res=mqtt_client:publish(path,value)
108 printLog("Pub "..path.." returned "..res);
112 function process_packet(packet)
120 mac = packet['Address']
121 uuid = packet['UUID']
122 type = packet['Type']
123 name = packet['Name (complete)']
127 if type and starts_with(type,'iBeacon') then
141 if not (type=="unknown") then
142 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
145 return mqtt_encode(type)
146 elseif name=="details" then
147 return mqtt_encode(details)
149 return '{'..name..'}'
153 if not pcall(mqtt_pub,mqtt_path,dump(packet)) then
154 printLog('Reconnecting MQTT...')
155 mqtt_client:connect(mqtt_id)
162 function starts(String,Start)
163 return string.sub(String,1,string.len(Start))==Start
166 function split(inputstr, sep)
171 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
195 value=trim(table.concat(t,':',2))
196 if name=="Address" then
197 value=split(value)[1]
200 elseif #t==1 and name then
201 if not(packet[name..'.list']) then
202 packet[name..'.list']={}
204 table.insert(packet[name..'.list'],(trim(t[1])))
208 if starts(str,'> HCI Event: LE Meta Event (0x3e)') then
211 elseif starts(str,'RSSI:') then
213 process_packet(packet)
223 io.stdout:setvbuf('no')
224 io.stdin:setvbuf('no')
229 MQTT = require "mosquitto"
230 mqtt_client = MQTT.new(mqtt_id)
232 mqtt_client:login_set(mqtt_user, mqtt_passwd)
234 mqtt_client:connect(mqtt_host,mqtt_port)