Commit 453ce884 by Martin Kotula

bug-fixes and example data

parent 33115e06
......@@ -4,7 +4,8 @@ define([
"components/sensor/sensorViewModel",
"queryHandlers/sensorReadingsQueryHandler",
"services/mqttListener",
"lodash"
"lodash",
"knockout-postbox"
], function (
ko,
homeTemplate,
......@@ -23,6 +24,8 @@ define([
var mqttListener = new MqttListener();
mqttListener.init();
ko.postbox.subscribe("sensorUpdate", _.bind(this.onSensorUpdated, this));
}
HomeViewModel.prototype.addSensor = function (key, sensorData) {
......@@ -41,6 +44,16 @@ define([
}
}
HomeViewModel.prototype.onSensorUpdated = function(data){
var key = data.name;
if(this.sensorsDict[key]){
this.updateSensor(key, data);
} else {
this.addSensor(key, data);
}
}
HomeViewModel.prototype.refresh = function () {
this.isRefreshing(true);
this.queryHandler.handle().then(_.bind(function(data) {
......@@ -53,6 +66,7 @@ define([
} else {
this.addSensor(key, latestReading);
}
}, this));
this.isRefreshing(false);
}, this));
......
......@@ -4,3 +4,16 @@
<input data-bind="textInput : inputText" type="text"/>
<button data-bind="click: publish" type="button">Publish</button>
<h3>Example data</h3>
<pre>
{ "name": "Sensor 1", "isOccupied": true, "timestamp": "2017-06-17T21:10:59.948Z"}
{ "name": "Sensor 2", "isOccupied": true, "timestamp": "2017-06-17T21:10:59.948Z"}
{ "name": "Sensor 1", "isOccupied": true, "timestamp": "2017-06-17T21:11:59.948Z"}
{ "name": "Sensor 2", "isOccupied": true, "timestamp": "2017-06-17T21:11:59.948Z"}
{ "name": "Sensor 1", "isOccupied": true, "timestamp": "2017-06-17T21:12:59.948Z"}
{ "name": "Sensor 1", "isOccupied": false, "timestamp": "2017-06-17T21:13:59.948Z"}
{ "name": "Sensor 3", "isOccupied": false, "timestamp": "2017-06-17T21:13:59.948Z"}
{ "name": "Sensor 3", "isOccupied": true, "timestamp": "2017-06-17T21:13:59.949Z"}
</pre>
\ No newline at end of file
......@@ -14,11 +14,17 @@ define(["app/config", "mqtt", "lodash", "knockout", "knockout-postbox"], functio
this.client.on('message', function (topic, message) {
// message is Buffer
try{
var messegeBody = JSON.parse(message.toString());
console.debug("Mqtt message received: " + message.toString());
ko.postbox.publish("sensorUpdate", messegeBody);
});
}
catch(e){
console.error("Failed to parse message into JSON: [" + message.toString() + "]");
}
});
};
MqttListener.prototype.end = function(){
if(this.client){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment