node-red-contrib-mi-sensors
Version:
A set of nodes to control some of the popular Xiaomi sensors which are connected to the Xiaomi Gateway, and the Gateway itself.
37 lines (31 loc) • 1.26 kB
JavaScript
module.exports = (RED) => {
var fs = require("fs");
var path = require("path");
var JsonDB = require("node-json-db");
var defaultPath = path.join(RED.settings.userDir, "mi");
function XiaomiConfiguratorNode(n) {
RED.nodes.createNode(this, n);
this.name = n.name;
this.deviceList = n.deviceList || [];
this.key = n.key;
this.ip = n.ip;
this.sid = this.sid || n.sid;
this.lSid = n.sid.toLowerCase()
var node = this;
var collectionFilePath = path.join(defaultPath, n.sid + ".json");
try {
var oldFile = path.join(process.cwd(), "JsonDB_" + n.collection + ".json");
var stats = fs.statSync(oldFile);
try {
stats = fs.statSync(defaultPath);
}catch(error) {
fs.mkdirSync(defaultPath);
}
fs.renameSync(oldFile, collectionFilePath);
this.log("Moved old file from '" + oldFile + "' to '" + collectionFilePath + '"');
} catch (error) {
}
this.db = new JsonDB(collectionFilePath, true, true);
}
RED.nodes.registerType("xiaomi-configurator", XiaomiConfiguratorNode);
}