Enumeration of Devices, Properties, Values, Events on a Gateway via Android/ReactJS?

I would like to enumerate the devices on a gateway (and later query for their properties and values) via Android or Javascript/React Native (ie similar to the Things and descendant pages from the gateway)?

I see that the gateway URL patterns are https://ipaddress/things and https://ipaddress/things/macAddress for human readable displays of devices on the gateway and device properties and values, respectively, on a web page. As a backup plan, I could manually parse those web pages.

Is there an API, guide, and/or example on directly enumerating devices and interacting with devices on the gateway? I’m new to Mozilla Web and have reviewed the docs and Gateway User Guide and searched this forum but didn’t find anything.

Hi @technoprobic, welcome.

The gateway implements the Web Thing API, documented here.

Tips:

  1. In order to receive JSON responses you need to set the HTTP Accept header to application/json, otherwise you will get the HTML representation of a resource.
  2. You will also need a JSON Web Token (JWT) in an Authorization header

You can generate a JWT from the gateway’s web interface in Settings -> Developer -> Create local authorization. Once you’ve generated the token you will be shown examples of how to use it in Curl, JavaScript, Python and Rust.

I hope this helps, feel free to ask any other questions.

1 Like

Thanks for that information. I’ve been able to enumerate devices, properties, and values of devices. I’ve also been able to enumerate historical events.

I’m currently trying to receive realtime event notifications, attempting to receive events notifications for a Flic button. I’m trying to use the websocket approach ( https://iot.mozilla.org/wot/#protocol-handshake ) and then sending an addEventSubscription message ( https://iot.mozilla.org/wot/#addeventsubscription-message ) to the gateway but it seems as soon as I send any message, the gateway closes the connection.

For the handshake, I tried various approaches to wss://mysubdomain.mozilla-iot.org/things/myflicname , including just a basic connection and also adding Authorization and Accept header fields with the same values that work with the HTTP queries. I consistently receive what appears to be the expected valid response from the gateway on the handshake:

101 Switching Protocols … (1083ms)
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: gWmxRLm4hlE2suUIqJI0QXC4aPA=

After the connection is open, I’ve tried to send various messages, such as:
{
“messageType”: “addEventSubscription”,
“data”: {
“singleClick”: {}
}
}

But whenever I try to send a message to the gateway, the gateway closes the connection with a 1005 response. If I wait several seconds before I send the message, the connection remains open until I try to send the message, but then closes when I try to send the message. I’ve also submitted the exact same handshake and messages to wss://echo.websocket.org , where my messages are successfully received and echoed back to me.

@benfrancis Do you have any suggestions?

How are you constructing your requests?

Hmm, I’ve never implemented the handshake manually because I’ve always used the JavaScript WebSocket API.

Ideally you would use an existing WebSocket API or library to form requests properly, though if wss://echo.websocket.org is echoing your messages back it does sound like you’re doing it right. Difficult to guess what’s wrong without more information.

I’ve constructed various requests using OKHTTP, one of the most popular HTTP and WebSocket Android clients and the same client that I’m successfully using for all of the HTTP requests and to wss://echo.websocket.org.

I’ve tried umpteen different requests with the key two being just an unmodified Websocket request and the other being to set the Authorization field and Accept fields to the same values that are used in the HTTP requests.