diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-08 20:39:01 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-08 20:39:01 +0200 |
commit | 76c273eaf316e0a8e3eadf93674ffa0aad35f7fd (patch) | |
tree | d8727077ba3a90191d49f5c0fb07b612fa43b6c3 /src | |
parent | 694cc18a40ae5c71ec280b3aea2e1fa4d51ab640 (diff) | |
download | beer-duke-76c273eaf316e0a8e3eadf93674ffa0aad35f7fd.tar.gz beer-duke-76c273eaf316e0a8e3eadf93674ffa0aad35f7fd.tar.bz2 beer-duke-76c273eaf316e0a8e3eadf93674ffa0aad35f7fd.tar.xz beer-duke-76c273eaf316e0a8e3eadf93674ffa0aad35f7fd.zip |
wip
Diffstat (limited to 'src')
-rw-r--r-- | src/BeerDuke.js | 42 | ||||
-rw-r--r-- | src/BeerDukeController.js | 28 | ||||
-rw-r--r-- | src/BeerDukeTap.js | 6 | ||||
-rw-r--r-- | src/controller.html | 1 | ||||
-rw-r--r-- | src/templates/controller.html | 11 | ||||
-rw-r--r-- | src/templates/tap.html | 2 |
6 files changed, 70 insertions, 20 deletions
diff --git a/src/BeerDuke.js b/src/BeerDuke.js index eb30307..b30ee33 100644 --- a/src/BeerDuke.js +++ b/src/BeerDuke.js @@ -54,14 +54,18 @@ var connected_ = false; var messages = []; var callbacks = {}; + var problems = {}; function connect(type) { var clientId = BeerDukeSettings.values.clientId + "-" + type; var client = new Paho.MQTT.Client("wss://trygvis.io:9001/", clientId); - client.onConnectionLost = function (a) { + client.onConnectionLost = function () { + console.log('onConnectionLost =', arguments); + problems.wat = 'hei'; + var args = arguments; $timeout(function () { $rootScope.$apply(function () { - onConnectionLost(a); + onConnectionLost.apply(self, args); }) }); }; @@ -78,6 +82,7 @@ self.client.connect({ onSuccess: function () { + problems.connect_response = arguments; var args = arguments; $timeout(function () { $rootScope.$apply(function () { @@ -89,19 +94,20 @@ } function subscribe(name) { + $log.info('subscribing to ' + name); return self.client.subscribe(name); } function connected() { - return self.connected_; + return connected_; } function onConnect() { - $log.info('Connected'); + $log.info('Connected', arguments); - self.connected_ = true; + connected_ = true; - invoke('onConnect'); + invoke('onConnect', arguments); } function invoke(name, args) { @@ -117,11 +123,9 @@ } function onConnectionLost(responseObject) { - self.connected_ = false; + connected_ = false; - //if (responseObject.errorCode !== 0) - $log.warn("onConnectionLost:", responseObject); - $log.warn("onConnectionLost:", responseObject.errorMessage); + problems.connectionLost = responseObject; } function onMessageArrived(message) { @@ -140,13 +144,21 @@ self.client.send(message); } + function updateSlots(slot, count) { + var message = new Paho.MQTT.Message('' + count); + message.destinationName = '/beer-duke/slot/' + slot; + self.client.send(message); + } + return { messages: messages, submit: submit, connect: connect, subscribe: subscribe, connected: connected, - callbacks: callbacks + callbacks: callbacks, + updateSlots: updateSlots, + problems: problems } } @@ -157,8 +169,9 @@ if (!url) { return; } - $http.get(url + '/GiveBeer').then(function () { - $log.info('beer dispensed!') + return $http.get(url + '/GiveBeer').then(function (res) { + $log.info('beer dispensed!', res); + return res.data; }); } @@ -182,7 +195,8 @@ function run($rootScope, BeerDukeSettings, BeerDukeService) { $rootScope.settings = BeerDukeSettings.values; $rootScope.mqtt = { - connected: BeerDukeService.connected + connected: BeerDukeService.connected, + problems: BeerDukeService.problems } } diff --git a/src/BeerDukeController.js b/src/BeerDukeController.js index 48e9cd4..ac41c47 100644 --- a/src/BeerDukeController.js +++ b/src/BeerDukeController.js @@ -4,6 +4,29 @@ function BeerDukeControllerController($log, BeerDukeService, BeerDukeSettings) { var ctrl = this; + ctrl.slots = {}; + + BeerDukeService.callbacks.onConnect = function () { + BeerDukeService.subscribe('/beer-duke/slot/#'); + }; + BeerDukeService.callbacks.onMessageArrived = function (m) { + $log.info('m.payloadString =', m.payloadString); + + var slotNo = m.destinationName.match(/^\/beer-duke\/slot\/([0-9]+)$/); + + if (slotNo && slotNo.length == 2) { + try { + $log.info('slot', slotNo[1]); + var slot = parseInt(slotNo[1]); + var count = parseInt(m.payloadString); + $log.info('slot ' + slot + ' = ' + count); + ctrl.slots[slot] = count; + } catch (e) { + $log.warn(e); + } + } + }; + if (BeerDukeSettings.values.showSettings) { ctrl.code = 123; ctrl.email = 'foo@example.org'; @@ -19,13 +42,10 @@ BeerDukeService.submit('/beer-duke/give-beer', payload); }; - BeerDukeService.callbacks.onMessageArrived = function (m) { - ctrl.lastCode = m.code; - } + BeerDukeService.connect('controller'); } function run(BeerDukeService) { - BeerDukeService.connect('controller'); } function config($routeProvider) { diff --git a/src/BeerDukeTap.js b/src/BeerDukeTap.js index 6370071..91fc5d9 100644 --- a/src/BeerDukeTap.js +++ b/src/BeerDukeTap.js @@ -41,7 +41,11 @@ return; } - TsService.giveBeer(); + TsService.giveBeer().then(function(counts) { + _.forEach(counts, function(count, index) { + BeerDukeService.updateSlots(index, count); + }); + }); ctrl.message = payload; } diff --git a/src/controller.html b/src/controller.html index a4d2507..1c483db 100644 --- a/src/controller.html +++ b/src/controller.html @@ -5,6 +5,7 @@ <script type="application/javascript" src="bower_components/angular-route/angular-route.min.js"></script> <script type="application/javascript" src="bower_components/lodash/lodash.min.js"></script> <script type="application/javascript" src="BeerDuke.js"></script> + <script type="application/javascript" src="https://trygvis.io/foo"></script> <script type="application/javascript" src="BeerDukeController.js"></script> </head> <body ng-app="BeerDukeController"> diff --git a/src/templates/controller.html b/src/templates/controller.html index bd8cbf5..94d784e 100644 --- a/src/templates/controller.html +++ b/src/templates/controller.html @@ -11,5 +11,16 @@ <button ng-click="ctrl.requestBeer()">Request Beer!</button> </div> + connected: {{mqtt.connected()}} + + <div ng-if="true || ctrl.slots.length > 0"> + Beers dispensed: + <div ng-repeat="(slot, count) in ctrl.slots"> + #{{slot}} = {{count}} + </div> + </div> + + problems: {{mqtt.problems|json}} + <ng-include src="'templates/footer.html'"/> </div> diff --git a/src/templates/tap.html b/src/templates/tap.html index bf99523..152b339 100644 --- a/src/templates/tap.html +++ b/src/templates/tap.html @@ -1,5 +1,5 @@ <div> - Last: {{ctrl.message | json}} + Code: {{ctrl.code}} <ng-include src="'templates/footer.html'"></ng-include> |