diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-08 12:49:53 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-08 12:49:53 +0200 |
commit | 615a043031f37fe5bc55fdf9cbc2e83a5e305908 (patch) | |
tree | 128a22f4d34934ec7c7d75a78ffea7c16c7c32fb /src | |
parent | bcf3120d437da01fda78397a345bf805b008eb7a (diff) | |
download | beer-duke-615a043031f37fe5bc55fdf9cbc2e83a5e305908.tar.gz beer-duke-615a043031f37fe5bc55fdf9cbc2e83a5e305908.tar.bz2 beer-duke-615a043031f37fe5bc55fdf9cbc2e83a5e305908.tar.xz beer-duke-615a043031f37fe5bc55fdf9cbc2e83a5e305908.zip |
wip
Diffstat (limited to 'src')
-rw-r--r-- | src/BeerDuke.js | 150 | ||||
-rw-r--r-- | src/BeerDukeController.js | 33 | ||||
-rw-r--r-- | src/BeerDukeTap.js | 45 | ||||
-rw-r--r-- | src/beer-duke.js | 31 | ||||
-rw-r--r-- | src/config.js | 15 | ||||
-rw-r--r-- | src/controller.html (renamed from src/index.html) | 10 | ||||
-rw-r--r-- | src/tap.html | 18 | ||||
-rw-r--r-- | src/templates/controller.html | 9 | ||||
-rw-r--r-- | src/templates/footer.html | 5 | ||||
-rw-r--r-- | src/templates/settings.html | 14 | ||||
-rw-r--r-- | src/templates/tap.html | 5 |
11 files changed, 292 insertions, 43 deletions
diff --git a/src/BeerDuke.js b/src/BeerDuke.js new file mode 100644 index 0000000..491872a --- /dev/null +++ b/src/BeerDuke.js @@ -0,0 +1,150 @@ +import angular from 'angular'; + +class SettingsController { + constructor(BeerDukeSettings) { + this.settings = BeerDukeSettings; + } +} + +class BeerDukeSettings { + constructor() { + this.clientId = this.clientId || 'beer-duke-' + Math.round(Math.random() * 100000); + this.showSettings = this.showSettings || false; + } + + get clientId() { + return BeerDukeSettings.load('clientId'); + } + + set clientId(clientId) { + BeerDukeSettings.store('clientId', clientId); + } + + get showSettings() { + return BeerDukeSettings.load('showSettings'); + } + + set showSettings(showSettings) { + BeerDukeSettings.store('showSettings', showSettings); + } + + static store(key, value) { + window.localStorage[key] = angular.toJson(value); + return value; + } + + static load(key) { + return angular.fromJson(window.localStorage[key]); + } +} + +class BeerDukeService { + constructor($timeout, $rootScope, BeerDukeSettings) { + this.$timeout = $timeout; + this.$rootScope = $rootScope; + + this.connected_ = false; + this.messages = []; + } + + connect(type) { + let client = new Paho.MQTT.Client("wss://trygvis.io:9001/", BeerDukeSettings.clientId + "-" + type); + client.onConnectionLost = (a) => { + this.$timeout(() => { + this.$rootScope.$apply(() => { + this.onConnectionLost(a); + }) + }); + }; + client.onMessageArrived = (a) => { + this.$timeout(() => { + this.$rootScope.$apply(() => { + this.onMessageArrived(a); + }) + }); + }; + + this.client = client; + + this.client.connect({ + onSuccess: () => { + this.$timeout(() => { + this.$rootScope.$apply(() => { + this.onConnect(); + }) + }); + } + }); + } + + get connected() { + return this.connected_; + } + + onConnect() { + console.log('Connected'); + + this.connected_ = true; + + this.client.subscribe("/beer-duke"); + } + + onConnectionLost(responseObject) { + this.connected_ = false; + + //if (responseObject.errorCode !== 0) + console.log("onConnectionLost:", responseObject); + console.log("onConnectionLost:", responseObject.errorMessage); + } + + onMessageArrived(message) { + console.log("onMessageArrived:" + message.payloadString); + + this.messages.push(message); + } + + requestBeer(code) { + let message = new Paho.MQTT.Message(angular.toJson({code: code})); + message.destinationName = "/beer-duke"; + this.client.send(message); + } +} + +class MqttController { + constructor(BeerDukeService) { + this.BeerDukeService = BeerDukeService; + } + + get connected() { + return this.BeerDukeService.connected; + } +} + +function config($routeProvider) { + console.log('BeerDuke: config'); + $routeProvider + .when('/settings', { + controller: SettingsController, + controllerAs: 'ctrl', + templateUrl: 'templates/settings.html' + }) + .otherwise({ + redirectTo: '/' + }) +} + +function run($rootScope, BeerDukeSettings, BeerDukeService) { + $rootScope.settings = BeerDukeSettings; + $rootScope.mqtt = { + get connected() { + return BeerDukeService.connected + } + }; +} + +angular.module('BeerDuke', ['ngRoute']) + .controller('MqttController', MqttController) + .service('BeerDukeService', BeerDukeService) + .service('BeerDukeSettings', BeerDukeSettings) + .config(config) + .run(run); diff --git a/src/BeerDukeController.js b/src/BeerDukeController.js new file mode 100644 index 0000000..fd70357 --- /dev/null +++ b/src/BeerDukeController.js @@ -0,0 +1,33 @@ +import angular from 'angular'; +import 'angular-route'; +import './BeerDuke.js'; + +class BeerDukeControllerController { + constructor(BeerDukeService) { + this.BeerDukeService = BeerDukeService; + } + + requestBeer() { + let code = this.code; + + console.log('code', code); + this.BeerDukeService.requestBeer(code); + } +} + +function run(BeerDukeService) { + BeerDukeService.connect('controller'); +} + +function config($routeProvider) { + $routeProvider + .when('/', { + controller: BeerDukeControllerController, + controllerAs: 'ctrl', + templateUrl: 'templates/controller.html' + }); +} +angular.module('BeerDukeController', ['ngRoute', 'BeerDuke']) + .run(run) + .config(config) + .controller('BeerDukeControllerController', BeerDukeControllerController); diff --git a/src/BeerDukeTap.js b/src/BeerDukeTap.js new file mode 100644 index 0000000..3392e85 --- /dev/null +++ b/src/BeerDukeTap.js @@ -0,0 +1,45 @@ +import angular from 'angular'; +import 'angular-route'; +import './BeerDuke.js'; + +class BeerDukeTapController { + constructor($timeout, BeerDukeSettings) { + console.log('BeerDukeTapController', this); + + this.$timeout = $timeout; + this.settings = BeerDukeSettings; + + this.rotateCode(); + this.count = 0; + this.code = ''; + } + + rotateCode() { + this.code = '' + this.count++; + this.$timeout(() => { + this.rotateCode(); + }, 1000); + } +} + +class TapSettingsController { + constructor() { + } +} + +function run(BeerDukeService) { + BeerDukeService.connect('tap'); +} + +function config($routeProvider) { + $routeProvider + .when('/', { + controller: BeerDukeTapController, + controllerAs: 'ctrl', + templateUrl: 'templates/tap.html' + }); +} + +angular.module('BeerDukeTap', ['BeerDuke', 'ngRoute']) + .run(run) + .config(config); diff --git a/src/beer-duke.js b/src/beer-duke.js deleted file mode 100644 index 8178836..0000000 --- a/src/beer-duke.js +++ /dev/null @@ -1,31 +0,0 @@ -import angular from 'angular'; - -function run() { - function onConnect() { - // Once a connection has been made, make a subscription and send a message. - console.log("onConnect"); - client.subscribe("/World"); - let message = new Paho.MQTT.Message("Hello"); - message.destinationName = "/World"; - client.send(message); - } - - function onConnectionLost(responseObject) { - if (responseObject.errorCode !== 0) - console.log("onConnectionLost:" + responseObject.errorMessage); - } - - function onMessageArrived(message) { - console.log("onMessageArrived:" + message.payloadString); - client.disconnect(); - } - - //let client = new Paho.MQTT.Client("trygvis.io", 9001, "/", "clientId"); - let client = new Paho.MQTT.Client("wss://trygvis.io:9001/", "clientId"); - client.onConnectionLost = onConnectionLost; - client.onMessageArrived = onMessageArrived; - client.connect({onSuccess: onConnect}); -} - -angular.module('BeerDuke', []) - .run(run); diff --git a/src/config.js b/src/config.js index 7b73f04..5bf6bd5 100644 --- a/src/config.js +++ b/src/config.js @@ -1,6 +1,6 @@ System.config({ defaultJSExtensions: true, - transpiler: "babel", + transpiler: "traceur", babelOptions: { "optional": [ "runtime", @@ -13,13 +13,14 @@ System.config({ }, map: { - "angular": "github:angular/bower-angular@1.4.4", - "babel": "npm:babel-core@5.8.22", - "babel-runtime": "npm:babel-runtime@5.8.20", + "angular": "npm:angular@1.4.5", + "angular-route": "npm:angular-route@1.4.5", "core-js": "npm:core-js@1.1.1", "jasmine-node": "npm:jasmine-node@1.14.5", "mhevery/jasmine-node": "github:mhevery/jasmine-node@1.14.5", "mqtt_over_websockets": "npm:mqtt_over_websockets@0.0.1-b", + "traceur": "github:jmcriffey/bower-traceur@0.0.91", + "traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.91", "github:jspm/nodelibs-assert@0.1.0": { "assert": "npm:assert@1.3.0" }, @@ -67,6 +68,9 @@ System.config({ "github:jspm/nodelibs-vm@0.1.0": { "vm-browserify": "npm:vm-browserify@0.0.4" }, + "npm:angular@1.4.5": { + "process": "github:jspm/nodelibs-process@0.1.1" + }, "npm:asn1.js@2.2.0": { "assert": "github:jspm/nodelibs-assert@0.1.0", "bn.js": "npm:bn.js@2.2.0", @@ -78,9 +82,6 @@ System.config({ "npm:assert@1.3.0": { "util": "npm:util@0.10.3" }, - "npm:babel-runtime@5.8.20": { - "process": "github:jspm/nodelibs-process@0.1.1" - }, "npm:browserify-aes@1.0.3": { "buffer": "github:jspm/nodelibs-buffer@0.1.0", "buffer-xor": "npm:buffer-xor@1.0.2", diff --git a/src/index.html b/src/controller.html index 463f704..c6b76b9 100644 --- a/src/index.html +++ b/src/controller.html @@ -1,18 +1,18 @@ <html> <head> - <script type="application/javascript" src="jspm_packages/system.js"></script> + <script type="application/javascript" src="jspm_packages/system.src.js"></script> <script type="application/javascript" src="../org.eclipse.paho.mqtt.javascript/src/mqttws31.js"></script> <script type="application/javascript" src="config.js"></script> - <!--<script type="application/javascript" src="beer-duke.js"></script>--> - <script type="application/javascript"> + <script type="text/ecmascript"> Promise.all([ System.import('angular'), - System.import('beer-duke')]) + System.import('BeerDukeController')]) .then(function (modules) { - modules[0].bootstrap(document, ['BeerDuke']); + modules[0].bootstrap(document, ['BeerDukeController']); }); </script> </head> <body> +<div ng-view></div> </body> </html> diff --git a/src/tap.html b/src/tap.html new file mode 100644 index 0000000..5f47e2e --- /dev/null +++ b/src/tap.html @@ -0,0 +1,18 @@ +<html> +<head> + <script type="application/javascript" src="jspm_packages/system.src.js"></script> + <script type="application/javascript" src="../org.eclipse.paho.mqtt.javascript/src/mqttws31.js"></script> + <script type="application/javascript" src="config.js"></script> + <script type="text/ecmascript"> + Promise.all([ + System.import('angular'), + System.import('BeerDukeTap')]) + .then(function (modules) { + modules[0].bootstrap(document, ['BeerDukeTap']); + }); + </script> +</head> +<body> +<div ng-view></div> +</body> +</html> diff --git a/src/templates/controller.html b/src/templates/controller.html new file mode 100644 index 0000000..c6cc4c8 --- /dev/null +++ b/src/templates/controller.html @@ -0,0 +1,9 @@ +<div ng-controller="MqttController as mqtt"> + <div ng-controller="BeerDukeControllerController as ctrl"> + <label> + Code from tap: + <input type="number" ng-model="ctrl.code"> + </label> + <button ng-click="ctrl.requestBeer()">Request Beer!</button> + </div> +</div> diff --git a/src/templates/footer.html b/src/templates/footer.html new file mode 100644 index 0000000..17483ae --- /dev/null +++ b/src/templates/footer.html @@ -0,0 +1,5 @@ +<div ng-if="settings.showSettings"> + <hr/> + <a href="#/settings">settings</a><br/> + connected = {{mqtt.connected}} +</div> diff --git a/src/templates/settings.html b/src/templates/settings.html new file mode 100644 index 0000000..5147065 --- /dev/null +++ b/src/templates/settings.html @@ -0,0 +1,14 @@ +<div> + <h2>Settings</h2> + <a href="#/">back</a> + <label> + showSettings + <input type="checkbox" ng-model="ctrl.settings.showSettings"> + </label> + + <label> + clientId + <input type="text" ng-model="ctrl.settings.clientId"> + </label> + +</div> diff --git a/src/templates/tap.html b/src/templates/tap.html new file mode 100644 index 0000000..c266f2d --- /dev/null +++ b/src/templates/tap.html @@ -0,0 +1,5 @@ +<div> + Code: {{ctrl.code}} + + <ng-include src="'templates/footer.html'"/> +</div> |