Add endpoint to get device state

This commit is contained in:
Pablo Carranza Velez 2016-02-15 20:34:11 +00:00
parent 38fb0c6257
commit 670f318c39
4 changed files with 44 additions and 1 deletions

View File

@ -1,3 +1,4 @@
* Add endpoint to get device state [Pablo]
* Check for valid strings or ints in all config values [Pablo]
* Remove quotes in OS version [Pablo]

View File

@ -405,3 +405,38 @@ $ curl -X POST --header "Content-Type:application/json" \
--data '{"deviceId": <deviceId>, "appId": <appId>}' \
"https://api.resin.io/supervisor/v1/regenerate-api-key"
```
<hr>
### GET /v1/device
Introduced in supervisor v1.6.
Returns the current device state, as reported to the Resin API.
The state is a JSON object that contains some or all of the following:
* `api_port`: Port on which the supervisor is listening.
* `ip_address`: Space-separated list of IP addresses of the device.
* `status`: Status of the device regarding the app, as a string, i.e. "Stopping", "Starting", "Downloading", "Installing", "Idle".
* `download_progress`: Amount of the application image that has been downloaded, expressed as a percentage.
* `os_version`: Version of the host OS running on the device.
* `supervisor_version`: Version of the supervisor running on the device.
Other attributes may be added in the future, and some may be missing or null if they haven't been set yet.
#### Examples:
From the app on the device:
```bash
$ curl -X GET --header "Content-Type:application/json" \
"$RESIN_SUPERVISOR_ADDRESS/v1/device?apikey=$RESIN_SUPERVISOR_API_KEY"
```
Response:
```json
{"api_port":48484,"ip_address":"192.168.0.114 10.42.0.3","status":"Downloading","download_progress":84,"os_version":"Resin OS 1.0.4 (fido)","supervisor_version":"1.6.0"}
```
Remotely via the API proxy:
```bash
$ curl -X POST --header "Content-Type:application/json" \
--header "Authorization: Bearer <auth token>" \
--data '{"deviceId": <deviceId>, "appId": <appId>, "method": "GET"}' \
"https://api.resin.io/supervisor/v1/device"
```

View File

@ -136,4 +136,7 @@ module.exports = (application) ->
.catch (err) ->
res.status(503).send(err?.message or err or 'Unknown error')
api.get '/v1/device', (req, res) ->
res.json(device.getState())
return api

View File

@ -128,12 +128,16 @@ exports.getDeviceType = do ->
throw new Error('Device type not specified in config file')
return configFromFile.deviceType
targetState = {}
exports.getState = ->
fieldsToOmit = ['api_secret', 'logs_channel', 'provisioning_progress', 'provisioning_state']
return _.omit(targetState, fieldsToOmit)
# Calling this function updates the local device state, which is then used to synchronise
# the remote device state, repeating any failed updates until successfully synchronised.
# This function will also optimise updates by merging multiple updates and only sending the latest state.
exports.updateState = do ->
applyPromise = Promise.resolve()
targetState = {}
actualState = {}
getStateDiff = ->