Commit 453ce884 by Martin Kotula

bug-fixes and example data

parent 33115e06
...@@ -4,7 +4,8 @@ define([ ...@@ -4,7 +4,8 @@ define([
"components/sensor/sensorViewModel", "components/sensor/sensorViewModel",
"queryHandlers/sensorReadingsQueryHandler", "queryHandlers/sensorReadingsQueryHandler",
"services/mqttListener", "services/mqttListener",
"lodash" "lodash",
"knockout-postbox"
], function ( ], function (
ko, ko,
homeTemplate, homeTemplate,
...@@ -23,6 +24,8 @@ define([ ...@@ -23,6 +24,8 @@ define([
var mqttListener = new MqttListener(); var mqttListener = new MqttListener();
mqttListener.init(); mqttListener.init();
ko.postbox.subscribe("sensorUpdate", _.bind(this.onSensorUpdated, this));
} }
HomeViewModel.prototype.addSensor = function (key, sensorData) { HomeViewModel.prototype.addSensor = function (key, sensorData) {
...@@ -41,6 +44,16 @@ define([ ...@@ -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 () { HomeViewModel.prototype.refresh = function () {
this.isRefreshing(true); this.isRefreshing(true);
this.queryHandler.handle().then(_.bind(function(data) { this.queryHandler.handle().then(_.bind(function(data) {
...@@ -53,6 +66,7 @@ define([ ...@@ -53,6 +66,7 @@ define([
} else { } else {
this.addSensor(key, latestReading); this.addSensor(key, latestReading);
} }
}, this)); }, this));
this.isRefreshing(false); this.isRefreshing(false);
}, this)); }, this));
......
...@@ -4,3 +4,16 @@ ...@@ -4,3 +4,16 @@
<input data-bind="textInput : inputText" type="text"/> <input data-bind="textInput : inputText" type="text"/>
<button data-bind="click: publish" type="button">Publish</button> <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 ...@@ -14,11 +14,17 @@ define(["app/config", "mqtt", "lodash", "knockout", "knockout-postbox"], functio
this.client.on('message', function (topic, message) { this.client.on('message', function (topic, message) {
// message is Buffer // message is Buffer
var messegeBody = JSON.parse(message.toString()); try{
console.debug("Mqtt message received: " + message.toString()); var messegeBody = JSON.parse(message.toString());
ko.postbox.publish("sensorUpdate", messegeBody); 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(){ MqttListener.prototype.end = function(){
if(this.client){ 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