summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-09-08 21:52:27 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-09-08 21:52:27 +0200
commit81e0bad6f6e66e41f04e2fc4bd9a533976eb58a9 (patch)
tree245ff63a8bee1882d23884531d2ef026ff1190b9
parent76c273eaf316e0a8e3eadf93674ffa0aad35f7fd (diff)
downloadbeer-duke-81e0bad6f6e66e41f04e2fc4bd9a533976eb58a9.tar.gz
beer-duke-81e0bad6f6e66e41f04e2fc4bd9a533976eb58a9.tar.bz2
beer-duke-81e0bad6f6e66e41f04e2fc4bd9a533976eb58a9.tar.xz
beer-duke-81e0bad6f6e66e41f04e2fc4bd9a533976eb58a9.zip
-rw-r--r--src/BeerDuke.js42
-rw-r--r--src/BeerDukeController.js58
-rw-r--r--src/BeerDukeTap.js26
-rw-r--r--src/templates/controller.html6
-rw-r--r--src/templates/login.html9
-rw-r--r--src/templates/settings.html7
6 files changed, 109 insertions, 39 deletions
diff --git a/src/BeerDuke.js b/src/BeerDuke.js
index b30ee33..5fe8539 100644
--- a/src/BeerDuke.js
+++ b/src/BeerDuke.js
@@ -36,12 +36,18 @@
values = angular.fromJson(window.localStorage['beer-duke'] || '{}');
}
+ function setRandomClientId() {
+ values.clientId = values.clientId || 'beer-duke-' + Math.round(Math.random() * 100000);
+ save();
+ }
+
load();
- values.clientId = values.clientId || 'beer-duke-' + Math.round(Math.random() * 100000);
values.showSettings = values.showSettings || false;
+ values.tap = values.tap || '/beer-duke';
save();
return {
+ setRandomClientId: setRandomClientId,
keys: keys,
values: values,
load: load,
@@ -81,6 +87,7 @@
self.client = client;
self.client.connect({
+ cleanSession: true,
onSuccess: function () {
problems.connect_response = arguments;
var args = arguments;
@@ -138,36 +145,53 @@
}
}
- function submit(destination, payload) {
+ function requestBeer(code) {
+ var payload = {code: code, email: BeerDukeSettings.values.clientId};
+ console.log('payload =', payload);
var message = new Paho.MQTT.Message(angular.toJson(payload));
- message.destinationName = destination;
+ message.destinationName = BeerDukeSettings.values.tap + '/give-beer';
+ message.qos = 1;
+ message.retained = false;
self.client.send(message);
}
- function updateSlots(slot, count) {
- var message = new Paho.MQTT.Message('' + count);
- message.destinationName = '/beer-duke/slot/' + slot;
+ function updateSlots(counts) {
+ var message = new Paho.MQTT.Message(angular.toJson(counts));
+ message.destinationName = BeerDukeSettings.values.tap + '/slots';
+ message.qos = 0;
+ message.retained = true;
self.client.send(message);
}
return {
messages: messages,
- submit: submit,
connect: connect,
subscribe: subscribe,
connected: connected,
callbacks: callbacks,
updateSlots: updateSlots,
+ requestBeer: requestBeer,
problems: problems
}
}
- function TsService($log, $http, BeerDukeSettings) {
+ function TsService($q, $log, $http, BeerDukeSettings) {
var url = BeerDukeSettings.values.tsUrl;
function giveBeer() {
if (!url) {
- return;
+ return $q.resolve([
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100),
+ Math.round(Math.random() * 100)
+ ]);
}
return $http.get(url + '/GiveBeer').then(function (res) {
$log.info('beer dispensed!', res);
diff --git a/src/BeerDukeController.js b/src/BeerDukeController.js
index ac41c47..6755465 100644
--- a/src/BeerDukeController.js
+++ b/src/BeerDukeController.js
@@ -1,26 +1,33 @@
(function () {
'use strict';
- function BeerDukeControllerController($log, BeerDukeService, BeerDukeSettings) {
+ function BeerDukeControllerController($log, $location, BeerDukeService, BeerDukeSettings) {
var ctrl = this;
+ console.log('ctrl: BeerDukeSettings.values.clientId =', BeerDukeSettings.values.clientId);
+ if(!BeerDukeSettings.values.clientId) {
+ $location.path('/login');
+ }
+
ctrl.slots = {};
BeerDukeService.callbacks.onConnect = function () {
- BeerDukeService.subscribe('/beer-duke/slot/#');
+ BeerDukeService.subscribe(BeerDukeSettings.values.tap + '/slots');
};
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) {
+ if (m.destinationName == BeerDukeSettings.values.tap + '/slots') {
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;
+ var slotCounts = angular.fromJson(m.payloadString);
+ if(_.isArray(slotCounts)) {
+ _.forEach(ctrl.slots, function(key) {
+ delete ctrl.slots[key];
+ });
+ _.forEach(slotCounts, function(count, index) {
+ ctrl.slots[index] = count;
+ });
+ }
} catch (e) {
$log.warn(e);
}
@@ -33,18 +40,32 @@
}
ctrl.requestBeer = function () {
- var payload = {
- code: this.code,
- email: this.email
- };
- $log.info('payload', payload);
+ var code = this.code;
+ $log.info('requesting beer, code=' + code);
+ BeerDukeService.requestBeer(code);
+ };
- BeerDukeService.submit('/beer-duke/give-beer', payload);
+ ctrl.logOut = function () {
+ console.log('BeerDukeSettings.values.clientId =', BeerDukeSettings.values.clientId);
+ delete BeerDukeSettings.values.clientId;
+ BeerDukeSettings.save();
+ console.log('BeerDukeSettings.values.clientId =', BeerDukeSettings.values.clientId);
+ $location.path('/login').replace();
};
BeerDukeService.connect('controller');
}
+ function LoginController($location, BeerDukeSettings) {
+ var ctrl = this;
+
+ ctrl.logIn = function () {
+ BeerDukeSettings.values.clientId = ctrl.email;
+ BeerDukeSettings.save();
+ $location.path('/');
+ }
+ }
+
function run(BeerDukeService) {
}
@@ -54,6 +75,11 @@
controller: BeerDukeControllerController,
controllerAs: 'ctrl',
templateUrl: 'templates/controller.html'
+ })
+ .when('/login', {
+ controller: LoginController,
+ controllerAs: 'ctrl',
+ templateUrl: 'templates/login.html'
});
}
diff --git a/src/BeerDukeTap.js b/src/BeerDukeTap.js
index 91fc5d9..d25fc64 100644
--- a/src/BeerDukeTap.js
+++ b/src/BeerDukeTap.js
@@ -1,9 +1,13 @@
(function () {
'use strict';
- function BeerDukeTapController($log, $timeout, BeerDukeService, TsService) {
+ function BeerDukeTapController($log, $timeout, BeerDukeService, BeerDukeSettings, TsService) {
var ctrl = this;
+ if(!BeerDukeSettings.values.clientId) {
+ BeerDukeSettings.setRandomClientId();
+ }
+
var messages = ctrl.messages = [];
ctrl.count = 0;
ctrl.code = '';
@@ -17,7 +21,7 @@
}
BeerDukeService.callbacks.onConnect = function () {
- BeerDukeService.subscribe('/beer-duke/give-beer');
+ BeerDukeService.subscribe(BeerDukeSettings.values.tap + '/give-beer');
};
BeerDukeService.callbacks.onMessageArrived = function (m) {
$log.info('m.payloadString =', m.payloadString);
@@ -32,7 +36,7 @@
return;
}
- if (m.destinationName == '/beer-duke/give-beer') {
+ if (m.destinationName == BeerDukeSettings.values.tap + '/give-beer') {
var code = payload.code;
var email = payload.email;
@@ -41,16 +45,18 @@
return;
}
- TsService.giveBeer().then(function(counts) {
- _.forEach(counts, function(count, index) {
- BeerDukeService.updateSlots(index, count);
- });
- });
-
- ctrl.message = payload;
+ onGiveBeerRequest(email, code);
}
};
+ function onGiveBeerRequest(email, code) {
+ TsService.giveBeer().then(function(counts) {
+ BeerDukeService.updateSlots(counts);
+ });
+
+ //ctrl.message = code;
+ }
+
BeerDukeService.connect('tap');
}
diff --git a/src/templates/controller.html b/src/templates/controller.html
index 94d784e..9b9f518 100644
--- a/src/templates/controller.html
+++ b/src/templates/controller.html
@@ -4,10 +4,6 @@
Code from tap:
<input type="number" ng-model="ctrl.code">
</label>
- <label>
- Your email address
- <input type="email" ng-model="ctrl.email">
- </label>
<button ng-click="ctrl.requestBeer()">Request Beer!</button>
</div>
@@ -20,6 +16,8 @@
</div>
</div>
+ <button type="button" ng-click="ctrl.logOut()">Disconnect</button><br/>
+
problems: {{mqtt.problems|json}}
<ng-include src="'templates/footer.html'"/>
diff --git a/src/templates/login.html b/src/templates/login.html
new file mode 100644
index 0000000..7c5dc03
--- /dev/null
+++ b/src/templates/login.html
@@ -0,0 +1,9 @@
+<div ng-form="form">
+
+ <label>
+ Email from JavaZone ticket:
+ <input type="email" ng-model="ctrl.email" required>
+ <input type="submit" ng-click="ctrl.logIn()" ng-disabled="form.$invalid" value="Connect">
+ </label>
+
+</div>
diff --git a/src/templates/settings.html b/src/templates/settings.html
index cca9f22..28d0e58 100644
--- a/src/templates/settings.html
+++ b/src/templates/settings.html
@@ -26,4 +26,11 @@
</label>
</p>
+ <p>
+ <label>
+ Tap
+ <input type="text" ng-model="ctrl.settings.tap">
+ </label>
+ </p>
+
</div>