ironflock 1.0.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +334 -23
- package/dist/index.cjs +3 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +169 -2
- package/dist/index.mjs +799 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +50 -17
- package/dist/crossbar/index.d.ts +0 -38
- package/dist/crossbar/index.js +0 -153
- package/dist/crossbar/serializer.d.ts +0 -8
- package/dist/crossbar/serializer.js +0 -55
- package/dist/index.js +0 -3
- package/dist/ironflock.d.ts +0 -19
- package/dist/ironflock.js +0 -166
- package/dist/test/index.d.ts +0 -1
- package/dist/test/index.js +0 -69
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -26
- package/src/crossbar/index.ts +0 -125
- package/src/crossbar/serializer.ts +0 -31
- package/src/index.ts +0 -3
- package/src/ironflock.ts +0 -109
- package/src/test/index.ts +0 -28
- package/src/utils/index.ts +0 -25
- package/tsconfig.json +0 -21
package/dist/ironflock.js
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.IronFlock = void 0;
|
|
51
|
-
var crossbar_1 = require("./crossbar");
|
|
52
|
-
var _a = process.env, DEVICE_SERIAL_NUMBER = _a.DEVICE_SERIAL_NUMBER, DEVICE_NAME = _a.DEVICE_NAME, SWARM_KEY = _a.SWARM_KEY, APP_KEY = _a.APP_KEY;
|
|
53
|
-
var IronFlock = /** @class */ (function () {
|
|
54
|
-
function IronFlock(options) {
|
|
55
|
-
this.onConnectTasks = [];
|
|
56
|
-
this.options = { quiet: true };
|
|
57
|
-
if (!DEVICE_SERIAL_NUMBER && !(options === null || options === void 0 ? void 0 : options.serialNumber)) {
|
|
58
|
-
throw new Error("the DEVICE_SERIAL_NUMBER environment variable is not defined");
|
|
59
|
-
}
|
|
60
|
-
var device_serial_number = DEVICE_SERIAL_NUMBER ? DEVICE_SERIAL_NUMBER : options === null || options === void 0 ? void 0 : options.serialNumber;
|
|
61
|
-
if (options) {
|
|
62
|
-
this.options = __assign(__assign({}, this.options), options);
|
|
63
|
-
}
|
|
64
|
-
this.onConnected = this.onConnected.bind(this);
|
|
65
|
-
this.crossbarSession = new crossbar_1.CrossbarConnection(device_serial_number);
|
|
66
|
-
this.crossbarSession.on("connected", this.onConnected);
|
|
67
|
-
}
|
|
68
|
-
IronFlock.prototype.onConnected = function () {
|
|
69
|
-
var _a;
|
|
70
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
71
|
-
return __generator(this, function (_b) {
|
|
72
|
-
if (!((_a = this.options) === null || _a === void 0 ? void 0 : _a.quiet)) {
|
|
73
|
-
console.log("Successfully established a socket connection as ".concat(this.crossbarSession.serial_number, " with ").concat(this.crossbarSession.socketURI, "!"));
|
|
74
|
-
}
|
|
75
|
-
return [2 /*return*/, this.executeAllQueuedTasks()];
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
};
|
|
79
|
-
IronFlock.prototype.executeAllQueuedTasks = function () {
|
|
80
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
81
|
-
return __generator(this, function (_a) {
|
|
82
|
-
switch (_a.label) {
|
|
83
|
-
case 0: return [4 /*yield*/, Promise.all(this.onConnectTasks.map(function (t) { return t(); }))];
|
|
84
|
-
case 1:
|
|
85
|
-
_a.sent();
|
|
86
|
-
this.onConnectTasks = [];
|
|
87
|
-
return [2 /*return*/];
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
};
|
|
92
|
-
IronFlock.prototype.queue = function (task) {
|
|
93
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
94
|
-
var taskResult;
|
|
95
|
-
var _this = this;
|
|
96
|
-
return __generator(this, function (_a) {
|
|
97
|
-
switch (_a.label) {
|
|
98
|
-
case 0:
|
|
99
|
-
if (!this.crossbarSession.isConnected()) return [3 /*break*/, 2];
|
|
100
|
-
return [4 /*yield*/, task()];
|
|
101
|
-
case 1:
|
|
102
|
-
taskResult = _a.sent();
|
|
103
|
-
return [2 /*return*/, taskResult !== null && taskResult !== void 0 ? taskResult : true];
|
|
104
|
-
case 2: return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
105
|
-
_this.onConnectTasks.push(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
106
|
-
var taskResult, error_1;
|
|
107
|
-
return __generator(this, function (_a) {
|
|
108
|
-
switch (_a.label) {
|
|
109
|
-
case 0:
|
|
110
|
-
_a.trys.push([0, 2, , 3]);
|
|
111
|
-
return [4 /*yield*/, task()];
|
|
112
|
-
case 1:
|
|
113
|
-
taskResult = _a.sent();
|
|
114
|
-
resolve(taskResult !== null && taskResult !== void 0 ? taskResult : true);
|
|
115
|
-
return [3 /*break*/, 3];
|
|
116
|
-
case 2:
|
|
117
|
-
error_1 = _a.sent();
|
|
118
|
-
reject(error_1);
|
|
119
|
-
return [3 /*break*/, 3];
|
|
120
|
-
case 3: return [2 /*return*/];
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
}); });
|
|
124
|
-
})];
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
IronFlock.prototype._publish = function (topic, data) {
|
|
130
|
-
// checks for null and undefined
|
|
131
|
-
if (data == null) {
|
|
132
|
-
throw new Error("null or undefined cannot be published as payload");
|
|
133
|
-
}
|
|
134
|
-
var args = Array.isArray(data) ? data : [data];
|
|
135
|
-
var kwargs = {
|
|
136
|
-
DEVICE_SERIAL_NUMBER: this.crossbarSession.serial_number,
|
|
137
|
-
DEVICE_NAME: DEVICE_NAME,
|
|
138
|
-
};
|
|
139
|
-
return this.crossbarSession.publish(topic, args, kwargs);
|
|
140
|
-
};
|
|
141
|
-
IronFlock.prototype.publish = function (topic, data) {
|
|
142
|
-
var _this = this;
|
|
143
|
-
return this.queue(function () { return _this._publish.apply(_this, [topic, data]); });
|
|
144
|
-
};
|
|
145
|
-
IronFlock.prototype.publishToTable = function (tablename, data) {
|
|
146
|
-
if (data == null) {
|
|
147
|
-
throw new Error("null or undefined cannot be published as payload");
|
|
148
|
-
}
|
|
149
|
-
if (!SWARM_KEY) {
|
|
150
|
-
throw new Error("SWARM_KEY is not set as environment variable");
|
|
151
|
-
}
|
|
152
|
-
if (!APP_KEY) {
|
|
153
|
-
throw new Error("APP_KEY is not set as environment variable");
|
|
154
|
-
}
|
|
155
|
-
var topic = "".concat(SWARM_KEY, ".").concat(APP_KEY, ".").concat(tablename);
|
|
156
|
-
return this.publish(topic, data);
|
|
157
|
-
};
|
|
158
|
-
IronFlock.prototype.onConnect = function (cb) {
|
|
159
|
-
this.crossbarSession.on("connected", function () { return cb(); });
|
|
160
|
-
};
|
|
161
|
-
IronFlock.prototype.onDisconnect = function (cb) {
|
|
162
|
-
this.crossbarSession.on("disconnected", function () { return cb(); });
|
|
163
|
-
};
|
|
164
|
-
return IronFlock;
|
|
165
|
-
}());
|
|
166
|
-
exports.IronFlock = IronFlock;
|
package/dist/test/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/test/index.js
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
var ironflock_1 = require("../ironflock");
|
|
40
|
-
(function () {
|
|
41
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
42
|
-
var options, ironflock, i;
|
|
43
|
-
return __generator(this, function (_a) {
|
|
44
|
-
switch (_a.label) {
|
|
45
|
-
case 0:
|
|
46
|
-
options = { serialNumber: '603fca74-c45b-4c5e-bd22-24988fd077e6' };
|
|
47
|
-
ironflock = new ironflock_1.IronFlock(options);
|
|
48
|
-
for (i = 0; i < 20; i++) {
|
|
49
|
-
ironflock.publish("re.datapods.meetup", {
|
|
50
|
-
temperature: i,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
console.log("Published 20 temperature entries");
|
|
54
|
-
console.log("Waiting 2 seconds...");
|
|
55
|
-
return [4 /*yield*/, new Promise(function (res) { return setTimeout(res, 2000); })];
|
|
56
|
-
case 1:
|
|
57
|
-
_a.sent();
|
|
58
|
-
console.log("Done waiting 2 seconds!");
|
|
59
|
-
for (i = i; i < 40; i++) {
|
|
60
|
-
ironflock.publish("re.datapods.meetup", {
|
|
61
|
-
temperature: i,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
console.log("Published another 20 temperature entries");
|
|
65
|
-
return [2 /*return*/];
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
})();
|
package/dist/utils/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function isPlainObject(o: any): boolean;
|
package/dist/utils/index.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isPlainObject = void 0;
|
|
4
|
-
function isObject(o) {
|
|
5
|
-
return Object.prototype.toString.call(o) === "[object Object]";
|
|
6
|
-
}
|
|
7
|
-
function isPlainObject(o) {
|
|
8
|
-
var ctor, prot;
|
|
9
|
-
if (isObject(o) === false)
|
|
10
|
-
return false;
|
|
11
|
-
// If has modified constructor
|
|
12
|
-
ctor = o.constructor;
|
|
13
|
-
if (ctor === undefined)
|
|
14
|
-
return true;
|
|
15
|
-
// If has modified prototype
|
|
16
|
-
prot = ctor.prototype;
|
|
17
|
-
if (isObject(prot) === false)
|
|
18
|
-
return false;
|
|
19
|
-
// If constructor does not have an Object-specific method
|
|
20
|
-
if (prot.hasOwnProperty("isPrototypeOf") === false) {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
// Most likely a plain Object
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
exports.isPlainObject = isPlainObject;
|
package/src/crossbar/index.ts
DELETED
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
import autobahn from "autobahn";
|
|
2
|
-
import { EventEmitter } from "stream";
|
|
3
|
-
// import { MsgpackSerializer } from "./serializer";
|
|
4
|
-
const { RESWARM_URL, SWARM_KEY, APP_KEY, ENV } = process.env
|
|
5
|
-
const CB_REALM = `realm-${SWARM_KEY}-${APP_KEY}-${ENV}`
|
|
6
|
-
|
|
7
|
-
export type Details = { caller_authid: string; caller_authrole: string };
|
|
8
|
-
|
|
9
|
-
export type AuthPayload = {
|
|
10
|
-
success: boolean;
|
|
11
|
-
role: string;
|
|
12
|
-
realm: string;
|
|
13
|
-
authid: string;
|
|
14
|
-
extra: {
|
|
15
|
-
username: string;
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const DATAPODS_WS_URI = "wss://cbw.datapods.io/ws-ua-usr";
|
|
20
|
-
const STUDIO_WS_URI = "wss://cbw.ironflock.com/ws-ua-usr";
|
|
21
|
-
const STUDIO_WS_URI_OLD = "wss://cbw.record-evolution.com/ws-ua-usr";
|
|
22
|
-
const LOCALHOST_WS_URI = "ws://localhost:8080/ws-ua-usr";
|
|
23
|
-
|
|
24
|
-
const socketURIMap: Record<string, string> = {
|
|
25
|
-
"https://studio.datapods.io": DATAPODS_WS_URI,
|
|
26
|
-
"https://studio.ironflock.com": STUDIO_WS_URI,
|
|
27
|
-
"https://studio.record-evolution.com": STUDIO_WS_URI_OLD,
|
|
28
|
-
"http://localhost:8085": LOCALHOST_WS_URI
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export class CrossbarConnection extends EventEmitter {
|
|
32
|
-
session?: autobahn.Session;
|
|
33
|
-
connection?: autobahn.Connection;
|
|
34
|
-
serial_number: string;
|
|
35
|
-
socketURI: string;
|
|
36
|
-
realm: string;
|
|
37
|
-
|
|
38
|
-
private reconnectTimeout?: any;
|
|
39
|
-
|
|
40
|
-
constructor(serial_number: string) {
|
|
41
|
-
super()
|
|
42
|
-
this.serial_number = serial_number;
|
|
43
|
-
this.realm = CB_REALM;
|
|
44
|
-
this.socketURI = RESWARM_URL ? socketURIMap[RESWARM_URL] : DATAPODS_WS_URI;
|
|
45
|
-
|
|
46
|
-
this.init();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private init() {
|
|
50
|
-
this.connection = new autobahn.Connection({
|
|
51
|
-
realm: this.realm,
|
|
52
|
-
url: this.socketURI,
|
|
53
|
-
authmethods: ["wampcra"],
|
|
54
|
-
authid: this.serial_number,
|
|
55
|
-
max_retries: -1,
|
|
56
|
-
max_retry_delay: 5,
|
|
57
|
-
initial_retry_delay: 1,
|
|
58
|
-
// @ts-ignore
|
|
59
|
-
// serializers: [new MsgpackSerializer()],
|
|
60
|
-
// @ts-ignore
|
|
61
|
-
autoping_interval: 2,
|
|
62
|
-
autoping_timeout: 4,
|
|
63
|
-
onchallenge: (session, method, extra) => {
|
|
64
|
-
return autobahn.auth_cra.sign(this.serial_number, extra.challenge);
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
this.connection.onopen = async (session, details) => {
|
|
69
|
-
this.session = session;
|
|
70
|
-
this.session.caller_disclose_me = true;
|
|
71
|
-
|
|
72
|
-
this.emit('connected', this.serial_number)
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
this.connection.onclose = (reason, details) => {
|
|
76
|
-
this.emit('disconnected')
|
|
77
|
-
|
|
78
|
-
console.log("connection closed: ", reason, details);
|
|
79
|
-
|
|
80
|
-
console.log("retrying in 1 second..");
|
|
81
|
-
|
|
82
|
-
if (this.reconnectTimeout) clearTimeout(this.reconnectTimeout);
|
|
83
|
-
this.reconnectTimeout = setTimeout(() => {
|
|
84
|
-
this.emit('reconnecting')
|
|
85
|
-
|
|
86
|
-
this.init();
|
|
87
|
-
}, 1000);
|
|
88
|
-
|
|
89
|
-
return true;
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
this.connection.open();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
getSession() {
|
|
96
|
-
return this.session;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
isConnected() {
|
|
100
|
-
return this.session && this.session.isOpen
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
*
|
|
105
|
-
* @param {string} procedure
|
|
106
|
-
* @param {any | undefined} args
|
|
107
|
-
* @param {any | undefined} kwargs
|
|
108
|
-
* @param {autobahn.ICallOptions} options
|
|
109
|
-
*/
|
|
110
|
-
call<T>(procedure: string, args?: any[], kwargs?: object, options?: any) {
|
|
111
|
-
if (!this.isConnected()) {
|
|
112
|
-
throw new Error("cannot call without a connected session");
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return this.session?.call<T>(procedure, args, kwargs, options);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
publish(topic: string, args?: any[], kwargs?: object, options?: any) {
|
|
119
|
-
if (!this.isConnected()) {
|
|
120
|
-
throw new Error("cannot call without a connected session");
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return this.session?.publish(topic, args, kwargs, options);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import * as msgpack from 'msgpack-lite';
|
|
2
|
-
|
|
3
|
-
export class MsgpackSerializer {
|
|
4
|
-
public SERIALIZER_ID: string;
|
|
5
|
-
public BINARY: boolean;
|
|
6
|
-
|
|
7
|
-
constructor() {
|
|
8
|
-
this.SERIALIZER_ID = 'msgpack';
|
|
9
|
-
this.BINARY = true;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public serialize(obj: any): Buffer {
|
|
13
|
-
try {
|
|
14
|
-
const payload: Buffer = msgpack.encode(obj);
|
|
15
|
-
return payload;
|
|
16
|
-
} catch (e) {
|
|
17
|
-
console.warn('MessagePack encoding error', e);
|
|
18
|
-
throw e;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public unserialize(payload: Buffer): any {
|
|
23
|
-
try {
|
|
24
|
-
const obj: any = msgpack.decode(payload);
|
|
25
|
-
return obj;
|
|
26
|
-
} catch (e) {
|
|
27
|
-
console.warn('MessagePack decoding error', e);
|
|
28
|
-
throw e;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
package/src/index.ts
DELETED
package/src/ironflock.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { CrossbarConnection } from "./crossbar";
|
|
2
|
-
|
|
3
|
-
const { DEVICE_SERIAL_NUMBER, DEVICE_NAME, SWARM_KEY, APP_KEY } = process.env;
|
|
4
|
-
|
|
5
|
-
type TOptions = {
|
|
6
|
-
quiet?: boolean;
|
|
7
|
-
serialNumber?: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export class IronFlock {
|
|
11
|
-
private crossbarSession: CrossbarConnection;
|
|
12
|
-
private onConnectTasks: Function[] = [];
|
|
13
|
-
private options?: TOptions = { quiet: true }
|
|
14
|
-
|
|
15
|
-
constructor(options?: TOptions) {
|
|
16
|
-
if (!DEVICE_SERIAL_NUMBER && !options?.serialNumber) {
|
|
17
|
-
throw new Error(
|
|
18
|
-
"the DEVICE_SERIAL_NUMBER environment variable is not defined"
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const device_serial_number = DEVICE_SERIAL_NUMBER ? DEVICE_SERIAL_NUMBER : options?.serialNumber as string
|
|
23
|
-
|
|
24
|
-
if (options) {
|
|
25
|
-
this.options = { ...this.options, ...options }
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
this.onConnected = this.onConnected.bind(this)
|
|
29
|
-
this.crossbarSession = new CrossbarConnection(device_serial_number);
|
|
30
|
-
this.crossbarSession.on("connected", this.onConnected)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private async onConnected() {
|
|
34
|
-
if (!this.options?.quiet) {
|
|
35
|
-
console.log(`Successfully established a socket connection as ${this.crossbarSession.serial_number} with ${this.crossbarSession.socketURI}!`)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return this.executeAllQueuedTasks()
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
private async executeAllQueuedTasks() {
|
|
42
|
-
await Promise.all(this.onConnectTasks.map(t => t()));
|
|
43
|
-
|
|
44
|
-
this.onConnectTasks = [];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
private async queue(task: Function) {
|
|
48
|
-
if (this.crossbarSession.isConnected()) {
|
|
49
|
-
const taskResult = await task()
|
|
50
|
-
return taskResult ?? true;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return new Promise((resolve, reject) => {
|
|
54
|
-
this.onConnectTasks.push(async () => {
|
|
55
|
-
try {
|
|
56
|
-
const taskResult = await task();
|
|
57
|
-
resolve(taskResult ?? true);
|
|
58
|
-
} catch (error) {
|
|
59
|
-
reject(error)
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private _publish(topic: string, data: any) {
|
|
66
|
-
// checks for null and undefined
|
|
67
|
-
if (data == null) {
|
|
68
|
-
throw new Error("null or undefined cannot be published as payload")
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const args = Array.isArray(data) ? data : [data];
|
|
72
|
-
const kwargs = {
|
|
73
|
-
DEVICE_SERIAL_NUMBER: this.crossbarSession.serial_number,
|
|
74
|
-
DEVICE_NAME,
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
return this.crossbarSession.publish(topic, args, kwargs);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
publish(topic: string, data: any) {
|
|
81
|
-
return this.queue(() => this._publish.apply(this, [topic, data]));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
publishToTable(tablename: string, data: any) {
|
|
85
|
-
if (data == null) {
|
|
86
|
-
throw new Error("null or undefined cannot be published as payload");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (!SWARM_KEY) {
|
|
90
|
-
throw new Error("SWARM_KEY is not set as environment variable");
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (!APP_KEY) {
|
|
94
|
-
throw new Error("APP_KEY is not set as environment variable");
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const topic = `${SWARM_KEY}.${APP_KEY}.${tablename}`;
|
|
98
|
-
|
|
99
|
-
return this.publish(topic, data);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
onConnect(cb: Function) {
|
|
103
|
-
this.crossbarSession.on("connected", () => cb());
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
onDisconnect(cb: Function) {
|
|
107
|
-
this.crossbarSession.on("disconnected", () => cb());
|
|
108
|
-
}
|
|
109
|
-
}
|
package/src/test/index.ts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { IronFlock } from "../ironflock";
|
|
2
|
-
|
|
3
|
-
(async function () {
|
|
4
|
-
const options = { serialNumber: '603fca74-c45b-4c5e-bd22-24988fd077e6' }
|
|
5
|
-
const ironflock = new IronFlock(options);
|
|
6
|
-
|
|
7
|
-
for (var i = 0; i < 20; i++) {
|
|
8
|
-
ironflock.publish("re.datapods.meetup", {
|
|
9
|
-
temperature: i,
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
console.log("Published 20 temperature entries")
|
|
14
|
-
|
|
15
|
-
console.log("Waiting 2 seconds...")
|
|
16
|
-
|
|
17
|
-
await new Promise((res) => setTimeout(res, 2000));
|
|
18
|
-
|
|
19
|
-
console.log("Done waiting 2 seconds!")
|
|
20
|
-
|
|
21
|
-
for (i = i; i < 40; i++) {
|
|
22
|
-
ironflock.publish("re.datapods.meetup", {
|
|
23
|
-
temperature: i,
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
console.log("Published another 20 temperature entries")
|
|
28
|
-
})();
|
package/src/utils/index.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
function isObject(o: any) {
|
|
2
|
-
return Object.prototype.toString.call(o) === "[object Object]";
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export function isPlainObject(o: any) {
|
|
6
|
-
var ctor, prot;
|
|
7
|
-
|
|
8
|
-
if (isObject(o) === false) return false;
|
|
9
|
-
|
|
10
|
-
// If has modified constructor
|
|
11
|
-
ctor = o.constructor;
|
|
12
|
-
if (ctor === undefined) return true;
|
|
13
|
-
|
|
14
|
-
// If has modified prototype
|
|
15
|
-
prot = ctor.prototype;
|
|
16
|
-
if (isObject(prot) === false) return false;
|
|
17
|
-
|
|
18
|
-
// If constructor does not have an Object-specific method
|
|
19
|
-
if (prot.hasOwnProperty("isPrototypeOf") === false) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// Most likely a plain Object
|
|
24
|
-
return true;
|
|
25
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es5",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"lib": [
|
|
6
|
-
"es2017",
|
|
7
|
-
"es7",
|
|
8
|
-
"es6"
|
|
9
|
-
],
|
|
10
|
-
"moduleResolution": "node",
|
|
11
|
-
"declaration": true,
|
|
12
|
-
"outDir": "dist",
|
|
13
|
-
"strict": true,
|
|
14
|
-
"esModuleInterop": true
|
|
15
|
-
},
|
|
16
|
-
"include": ["src"],
|
|
17
|
-
"exclude": [
|
|
18
|
-
"node_modules",
|
|
19
|
-
"dist"
|
|
20
|
-
]
|
|
21
|
-
}
|