-
-
Save myndzi/df1ba9ce437a9a8a26f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
var Promise = require('bluebird'); | |
modeminit({ | |
ports: | |
[ { DEVNAME: '/dev/ttyUSB1', | |
ID_MODEL: 'E173s 3G broadband stick (modem on)' }, | |
{ DEVNAME: '/dev/ttyUSB0', | |
ID_MODEL: 'E173s 3G broadband stick (modem on)' }, | |
{ DEVNAME: '/dev/ttyUSB2', | |
ID_MODEL: 'E173s 3G broadband stick (modem on)' } ] | |
}).then(function (port) { | |
console.log("initialized with port:", port); | |
}); | |
function modem(cfg) { | |
this.count = 0; | |
this.ports = cfg.ports; | |
} | |
modem.prototype.openSerial = function (port) { | |
if (!this.count++) { | |
return Promise.resolve(null); | |
} | |
return Promise.resolve('Success on port '+port.DEVNAME); | |
}; | |
function modeminit(mo) { | |
var m = new modem(mo); | |
return Promise.reduce(mo.ports, function (retval, port) { | |
return retval || m.openSerial(port); | |
}, null) | |
.then(function (port) { | |
if (!port) { | |
throw new Error('couldn\'t open anything'); | |
} | |
return port; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment