Global Caché communication library for devices supporting the Unified TCP API. Handles device discovery, reconnection (if connection lost), resending (on busyIR), smooth continuous IR repeat mode, and error response handling.
The library focuses on sending IR without abstracting away the Unified TCP API. Other functionality like relay control and module configuration is still possible.
Note: Only tested with GC-100-12 and IP2IR devices, but it should work with any of the Global Caché product family devices.
Requirements:
- Install nvm (Node.js version manager) for local development
- Node.js v20.16 or newer (older versions are not tested)
Node module dependencies:
- debug for log output handling.
- reconnecting-socket, node-backoff for sockets reconnection.
This module is not yet available in the npmjs registry and must be installed from GitHub:
npm install https://github.com/zehnm/gc-unified-lib.git
npm update
(which pulls the latest version from the default
branch).
A specific Git hash can be added to pin the version:
npm install https://github.com/zehnm/gc-unified-lib.git#$HASH
See npm documentation for all options.
const { UnifiedClient } = require("gc-unified-lib");
const client = new UnifiedClient();
client.on("connect", async () => {
console.log("Connected to itach");
try {
const result = await client.send("sendir:..")
} catch (error) {
// Some error happened
}
});
client.connect({host: "192.168.1.234", reconnect: true});
All commands are enqueued and are only sent when a connection to a device is present. The queue is paused if connection is lost and then resumed when connection is restored.
If iTach is already busy sending IR from another connection it will retry every options.retryInterval
until
options.sendTimeout
is reached.
setOptions(options)
Changes options
Arguments
options
- (Mandatory) An options Objectoptions.host
- (Default: null) Hostname/IP to connect tooptions.port
- (Default: 4998) Port to connect tooptions.reconnect
- (Default: false) Try to reconnect if connection is lostoptions.reconnectDelay
- (Default: 200) Delay (in milliseconds) for initial reconnection attempt if a connection has been dropped after connection has been established.options.backoff
- reconnection backoff options from MathieuTurcotte/node-backoff.
Default:{ strategy: "fibonacci", randomisationFactor: 0, initialDelay: 500, maxDelay: 10000, failAfter: null }
options.connectionTimeout
- (Default: 3000) Timeout (in milliseconds) when connection attempt is assumed to be failed. Error event will be emitted.options.retryInterval
- (Default: 99) Time (in milliseconds) between resending attempts (when busyIR is received)options.queueTimeout
- (Default: 200) Maximum time (in milliseconds) a new request may remain in the queue before it has to be sent to the device.options.sendTimeout
- (Default: 500) Time (in milliseconds) after which a sent command will be assumed lostoptions.tcpKeepAlive
- (Default: false) Enable/ disable the native TCP keep-alive functionalityoptions.tcpKeepAliveInitialDelay
- (Default: 30000) Set the delay in milliseconds between the last data packet received and the first keepalive probe. Setting 0 will leave the value unchanged from the default (or previous) setting.
options.backoff
can only be set in the UnifiedClient()
constructor and has no effect in the setOptions
,
connect()
and close()
calls!
Examples
client.setOptions({host: "itachIP2IR", reconnect: true});
connect(options)
Connects to a device and optionally changes options before connecting.
Arguments
options
- An options Object (see setOptions method)
Examples
client.connect();
client.connect({host: "itachIP2IR", reconnect: true});
close(options)
Closes the connection to the device. Note: If reconnect is enabled the connection will not stay closed. If you
want that you have to pass in { reconnect: false }
.
Also note: You can change any options.
Example
client.close();
client.close({reconnect: false});
send(data)
Sends a Unified API command to be executed.
Arguments
data
- (Mandatory) String containing a Unified API command (carriage return not required)
Returns
A promise that will resolve to the result of the sent command.
Example
try {
const result = await client.send('sendir,1:1,1,38400,1,1,347,173,22,22,22,65,22,22,22,22,22,65,22,22,22,22,22,22,22,22,22,22,22,65,22,22,22,65,22,65,22,22,22,22,22,22,22,22,22,65,22,22,22,22,22,22,22,22,22,22,22,65,22,65,22,22,22,65,22,65,22,65,22,65,22,65,22,1657')
console.log(result) // completeir...
} catch (error) {
// handle error
}
Events
state
- Connection state events:stopped
,opening
,opened
,closing
,closed
,reopening
,failed
,connectionTimeout
.connect
- Connection to device has been established and commands will now be sentclose
- Connection to device has been closederror
- Some error with the socket connection
Example
client.on("state", function (state) {
log.debug("Connection state change:", state);
});
client.on("connect", function () {
// Connection established
});
client.on("close", function () {
// Connection closed
});
client.on("error", function (error) {
// Error occurred
});
Logging any kind of output is directed to the debug module.
To let the gc-unified-lib output anything, run your app with the DEBUG
environment variable set like:
DEBUG=gclib:* node app.js
gc-unified-lib exposes the following log-levels:
gclib:msg
: TCP socket message tracegclib:debug
: debugging messagesgclib:debug:socket
: socket related debugging messagesgclib:info
: informational messagesgclib:warn
: warningsgclib:error
: errors
If you only want to get errors and warnings reported:
DEBUG=gclib:warn,gclib:error node app.js
Combine those settings with your existing application if any of your other modules or libs also uses debug
- Rename itach module, goal is to support the complete product family.
- IR learning support, emit events for every received sendir message.
- Unified TCP API: https://www.globalcache.com/files/docs/api-gc-unifiedtcp.pdf
- iTach API: http://www.globalcache.com/files/docs/API-iTach.pdf
We use SemVer for versioning. For the versions available, see the tags and releases on this repository.
The major changes found in each new release are listed in the changelog and under the GitHub releases.
Please read our contribution guidelines before opening a pull request.
This project is licensed under the MIT. See the LICENSE file for details.