@w3bstream/w3bstream-http-client-simulator
Version:
[](https://www.npmjs.com/package/@w3bstream/w3bstream-http-client-simulator)
115 lines (114 loc) • 4.67 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { W3bstreamClient } from "w3bstream-client-js";
import { SimulatorSigner } from "../SimulatorSigner/index.js";
import { SimulatorKeys } from "../SimulatorKeys/index.js";
import { PrivateKeyFile } from "../PrivateKeyFile/index.js";
export class NoDataPointGeneratorError extends Error {
}
export class SendingMessageError extends Error {
}
export class Simulator {
constructor(apiKey, httpRoute) {
this._privateKey = "";
this.publicKey = "";
this._client = new W3bstreamClient(httpRoute, apiKey);
}
init(pathToPrivateKey) {
this.initFromPathOrGenerateNew(pathToPrivateKey !== null && pathToPrivateKey !== void 0 ? pathToPrivateKey : "./");
}
generateSingleMessage() {
const dataPoint = this.generateDataPoint();
const signature = this.signDataPoint(dataPoint);
return {
data: dataPoint,
public_key: this.publicKey,
deviceId: "0x" + SimulatorKeys.hashPublicKey(this.publicKey),
signature,
};
}
powerOn(intervalInSec, eventType) {
const intervalInMs = intervalInSec * 1000;
this._interval = setInterval(() => __awaiter(this, void 0, void 0, function* () {
try {
yield this.sendSingleMessage(eventType);
}
catch (e) {
console.log(e);
console.log("Stopping simulator due to error");
this.powerOff();
}
}), intervalInMs);
}
powerOff() {
if (this._interval) {
clearInterval(this._interval);
delete this._interval;
}
}
sendSingleMessage(eventType) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const message = this.generateSingleMessage();
const header = {
device_id: message.deviceId,
event_type: eventType,
};
const res = yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.publishSingle(header, message));
if (!res) {
throw new SendingMessageError("No response");
}
if ((res === null || res === void 0 ? void 0 : res.status) && (res.status < 200 || res.status >= 300)) {
throw new SendingMessageError("Response status is: " + res.status);
}
this.logSuccessfulMessage(res, message);
return { res, msg: message };
});
}
set dataPointGenerator(generator) {
this._dataPointGenerator = generator;
}
initFromPathOrGenerateNew(pathToPk) {
try {
const privateKey = PrivateKeyFile.getFromPath(pathToPk);
const publicKey = SimulatorKeys.derivePublicKey(privateKey);
this.updateId(privateKey, publicKey);
}
catch (err) {
this.initializeNewId();
}
}
updateId(pk, pubk) {
this._privateKey = pk;
this.publicKey = pubk;
}
initializeNewId() {
const { privateKey, publicKey } = SimulatorKeys.generateKeys();
this.updateId(privateKey, publicKey);
PrivateKeyFile.save(privateKey);
}
signDataPoint(dataPoint) {
return SimulatorSigner.sign(JSON.stringify(dataPoint), this._privateKey);
}
generateDataPoint() {
if (this._dataPointGenerator === undefined) {
throw new NoDataPointGeneratorError();
}
return this._dataPointGenerator.generateDataPoint();
}
logSuccessfulMessage(res, msg) {
var _a, _b;
console.log({
httpResult: (res === null || res === void 0 ? void 0 : res.status) || "",
w3bstreamError: ((_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.errMsg) || ((_b = res === null || res === void 0 ? void 0 : res.data) === null || _b === void 0 ? void 0 : _b.error) || "",
payload: msg,
});
}
}