new mikronode.Connection(host, user, password, options)
An instance of Connection is fully self-contained. You can have as many open
in parallel in the same node process as your environment can handle.
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
host |
string |
The host name or ip address |
||||||||||||||||||||||||
user |
string |
The user name |
||||||||||||||||||||||||
password |
string |
The users password |
||||||||||||||||||||||||
options |
object |
optional
|
Fires:
- mikronode.Connection#event:trap
- mikronode.Connection#event:error
- mikronode.Connection#event:timeout
- mikronode.Connection#event:close
Throws:
WARNING: If you do not listen for 'error' or 'timeout' events and
one occurrs during the initial connection (host unreachable, connection
refused, etc.), an "Unhandled 'error' event" exception will be thrown.
Example
var MikroNode = require('mikronode'); var connection = new MikroNode.Connection('192.168.88.1', 'admin', 'mypassword', { timeout : 4, closeOnDone : true, closeOnTimeout : true, }); connection.on('error', function(err) { console.error('Error: ', err); });
Members
-
readonlychannelsobject
-
Active channels keyed by channel id
-
closeOnDoneboolean
-
If set, when the last channel closes, the
connection will automatically close. -
readonlyhoststring
-
Hostname or ip address
-
readonlypasswordstring
-
Password
-
readonlyportnumber
-
Port
- Default Value:
- 8728
-
readonlystatusstring
-
Connection status
-
readonlytimeoutnumber
-
Socket inactivity timeout
-
readonlytlsobject boolean
-
Set to true to use TLS for this connection
with default options. Set to an object to use TLS and pass the object to
tls.connect as the tls options. If your device uses self-signed
certificates, you'll either need to set 'rejectUnauthorized : false' or
supply the proper CA certificate. See the options for
tls.connect()
for more info. -
readonlyuserstring
-
User ID
Methods
-
staticmikronode.Connection.parseItems(data){Array.<object>}
-
Parse !re return records into an array of objects
Name Type Description data
Array.<string> The data[] returned from Channel.on('done')
-
closeChannel(id)
-
Closes the channel specified by id.
Name Type Description id
number The id of the channel to close
-
connect(callback){mikronode.Connection}
-
Opens the socket and performs authentication
Name Type Description callback
mikronode.Connection.connectCallback Called when authentication
succeeds and the connection is ready for channel activityFires:
- mikronode.Connection#event:trap
- mikronode.Connection#event:error
- mikronode.Connection#event:timeout
- mikronode.Connection#event:close
Throws:
WARNING: If you do not listen for 'error' or 'timeout' events and an
error occurrs during the initial connection (host unreachable, connection
refused, etc.), an "Unhandled 'error' event" exception will be thrown. -
getChannel(id){mikronode.Channel}
-
Returns the channel specified by id.
Name Type Description id
number The id of the channel desired
-
getCommandPromise(data, parameters, options){Promise}
-
** Returnes a Promise of a completed command.
The promise will resolve when the command completes or reject if there's an error or
trap. If resolved, the result will be an array of instances of DestinationClass (or
Object, if no destination class was specified). If rejected, the result will be an
Error if there was a socket error or timeout, or amikronode.Trap
if the
command failed on the device.Name Type Description data
string | Array.<string> Can be a single string with the command and
optional parameters separated by '\n' or an array of strings with the
command in the first position and the parameters in the rest.parameters
object | Array.<string> optional If the first parameter is a command
string, this object will be treated as the parameters for the command.
It can be an array or strings...['name=value','name=value'...]
or an Object...
{'name': 'value', 'name': 'value'...}
options
object optional A set of options that determine what to do with the
return data (if any). If neither dataClass nor itemClass are provided, the
default behavior will be as though itemClass were set to Object. This will
result in Promise.resolve() being called with an array of plain Objects,
one for each parsed item.Name Type Default Description closeOnDone
boolean true optional If true, the channel will
automatically close when the command completes.dontParse
boolean false optional If true, Promise.resolve() will be
called with the unaltered data array provided by the channel's 'done'
event. with the unaltered data array provided by the channel's 'done'
event.dataClass
class optional If provided, this class will be instantiated
with the data array provided by the channel's 'done' event as the
constructor's sole argument. Promise.resolve() will then be called with
this object.itemClass
class optional If provided, {mikronode.parseItems} will be
called on the returned data and this class will be instantiated once for
each resulting item. The item object will be passed as the sole argument
to the constructor. An array of itemClass objects will be passed to
Promise.resolve().itemKey
string optional If provided, instead of an array of parsed
objects being passed to Promise.resolve(), the parsed objects will be
added to a wrapper object using the value of itemKey as the property name.Returns:
promise will have a channel property added which will be set
to the channel used to fulfill the promise.
Example
function Interface(intf) { var _this = this; Object.keys(intf).forEach(function(key) { _this[key] = intf[key]; }); } var chan1Promise = conn.getCommandPromise('/interface/print', { itemClass : Interface, itemKey : 'name' }); chan1Promise.then(function(values){ // It succeeded. You'll have a hash of Interfaces keyed by interface name. }); chan1Promise.catch(function(result){ // It failed. result will tell you why. });
-
getConnectPromise(){Promise}
-
Returns a Promise for an open connection.
The promise will resolve when the connection is ready for use or reject if there's
an error or trap. If resolved, the result object will be themikronode.Connection
with authentication completed and ready for channels.
If rejected, the result object will be an Error if there was a socket error or
timeout during connection or login or amikronode.Trap
if there was a
problem with the login credentials.Example
var MikroNode = require('mikronode'); var connection = new MikroNode.Connection(process.argv[2], process.argv[3], process.argv[4], { closeOnDone : true }); var connPromise = connection.getConnectPromise().then(function resolve(conn) { // You now have an open, authenticated connection // To issue some commands see
getCommandPromise
}); -
openChannel(id){mikronode.Channel}
-
Opens a new Channel
Name Type Default Description id
string next available optional Automatically assigned ids are numbers but you
can specify any string.
Type Definitions
-
mikronode.Connection.connectCallback()
-
Called when the connection is established and authenticated
Type Description mikronode.Connection
Events
-
event:close
-
Emitted when the connection is closed either by an explicit call to
mikronode.Connection#close
or when the connection is closed automatically
viamikronode.Connection#closeOnDone
Properties:
Name Type Description connection
mikronode.Connection The connection originating the event
-
event:error
-
Emitted when a non-recoverable error has occurred on the socket. No further commands
can be processed on any channel.Properties:
Name Type Description error
error The error object
connection
mikronode.Connection The connection originating the event
-
event:timeout
-
Emitted when a socket has been idle too long.
Properties:
Name Type Description message
string 'Socket Timeout'
socketStillOpen
boolean If true, communications can continue
connection
mikronode.Connection The connection originating the event
-
event:trap
-
Emitted when a login has failed. No further commands can be processed on any
channel.Properties:
Name Type Description trap
mikronode.Trap The trap object