Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 1 | angular.module('bmcApp').controller('sensorController', [ |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 2 | '$scope', '$http', '$location', 'websocketService', |
| 3 | function($scope, $http, $location, websocketService) { |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame^] | 4 | $scope.smartTablePageSize = 10; |
| 5 | $scope.next_id = 0; |
| 6 | websocketService.start('/sensorws', function(evt) { |
| 7 | var obj = JSON.parse(evt.data); |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 8 | |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame^] | 9 | $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 Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 18 | } |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame^] | 19 | } |
| 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 Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 32 | }); |
| 33 | |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame^] | 34 | $scope.rowCollection = []; |
| 35 | |
Ed Tanous | cc5a37f | 2017-05-11 10:27:23 -0700 | [diff] [blame] | 36 | } |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 37 | ]); |
| 38 | |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame^] | 39 | app.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 Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 47 | } |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame^] | 48 | var websocket = new WebSocket(protocol + host + ':' + port + url); |
| 49 | websocket.onopen = function() {}; |
| 50 | websocket.onclose = function() {}; |
| 51 | websocket.onmessage = function(evt) { callback(evt); }; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 52 | } |
Ed Tanous | 5fceeb4 | 2017-06-28 09:43:09 -0700 | [diff] [blame^] | 53 | } |
| 54 | }); |