Skip to content

Commit

Permalink
Release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gablau committed Jun 27, 2021
1 parent 99736cb commit a2d1fd1
Show file tree
Hide file tree
Showing 31 changed files with 234 additions and 157 deletions.
20 changes: 17 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.2.0] - 2021-06-27

### Added
- Node - *Sync* - allow multiple pins send with "syncVirtual" mode. (separate multiple pins with comma)

### Fixed
- Recover accidentally deleted node Sync in package.json - See [Issue #3](https://github.com/gablau/node-red-contrib-blynk-iot/issues/3)
- Old email node not deleted in package.json - See [Issue #2](https://github.com/gablau/node-red-contrib-blynk-iot/issues/2)
- Right handle of socket close/end connection - See [Issue #1](https://github.com/gablau/node-red-contrib-blynk-iot/issues/1)

### Removed from old [Blynk legacy library](https://github.com/gablau/node-red-contrib-blynk-ws)
- Node - *Set Property* - removed "max/min" property

## 0.1.0 - 2021-06-26
### Added
- Implemented TSL connection.
- Implemented TLS connection.
- Add Template ID in configuration node
- Review Redirect command

### Removed from old [Blynk legacy library](https://github.com/gablau/node-red-contrib-blynk-ws)
- Connection throws WebSocket
- Connection through WebSocket
- Removed unsupported node - * Notify *, * Email *, * Bridge *, * Table *
- Removed Proxy in configuration node


[Unreleased]: https://github.com/gablau/node-red-contrib-blynk-ws/compare/1.0.2...HEAD
[0.2.0]: https://github.com/gablau/node-red-contrib-blynk-ws/compare/0.1.0...0.2.0
Binary file modified docs/img/blynk-palette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 7 additions & 6 deletions libs/blynk-enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const MsgType = {
REDIRECT: 41,
DEBUG_PRINT: 55,
EVENT_LOG: 64,
EVENT_CLEAR: 65,

};

Expand All @@ -37,18 +38,18 @@ const MsgStatus = {
NO_ACTIVE_DASHBOARD: 8,
INVALID_TOKEN: 9,
ILLEGAL_COMMAND_BODY: 11,
// GET_GRAPH_DATA_EXCEPTION : 12,
GET_GRAPH_DATA_EXCEPTION: 12,
NOTIFICATION_INVALID_BODY: 13,
NOTIFICATION_NOT_AUTHORIZED: 14,
NOTIFICATION_ERROR: 15,
// reserved
// BLYNK_TIMEOUT : 16,
TIMEOUT: 16,
NO_DATA_EXCEPTION: 17,
// DEVICE_WENT_OFFLINE : 18, //removed
DEVICE_WENT_OFFLINE: 18,
SERVER_EXCEPTION: 19,
// NOT_SUPPORTED_VERSION : 20, //removed
NOT_SUPPORTED_VERSION: 20,
ENERGY_LIMIT: 21,
FACEBOOK_USER_LOGIN_WITH_PASS: 22,
OPERATION_ACCEPT: 22,
OPERATION_DECLINE: 23,
};

module.exports = {
Expand Down
51 changes: 36 additions & 15 deletions libs/blynk-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const sendMsg = function sendMsg(data) {
this.lastHeartBeat = currTimestamp;
this.lastActivityOut = currTimestamp;
// this.log(rawdata);
this.log(`## TLS state: ${this.tlsClient.readyState}`);
this.tlsClient.write(rawdata);
};

Expand All @@ -123,20 +124,30 @@ const processCommand = function processCommand(cmd) {

if (!this.logged) {
if (cmd.type === MsgType.RSP && cmd.msgId === 1) {
if (
cmd.len === MsgStatus.OK
|| cmd.len === MsgStatus.ALREADY_REGISTERED
) {
this.log('Client logged');
this.logged = true;
this.sendInfo();
this.emit('connected', '');
} else if (cmd.len === MsgStatus.BLYNK_INVALID_TOKEN) {
this.log('Invalid auth token');
} else if (cmd.len === MsgStatus.NOT_AUTHENTICATED) {
this.log('Not autenticated');
} else {
this.log(`Connect failed. code: ${cmd.len}`);
switch (cmd.len) {
case MsgStatus.OK:
case MsgStatus.ALREADY_REGISTERED:
this.log('Client logged');
this.logged = true;
this.log('## SET logged TRUE');
this.sendInfo();
this.emit('status-connected', '');
break;
case MsgStatus.INVALID_TOKEN:
this.log('Invalid auth token');
this.logged = false;
this.log('## SET logged FALSE');
break;
case MsgStatus.NOT_AUTHENTICATED:
this.log('Not autenticated');
this.logged = false;
this.log('## SET logged FALSE');
break;
default:
this.log(`Connect failed. code: ${getStatusByCode(cmd.len)}`);
this.logged = false;
this.log('## SET logged FALSE');
break;
}
}
if (cmd.type === MsgType.RSP && cmd.len === MsgStatus.NOT_AUTHENTICATED) {
Expand Down Expand Up @@ -177,7 +188,7 @@ const processCommand = function processCommand(cmd) {
case MsgStatus.NOT_AUTHENTICATED:
this.log('Not autenticated');
break;
case MsgStatus.BLYNK_INVALID_TOKEN:
case MsgStatus.INVALID_TOKEN:
this.log('Invalid auth token');
break;
case MsgStatus.ILLEGAL_COMMAND_BODY:
Expand Down Expand Up @@ -234,6 +245,16 @@ const processCommand = function processCommand(cmd) {
case 'adis':
this.handleAppEvent(cmd.body);
break;
case 'rtc':
case 'utc':
case 'ota':
case 'vfs':
case 'dbg':
case 'meta':
this.warn(
RED._(`Not implemented INTERNAL cmd: ${commandToDebugString(cmd)}`),
);
break;
default:
this.warn(
RED._(`Invalid INTERNAL cmd: ${commandToDebugString(cmd)}`),
Expand Down
2 changes: 1 addition & 1 deletion nodes/blynk-iot-client.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@

<p class="form-tips">
Official documentation:
<a href="http://docs.blynk.cc/#getting-started-getting-started-with-the-blynk-app-4-auth-token">Auth Token</a>
<a href=https://docs.blynk.io/en/getting-started/activating-devices/blynk-edent-static-authtoken#getting-template-id-and-auth-token-for-device">Getting Template ID and Auth Token for device</a>
<p>
</script>

106 changes: 69 additions & 37 deletions nodes/blynk-iot-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const blynkUtil = require('../libs/blynk-util');
const blynkLib = require('../libs/blynk-lib');

module.exports = (RED) => {
const LIBRARY_VERSION = '2.0.0'; // node-red lib version
const LIBRARY_DATE = '2021-06-15'; // node-red lib date
const LIBRARY_VERSION = '0.2.0'; // node-red lib version
const LIBRARY_DATE = '2021-06-27'; // node-red lib date

const RECONNECT_TIMEOUT_SECONDS = 5; // number of seconds for reconnection when disconnected or socket error

Expand Down Expand Up @@ -82,9 +82,27 @@ module.exports = (RED) => {

node.log(`LOG PINS ${JSON.stringify(this.log_pins)}`);

function resetReconnect(err) {
node.log(`## FUNC resetReconnect`);
node.logged = false;
node.log(`## SET logged FALSE`);
node.log(`## TLS state: ${node.tlsClient.readyState}`);
if (node.tlsClient) {
node.tlsClient.destroy(err);
node.log(`TLS destroy`);
node.emit('status-disconnnected'); // set disconnect on Node-Red UI
}
if (node.reconnect_timeout) {
clearTimeout(node.reconnect_timeout);
}
}


function reconnect() {
node.log(`## FUNC reconnect`);
node.logged = false;
node.log(`## SET logged FALSE`);
node.log(`## TLS state: ${node.tlsClient.readyState}`);
if (!node.closing) {
node.log(`Reconnect in ${node.RECONNECT_TIMEOUT_SECONDS} seconds...`);
clearTimeout(node.reconnect_timeout);
Expand All @@ -96,7 +114,10 @@ module.exports = (RED) => {

function startconn() { // Connect to remote endpoint
// should not start connection if no server or key
node.log(`## FUNC startconn`);
node.logged = false;
node.log(`## SET logged FALSE`);
node.emit('status-connecting');

const values = node.path.split(':');
const host = values[0];
Expand All @@ -106,18 +127,14 @@ module.exports = (RED) => {
const tlsOptions = {
host: host,
port: port,
rejectUnauthorized: false
rejectUnauthorized: false,
};

node.log(`Start TLS connection: ${tlsOptions.host}:${tlsOptions.port}`);
const tlsClient = tls.connect(tlsOptions, function () {
// Check if the authorization worked
if (tlsClient.authorized) {
node.log('Connection authorized by a Certificate Authority.');
} else {
node.log('Connection not authorized: ' + tlsClient.authorizationError);
const tlsClient = tls.connect(tlsOptions, () => {
if (!tlsClient.authorized) {
node.log(`Connection not authorized: ${tlsClient.authorizationError}`);
}

tlsClient.setNoDelay(true);
tlsClient.setEncoding('binary');
});
Expand All @@ -128,17 +145,19 @@ module.exports = (RED) => {
node.lastActivityIn = currTimestamp;
node.lastActivityOut = currTimestamp;

/*
tlsClient.on('connect', () => {
node.log('TLS: connect');
});
*/

tlsClient.on('timeout', () => {
node.log('TLS: timeout');
});


tlsClient.on('ready', () => {
node.log('TLS: ready');
// node.log('TLS: ready');
node.login(node.key);
});

Expand Down Expand Up @@ -173,54 +192,67 @@ module.exports = (RED) => {
} // process message
});

tlsClient.on('close', () => {
node.log('TLS: close');
tlsClient.on('close', (hadError) => {
if (hadError) return;
// node.log('TLS: close');
node.log(`Socket closed: ${node.path}`);
node.emit('closed');
reconnect();
node.log(`## TLS state: ${tlsClient.readyState}`);
//node.emit('status-disconnnected'); // set disconnect on Node-Red UI
if (tlsClient.readyState !== 'open') {
node.log('## socket is close -> reconnect');
reconnect();
}
});

tlsClient.on('error', (err) => {
node.log('TLS: error');
node.error(`Socket ${err}`);
node.emit('error');
// node.log('TLS: error');
node.error(`Socket error${err}`);
node.log(`## TLS state: ${tlsClient.readyState}`);
node.emit('error', err);
reconnect();
});
}

function resetReconnect() {
node.logged = false;
if (node.tlsClient) {
node.tlsClient.close();
}
if (node.reconnect_timeout) {
clearTimeout(node.reconnect_timeout);
}
tlsClient.on('end', () => {
// node.log('TLS: end');
node.log(`Socket end: ${node.path}`);
node.log(`## TLS state: ${tlsClient.readyState}`);
//node.emit('end');
//reconnect();
});
}

node.on('close', () => {
// Workaround https://github.com/einaros/ws/pull/253
// Remove listeners from RED.server

node.on('close', (removed, done) => {
node.log('Client Close');
node.closing = true;
node.log(`## TLS state: ${this.tlsClient.readyState}`);
resetReconnect();
if (removed) {
// This node has been disabled/deleted
} else {
// This node is being restarted
}
done();
});

node.on('error', () => {
// Workaround https://github.com/einaros/ws/pull/253
// Remove listeners from RED.server
node.on('error', (err) => {
node.log('Client Error');
node.log(`## TLS state: ${this.tlsClient.readyState}`);
node.emit('status-error');
node.closing = false;
resetReconnect();
resetReconnect(err);
});

// test enabled check
if (this.enabled) {
startconn(); // start outbound connection
node.emit('status-connecting');
//setTimeout(() => {
startconn(); // start outbound connection
//}, 4000);
} else {
node.emit('disabled');
node.emit('status-disabled');
setTimeout(() => {
node.emit('disabled');
node.emit('status-disabled');
node.log('Connection disabled by configuration');
}, 2000);
}
Expand Down
13 changes: 7 additions & 6 deletions nodes/blynk-iot-image-gallery.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@ module.exports = (RED) => {
this.blynkClient = RED.nodes.getNode(this.client);
if (this.blynkClient) {
// this.blynkClient.registerInputNode(this);
this.blynkClient.on('opened', (n) => { // eslint-disable-line no-shadow
this.blynkClient.on('status-connecting', () => { // eslint-disable-line no-shadow
node.status({
fill: 'yellow',
shape: 'dot',
text: RED._('blynk-iot-image-gallery.status.connecting') + n,
text: RED._('blynk-iot-image-gallery.status.connecting'),
});
});
this.blynkClient.on('connected', () => {
this.blynkClient.on('status-connected', () => {
node.status({
fill: 'green',
shape: 'dot',
text: node.connected_label,
});
});
this.blynkClient.on('error', () => {
this.blynkClient.on('status-error', () => {
node.status({
fill: 'red',
shape: 'ring',
text: 'blynk-iot-image-gallery.status.error',
});
});
this.blynkClient.on('closed', () => {
this.blynkClient.on('status-disconnnected', () => {
node.status({
fill: 'red',
shape: 'ring',
text: 'blynk-iot-image-gallery.status.disconnected',
});
});
this.blynkClient.on('disabled', () => {
this.blynkClient.on('status-disabled', () => {
node.status({
fill: 'red',
shape: 'dot',
Expand All @@ -61,6 +61,7 @@ module.exports = (RED) => {
this.on('input', (msg) => {
// no input operation if client not connected or disabled
if (!node.blynkClient || !node.blynkClient.logged) {
node.log(`## logged is FALSE`);
return;
}

Expand Down
Loading

0 comments on commit a2d1fd1

Please sign in to comment.