-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
87 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |