-
Notifications
You must be signed in to change notification settings - Fork 7
Examples
Johann Forster edited this page Jun 12, 2019
·
1 revision
... with JavaScript and fetch.
var resp = await fetch("/devices", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
// id: "5cde6d034b9f61" // let the server choose an id
name: "My Device 1", // readable device name
sensors: [{ // sensors list:
id: "6f840f0b1", // sensor id (hardware id)
name: "My Sensor 1", // readable name
}, {
id: "df34b9f612",
name: "My Sensor 2",
}],
actuators: [{ // actuators list:
id: "40f034",
name: "My Actuator 1",
}],
})
});
// the device id will be returned
var deviceId = await resp.json();
alert(`new device.id: ${deviceId}`);
Console output will be like:
new device.id: 5cde6d034b9f610ff8373bdb
var deviceId = "5cde6d034b9f610ff8373bdb";
await fetch(`/devices/${deviceId}/name`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify("New Device Name")
});
var deviceId = "5cde6d034b9f610ff8373bdb";
fetch(`/devices/${deviceId}`, {
method: "DELETE",
});
var resp = await fetch("/devices");
var devices = await resp.json();
console.log(devices);
Console output will be like: (without comments)
[{
name: "My Device 1",
id: "5cde6d034b9f61",
// Sensors List
sensors: [{
// unique sensor id
id: "6f840f0b1",
// Sensor name
name: "My Sensor 1",
// time when last modified
modified: "2019-06-03T12:31:34.331Z",
// time when created
created: "2019-06-03T12:31:34.331Z",
// time last value received
time: "2019-06-03T12:15:47.971Z",
// last value
value: null
},{
id: "df34b9f612",
name: "My Sensor 2",
modified: "0001-01-01T00:00:00Z",
created: "2019-06-03T12:31:34.331Z",
time: "2019-06-03T12:15:47.971Z",
value: null
}],
// Actuators List
actuators: [{
id: "40f034",
name: "My Actuator 1",
modified: "2019-06-03T12:30:52.106Z",
created: "2019-06-03T12:31:34.331Z",
time: "2019-06-03T12:15:47.971Z",
value: null
}],
modified: "2019-06-03T12:30:52.106Z",
created: "2019-06-03T12:31:34.331Z"
}]
- Modified-times for devices get updated when the device name changes (using POST .../name) but not if a new sensor or actuator gets added or deleted.
- Modified-times on sensors or actuators get updated when the sensor or actuator name changes, but not if a new value gets posted.
- Time and Value for each sensor are the same as at GET .../value
var deviceId = "5cde6d034b9f610ff8373bdb";
await fetch(`/devices/${deviceId}/sensors`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
id: "0ff8373bd", // sensor id (hardware id)
name: "My Sensor 3", // readable name
})
});
The same goes for actuators. Just replace sensors
with actuators
.
var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
await fetch(`/devices/${deviceId}/sensors/${sensorId}/name`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify("New Sensor Name")
});
var deviceId = "5cde6c194b9f610ff8373bda";
var resp = await fetch(`/devices/${deviceId}/sensors`);
var sensors = await resp.json();
console.log(sensors);
var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
fetch(`/devices/${deviceId}/sensors/${sensorId}`, {
method: "DELETE",
});
var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
var value = 42; // numeric value
// or
var value = "Temp45%23"; // string value
// or
var value = {lat: 52, long: 7}; // complex value
// or
var value = {
time: new Date(), // value at specific time
value: 42 // value
};
fetch(`/devices/${deviceId}/sensors/${sensorId}/value`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(value)
});
var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
var values = [42, 45, 47]; // values array
// or
var values = ["a", 0x0b, "cde"]; // ...
// or
var values = [
{ // values with timestamp
time: new Date(),
value: 42,
}, {
time: new Date(),
value: 43,
}
]
fetch(`/devices/${deviceId}/sensors/${sensorId}/values`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(values)
});
var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
var resp = await fetch(`/devices/${deviceId}/sensors/${sensorId}/value`);
var value = await resp.json();
console.log(value);
var sensorId = "0ff8373bd";
var deviceId = "5cde6d034b9f610ff8373bdb";
var resp = await fetch(`/devices/${deviceId}/sensors/${sensorId}/values`);
var values = await resp.json();
console.log(values);
fetch(`/clouds`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
rest: "api.waziup.io/api/v2",
mqtt: "api.waziup.io",
paused: true, // default false
credentials: {
username: "myUsername",
token: "myPassword"
}
})
});
// the cloud id will be returned
var cloudId = await resp.json();
alert(`new cloud.id: ${cloudId}`);
Console output will be like:
new cloud.id: 5ce2793d4b9f612a04a7951d
var resp = await fetch("/clouds");
var clouds = await resp.json();
console.log(clouds);
Output will be like:
{
"5ce2793d4b9f612a04a7951d": {
id: "5ce2793d4b9f612a04a7951d",
paused: true,
rest: "api.waziup.io/api/v2",
mqtt: "api.waziup.io",
credentials: {
username: "myUsername",
token: "myPassword"
},
// bytes in queue waiting for sync with server
queue: 136,
// sync status code (200 = OK)
statusCode: 200,
// readable status text or error, if any
statusText: "MQTT connected for persistent sync."
}
}
statusCode can be one of:
200
OK. The synchronization is active.
0
The synchronization is paused.
Any other code: There was a problem with this synchronization.
var cloudId = "5ce2793d4b9f612a04a7951d";
fetch(`/clouds/${cloudId}/paused`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(false) // or true
});
var cloudId = "5ce2793d4b9f612a04a7951d";
fetch(`/clouds/${cloudId}/credentials`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: "myNewUserName",
token: "myNewPassword",
})
});
var cloudId = "5ce2793d4b9f612a04a7951d";
fetch(`/clouds/${cloudId}/mqtt`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify("waziup.myserver.com")
});
var cloudId = "5ce2793d4b9f612a04a7951d";
fetch(`/clouds/${cloudId}/rest`, {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify("waziup.myserver.com")
});