Skip to content

Commit

Permalink
split device out
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Aug 22, 2013
1 parent 167f38a commit 447b34b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 105 deletions.
120 changes: 15 additions & 105 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var url = require('url');

var SSDP = require('node-ssdp');
var parser = require('xml2json');
var rest = require('rest');
var defaultRequest = require('rest/interceptor/defaultRequest');
var mime = require('rest/interceptor/mime');
var Device = require('./lib/Device');

function Dial(){
if(!(this instanceof Dial)) return new Dial();
Expand All @@ -15,43 +11,25 @@ function Dial(){

EventEmitter.call(self);

self.on('headers', function(headers){
self.deviceDescription(headers.location);
var ssdp = new SSDP({
log: false
});

var ssdp = this.ssdp = new SSDP();

ssdp.on('response', function onResponse(msg){
ssdp.on('response', function(msg){
// parse the response
var data = msg.toString('utf-8');
var lines = data.split(/\r\n|\n|\r/);

var headers = {};
lines.forEach(function(line, idx){
if(idx === 0){
// the first response is the HTTP status
console.log('STATUS: ', line);
return;
}
// splitting based on first colon
var a = line.split(/:/);
var key = a.shift();
var val = a.join(':');
var header = msg.toString('utf-8');
var headerLocation = header.match(/Location:(.*)/i);
if(!(headerLocation && headerLocation[1])) return;
var location = headerLocation[1].trim();

if(key && val){
// lowercase the keys and leave off key-value pairs where one is empty
headers[key.toLowerCase()] = val.trim();
}
var device = new Device({
location: location
});

self.client = rest
.chain(defaultRequest, {
headers: {
'Host': self.host
}
device.description()
.then(function(){
self.emit('device', device);
});

self.emit('headers', headers);
});

}
Expand All @@ -63,73 +41,5 @@ Dial.prototype.discover = function(){
this.ssdp.search('urn:dial-multiscreen-org:service:dial:1');
};

Dial.prototype.deviceDescription = function(url){
var self = this;

if(typeof url !== 'string'){
url = url.location;
}

return this.client({
path: url
}).then(function(resp){
var description = parser.toJson(resp.entity, {
object: true
});

// TODO: lowercase the header key
self.applicationUrl = resp.headers['Application-Url'];

self.emit('device', description);
}, function(err){
// TODO: error
});
};

Dial.prototype.launch = function(applicationName, data, cb){
var client = this.client.chain(mime, { mime: 'application/x-www-form-urlencoded.js' });

this.applicationResourceUrl = this.applicationUrl + applicationName;

console.log(this.applicationResourceUrl);

return client({
path: this.applicationResourceUrl,
method: 'POST',
entity: data
}).then(function(resp){
if(typeof cb === 'function'){
cb(null, resp);
} else {
return resp;
}
});
};

Dial.prototype.appInfo = function(applicationResourceUrl, cb){
if(typeof applicationResourceUrl === 'function' && typeof cb !== 'function'){
// juggle callback
cb = applicationResourceUrl;
}
if(typeof applicationResourceUrl !== 'string'){
// use applicationResourceUrl of object if one not provided
applicationResourceUrl = this.applicationResourceUrl;
}
return this.client({
path: applicationResourceUrl
}).then(function(resp){
var applicationInfo = parser.toJson(resp.entity, {
object: true
});

if(typeof cb === 'function'){
cb(null, applicationUrl);
} else {
return applicationInfo;
}
}, function(err){
// TODO: error
});
};

module.exports = Dial;
// it's fine for this to be a singleton
module.exports = Dial();
72 changes: 72 additions & 0 deletions lib/Device.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var rest = require('rest');
var parser = require('xml2json');

var mime = require('rest/interceptor/mime');

function Device(opts){
if(!(this instanceof Device)) return new Device(opts);

this.location = opts.location;
}

Device.prototype.description = function(){
var self = this;

return rest({
path: self.location
}).then(function(resp){
// TODO: lowercase the header key
self.applicationUrl = resp.headers['Application-Url'];

var description = parser.toJson(resp.entity, {
object: true
});

var info = description.root.device;

self.name = info.friendlyName;
// TODO: mapping of code names to common name
self.model = info.modelName;
self.info = info;
}, function(err){
// TODO: error
});
};

Device.prototype.launch = function(applicationName, data, cb){
var client = rest.chain(mime, { mime: 'application/x-www-form-urlencoded.js' });

this.applicationResourceUrl = this.applicationUrl + applicationName;

return client({
path: this.applicationResourceUrl,
method: 'POST',
entity: data
}).then(function(resp){
if(typeof cb === 'function'){
cb(null, resp);
} else {
return resp;
}
});
};

Device.prototype.appInfo = function(cb){
return rest({
path: this.applicationResourceUrl
}).then(function(resp){
var applicationInfo = parser.toJson(resp.entity, {
object: true
});

if(typeof cb === 'function'){
cb(null, applicationInfo);
} else {
return applicationInfo;
}
}, function(err){
// TODO: error
});
};

module.exports = Device;

0 comments on commit 447b34b

Please sign in to comment.