Skip to content

Instantly share code, notes, and snippets.

@olliemaitland
Last active December 19, 2015 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olliemaitland/6027519 to your computer and use it in GitHub Desktop.
Save olliemaitland/6027519 to your computer and use it in GitHub Desktop.
DiffusionService used on the Oracle and Push Technology demo
define(['App'], function(App) {
console.log('Orc Service', App);
App.factory('DiffusionService' , ['$http', 'Events', function($http, Events) {
return {
is_connected: false,
diffusion_client: null,
connect_hasBeenCalled: false,
connect: function (topic, scope){
var self = this;
self.connect_hasBeenCalled = true;
self.diffusion_client = DiffusionClient.connect({
debug: true,
onDataFunction: function() {},
onCallbackFunction: function(connected){
if (connected) {
scope.callTiles();
scope.$root.diffusion_connected = true;
self.is_connected = true;
DiffusionClient.addTopicListener(topic, scope.update);
DiffusionClient.addTopicListener("LockService/Command", self.lockCommand);
DiffusionClient.addTopicListener("PriceUniverse/Command", self.priceCommand);
DiffusionClient.subscribe(topic);
DiffusionClient.subscribe("PriceUniverse/Command");
DiffusionClient.subscribe("LockService/Command");
}
},
onLostConnectionFunction: function() {
alert("Lost connection! :(");
},
flashHost: '5.255.148.14',
flashPort: '8080',
flashURL: 'http://5.255.148.14:8080',
wsURL : 'ws://5.255.148.14:8080',
disableSilverlight: true,
libPath: '/lib/DIFFUSION'
});
},
sendLockCommand : function (scope) {
// send lock
scope.tryToUnlock = false;
DiffusionClient.send("LockService/Command", "LOCK " + scope.currency);
},
sendPriceCommand: function (scope, floatValues) {
// set price
DiffusionClient.send("PriceUniverse/Command", "SET " + scope.currency + floatValues);
},
sendUnlockCommand: function (scope) {
scope.tryToUnlock = true;
DiffusionClient.send("LockService/Command", "UNLOCK " + scope.currency);
},
lockCommand: function(msg) {
console.log(">>>>COMMAND>>", msg);
},
priceCommand: function (msg) {
},
lockStatus: function(msg) {
},
initLockSubscriptions : function (scope) {
if(!this.connect_hasBeenCalled){
return (scope.lockTryCount + 1);
}else{
if(this.is_connected) {
var self = this;
// @todo add new linker to the LockService/Status service
// subscribe to command topic if not already
DiffusionClient.addTopicListener("LockService/Status", function(msg){
if(msg.payload == 'LockService/StatusLOCKED '+scope.currency) {
if(msg.payload.indexOf(scope.currency) != -1){
scope.showMode = 'unactive';
scope.lastStatus = msg.payload;
scope.isLocked = true;
}
}
if(msg.payload == 'LockService/StatusUNLOCKED '+scope.currency) {
if(msg.payload.indexOf(scope.currency) != -1){
scope.showMode = 'normal';
scope.lastStatus = msg.payload;
scope.isLocked = false;
}
}
if((msg.payload == 'LockService/StatusTIMEOUT '+scope.currency) &&(scope.tryToUnlock)){
if((msg.payload.indexOf(scope.currency) != -1)&&(scope.tryToUnlock)){
self.subscribe(scope.topic, scope);
scope.showMode = 'normal';
scope.lastStatus = msg.payload;
scope.isLocked = false;
}
}
if((scope.isLocked) &&(scope.tryToUnlock)){
if((scope.lastStatus.indexOf(scope.currency) != -1)&&(scope.tryToUnlock)){
self.subscribe(scope.topic, scope);
scope.showMode = 'normal';
scope.isLocked = false;
}
}
});
return false;
}else{
return (scope.lockTryCount + 1);
}
}
},
subscribe : function(topic, scope) {
DiffusionClient.addTopicListener(topic, scope.update);
DiffusionClient.subscribe(topic);
}
}
}]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment