diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-08 17:59:56 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-08 17:59:56 +0200 |
commit | 694cc18a40ae5c71ec280b3aea2e1fa4d51ab640 (patch) | |
tree | b052776e72bab7d9fed89975a5b2dd44049eb418 /src | |
parent | a767c17582caf9df45df587451773ee6f7bc2e49 (diff) | |
download | beer-duke-694cc18a40ae5c71ec280b3aea2e1fa4d51ab640.tar.gz beer-duke-694cc18a40ae5c71ec280b3aea2e1fa4d51ab640.tar.bz2 beer-duke-694cc18a40ae5c71ec280b3aea2e1fa4d51ab640.tar.xz beer-duke-694cc18a40ae5c71ec280b3aea2e1fa4d51ab640.zip |
wip
Diffstat (limited to 'src')
-rw-r--r-- | src/BeerDuke.js | 78 | ||||
-rw-r--r-- | src/BeerDukeController.js | 4 | ||||
-rw-r--r-- | src/BeerDukeTap.js | 38 | ||||
-rw-r--r-- | src/controller.html | 5 | ||||
-rw-r--r-- | src/tap.html | 1 | ||||
-rw-r--r-- | src/templates/footer.html | 2 | ||||
-rw-r--r-- | src/templates/settings.html | 13 | ||||
-rw-r--r-- | src/templates/tap.html | 8 |
8 files changed, 91 insertions, 58 deletions
diff --git a/src/BeerDuke.js b/src/BeerDuke.js index 6f88016..eb30307 100644 --- a/src/BeerDuke.js +++ b/src/BeerDuke.js @@ -4,60 +4,46 @@ function SettingsController($log, $location, BeerDukeSettings) { var ctrl = this; - ctrl.clientId = BeerDukeSettings.clientId(); - ctrl.showSettings = BeerDukeSettings.showSettings(); + ctrl.settings = BeerDukeSettings.values; ctrl.save = function () { - BeerDukeSettings.clientId(ctrl.clientId); - BeerDukeSettings.showSettings(ctrl.showSettings); BeerDukeSettings.save(); - $location.path('/') } } function BeerDukeSettings() { + var keys = [ + 'clientId', + 'showSettings', + 'tsUrl' + ]; + + var values = {}; + function save() { - store('clientId', clientId_); - store('showSettings', showSettings_); + _.forEach(keys, function (key) { + store('key', values[key]); + }); } - function store(key, value) { - window.localStorage[key] = angular.toJson(value); - return value; + function store() { + window.localStorage['beer-duke'] = angular.toJson(values); } - function load(key) { - var x = window.localStorage[key]; - if (typeof x === 'undefined') { - return x; - } - - return angular.fromJson(x); + function load() { + values = angular.fromJson(window.localStorage['beer-duke'] || '{}'); } - var clientId_ = load('clientId') || 'beer-duke-' + Math.round(Math.random() * 100000); - var showSettings_ = load('showSettings') || false; + load(); + values.clientId = values.clientId || 'beer-duke-' + Math.round(Math.random() * 100000); + values.showSettings = values.showSettings || false; save(); - function clientId(x) { - if (x) { - clientId_ = x; - } - return clientId_; - } - - function showSettings(x) { - if (x) { - showSettings_ = x; - } - return showSettings_; - } - return { - clientId: clientId, - showSettings: showSettings, + keys: keys, + values: values, load: load, save: save } @@ -70,7 +56,7 @@ var callbacks = {}; function connect(type) { - var clientId = BeerDukeSettings.clientId() + "-" + type; + var clientId = BeerDukeSettings.values.clientId + "-" + type; var client = new Paho.MQTT.Client("wss://trygvis.io:9001/", clientId); client.onConnectionLost = function (a) { $timeout(function () { @@ -164,6 +150,23 @@ } } + function TsService($log, $http, BeerDukeSettings) { + var url = BeerDukeSettings.values.tsUrl; + + function giveBeer() { + if (!url) { + return; + } + $http.get(url + '/GiveBeer').then(function () { + $log.info('beer dispensed!') + }); + } + + return { + giveBeer: giveBeer + } + } + function config($routeProvider) { $routeProvider .when('/settings', { @@ -177,7 +180,7 @@ } function run($rootScope, BeerDukeSettings, BeerDukeService) { - $rootScope.settings = BeerDukeSettings; + $rootScope.settings = BeerDukeSettings.values; $rootScope.mqtt = { connected: BeerDukeService.connected } @@ -185,6 +188,7 @@ angular.module('BeerDuke', ['ngRoute']) .factory('BeerDukeService', BeerDukeService) + .factory('TsService', TsService) .factory('BeerDukeSettings', BeerDukeSettings) .config(config) .run(run); diff --git a/src/BeerDukeController.js b/src/BeerDukeController.js index 8aeafb8..48e9cd4 100644 --- a/src/BeerDukeController.js +++ b/src/BeerDukeController.js @@ -4,7 +4,7 @@ function BeerDukeControllerController($log, BeerDukeService, BeerDukeSettings) { var ctrl = this; - if (BeerDukeSettings.showSettings()) { + if (BeerDukeSettings.values.showSettings) { ctrl.code = 123; ctrl.email = 'foo@example.org'; } @@ -16,7 +16,7 @@ }; $log.info('payload', payload); - BeerDukeService.submit('/beer-duke', payload); + BeerDukeService.submit('/beer-duke/give-beer', payload); }; BeerDukeService.callbacks.onMessageArrived = function (m) { diff --git a/src/BeerDukeTap.js b/src/BeerDukeTap.js index ed12a17..6370071 100644 --- a/src/BeerDukeTap.js +++ b/src/BeerDukeTap.js @@ -1,36 +1,50 @@ (function () { 'use strict'; - function BeerDukeTapController($log, $timeout, BeerDukeService) { + function BeerDukeTapController($log, $timeout, BeerDukeService, TsService) { var ctrl = this; + var messages = ctrl.messages = []; + ctrl.count = 0; + ctrl.code = ''; rotateCode(); - self.count = 0; - self.code = ''; function rotateCode() { - self.code = '' + self.count++; + ctrl.code = '' + ctrl.count++; $timeout(function () { rotateCode(); }, 1000); } BeerDukeService.callbacks.onConnect = function () { - BeerDukeService.subscribe('/beer-duke'); + BeerDukeService.subscribe('/beer-duke/give-beer'); }; BeerDukeService.callbacks.onMessageArrived = function (m) { $log.info('m.payloadString =', m.payloadString); - var payload = angular.fromJson(m.payloadString); - var code = payload.code; - var email = payload.email; - - if (typeof code !== 'string' && typeof email !== 'string') { - $log.warn('bad payload', payload.code); + var payload; + try { + payload = angular.fromJson(m.payloadString); + messages.unshift(payload); + } catch (e) { + $log.warn('could not parse json payload', e); + $log.warn('JSON:', payloadString); return; } - ctrl.message = payload; + if (m.destinationName == '/beer-duke/give-beer') { + var code = payload.code; + var email = payload.email; + + if (typeof code !== 'string' && typeof email !== 'string') { + $log.warn('bad payload', payload.code); + return; + } + + TsService.giveBeer(); + + ctrl.message = payload; + } }; BeerDukeService.connect('tap'); diff --git a/src/controller.html b/src/controller.html index 33477f9..a4d2507 100644 --- a/src/controller.html +++ b/src/controller.html @@ -1,8 +1,9 @@ <html> <head> <script type="application/javascript" src="mqttws31.js"></script> - <script type="application/javascript" src="bower_components/angular/angular.js"></script> - <script type="application/javascript" src="bower_components/angular-route/angular-route.js"></script> + <script type="application/javascript" src="bower_components/angular/angular.min.js"></script> + <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="BeerDukeController.js"></script> </head> diff --git a/src/tap.html b/src/tap.html index 5196b97..85aab0a 100644 --- a/src/tap.html +++ b/src/tap.html @@ -3,6 +3,7 @@ <script type="application/javascript" src="mqttws31.js"></script> <script type="application/javascript" src="bower_components/angular/angular.js"></script> <script type="application/javascript" src="bower_components/angular-route/angular-route.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="BeerDukeTap.js"></script> </head> diff --git a/src/templates/footer.html b/src/templates/footer.html index 182554d..4820e68 100644 --- a/src/templates/footer.html +++ b/src/templates/footer.html @@ -1,4 +1,4 @@ -<div ng-if="settings.showSettings()"> +<div ng-if="settings.showSettings"> <hr/> <a href="#/settings">settings</a><br/> connected = {{mqtt.connected()}} diff --git a/src/templates/settings.html b/src/templates/settings.html index f118e47..cca9f22 100644 --- a/src/templates/settings.html +++ b/src/templates/settings.html @@ -1,4 +1,4 @@ -<div ng-init="clientId = ctrl.settings.clientId(); showSettings = ctrl.settings.showSettings()"> +<div> <h2>Settings</h2> <p> @@ -8,14 +8,21 @@ <p> <label> showSettings - <input type="checkbox" ng-model="ctrl.showSettings"> + <input type="checkbox" ng-model="ctrl.settings.showSettings"> </label> </p> <p> <label> clientId - <input type="text" ng-model="ctrl.clientId"> + <input type="text" ng-model="ctrl.settings.clientId"> + </label> + </p> + + <p> + <label> + TS + <input type="text" ng-model="ctrl.settings.tsUrl"> </label> </p> diff --git a/src/templates/tap.html b/src/templates/tap.html index 48d5e37..bf99523 100644 --- a/src/templates/tap.html +++ b/src/templates/tap.html @@ -1,5 +1,11 @@ <div> Last: {{ctrl.message | json}} - <ng-include src="'templates/footer.html'"/> + <ng-include src="'templates/footer.html'"></ng-include> + + <div ng-if="settings.showSettings"> + Messages: + <pre ng-repeat="m in ctrl.messages|limitTo:10">{{m}}</pre> + </div> + </div> |