Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languageros
/system script add dont-require-permissions=no name=tracking owner=admin policy=\
    ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon source="    # Name of an e\
    xisting 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\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. Same\r\
    \n    # usage as with 'addressRegex'.\r\
    \n    :local advertisingDataRegex \"\"\r\
    \n\r\
    \n    # Signal strength filter. E.g. -40 would only include Bluetooth advertisements\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    #KNOT identifier. Setup the KNOT name, for example KNOTA:\r\
    \n    :local knotname \"KNOT_A\"\r\
    \n\r\
    \n    ################################## Bluetooth ##################################\r\
    \n    # Find fresh Bluetooth advertisements\r\
    \n    :global btOldestAdvertisementTimestamp\r\
    \n    :if ([:typeof \$btOldestAdvertisementTimestamp] = \"nothing\") do={\r\
    \n        # First time this script has been run since booting, need to initialize\r\
    \n        # persistent variables\r\
    \n        :set \$btOldestAdvertisementTimestamp 0\r\
    \n    }\r\
    \n    :local advertisements [/iot bluetooth scanners advertisements print detail \\\r\
    \n        as-value where \\\r\
    \n            epoch > \$btOldestAdvertisementTimestamp and \\\r\
    \n            address ~ \$addressRegex and \\\r\
    \n            data ~ \$advertisingDataRegex and \\\r\
    \n            rssi > \$rssiThreshold\r\
    \n    ]\r\
    \n    :local advCount 0\r\
    \n    :local lastAdvTimestamp 0\r\
    \n    :local advJson \"\"\r\
    \n    :local advSeparator \"\"\r\
    \n\r\
    \n    # Remove semicolons from MAC/Bluetooth addresses\r\
    \n    :local minimizeMac do={\r\
    \n        :local minimized\r\
    \n        :local lastIdx ([:len \$address] - 1)\r\
    \n        :for idx from=0 to=\$lastIdx step=1 do={\r\
    \n            :local char [:pick \$address \$idx]\r\
    \n            :if (\$char != \":\") do={\r\
    \n                :set \$minimized \"\$minimized\$char\"\r\
    \n            }\r\
    \n        }\r\
    \n        :return \$minimized\r\
    \n    }\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        :local ad (\$adv->\"data\")\r\
    \n\r\
    \n:local obj \"\\\r\
    \n\\\"\$address\\\":[{\\\r\
    \n\\\"reporter\\\":\\\"\$knotname\\\",\\\r\
    \n\\\"ts\\\":\$epoch,\\\r\
    \n\\\"rssi\\\":\$rssi\\\r\
    \n}]\"\r\
    \n\r\
    \n        :set \$advCount (\$advCount + 1)\r\
    \n        :set \$lastAdvTimestamp \$epoch\r\
    \n       # Ensure that the last object is not terminated by a comma\r\
    \n        :set \$advJson \"\$advJson\$advSeparator\$obj\"\r\
    \n        :if (\$advSeparator = \"\") do={\r\
    \n            :set \$advSeparator \",\"}\r\
    \n    }\r\
    \n    :if (\$advCount > 0) do={\r\
    \n        :set \$btOldestAdvertisementTimestamp \$lastAdvTimestamp\r\
    \n    }\r\
    \n\r\
    \n    #################################### MQTT #####################################\r\
    \n    :local message \\\r\
    \n        \"{\$advJson}\"\r\
    \n    :log info \"\$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\r\
    \n    :put (\"[*] Done\")"

...