From e9914f533557c6ba4d34d52125049d60f040e856 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 8 Sep 2015 15:03:31 +0200 Subject: wip --- org.eclipse.paho.mqtt.javascript | 1 - src/BeerDuke.js | 54 +- src/BeerDukeController.js | 15 +- src/controller.html | 2 +- src/mqttws31.js | 2143 ++++++++++++++++++++++++++++++++++++++ src/tap.html | 2 +- src/templates/controller.html | 2 + 7 files changed, 2196 insertions(+), 23 deletions(-) delete mode 160000 org.eclipse.paho.mqtt.javascript create mode 100644 src/mqttws31.js diff --git a/org.eclipse.paho.mqtt.javascript b/org.eclipse.paho.mqtt.javascript deleted file mode 160000 index ffd2702..0000000 --- a/org.eclipse.paho.mqtt.javascript +++ /dev/null @@ -1 +0,0 @@ -Subproject commit ffd270217aec7413ea915b2f887837c2be25b692 diff --git a/src/BeerDuke.js b/src/BeerDuke.js index 491872a..82f37c2 100644 --- a/src/BeerDuke.js +++ b/src/BeerDuke.js @@ -8,23 +8,13 @@ class SettingsController { class BeerDukeSettings { constructor() { - this.clientId = this.clientId || 'beer-duke-' + Math.round(Math.random() * 100000); - this.showSettings = this.showSettings || false; + this.clientId = BeerDukeSettings.load('clientId') || 'beer-duke-' + Math.round(Math.random() * 100000); + console.log('this =', this); + this.showSettings = BeerDukeSettings.load('showSettings') == 'true' || false; } - get clientId() { - return BeerDukeSettings.load('clientId'); - } - - set clientId(clientId) { + save() { BeerDukeSettings.store('clientId', clientId); - } - - get showSettings() { - return BeerDukeSettings.load('showSettings'); - } - - set showSettings(showSettings) { BeerDukeSettings.store('showSettings', showSettings); } @@ -34,7 +24,9 @@ class BeerDukeSettings { } static load(key) { - return angular.fromJson(window.localStorage[key]); + let x = window.localStorage[key]; + console.log('key =', key, 'x =', x); + return angular.fromJson(x); } } @@ -47,8 +39,17 @@ class BeerDukeService { this.messages = []; } - connect(type) { - let client = new Paho.MQTT.Client("wss://trygvis.io:9001/", BeerDukeSettings.clientId + "-" + type); + static xxx() { + return new BeerDukeSettings(); + } + + connect(type, opts) { + this.opts = opts || {}; + + console.log('BeerDukeSettings =', BeerDukeSettings); + let clientId = BeerDukeSettings.clientId + "-" + type; + console.log('clientId =', clientId); + let client = new Paho.MQTT.Client("wss://trygvis.io:9001/", clientId); client.onConnectionLost = (a) => { this.$timeout(() => { this.$rootScope.$apply(() => { @@ -86,7 +87,14 @@ class BeerDukeService { this.connected_ = true; - this.client.subscribe("/beer-duke"); + if (this.opts.onConnect) { + try { + this.opts.onConnect(); + } catch (e) { + } + } + + //this.client.subscribe("/beer-duke"); } onConnectionLost(responseObject) { @@ -101,6 +109,14 @@ class BeerDukeService { console.log("onMessageArrived:" + message.payloadString); this.messages.push(message); + + if (this.opts.onMessageArrived) { + try { + let m = angular.fromJson(message); + this.opts.onMessageArrived(m); + } catch (e) { + } + } } requestBeer(code) { @@ -145,6 +161,6 @@ function run($rootScope, BeerDukeSettings, BeerDukeService) { angular.module('BeerDuke', ['ngRoute']) .controller('MqttController', MqttController) .service('BeerDukeService', BeerDukeService) - .service('BeerDukeSettings', BeerDukeSettings) + .factory('BeerDukeSettings', () => new BeerDukeSettings()) .config(config) .run(run); diff --git a/src/BeerDukeController.js b/src/BeerDukeController.js index fd70357..85a47f4 100644 --- a/src/BeerDukeController.js +++ b/src/BeerDukeController.js @@ -5,6 +5,11 @@ import './BeerDuke.js'; class BeerDukeControllerController { constructor(BeerDukeService) { this.BeerDukeService = BeerDukeService; + + this.BeerDukeService.connect('controller', { + onConnect: this.onConnect, + onMessageArrived: this.onMessageArrived + }); } requestBeer() { @@ -13,10 +18,18 @@ class BeerDukeControllerController { console.log('code', code); this.BeerDukeService.requestBeer(code); } + + onConnect() { + console.log('onConnect'); + } + + onMessageArrived(msg) { + console.log('msg =', msg); + } } function run(BeerDukeService) { - BeerDukeService.connect('controller'); + //BeerDukeService.connect('controller'); } function config($routeProvider) { diff --git a/src/controller.html b/src/controller.html index c6b76b9..bbb34ca 100644 --- a/src/controller.html +++ b/src/controller.html @@ -1,7 +1,7 @@ - + - +