...
Code Block | ||
---|---|---|
| ||
/system script add dont-require-permissions=no name=tracking owner=admin policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="# Requ\
ired packages: iot\r\
\n\r\
\n################################ Configuration ##############################\
##\r\
\n# Name of an existing MQTT broker that should be used for publishing\r\
\n:local broker \"tb\"\r\
\n\r\
\n# MQTT topic where the message should be published\r\
\n:local topic \"v1/gateway/telemetry\"\r\
\n\r\
\n# POSIX regex for filtering advertisement Bluetooth addresses. E.g. \"^BC:33:\
AC\"\r\
\n# would only include addresses which start with those 3 octets.\r\
\n# To disable this filter, set it to \"\"\r\
\n:local addressRegex \"\"\r\
\n\r\
\n# POSIX regex for filtering Bluetooth advertisements based on their data. Sam\
e\r\
\n# usage as with 'addressRegex'.\r\
\n:local advertisingDataRegex \"\"\r\
\n\r\
\n# Signal strength filter. E.g. -40 would only include Bluetooth advertisement\
s\r\
\n# whose signal strength is stronger than -40dBm.\r\
\n# To disable this filter, set it to \"\"\r\
\n:local rssiThreshold \"\"\r\
\n\r\
\n#Name the KNOT. Identity of the unit that will be senting the message. This n\
ame will be reported to the MQTT broker.\r\
\n:local gwName \"KNOT_A\"\r\
\n\r\
\n################################## Bluetooth ################################\
##\r\
\n:put (\"[*] Gathering Bluetooth info...\")\r\
\n\r\
\n:global makeRecord do={\r\
\n :local jsonStr \"{\\\"ts\\\":\$ts,\\\"values\\\":{\\\"reporter\\\":\\\"\$\
gwName\\\",\\\"rssi\\\":\$rssi}}\"\r\
\n :return \$jsonStr\r\
\n} \r\
\n\r\
\n# array of record strings collected for each advertising MAC address\r\
\n:global macRecords [:toarray \"\"]\r\
\n\r\
\n# process advertisements and update macRecords\r\
\n:local gwName \"KNOT_1\"\r\
\n:local advertisements [/iot bluetooth scanners advertisements print detail as\
-value where \\\r\
\naddress ~ \$addressRegex and \\\r\
\ndata ~ \$advertisingDataRegex and \\\r\
\nrssi > \$rssiThreshold]\r\
\n\r\
\n/iot/bluetooth/scanners/advertisements clear\r\
\n\r\
\n:foreach adv in=\$advertisements do={\r\
\n:local address (\$adv->\"address\")\r\
\n:local rssi (\$adv->\"rssi\")\r\
\n:local epoch (\$adv->\"epoch\")\r\
\n \r\
\n:local recordStr [\$makeRecord ts=\$epoch gwName=\$gwName rssi=\$rssi]\r\
\n\r\
\n:if ([:len (\$macRecords->\$address)] > 0) do={\r\
\n:local str (\$macRecords->\$address)\r\
\n:local newStr \"\$str,\$recordStr\"\r\
\n:set (\$macRecords->\$address) \$newStr} else={:set (\$macRecords->\$address)\
\_\$recordStr}}\r\
\n\r\
\n# TODO: add some logic to decide when we want to send data\r\
\n:local sendData true\r\
\n\r\
\n:if (\$sendData) do={\r\
\n:local jsonStr \"{\"\r\
\n\r\
\n:foreach addr,advRec in=\$macRecords do={\r\
\n:set jsonStr \"\$jsonStr\\\"\$addr\\\":[\$advRec],\"}\r\
\n\r\
\n:local payloadlength\r\
\n:set payloadlength [:len (\$jsonStr)]\r\
\n:local remcom\r\
\n:set remcom [:pick \$jsonStr 0 (\$payloadlength-1)]\r\
\n:set jsonStr \"\$remcom}\"\r\
\n:local message\r\
\n:set message \"\$jsonStr\"\r\
\n:log info \"\$message\";\r\
\n:put (\"[*] Message structured: \$message\")\r\
\n:put (\"[*] Total message size: \$[:len \$message] bytes\")\r\
\n:put (\"[*] Sending message to MQTT broker...\")\r\
\n/iot mqtt publish broker=\"\$broker\" topic=\"\$topic\" message=\$message}" |
...