blob: 0fa9045d8876fcc35599c049eff7529a0d8e751c [file] [log] [blame]
Ed Tanouscc5a37f2017-05-11 10:27:23 -07001angular.module('bmcApp').controller('sensorController', [
Ed Tanous4d92cbf2017-06-22 15:41:02 -07002 '$scope', '$http', '$location', 'websocketService',
3 function($scope, $http, $location, websocketService) {
Ed Tanous5fceeb42017-06-28 09:43:09 -07004 $scope.smartTablePageSize = 10;
5 $scope.next_id = 0;
6 websocketService.start('/sensorws', function(evt) {
7 var obj = JSON.parse(evt.data);
Ed Tanous4d92cbf2017-06-22 15:41:02 -07008
Ed Tanous5fceeb42017-06-28 09:43:09 -07009 $scope.$apply(function() {
10 for (var sensor_name in obj) {
11 var found = false;
12 for (var sensor_index in $scope.rowCollection) {
13 var sensor_object = $scope.rowCollection[sensor_index];
14 if (sensor_object.name === sensor_name) {
15 sensor_object.value = obj[sensor_name];
16 found = true;
17 break;
Ed Tanous4d92cbf2017-06-22 15:41:02 -070018 }
Ed Tanous5fceeb42017-06-28 09:43:09 -070019 }
20 if (!found) {
21 console.log(sensor_name + ' -> ' + obj[sensor_name]);
22 $scope.next_id = $scope.next_id + 1;
23
24 $scope.rowCollection.push({
25 id : $scope.next_id,
26 name : sensor_name,
27 value : obj[sensor_name],
28 });
29 }
30 };
31 });
Ed Tanous4d92cbf2017-06-22 15:41:02 -070032 });
33
Ed Tanous5fceeb42017-06-28 09:43:09 -070034 $scope.rowCollection = [];
35
Ed Tanouscc5a37f2017-05-11 10:27:23 -070036 }
Ed Tanous4d92cbf2017-06-22 15:41:02 -070037]);
38
Ed Tanous5fceeb42017-06-28 09:43:09 -070039app.factory('websocketService', function($location) {
40 return {
41 start: function(url, callback) {
42 var host = $location.host();
43 var port = 18080;
44 var protocol = 'wss://';
45 if ($location.protocol() === 'http') {
46 protocol = 'ws://';
Ed Tanous4d92cbf2017-06-22 15:41:02 -070047 }
Ed Tanous5fceeb42017-06-28 09:43:09 -070048 var websocket = new WebSocket(protocol + host + ':' + port + url);
49 websocket.onopen = function() {};
50 websocket.onclose = function() {};
51 websocket.onmessage = function(evt) { callback(evt); };
Ed Tanous4d92cbf2017-06-22 15:41:02 -070052 }
Ed Tanous5fceeb42017-06-28 09:43:09 -070053 }
54});