-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsgpio-client.js
34 lines (30 loc) · 999 Bytes
/
jsgpio-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Creates a Socket.IO and handles GPIO events.
* @param config {Object} Configuration object. Requires SocketIo.URL, GPIO.wroteEventHandler and GPIO.errEventHandler properties.
* @constructor
*/
JSGPIOClient = function(config) {
this.config = config;
this.init();
}
/**
* Creates the Socket.IO client.
* @function
*/
JSGPIOClient.prototype.init = function () {
// Create socket io client.
this.socketIo = io.connect(this.config.SocketIo.URL);
// Bind 'wrote' event handler.
this.socketIo.on('wrote', function(data) { this.config.GPIO.wroteEventHandler(data);}.bind(this));
// Bind 'error' event handler.
this.socketIo.on('err', function(data) { this.config.GPIO.errEventHandler(data);}.bind(this));
}
/**
* Writes to pin.
* @param pin {Number} Pin number.
* @param value {Number} Value to write: 0 - low, 1 - high.
* @function
*/
JSGPIOClient.prototype.write = function (pin, value) {
this.socketIo.emit('write', { pin: pin, value: value });
}