Measurements service has a json interface which can be accessed from urls:
- get json object template for saving measurements array from url https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/measurements/template
- to save measurements array use url https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/measurements/save
- to query measurements use url https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/measurements/your-key-here?obj=your-meas-object&tag=your-meas-tag&data-tags=comma-separated-list-of-data-tags&from=from&to=to&take=20&inclusiveFrom=true&inclusiveTo=true&binaryValueFormat=ByteArray
- to get sensor info use url https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/sensors/your-key-here
- to add new sensor info or update sensor info use HTTP PUT to url (NB. the key must have modify access) https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/sensors/your-key-here/sensor-tag-here
- to delete sensor info use HTTP DELETE to url (NB. the key must have delete access) https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/sensors/your-key-here/sensor-tag-here
To save measurements via json post request
/json/measurements/save takes two "parameters": a key and an array of measurement objects
POST https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/measurements/save HTTP/1.1
User-Agent: Fiddler
Host: sami.savonia.fi
Content-Type: text/json
{
"key":"your-key-goes-here",
"measurements":
[
{
"Data":
[
{
"BinaryValue":[1,2,33,255],
"BinaryValueBase64":null,
"LongValue":null,
"Tag":"This is your sensor identification. This is unique to you. Tag max length is 50 characters.",
"TextValue":"your sensor can have some textual values also.",
"Value":3.14,
"XmlValue":null
}
],
"Location":{"Latitude":62.8989,"Longitude":27.6630},
"Note":"if this measurement has a note",
"Object":"your-measurement-object, Object max length is 50 characters.",
"Tag":"your-measurement-tag, Tag max length is 50 characters.",
"Timestamp":{"DateTime":"\/Date(1412878153764)\/","OffsetMinutes":180},
"TimestampISO8601": "2017-01-10 15:30:00"
}
]
}
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function () {
var writeMeasurement = function () {
var key = 'your-key-here';
var url = 'https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/measurements/save';
var measurementPackageContent = {
"key": key,
"measurements": [
{
"Data": [
{
"Tag": "json test",
"Value": 3.14
}
],
"Object": "json cors test",
"Tag": "json cors tag",
"TimestampISO8601": "2017-01-10 12:30:00"
}
]
}
$.ajax({
type: 'POST',
url: url,
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify(measurementPackageContent)
})
.done(function (data) {
// post request succeeded, check the result
if (data.Success) {
// data was saved!
}
})
.fail(function (jqXHR, textStatus, error) {
// something went horribly wrong...
});
}
});
</script>
Note 1! TimestampISO8601 or Timestamp must be present. If both timestamps are present the value in TimestampISO8601 wins.
Note 2! It's probably easier to use the TimestampISO8601 for measurement time (http://www.w3.org/TR/NOTE-datetime).
Note 3! Javascript date value Date(1412878153764) in the samples abowe equals 9.10.2014 21:09:13 when presented as Finnish (FLE Standard Time) datetime.
Note 4! Binary data can be send in Data with properties BinaryValue or BinaryValueBase64. When both are present and the value in BinaryValueBase64 is valid base64 encoded string then that value wins.
Note 5! Binary data property BinaryValue must be array of unsigned byte values i.e. array of values in range [0...255].
Note 6! Location uses DD, decimal degree notation. Valid values are: latitude -90 to 90 and longitude -180 to 180.
More info about DateTime parsing is in Serializing Dates in JSON.
To read measurements via json get request
GET https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/measurements/your-key-here?obj=your-meas-object&tag=your-meas-tag&data-tags=comma-separated-list-of-data-tags&from=from&to=to&take=20&inclusiveFrom=true&inclusiveTo=true&binaryValueFormat=ByteArray HTTP/1.1
User-Agent: Fiddler
Host: sami.savonia.fi
Content-Type: text/json
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function () {
var readMeasurements = function () {
var key = 'your-key-here';
var urlBase = 'https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/measurements/';
var url = urlBase + key;
// you can append query parameters to url if needed
url = url + '?obj=your-meas-object&tag=your-meas-tag&data-tags=comma-separated-list-of-data-tags&from=from&to=to&take=20&inclusiveFrom=true&inclusiveTo=true&binaryValueFormat=ByteArray';
$.getJSON(url, null, function (data) {
var receivedData = JSON.stringify(data);
});
}
});
</script>
Note 1! For parameters from and to use YYYY-MM-DD format. Example: from=2014-10-23. This is also known as ISO 8601 format http://www.w3.org/TR/NOTE-datetime.
Note 2! Parameters from and to may also contain time value in YYYY-MM-DDTHH:mm:ss format. Example: from=2014-10-23T12:45:00. If time portion is not provided then it is set to 0:00:00 before database search.
Note 3! key parameter is required, other parameters are optional.
Note 4! If both from and to parameters are submitted then take can be omitted. Otherwise take will have default value of 20.
if using jQuery and jsonp add callback=? query parameter as seen in http://www.bendewey.com/index.php/186/using-jsonp-with-wcf-and-jquery
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function () {
var readMeasurements = function () {
var key = 'your-key-here';
var urlBase = 'https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/measurements/';
var url = urlBase + key;
// you can append query parameters to url if needed
url = url + '?obj=your-meas-object&tag=your-meas-tag&data-tags=comma-separated-list-of-data-tags&from=from&to=to&take=20&inclusiveFrom=true&inclusiveTo=true&binaryValueFormat=ByteArray';
// add callback to url
url = url + '?callback=?';
$.getJSON(url, null, function (data) {
var receivedData = JSON.stringify(data);
});
}
});
</script>
To get, add or update and delete sensor information via json
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function () {
// get sensors
var getSensors = function () {
var key = 'your-read-key';
var url = 'https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/sensors/' + key;
$.getJSON(url, null, function (data) {
var sensorInfo = JSON.stringify(data, null, 4));
});
};
});
</script>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function () {
// add / update sensor
var saveSensor = function () {
var tag = 'your-sensor-tag';
var key = 'your-modify-key';
var url = 'https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/sensors/' + key + '/' + tag;
var sensorData = {};
sensorData = JSON.parse($('#sensor-data').val()); // get sensor data from somewhere
$.ajax({
type: 'PUT',
url: url,
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: JSON.stringify(sensorData)
})
.done(function (data) {
content.html('Success: ' + JSON.stringify(data));
})
.fail(function (jqXHR, textStatus, error) {
content.html('PUT error: ' + jqXHR + textStatus + error);
});
};
});
</script>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function () {
// delete sensor
var deleteSensor = function () {
var tag = 'your-sensor-tag';
var key = 'your-delete-key';
var url = 'https://sami.savonia.fi/Service/3.0/MeasurementsService.svc/json/sensors/' + key + '/' + tag;
$.ajax({
type: 'DELETE',
url: url,
contentType: "application/json; charset=utf-8",
dataType: 'json'
})
.done(function (data) {
content.html('Success: ' + JSON.stringify(data));
})
.fail(function (jqXHR, textStatus, error) {
content.html('DELETE error: ' + jqXHR + textStatus + error);
});
};
});
</script>