Skip to content

Commit

Permalink
Fix connection, boost ver
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Feb 14, 2019
1 parent ade9df7 commit 95662a6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 42 deletions.
38 changes: 15 additions & 23 deletions blynk-espruino.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/* Copyright (c) 2015 Volodymyr Shymanskyy. See the file LICENSE for copying permission. */

function string_of_enum(e,value)
{
for (var k in e) if (e[k] == value) return k;
return "Unknown(" + value + ")";
}
/* Copyright (c) 2015-2019 Volodymyr Shymanskyy. See the file LICENSE for copying permission. */
'use strict';

function blynkHeader(msg_type, msg_id, msg_len) {
return String.fromCharCode(
Expand All @@ -16,20 +11,22 @@ function blynkHeader(msg_type, msg_id, msg_len) {

var MsgType = {
RSP : 0,
LOGIN : 2, //"token" or "mail pass"
LOGIN : 2,
PING : 6,
TWEET : 12,
EMAIL : 13,
NOTIFY : 14,
BRIDGE : 15,
HW_SYNC : 16,
INTERNAL : 17,
SMS : 18,
PROPERTY : 19,
HW : 20,
HW_LOGIN : 29,

REDIRECT : 41,
DEBUG_PRINT : 55
DEBUG_PRINT : 55,

EVENT_LOG : 64,
};

var MsgStatus = {
Expand Down Expand Up @@ -130,7 +127,7 @@ var Blynk = function(auth, options) {

this.auth = auth;
var options = options || {};
this.heartbeat = options.heartbeat || (10*1000);
this.heartbeat = options.heartbeat || 10000;

// Auto-detect board
if (options.board) {
Expand Down Expand Up @@ -233,7 +230,7 @@ Blynk.prototype.onReceive = function(data) {

if (msg_type === MsgType.RSP) {
//console.log('> ', string_of_enum(MsgType, msg_type), msg_id, string_of_enum(MsgStatus, msg_len));
if (!self.profile) {

if (self.timerConn && msg_id === 1) {
if (msg_len === MsgStatus.OK || msg_len === MsgStatus.ALREADY_REGISTERED) {
clearInterval(self.timerConn);
Expand All @@ -243,7 +240,7 @@ Blynk.prototype.onReceive = function(data) {
self.sendMsg(MsgType.PING);
}, self.heartbeat);
console.log('Authorized');
self.sendMsg(MsgType.INTERNAL, ['ver', '0.5.3', 'buff-in', 256, 'dev', 'espruino']);
self.sendMsg(MsgType.INTERNAL, ['ver', '0.5.4', 'buff-in', 256, 'dev', 'espruino']);
self.emit('connect');
} else {
//if invalid token, no point in trying to reconnect
Expand All @@ -261,7 +258,7 @@ Blynk.prototype.onReceive = function(data) {
}
}
}
}

self.buff_in = self.buff_in.substr(5);
continue;
}
Expand Down Expand Up @@ -370,17 +367,13 @@ Blynk.prototype.connect = function() {
self.conn.on('data', function(data) { self.onReceive(data); });
self.conn.on('end', function() { self.end(); });

self.sendRsp(MsgType.LOGIN, 1, self.auth.length, self.auth);
self.sendRsp(MsgType.HW_LOGIN, 1, self.auth.length, self.auth);
});
self.conn.on('error', function(err) { self.error(err); });
};

if (self.profile) {
doConnect();
} else {
self.timerConn = setInterval(doConnect, 10000);
doConnect();
}
};

Blynk.prototype.disconnect = function(reconnect) {
Expand Down Expand Up @@ -431,6 +424,9 @@ Blynk.prototype.setProperty = function(pin, prop, val) {
this.sendMsg(MsgType.PROPERTY, [pin, prop].concat(val));
};

Blynk.prototype.eventLog = function(name, descr) {
this.sendMsg(MsgType.EVENT_LOG, [name].concat(descr));
};

Blynk.prototype.syncAll = function() {
this.sendMsg(MsgType.HW_SYNC);
Expand All @@ -453,8 +449,4 @@ Blynk.prototype.tweet = function(message) {
this.sendMsg(MsgType.TWEET, [message]);
};

Blynk.prototype.sms = function(message) {
this.sendMsg(MsgType.SMS, [message]);
};

exports.Blynk = Blynk;
21 changes: 6 additions & 15 deletions blynk.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* Copyright (c) 2015 Volodymyr Shymanskyy. See the file LICENSE for copying permission. */

/* Copyright (c) 2015-2019 Volodymyr Shymanskyy. See the file LICENSE for copying permission. */
'use strict';

var C = {
};

/*
* Helpers
*/
Expand Down Expand Up @@ -43,8 +39,7 @@ function blynkHeader(msg_type, msg_id, msg_len) {

var MsgType = {
RSP : 0,
REGISTER : 1, //"mail pass"
LOGIN : 2, //"token" or "mail pass"
LOGIN : 2,
SAVE_PROF : 3,
LOAD_PROF : 4,
GET_TOKEN : 5,
Expand All @@ -58,9 +53,9 @@ var MsgType = {
BRIDGE : 15,
HW_SYNC : 16,
INTERNAL : 17,
SMS : 18,
PROPERTY : 19,
HW : 20,
HW_LOGIN : 29,

REDIRECT : 41,
DEBUG_PRINT : 55,
Expand Down Expand Up @@ -265,7 +260,7 @@ var Blynk = function(auth, options) {

this.auth = auth;
var options = options || {};
this.heartbeat = options.heartbeat || (10*1000);
this.heartbeat = options.heartbeat || 10000;

console.log("\n\
___ __ __\n\
Expand Down Expand Up @@ -461,7 +456,7 @@ Blynk.prototype.onReceive = function(data) {
self.sendMsg(MsgType.PING);
}, self.heartbeat);
console.log('Authorized');
self.sendMsg(MsgType.INTERNAL, ['ver', '0.5.3', 'buff-in', 4096, 'dev', 'js']);
self.sendMsg(MsgType.INTERNAL, ['ver', '0.5.4', 'buff-in', 4096, 'dev', 'js']);
self.emit('connect');
} else {
console.log('Could not login:', string_of_enum(MsgStatus, msg_len));
Expand Down Expand Up @@ -612,7 +607,7 @@ Blynk.prototype.connect = function() {
self.conn.on('data', function(data) { self.onReceive(data); });
self.conn.on('end', function() { self.end(); });

self.sendRsp(MsgType.LOGIN, 1, self.auth.length, self.auth);
self.sendRsp(MsgType.HW_LOGIN, 1, self.auth.length, self.auth);
});
self.conn.on('error', function(err) { self.error(err); });
};
Expand Down Expand Up @@ -698,10 +693,6 @@ Blynk.prototype.tweet = function(message) {
this.sendMsg(MsgType.TWEET, [message]);
};

Blynk.prototype.sms = function(message) {
this.sendMsg(MsgType.SMS, [message]);
};

if (typeof module !== 'undefined' && ('exports' in module)) {
exports.Blynk = Blynk;

Expand Down
2 changes: 1 addition & 1 deletion control
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: blynk-library-js
Version: 0.5.3
Version: 0.5.4
Description: Blynk library implementation for JavaScript (Node.js, Espruino)
Section: extras
Priority: optional
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blynk-library",
"version": "0.5.3",
"version": "0.5.4",
"description": "Blynk library implementation for JavaScript (Node.js, Espruino)",
"author": "Volodymyr Shymanskyy",
"license": "MIT",
Expand All @@ -20,8 +20,7 @@
"browser": {
"./blynk-node.js": false
},
"dependencies": {
},
"dependencies": {},
"keywords": [
"Arduino",
"Espruino",
Expand Down

0 comments on commit 95662a6

Please sign in to comment.