From 615a043031f37fe5bc55fdf9cbc2e83a5e305908 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 8 Sep 2015 12:49:53 +0200 Subject: wip --- package.json | 12 ++-- src/BeerDuke.js | 150 ++++++++++++++++++++++++++++++++++++++++++ src/BeerDukeController.js | 33 ++++++++++ src/BeerDukeTap.js | 45 +++++++++++++ src/beer-duke.js | 31 --------- src/config.js | 15 +++-- src/controller.html | 18 +++++ src/index.html | 18 ----- src/tap.html | 18 +++++ src/templates/controller.html | 9 +++ src/templates/footer.html | 5 ++ src/templates/settings.html | 14 ++++ src/templates/tap.html | 5 ++ 13 files changed, 310 insertions(+), 63 deletions(-) create mode 100644 src/BeerDuke.js create mode 100644 src/BeerDukeController.js create mode 100644 src/BeerDukeTap.js delete mode 100644 src/beer-duke.js create mode 100644 src/controller.html delete mode 100644 src/index.html create mode 100644 src/tap.html create mode 100644 src/templates/controller.html create mode 100644 src/templates/footer.html create mode 100644 src/templates/settings.html create mode 100644 src/templates/tap.html diff --git a/package.json b/package.json index 304157e..a1ae283 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,6 @@ "version": "0.0.0", "description": "", "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, "author": "", "license": "MIT", "devDependencies": { @@ -17,12 +14,13 @@ "baseURL": "src" }, "dependencies": { - "angular": "github:angular/bower-angular@^1.4.4" + "angular": "npm:angular@^1.4.5", + "angular-route": "npm:angular-route@^1.4.5" }, "devDependencies": { - "babel": "npm:babel-core@^5.8.22", - "babel-runtime": "npm:babel-runtime@^5.8.20", - "core-js": "npm:core-js@^1.1.0" + "core-js": "npm:core-js@^1.1.0", + "traceur": "github:jmcriffey/bower-traceur@0.0.91", + "traceur-runtime": "github:jmcriffey/bower-traceur-runtime@0.0.91" } } } 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/controller.html b/src/controller.html new file mode 100644 index 0000000..c6b76b9 --- /dev/null +++ b/src/controller.html @@ -0,0 +1,18 @@ + + + + + + + + +
+ + diff --git a/src/index.html b/src/index.html deleted file mode 100644 index 463f704..0000000 --- a/src/index.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - 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 @@ + + + + + + + + +
+ + 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 @@ +
+
+ + +
+
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 @@ +
+
+ settings
+ connected = {{mqtt.connected}} +
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 @@ +
+

Settings

+ back + + + + +
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 @@ +
+ Code: {{ctrl.code}} + + +
-- cgit v1.2.3