Skip to content

Instantly share code, notes, and snippets.

@xrl
Last active August 29, 2015 13:57
Show Gist options
  • Save xrl/9924183 to your computer and use it in GitHub Desktop.
Save xrl/9924183 to your computer and use it in GitHub Desktop.
App.UnitActionAdapter = DS.Adapter.extend({
connection: null,
find: function(){
debugger
},
createRecord: function(store,type,record){
var action = record.get('action');
var connection = this.get('connection');
var retval = null;
if(action == 'trigger'){
retval = connection.trigger(record.get('file_type'));
} else if(action == 'capture'){
retval = connection.capture();
} else{
retval = this.sendMove(connection,record);
var successCallback = function(successData){
return successData;
},
errorCallback = function(errorData){
return errorData;
};
retval.deferred().then(successCallback,errorCallback);
}
return retval.deferred();
},
updateRecord: function(store,type,record){
debugger
},
deleteRecord: function(){
debugger
},
findAll: function(){
debugger
},
findQuery: function(){
debugger
},
sendMove: function(connection,record){
// TODO: This should have been done in a serializer.
// I declared 'number'. What's up with that?
var pan = Number(record.get('pan')),
tilt = Number(record.get('tilt'));
var retval = connection.move(pan,tilt);
return retval;
},
init: function(arguments){
var controlUrl = "ws://"+window.location.host+"/control";
var connection = new CameraControl(controlUrl,Ember.Logger);
this.set('connection',connection);
}
});
CameraControl = function(url,logger){
this.createGUID = function(){
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
this.trigger = function(res){
if(typeof res === 'undefined'){
res = 'large';
}
var command = {action: 'trigger', uuid: this.createGUID(), file_type: res};
this.controlConnection.sendJSONCommand([command]);
this.logger.debug("issuing trigger "+command['uuid'])
var self = this;
var deferred = this.reconciler.defer(command.uuid)
deferred.then(function(successData){
self.logger.debug("resolving trigger "+command['uuid']);
// var photo = document.getElementById("photo");
// TODO: Make more defense, check 'type' property for 'base64_img'
// photo.src = "data:image/png;base64,"+successData['payload']['data'];
return successData;
},function(failureData){
self.logger.debug("failed trigger: "+window.JSON.stringify(failureData))
return failureData;
});
return {deferred: deferred, action: command};
}
this.capture = function(){
var command = {action: 'capture', uuid: this.createGUID()};
this.controlConnection.sendJSONCommand([command]);
var self = this;
this.reconciler.defer(command.uuid).then(function(successData){
self.logger.debug("successful capture");
},function(failureData){
self.logger.debug("failed capture: "+window.JSON.stringify(failureData));
})
}
this.move = function(pan,tilt){
var command = {action: 'move', pan: pan, tilt: tilt, uuid: this.createGUID()};
this.controlConnection.sendJSONCommand([command]);
var self = this;
var deferred = this.reconciler.defer(command.uuid)
deferred.then(function(successData){
self.logger.debug("successful move "+window.JSON.stringify(successData));
return successData;
},function(failureData){
self.logger.debug("failed move: "+window.JSON.stringify(failureData));
return failureData;
});
return {deferred: deferred, action: command};
}
this.zero = function(){
var command = {action: 'zero', uuid: this.createGUID()};
this.controlConnection.sendJSONCommand([command]);
var self = this;
this.reconciler.defer(command.uuid).then(function(successData){
self.logger.debug("successful zero "+window.JSON.stringify(successData));
},function(failureData){
self.logger.debug("failed zero: "+window.JSON.stringify(failureData));
})
};
this.sequenceSet = function(){
// TODO generates the sequence on the fly because we don't have a creation routine
var seqId = 1
var seqSteps = []
for(var i=-1000; i<1000; i+=100){
for(var j=-1000; j<1000; j+=100){
seqSteps.push({action: 'move', uuid: this.createGUID(), pan: i, tilt: j});
seqSteps.push({action: 'trigger', uuid: this.createGUID(), file_type: 'large'});
}
}
command = {action: 'sequence_set', uuid: this.createGUID(), sequence_id: seqId, steps: seqSteps}
this.controlConnection.sendJSONCommand([command]);
var self = this;
this.reconciler.defer(command.uuid).then(function(successData){
self.logger.debug("sequenceSet successful " + successData)
},function(errorData){
self.logger.debug("sequenceSet failed! " + errorData)
})
}
this.sequenceExecute = function(){
command = {action: 'sequence_execute', uuid: this.createGUID(), sequence_id: 1}
this.controlConnection.sendJSONCommand([command]);
var self = this;
this.reconciler.defer(command.uuid).then(function(successData){
this.logger.debug("sequence executed!")
},function(errorData){
this.logger.debug("sequence could not be execute")
})
}
if(typeof logger === 'undefined'){
this.logger = new Logger('debug',"log");
} else {
this.logger = logger;
}
this.reconciler = new UUIDReconciler(logger);
this.controlConnection = newReqRepWebSocket(url,this.reconciler,this.logger);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment