4 socket = require("socket")
6 function getConfig(configname)
8 local status,uci = pcall(require,"uci")
20 local cur=uci.cursor()
22 logging = cur.get(config,"logging","enabled")
24 mqtt_host = cur.get(config,"mqtt","host")
25 mqtt_port = cur.get(config,"mqtt","port")
26 mqtt_id = cur.get(config,"mqtt","id")
27 mqtt_topic = cur.get(config,"mqtt","topic")
29 mqtt_user = cur.get(config,"mqtt","user")
30 mqtt_passwd = cur.get(config,"mqtt","password")
34 local status,ini = pcall(require,"ini")
42 config="/etc/beacon.ini"
45 local cur=ini.parse_file(config)
47 logging = cur["logging"]["enabled"]
49 mqtt_host = cur["mqtt"]["host"]
50 mqtt_port = cur["mqtt"]["port"]
51 mqtt_id = cur["mqtt"]["id"]
52 mqtt_topic = cur["mqtt"]["topic"]
54 mqtt_user = cur["mqtt"]["user"]
55 mqtt_passwd = cur["mqtt"]["password"]
59 hostname = socket.dns.gethostname()
60 if mqtt_host and not mqtt_id then
61 socket = require("socket")
62 posix = require("posix")
64 mqtt_id="beaconmon-"..hostname.."-"..pid
67 if mqtt_host and not mqtt_port then
71 if mqtt_host and not mqtt_topic then
72 mqtt_topic = 'beaconmon/{type}/{details}'
77 function capture(cmd, raw)
78 local f = assert(io.popen(cmd, 'r'))
79 local s = assert(f:read('*a'))
81 if raw then return s end
82 s = string.gsub(s, '^%s+', '')
83 s = string.gsub(s, '%s+$', '')
84 s = string.gsub(s, '[\n\r]+', ' ')
88 function mqtt_encode(str)
90 str = string.gsub (str, "\n", "")
91 str = string.gsub (str, ":", "-")
92 str = string.gsub (str, "/", "-")
93 str = string.gsub (str, " ", "_")
98 function printLog(str)
99 if logging=="yes" then
100 capture("logger -t beaconmon \""..str.."\"")
106 function run_command(cmd)
108 local file = assert(io.popen(cmd, 'r'))
109 local output = file:read('*all')
116 f = assert(io.popen ("/usr/bin/btmon"))
117 run_command("hciconfig hci0 down")
118 run_command("hciconfig hci0 up")
119 f_null = assert(io.popen ("hcitool lescan --duplicates"))
126 return json.encode(o)
130 return (s:gsub("^%s*(.-)%s*$", "%1"))
133 local function starts_with(str, start)
134 return str:sub(1, #start) == start
137 function mqtt_pub(path,value)
138 res=mqtt_client:publish(path,value,0,false)
139 printLog("Pub "..path.." returned "..res);
143 function process_packet(packet)
151 packet['origin'] = hostname
153 mac = packet['Address']
154 uuid = packet['UUID']
155 type = packet['Type']
156 name = packet['Name (complete)']
158 if type and starts_with(type,'iBeacon') then
172 if not (type=="unknown") then
173 mqtt_path=string.gsub(mqtt_topic,"{(.-)}",
176 return mqtt_encode(type)
177 elseif name=="details" then
178 return mqtt_encode(details)
180 return '{'..name..'}'
184 if not pcall(mqtt_pub,mqtt_path,dump(packet)) then
185 printLog('Reconnecting MQTT...')
186 mqtt_client:connect(mqtt_host,mqtt_port)
193 function starts(String,Start)
194 return string.sub(String,1,string.len(Start))==Start
197 function split(inputstr, sep)
202 for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
226 value=trim(table.concat(t,':',2))
227 if name=="Address" then
228 value=split(value)[1]
231 elseif #t==1 and name then
232 if not(packet[name..'.list']) then
233 packet[name..'.list']={}
235 table.insert(packet[name..'.list'],(trim(t[1])))
239 if starts(str,'> HCI Event: LE Meta Event (0x3e)') then
242 elseif starts(str,'RSSI:') then
244 process_packet(packet)
254 io.stdout:setvbuf('no')
255 io.stdin:setvbuf('no')
260 MQTT = require "mosquitto"
261 mqtt_client = MQTT.new(mqtt_id)
263 mqtt_client:login_set(mqtt_user, mqtt_passwd)
265 mqtt_client:connect(mqtt_host,mqtt_port)