Async/await let's go!
This commit is contained in:
1 parent
1e6d3acd1b
commit
c3501debd5
20 files changed
+2617
-2916
No files matched your search
+1
-1
@@ -14,10 +14,10 @@
|
||||
"handle-callback-err": "error",
|
||||
"prefer-arrow-callback": "error",
|
||||
"curly": ["error", "multi-or-nest", "consistent"],
|
||||
"consistent-return": ["error", {"treatUndefinedAsUnspecified": true}],
|
||||
"no-buffer-constructor": "error",
|
||||
"prefer-const": ["error", {"destructuring": "all"}],
|
||||
"eqeqeq": ["error", "always", {"null": "ignore"}],
|
||||
"no-return-await": "error",
|
||||
"@typescript-eslint/no-explicit-any": 0,
|
||||
"@typescript-eslint/no-use-before-define": ["error", { "functions": false }]
|
||||
}
|
||||
|
||||
+8
-6
@@ -27,7 +27,7 @@ function exitWorkerGracefully(): void {
|
||||
}
|
||||
|
||||
function exitWorkerUngracefully(): void {
|
||||
extensions.killAll(() => {
|
||||
extensions.killAll().then(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
@@ -97,10 +97,8 @@ if (!cluster.worker) {
|
||||
.catch(exitWorkerUngracefully);
|
||||
});
|
||||
|
||||
db.connect(err => {
|
||||
if (err) throw err;
|
||||
cache.connect(err => {
|
||||
if (err) throw err;
|
||||
Promise.all([db.connect(), cache.connect()])
|
||||
.then(() => {
|
||||
server.start(
|
||||
SERVICE_PORT,
|
||||
SERVICE_ADDRESS,
|
||||
@@ -108,6 +106,10 @@ if (!cluster.worker) {
|
||||
cwmp.listener,
|
||||
cwmp.onConnection
|
||||
);
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
+2
-2
@@ -59,7 +59,7 @@ function errorToFault(err: Error): Fault {
|
||||
process.on("uncaughtException", err => {
|
||||
const fault = errorToFault(err);
|
||||
jobs.forEach(jobId => {
|
||||
process.send([jobId, fault]);
|
||||
process.send([jobId, fault, null]);
|
||||
});
|
||||
jobs.clear();
|
||||
process.disconnect();
|
||||
@@ -81,7 +81,7 @@ process.on("message", message => {
|
||||
code: "ext",
|
||||
message: `No such function '${funcName}' in extension '${fileName}'`
|
||||
};
|
||||
process.send([message[0], fault]);
|
||||
process.send([message[0], fault, null]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+7
-5
@@ -93,11 +93,13 @@ if (!cluster.worker) {
|
||||
.catch(exitWorkerUngracefully);
|
||||
});
|
||||
|
||||
db.connect(err => {
|
||||
if (err) throw err;
|
||||
cache.connect(err => {
|
||||
if (err) throw err;
|
||||
Promise.all([db.connect(), cache.connect()])
|
||||
.then(() => {
|
||||
server.start(SERVICE_PORT, SERVICE_ADDRESS, ssl, listener);
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
+8
-6
@@ -27,7 +27,7 @@ function exitWorkerGracefully(): void {
|
||||
}
|
||||
|
||||
function exitWorkerUngracefully(): void {
|
||||
extensions.killAll(() => {
|
||||
extensions.killAll().then(() => {
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
@@ -97,11 +97,13 @@ if (!cluster.worker) {
|
||||
.catch(exitWorkerUngracefully);
|
||||
});
|
||||
|
||||
db.connect(err => {
|
||||
if (err) throw err;
|
||||
cache.connect(err => {
|
||||
if (err) throw err;
|
||||
Promise.all([db.connect(), cache.connect()])
|
||||
.then(() => {
|
||||
server.start(SERVICE_PORT, SERVICE_ADDRESS, ssl, listener);
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
+7
-5
@@ -105,11 +105,13 @@ if (!cluster.worker) {
|
||||
listener(req, res);
|
||||
};
|
||||
|
||||
db2.connect(err => {
|
||||
if (err) throw err;
|
||||
cache.connect(err => {
|
||||
if (err) throw err;
|
||||
Promise.all([db2.connect(), cache.connect()])
|
||||
.then(() => {
|
||||
server.start(SERVICE_PORT, SERVICE_ADDRESS, ssl, _listener);
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throw err;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -31,6 +31,7 @@ const externals = [
|
||||
"https",
|
||||
"zlib",
|
||||
"crypto",
|
||||
"util",
|
||||
"mongodb",
|
||||
"libxmljs",
|
||||
"vm",
|
||||
|
||||
+138
-167
@@ -29,9 +29,9 @@ import {
|
||||
httpConnectionRequest,
|
||||
udpConnectionRequest
|
||||
} from "./connection-request";
|
||||
import { Expression } from "./types";
|
||||
import { Expression, Task } from "./types";
|
||||
|
||||
export function connectionRequest(deviceId, callback): void {
|
||||
export async function connectionRequest(deviceId): Promise<void> {
|
||||
const options = {
|
||||
projection: {
|
||||
_deviceId: 1,
|
||||
@@ -46,133 +46,119 @@ export function connectionRequest(deviceId, callback): void {
|
||||
}
|
||||
};
|
||||
|
||||
db.devicesCollection.findOne({ _id: deviceId }, options, (err, device) => {
|
||||
if (err) return void callback(err);
|
||||
if (!device) return void callback(new Error("No such device"));
|
||||
const device = await db.devicesCollection.findOne({ _id: deviceId }, options);
|
||||
if (!device) throw new Error("No such device");
|
||||
|
||||
let managementServer,
|
||||
connectionRequestUrl,
|
||||
let managementServer,
|
||||
connectionRequestUrl,
|
||||
udpConnectionRequestAddress,
|
||||
username,
|
||||
password;
|
||||
if (device.Device)
|
||||
// TR-181 data model
|
||||
managementServer = device.Device.ManagementServer;
|
||||
// TR-098 data model
|
||||
else managementServer = device.InternetGatewayDevice.ManagementServer;
|
||||
|
||||
if (managementServer.ConnectionRequestURL)
|
||||
connectionRequestUrl = managementServer.ConnectionRequestURL._value;
|
||||
if (managementServer.UDPConnectionRequestAddress) {
|
||||
udpConnectionRequestAddress =
|
||||
managementServer.UDPConnectionRequestAddress._value;
|
||||
}
|
||||
if (managementServer.ConnectionRequestUsername)
|
||||
username = managementServer.ConnectionRequestUsername._value;
|
||||
if (managementServer.ConnectionRequestPassword)
|
||||
password = managementServer.ConnectionRequestPassword._value;
|
||||
|
||||
const context = {
|
||||
id: device["_id"],
|
||||
serialNumber: device["_deviceId"]["SerialNumber"],
|
||||
productClass: device["_deviceId"]["ProductClass"],
|
||||
oui: device["_deviceId"]["OUI"],
|
||||
username: username || "",
|
||||
password: password || ""
|
||||
};
|
||||
|
||||
const snapshot = await getCurrentSnapshot();
|
||||
const now = Date.now();
|
||||
const UDP_CONNECTION_REQUEST_PORT = +getConfig(
|
||||
snapshot,
|
||||
"cwmp.udpConnectionRequestAuth",
|
||||
context,
|
||||
now
|
||||
);
|
||||
const CONNECTION_REQUEST_TIMEOUT = +getConfig(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestTimeout",
|
||||
context,
|
||||
now
|
||||
);
|
||||
const CONNECTION_REQUEST_ALLOW_BASIC_AUTH = !!getConfig(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestAllowBasicAuth",
|
||||
context,
|
||||
now
|
||||
);
|
||||
let authExp: Expression = getConfigExpression(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestAuth"
|
||||
);
|
||||
|
||||
if (authExp === undefined) {
|
||||
authExp = [
|
||||
"FUNC",
|
||||
"AUTH",
|
||||
["PARAM", "username"],
|
||||
["PARAM", "password"]
|
||||
] as Expression;
|
||||
}
|
||||
|
||||
let udpProm;
|
||||
if (udpConnectionRequestAddress) {
|
||||
udpProm = udpConnectionRequest(
|
||||
udpConnectionRequestAddress,
|
||||
username,
|
||||
password;
|
||||
if (device.Device)
|
||||
// TR-181 data model
|
||||
managementServer = device.Device.ManagementServer;
|
||||
// TR-098 data model
|
||||
else managementServer = device.InternetGatewayDevice.ManagementServer;
|
||||
|
||||
if (managementServer.ConnectionRequestURL)
|
||||
connectionRequestUrl = managementServer.ConnectionRequestURL._value;
|
||||
if (managementServer.UDPConnectionRequestAddress) {
|
||||
udpConnectionRequestAddress =
|
||||
managementServer.UDPConnectionRequestAddress._value;
|
||||
}
|
||||
if (managementServer.ConnectionRequestUsername)
|
||||
username = managementServer.ConnectionRequestUsername._value;
|
||||
if (managementServer.ConnectionRequestPassword)
|
||||
password = managementServer.ConnectionRequestPassword._value;
|
||||
|
||||
const context = {
|
||||
id: device["_id"],
|
||||
serialNumber: device["_deviceId"]["SerialNumber"],
|
||||
productClass: device["_deviceId"]["ProductClass"],
|
||||
oui: device["_deviceId"]["OUI"],
|
||||
username: username || "",
|
||||
password: password || ""
|
||||
};
|
||||
|
||||
getCurrentSnapshot()
|
||||
.then(snapshot => {
|
||||
const now = Date.now();
|
||||
const UDP_CONNECTION_REQUEST_PORT = +getConfig(
|
||||
snapshot,
|
||||
"cwmp.udpConnectionRequestAuth",
|
||||
context,
|
||||
now
|
||||
);
|
||||
const CONNECTION_REQUEST_TIMEOUT = +getConfig(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestTimeout",
|
||||
context,
|
||||
now
|
||||
);
|
||||
const CONNECTION_REQUEST_ALLOW_BASIC_AUTH = !!getConfig(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestAllowBasicAuth",
|
||||
context,
|
||||
now
|
||||
);
|
||||
let authExp: Expression = getConfigExpression(
|
||||
snapshot,
|
||||
"cwmp.connectionRequestAuth"
|
||||
);
|
||||
|
||||
if (authExp === undefined) {
|
||||
authExp = [
|
||||
"FUNC",
|
||||
"AUTH",
|
||||
["PARAM", "username"],
|
||||
["PARAM", "password"]
|
||||
] as Expression;
|
||||
}
|
||||
|
||||
let udpProm;
|
||||
if (udpConnectionRequestAddress) {
|
||||
udpProm = udpConnectionRequest(
|
||||
udpConnectionRequestAddress,
|
||||
authExp,
|
||||
db.configCollection,
|
||||
UDP_CONNECTION_REQUEST_PORT
|
||||
);
|
||||
}
|
||||
|
||||
httpConnectionRequest(
|
||||
connectionRequestUrl,
|
||||
authExp,
|
||||
context,
|
||||
CONNECTION_REQUEST_ALLOW_BASIC_AUTH,
|
||||
CONNECTION_REQUEST_TIMEOUT
|
||||
)
|
||||
.then(callback)
|
||||
.catch(err => {
|
||||
if (udpProm) return void udpProm.then(callback).catch(callback);
|
||||
callback(err);
|
||||
});
|
||||
})
|
||||
.catch(callback);
|
||||
});
|
||||
}
|
||||
|
||||
export function watchTask(deviceId, taskId, timeout, callback): void {
|
||||
setTimeout(() => {
|
||||
db.tasksCollection.findOne(
|
||||
{ _id: taskId },
|
||||
{ projection: { _id: 1 } },
|
||||
(err, task) => {
|
||||
if (err) return void callback(err);
|
||||
|
||||
if (!task) return void callback(null, "completed");
|
||||
|
||||
const q = { _id: `${deviceId}:task_${taskId}` };
|
||||
db.faultsCollection.findOne(
|
||||
q,
|
||||
{ projection: { _id: 1 } },
|
||||
(err, fault) => {
|
||||
if (err) return void callback(err);
|
||||
|
||||
if (fault) return void callback(null, "fault");
|
||||
|
||||
if ((timeout -= 500) <= 0) return void callback(null, "timeout");
|
||||
|
||||
watchTask(deviceId, taskId, timeout, callback);
|
||||
}
|
||||
);
|
||||
}
|
||||
authExp,
|
||||
db.configCollection,
|
||||
UDP_CONNECTION_REQUEST_PORT
|
||||
);
|
||||
}, 500);
|
||||
}
|
||||
|
||||
try {
|
||||
await httpConnectionRequest(
|
||||
connectionRequestUrl,
|
||||
authExp,
|
||||
context,
|
||||
CONNECTION_REQUEST_ALLOW_BASIC_AUTH,
|
||||
CONNECTION_REQUEST_TIMEOUT
|
||||
);
|
||||
} catch (err) {
|
||||
if (!udpProm) throw err;
|
||||
await udpProm;
|
||||
}
|
||||
}
|
||||
|
||||
function sanitizeTask(task, callback): void {
|
||||
export async function watchTask(deviceId, taskId, timeout): Promise<string> {
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
|
||||
const task = await db.tasksCollection.findOne(
|
||||
{ _id: taskId },
|
||||
{ projection: { _id: 1 } }
|
||||
);
|
||||
if (!task) return "completed";
|
||||
|
||||
const q = { _id: `${deviceId}:task_${taskId}` };
|
||||
const fault = await db.faultsCollection.findOne(q, {
|
||||
projection: { _id: 1 }
|
||||
});
|
||||
if (fault) return "fault";
|
||||
|
||||
if ((timeout -= 500) <= 0) return "timeout";
|
||||
|
||||
return watchTask(deviceId, taskId, timeout);
|
||||
}
|
||||
|
||||
function sanitizeTask(task): void {
|
||||
task.timestamp = new Date(task.timestamp || Date.now());
|
||||
if (task.expiry) {
|
||||
if (common.typeOf(task.expiry) === common.DATE_TYPE || isNaN(task.expiry))
|
||||
@@ -180,55 +166,40 @@ function sanitizeTask(task, callback): void {
|
||||
else task.expiry = new Date(task.timestamp.getTime() + +task.expiry * 1000);
|
||||
}
|
||||
|
||||
callback(task);
|
||||
return task;
|
||||
}
|
||||
|
||||
export function insertTasks(tasks, callback): void {
|
||||
export async function insertTasks(tasks): Promise<Task[]> {
|
||||
if (tasks && common.typeOf(tasks) !== common.ARRAY_TYPE) tasks = [tasks];
|
||||
else if (!tasks || tasks.length === 0)
|
||||
return void callback(null, tasks || []);
|
||||
else if (!tasks || tasks.length === 0) return tasks || [];
|
||||
|
||||
let counter = tasks.length;
|
||||
for (const task of tasks) {
|
||||
sanitizeTask(task, t => {
|
||||
if (t.uniqueKey) {
|
||||
db.tasksCollection.deleteOne(
|
||||
{ device: t.device, uniqueKey: t.uniqueKey },
|
||||
() => {}
|
||||
);
|
||||
}
|
||||
|
||||
if (--counter === 0) db.tasksCollection.insertMany(tasks, callback);
|
||||
});
|
||||
sanitizeTask(task);
|
||||
if (task.uniqueKey) {
|
||||
await db.tasksCollection.deleteOne({
|
||||
device: task.device,
|
||||
uniqueKey: task.uniqueKey
|
||||
});
|
||||
}
|
||||
}
|
||||
await db.tasksCollection.insertMany(tasks);
|
||||
return tasks;
|
||||
}
|
||||
|
||||
export function deleteDevice(deviceId, callback): void {
|
||||
db.tasksCollection.deleteMany({ device: deviceId }, err => {
|
||||
if (err) return void callback(err);
|
||||
db.devicesCollection.deleteOne({ _id: deviceId }, err => {
|
||||
if (err) return void callback(err);
|
||||
db.faultsCollection.deleteMany(
|
||||
{
|
||||
_id: {
|
||||
$regex: `^${common.escapeRegExp(deviceId)}\\:`
|
||||
}
|
||||
},
|
||||
err => {
|
||||
if (err) return void callback(err);
|
||||
db.operationsCollection.deleteMany(
|
||||
{
|
||||
_id: {
|
||||
$regex: `^${common.escapeRegExp(deviceId)}\\:`
|
||||
}
|
||||
},
|
||||
err => {
|
||||
if (err) return void callback(err);
|
||||
cache.del(`${deviceId}_tasks_faults_operations`, callback);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
export async function deleteDevice(deviceId): Promise<void> {
|
||||
await Promise.all([
|
||||
db.tasksCollection.deleteMany({ device: deviceId }),
|
||||
db.devicesCollection.deleteOne({ _id: deviceId }),
|
||||
db.faultsCollection.deleteMany({
|
||||
_id: {
|
||||
$regex: `^${common.escapeRegExp(deviceId)}\\:`
|
||||
}
|
||||
}),
|
||||
db.operationsCollection.deleteMany({
|
||||
_id: {
|
||||
$regex: `^${common.escapeRegExp(deviceId)}\\:`
|
||||
}
|
||||
}),
|
||||
cache.del(`${deviceId}_tasks_faults_operations`)
|
||||
]);
|
||||
}
|
||||
+70
-108
@@ -17,162 +17,124 @@
|
||||
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { MongoClient } from "mongodb";
|
||||
import { MongoClient, Collection } from "mongodb";
|
||||
import * as config from "./config";
|
||||
|
||||
const MAX_CACHE_TTL = +config.get("MAX_CACHE_TTL");
|
||||
|
||||
let mongoClient;
|
||||
let mongoCollection;
|
||||
let mongoClient: MongoClient;
|
||||
let mongoCollection: Collection;
|
||||
let mongoTimeOffset = 0;
|
||||
|
||||
export function connect(callback): void {
|
||||
export async function connect(): Promise<void> {
|
||||
const MONGODB_CONNECTION_URL = "" + config.get("MONGODB_CONNECTION_URL");
|
||||
|
||||
MongoClient.connect(
|
||||
MONGODB_CONNECTION_URL,
|
||||
{ useNewUrlParser: true },
|
||||
(err, client) => {
|
||||
if (err) return void callback(err);
|
||||
|
||||
const db = client.db();
|
||||
mongoClient = client;
|
||||
mongoCollection = db.collection("cache");
|
||||
mongoCollection.createIndex({ expire: 1 }, { expireAfterSeconds: 0 });
|
||||
|
||||
const now = Date.now();
|
||||
db.command({ hostInfo: 1 }, (err, res) => {
|
||||
if (err) return void callback(err);
|
||||
mongoTimeOffset = res.system.currentTime.getTime() - now;
|
||||
callback();
|
||||
});
|
||||
}
|
||||
);
|
||||
mongoClient = await MongoClient.connect(MONGODB_CONNECTION_URL, {
|
||||
useNewUrlParser: true
|
||||
});
|
||||
const db = mongoClient.db();
|
||||
mongoCollection = db.collection("cache");
|
||||
await mongoCollection.createIndex({ expire: 1 }, { expireAfterSeconds: 0 });
|
||||
const now = Date.now();
|
||||
const res = await db.command({ hostInfo: 1 });
|
||||
mongoTimeOffset = res.system.currentTime.getTime() - now;
|
||||
}
|
||||
|
||||
export function disconnect(): void {
|
||||
if (mongoClient) mongoClient.close();
|
||||
export async function disconnect(): Promise<void> {
|
||||
if (mongoClient) await mongoClient.close();
|
||||
}
|
||||
|
||||
export function get(key, callback): void {
|
||||
export async function get(key): Promise<any> {
|
||||
const expire = new Date(Date.now() - mongoTimeOffset);
|
||||
if (Array.isArray(key)) {
|
||||
mongoCollection.find({ _id: { $in: key } }).toArray((err, res) => {
|
||||
if (err) return void callback(err);
|
||||
const res = await mongoCollection.find({ _id: { $in: key } }).toArray();
|
||||
|
||||
const indices = {};
|
||||
key.forEach((v, i) => {
|
||||
indices[v] = i;
|
||||
});
|
||||
|
||||
const values = [];
|
||||
res.forEach(r => {
|
||||
if (r["expire"] > expire) values[indices[r["_id"]]] = r["value"];
|
||||
});
|
||||
callback(null, values);
|
||||
const indices = {};
|
||||
key.forEach((v, i) => {
|
||||
indices[v] = i;
|
||||
});
|
||||
|
||||
const values = [];
|
||||
res.forEach(r => {
|
||||
if (r["expire"] > expire) values[indices[r["_id"]]] = r["value"];
|
||||
});
|
||||
|
||||
return values;
|
||||
} else {
|
||||
mongoCollection.findOne({ _id: { $in: [key] } }, (err, res) => {
|
||||
if (err || !res) return void callback(err);
|
||||
|
||||
if (res["expire"] > expire) return void callback(null, res["value"]);
|
||||
|
||||
callback();
|
||||
});
|
||||
const res = await mongoCollection.findOne({ _id: { $in: [key] } });
|
||||
if (res && res["expire"] > expire) return res["value"];
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export function del(key, callback): void {
|
||||
export async function del(key): Promise<void> {
|
||||
if (Array.isArray(key))
|
||||
mongoCollection.deleteMany({ _id: { $in: key } }, callback);
|
||||
else mongoCollection.deleteOne({ _id: key }, callback);
|
||||
await mongoCollection.deleteMany({ _id: { $in: key } });
|
||||
else await mongoCollection.deleteOne({ _id: key });
|
||||
}
|
||||
|
||||
export function set(
|
||||
export async function set(
|
||||
key: string,
|
||||
value: string | number,
|
||||
callback: (err?: Error) => void
|
||||
): void;
|
||||
export function set(
|
||||
key: string,
|
||||
value: string | number,
|
||||
ttl: number,
|
||||
callback: (err?: Error) => void
|
||||
): void;
|
||||
export function set(
|
||||
key: string,
|
||||
value: string | number,
|
||||
ttl: number | ((err?: Error) => void),
|
||||
callback?: (err?: Error) => void
|
||||
): void {
|
||||
if (!callback && typeof ttl === "function") {
|
||||
callback = ttl;
|
||||
ttl = null;
|
||||
}
|
||||
|
||||
if (!ttl) ttl = MAX_CACHE_TTL;
|
||||
|
||||
const expire = new Date(
|
||||
Date.now() - mongoTimeOffset + (ttl as number) * 1000
|
||||
);
|
||||
mongoCollection.replaceOne(
|
||||
ttl: number = MAX_CACHE_TTL
|
||||
): Promise<void> {
|
||||
const expire = new Date(Date.now() - mongoTimeOffset + ttl * 1000);
|
||||
await mongoCollection.replaceOne(
|
||||
{ _id: key },
|
||||
{ _id: key, value: value, expire: expire },
|
||||
{ upsert: true },
|
||||
callback
|
||||
{ upsert: true }
|
||||
);
|
||||
}
|
||||
|
||||
export function pop(key, callback): void {
|
||||
mongoCollection.findOneAndDelete({ _id: key }, (err, res) => {
|
||||
if (err || !res["value"]) return void callback(err);
|
||||
export async function pop(key): Promise<any> {
|
||||
const res = await mongoCollection.findOneAndDelete({ _id: key });
|
||||
|
||||
if (res["value"]["expire"] > new Date(Date.now() - mongoTimeOffset))
|
||||
return void callback(null, res["value"]["value"]);
|
||||
if (
|
||||
res &&
|
||||
res["value"] &&
|
||||
+res["value"]["expire"] - (Date.now() - mongoTimeOffset)
|
||||
)
|
||||
return res["value"]["value"];
|
||||
|
||||
callback();
|
||||
});
|
||||
return null;
|
||||
}
|
||||
|
||||
export function lock(lockName, ttl, callback): void {
|
||||
export async function lock(lockName, ttl): Promise<Function> {
|
||||
const token = Math.random()
|
||||
.toString(36)
|
||||
.slice(2);
|
||||
|
||||
function unlockOrExtend(extendTtl): void {
|
||||
async function unlockOrExtend(extendTtl): Promise<void> {
|
||||
if (!extendTtl) {
|
||||
mongoCollection.deleteOne({ _id: lockName, value: token }, (err, res) => {
|
||||
if (err || res["result"]["n"] !== 1)
|
||||
throw err || new Error("Lock expired");
|
||||
const res = await mongoCollection.deleteOne({
|
||||
_id: lockName,
|
||||
value: token
|
||||
});
|
||||
if (res["result"]["n"] !== 1) throw new Error("Lock expired");
|
||||
} else {
|
||||
const expire = new Date(Date.now() - mongoTimeOffset + extendTtl * 1000);
|
||||
mongoCollection.updateOne(
|
||||
const res = await mongoCollection.updateOne(
|
||||
{ _id: lockName, value: token },
|
||||
{ expire: expire },
|
||||
(err, res) => {
|
||||
if (err || res["result"]["n"] !== 1)
|
||||
throw err || new Error("Lock expired");
|
||||
}
|
||||
{ expire: expire }
|
||||
);
|
||||
if (res["result"]["n"] !== 1) throw new Error("Lock expired");
|
||||
}
|
||||
}
|
||||
|
||||
const expireTest = new Date(Date.now() - mongoTimeOffset);
|
||||
const expireSet = new Date(Date.now() - mongoTimeOffset + ttl * 1000);
|
||||
|
||||
mongoCollection.updateOne(
|
||||
{ _id: lockName, expire: { $lte: expireTest } },
|
||||
{ $set: { value: token, expire: expireSet } },
|
||||
{ upsert: true },
|
||||
err => {
|
||||
if (err && err.code === 11000) {
|
||||
return setTimeout(() => {
|
||||
lock(lockName, ttl, callback);
|
||||
}, 200);
|
||||
}
|
||||
|
||||
return callback(err, unlockOrExtend);
|
||||
try {
|
||||
await mongoCollection.updateOne(
|
||||
{ _id: lockName, expire: { $lte: expireTest } },
|
||||
{ $set: { value: token, expire: expireSet } },
|
||||
{ upsert: true }
|
||||
);
|
||||
} catch (err) {
|
||||
if (err && err.code === 11000) {
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
return lock(lockName, ttl);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return unlockOrExtend;
|
||||
}
|
||||
+643
-774
File diff suppressed because it is too large.
Load diff
@@ -17,59 +17,60 @@
|
||||
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { MongoClient, ObjectID } from "mongodb";
|
||||
import { MongoClient, ObjectID, Collection, FindOneOptions } from "mongodb";
|
||||
import { get } from "./config";
|
||||
import { escapeRegExp } from "./common";
|
||||
import { parse } from "./common/expression-parser";
|
||||
import { DeviceData, Attributes } from "./types";
|
||||
import {
|
||||
DeviceData,
|
||||
Attributes,
|
||||
SessionFault,
|
||||
Task,
|
||||
Operation,
|
||||
Expression
|
||||
} from "./types";
|
||||
import Path from "./common/path";
|
||||
|
||||
export let tasksCollection,
|
||||
devicesCollection,
|
||||
presetsCollection,
|
||||
objectsCollection,
|
||||
provisionsCollection,
|
||||
virtualParametersCollection,
|
||||
faultsCollection,
|
||||
filesCollection,
|
||||
operationsCollection,
|
||||
permissionsCollection,
|
||||
usersCollection,
|
||||
configCollection;
|
||||
export let tasksCollection: Collection,
|
||||
devicesCollection: Collection,
|
||||
presetsCollection: Collection,
|
||||
objectsCollection: Collection,
|
||||
provisionsCollection: Collection,
|
||||
virtualParametersCollection: Collection,
|
||||
faultsCollection: Collection,
|
||||
filesCollection: Collection,
|
||||
operationsCollection: Collection,
|
||||
permissionsCollection: Collection,
|
||||
usersCollection: Collection,
|
||||
configCollection: Collection;
|
||||
|
||||
export let client;
|
||||
export let client: MongoClient;
|
||||
|
||||
export function connect(callback): void {
|
||||
MongoClient.connect(
|
||||
"" + get("MONGODB_CONNECTION_URL"),
|
||||
{ useNewUrlParser: true },
|
||||
(err, _client) => {
|
||||
if (err) return void callback(err);
|
||||
client = _client;
|
||||
const db = client.db();
|
||||
export async function connect(): Promise<void> {
|
||||
client = await MongoClient.connect("" + get("MONGODB_CONNECTION_URL"), {
|
||||
useNewUrlParser: true
|
||||
});
|
||||
|
||||
tasksCollection = db.collection("tasks");
|
||||
tasksCollection.createIndex({ device: 1, timestamp: 1 });
|
||||
const db = client.db();
|
||||
|
||||
devicesCollection = db.collection("devices");
|
||||
presetsCollection = db.collection("presets");
|
||||
objectsCollection = db.collection("objects");
|
||||
filesCollection = db.collection("fs.files");
|
||||
provisionsCollection = db.collection("provisions");
|
||||
virtualParametersCollection = db.collection("virtualParameters");
|
||||
faultsCollection = db.collection("faults");
|
||||
operationsCollection = db.collection("operations");
|
||||
permissionsCollection = db.collection("permissions");
|
||||
usersCollection = db.collection("users");
|
||||
configCollection = db.collection("config");
|
||||
tasksCollection = db.collection("tasks");
|
||||
await tasksCollection.createIndex({ device: 1, timestamp: 1 });
|
||||
|
||||
callback();
|
||||
}
|
||||
);
|
||||
devicesCollection = db.collection("devices");
|
||||
presetsCollection = db.collection("presets");
|
||||
objectsCollection = db.collection("objects");
|
||||
filesCollection = db.collection("fs.files");
|
||||
provisionsCollection = db.collection("provisions");
|
||||
virtualParametersCollection = db.collection("virtualParameters");
|
||||
faultsCollection = db.collection("faults");
|
||||
operationsCollection = db.collection("operations");
|
||||
permissionsCollection = db.collection("permissions");
|
||||
usersCollection = db.collection("users");
|
||||
configCollection = db.collection("config");
|
||||
}
|
||||
|
||||
export function disconnect(): void {
|
||||
if (client) client.close();
|
||||
export async function disconnect(): Promise<void> {
|
||||
if (client) await client.close();
|
||||
}
|
||||
|
||||
// Optimize projection by removing overlaps
|
||||
@@ -93,12 +94,11 @@ function optimizeProjection(obj: {}): {} {
|
||||
return obj;
|
||||
}
|
||||
|
||||
export function fetchDevice(
|
||||
export async function fetchDevice(
|
||||
id: string,
|
||||
timestamp: number,
|
||||
patterns: [Path, number][],
|
||||
callback
|
||||
): void {
|
||||
patterns: [Path, number][]
|
||||
): Promise<[[Path, number, Attributes?][], [Path, number][]]> {
|
||||
const MAX_DEPTH = +get("MAX_DEPTH", id);
|
||||
|
||||
if (!patterns || !patterns.length)
|
||||
@@ -273,259 +273,257 @@ export function fetchDevice(
|
||||
}
|
||||
}
|
||||
|
||||
let options;
|
||||
let options: FindOneOptions;
|
||||
if (!projection[""]) {
|
||||
if (!Object.keys(projection).length) options = { projection: { _id: 1 } };
|
||||
else options = { projection: projection };
|
||||
}
|
||||
|
||||
devicesCollection.findOne({ _id: id }, options, (err, device) => {
|
||||
if (err || !device) return void callback(err);
|
||||
const device = await devicesCollection.findOne({ _id: id }, options);
|
||||
if (!device) return [null, null];
|
||||
|
||||
function storeParams(
|
||||
obj,
|
||||
path: string,
|
||||
pathLength: number,
|
||||
ts,
|
||||
descendantsFetched,
|
||||
projTree
|
||||
): void {
|
||||
let thisFetched = false;
|
||||
if (descendantsFetched) {
|
||||
thisFetched = true;
|
||||
} else if (projection[path.slice(0, -1)]) {
|
||||
descendantsFetched = true;
|
||||
if (
|
||||
pathLength &&
|
||||
projection[
|
||||
path.slice(0, path.lastIndexOf(".", path.length - 2) + 1) +
|
||||
"_timestamp"
|
||||
]
|
||||
) {
|
||||
thisFetched = true;
|
||||
loaded.push([
|
||||
Path.parse(path.slice(0, -1)),
|
||||
((1 << (pathLength - 1)) - 1) ^ ((1 << MAX_DEPTH) - 1)
|
||||
]);
|
||||
} else {
|
||||
loaded.push([
|
||||
Path.parse(path.slice(0, -1)),
|
||||
((1 << pathLength) - 1) ^ ((1 << MAX_DEPTH) - 1)
|
||||
]);
|
||||
}
|
||||
} else if (projection[path + "_writable"]) {
|
||||
loaded.push([Path.parse(path.slice(0, -1)), 1 << (pathLength - 1)]);
|
||||
function storeParams(
|
||||
obj,
|
||||
path: string,
|
||||
pathLength: number,
|
||||
ts,
|
||||
descendantsFetched,
|
||||
projTree
|
||||
): void {
|
||||
let thisFetched = false;
|
||||
if (descendantsFetched) {
|
||||
thisFetched = true;
|
||||
} else if (projection[path.slice(0, -1)]) {
|
||||
descendantsFetched = true;
|
||||
if (
|
||||
pathLength &&
|
||||
projection[
|
||||
path.slice(0, path.lastIndexOf(".", path.length - 2) + 1) +
|
||||
"_timestamp"
|
||||
]
|
||||
) {
|
||||
thisFetched = true;
|
||||
loaded.push([
|
||||
Path.parse(path.slice(0, -1)),
|
||||
((1 << (pathLength - 1)) - 1) ^ ((1 << MAX_DEPTH) - 1)
|
||||
]);
|
||||
} else {
|
||||
loaded.push([
|
||||
Path.parse(path.slice(0, -1)),
|
||||
((1 << pathLength) - 1) ^ ((1 << MAX_DEPTH) - 1)
|
||||
]);
|
||||
}
|
||||
} else if (projection[path + "_writable"]) {
|
||||
loaded.push([Path.parse(path.slice(0, -1)), 1 << (pathLength - 1)]);
|
||||
thisFetched = true;
|
||||
}
|
||||
|
||||
if (obj["_timestamp"]) obj["_timestamp"] = +obj["_timestamp"];
|
||||
if (obj["_timestamp"]) obj["_timestamp"] = +obj["_timestamp"];
|
||||
|
||||
// For compatibility with v1.0 database
|
||||
if (obj["_instance"] && obj["_object"] == null) obj["_object"] = true;
|
||||
// For compatibility with v1.0 database
|
||||
if (obj["_instance"] && obj["_object"] == null) obj["_object"] = true;
|
||||
|
||||
if (thisFetched) {
|
||||
const attrs: Attributes = {};
|
||||
let t = obj["_timestamp"] || 1;
|
||||
if (ts > t) t = ts;
|
||||
if (thisFetched) {
|
||||
const attrs: Attributes = {};
|
||||
let t = obj["_timestamp"] || 1;
|
||||
if (ts > t) t = ts;
|
||||
|
||||
if (obj["_value"] != null) {
|
||||
attrs.value = [obj["_timestamp"] || 1, [obj["_value"], obj["_type"]]];
|
||||
if (obj["_type"] === "xsd:dateTime")
|
||||
attrs.value[1][0] = +attrs.value[1][0];
|
||||
if (obj["_value"] != null) {
|
||||
attrs.value = [obj["_timestamp"] || 1, [obj["_value"], obj["_type"]]];
|
||||
if (obj["_type"] === "xsd:dateTime")
|
||||
attrs.value[1][0] = +attrs.value[1][0];
|
||||
|
||||
obj["_object"] = false;
|
||||
}
|
||||
if (obj["_writable"] != null)
|
||||
attrs.writable = [ts || 1, obj["_writable"] ? 1 : 0];
|
||||
|
||||
if (obj["_object"] != null) attrs.object = [t, obj["_object"] ? 1 : 0];
|
||||
|
||||
res.push([Path.parse(path.slice(0, -1)), t, attrs]);
|
||||
obj["_object"] = false;
|
||||
}
|
||||
if (obj["_writable"] != null)
|
||||
attrs.writable = [ts || 1, obj["_writable"] ? 1 : 0];
|
||||
|
||||
for (const [k, v] of Object.entries(obj)) {
|
||||
if (!k.startsWith("_")) {
|
||||
obj["_object"] = true;
|
||||
storeParams(
|
||||
v,
|
||||
path + k + ".",
|
||||
pathLength + 1,
|
||||
obj["_timestamp"],
|
||||
descendantsFetched,
|
||||
projTree ? projTree[k] : null
|
||||
);
|
||||
if (projTree) delete projTree[k];
|
||||
}
|
||||
}
|
||||
if (obj["_object"] != null) attrs.object = [t, obj["_object"] ? 1 : 0];
|
||||
|
||||
if (!descendantsFetched) {
|
||||
if (projTree) {
|
||||
for (const k of Object.keys(projTree)) {
|
||||
const p = Path.parse(path + k);
|
||||
loaded.push([p, ((1 << pathLength) - 1) ^ ((1 << MAX_DEPTH) - 1)]);
|
||||
if ((obj["_object"] || !pathLength) && obj["_timestamp"])
|
||||
res.push([p, obj["_timestamp"]]);
|
||||
}
|
||||
}
|
||||
} else if ((obj["_object"] || !pathLength) && obj["_timestamp"]) {
|
||||
res.push([Path.parse(path + "*"), obj["_timestamp"]]);
|
||||
res.push([Path.parse(path.slice(0, -1)), t, attrs]);
|
||||
}
|
||||
|
||||
for (const [k, v] of Object.entries(obj)) {
|
||||
if (!k.startsWith("_")) {
|
||||
obj["_object"] = true;
|
||||
storeParams(
|
||||
v,
|
||||
path + k + ".",
|
||||
pathLength + 1,
|
||||
obj["_timestamp"],
|
||||
descendantsFetched,
|
||||
projTree ? projTree[k] : null
|
||||
);
|
||||
if (projTree) delete projTree[k];
|
||||
}
|
||||
}
|
||||
|
||||
for (const [k, v] of Object.entries(device)) {
|
||||
switch (k) {
|
||||
case "_lastInform":
|
||||
if (!descendantsFetched) {
|
||||
if (projTree) {
|
||||
for (const k of Object.keys(projTree)) {
|
||||
const p = Path.parse(path + k);
|
||||
loaded.push([p, ((1 << pathLength) - 1) ^ ((1 << MAX_DEPTH) - 1)]);
|
||||
if ((obj["_object"] || !pathLength) && obj["_timestamp"])
|
||||
res.push([p, obj["_timestamp"]]);
|
||||
}
|
||||
}
|
||||
} else if ((obj["_object"] || !pathLength) && obj["_timestamp"]) {
|
||||
res.push([Path.parse(path + "*"), obj["_timestamp"]]);
|
||||
}
|
||||
}
|
||||
|
||||
for (const [k, v] of Object.entries(device)) {
|
||||
switch (k) {
|
||||
case "_lastInform":
|
||||
res.push([
|
||||
Path.parse("Events.Inform"),
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
writable: [+v, 0],
|
||||
value: [+v, [+v, "xsd:dateTime"]]
|
||||
}
|
||||
]);
|
||||
delete device[k];
|
||||
break;
|
||||
case "_lastBoot":
|
||||
res.push([
|
||||
Path.parse("Events.1_BOOT"),
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
writable: [+v, 0],
|
||||
value: [+v, [+v, "xsd:dateTime"]]
|
||||
}
|
||||
]);
|
||||
delete device[k];
|
||||
break;
|
||||
case "_lastBootstrap":
|
||||
res.push([
|
||||
Path.parse("Events.0_BOOTSTRAP"),
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
writable: [+v, 0],
|
||||
value: [+v, [+v, "xsd:dateTime"]]
|
||||
}
|
||||
]);
|
||||
delete device[k];
|
||||
break;
|
||||
case "_registered":
|
||||
// Use current timestamp for registered event attribute timestamps
|
||||
res.push([
|
||||
Path.parse("Events.Registered"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [+v, "xsd:dateTime"]]
|
||||
}
|
||||
]);
|
||||
delete device[k];
|
||||
break;
|
||||
case "_id":
|
||||
if (projection[""] || projection["_id"]) {
|
||||
res.push([
|
||||
Path.parse("Events.Inform"),
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
writable: [+v, 0],
|
||||
value: [+v, [+v, "xsd:dateTime"]]
|
||||
}
|
||||
]);
|
||||
delete device[k];
|
||||
break;
|
||||
case "_lastBoot":
|
||||
res.push([
|
||||
Path.parse("Events.1_BOOT"),
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
writable: [+v, 0],
|
||||
value: [+v, [+v, "xsd:dateTime"]]
|
||||
}
|
||||
]);
|
||||
delete device[k];
|
||||
break;
|
||||
case "_lastBootstrap":
|
||||
res.push([
|
||||
Path.parse("Events.0_BOOTSTRAP"),
|
||||
+v,
|
||||
{
|
||||
object: [+v, 0],
|
||||
writable: [+v, 0],
|
||||
value: [+v, [+v, "xsd:dateTime"]]
|
||||
}
|
||||
]);
|
||||
delete device[k];
|
||||
break;
|
||||
case "_registered":
|
||||
// Use current timestamp for registered event attribute timestamps
|
||||
res.push([
|
||||
Path.parse("Events.Registered"),
|
||||
Path.parse("DeviceID.ID"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [+v, "xsd:dateTime"]]
|
||||
value: [timestamp, [v as string, "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
delete device[k];
|
||||
break;
|
||||
case "_id":
|
||||
if (projection[""] || projection["_id"]) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.ID"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v as string, "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
delete device[k];
|
||||
break;
|
||||
case "_tags":
|
||||
if ((v as string[]).length) {
|
||||
res.push([
|
||||
Path.parse("Tags"),
|
||||
timestamp,
|
||||
{ object: [timestamp, 1], writable: [timestamp, 0] }
|
||||
]);
|
||||
}
|
||||
delete device[k];
|
||||
break;
|
||||
case "_tags":
|
||||
if ((v as string[]).length) {
|
||||
res.push([
|
||||
Path.parse("Tags"),
|
||||
timestamp,
|
||||
{ object: [timestamp, 1], writable: [timestamp, 0] }
|
||||
]);
|
||||
}
|
||||
|
||||
for (let t of v as string[]) {
|
||||
t = t.replace(/[^a-zA-Z0-9-]+/g, "_");
|
||||
res.push([
|
||||
Path.parse("Tags." + t),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 1],
|
||||
value: [timestamp, [true, "xsd:boolean"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
delete device[k];
|
||||
break;
|
||||
case "_deviceId":
|
||||
if (v["_Manufacturer"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.Manufacturer"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v["_Manufacturer"], "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
for (let t of v as string[]) {
|
||||
t = t.replace(/[^a-zA-Z0-9-]+/g, "_");
|
||||
res.push([
|
||||
Path.parse("Tags." + t),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 1],
|
||||
value: [timestamp, [true, "xsd:boolean"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
delete device[k];
|
||||
break;
|
||||
case "_deviceId":
|
||||
if (v["_Manufacturer"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.Manufacturer"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v["_Manufacturer"], "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
if (v["_OUI"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.OUI"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v["_OUI"], "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
if (v["_OUI"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.OUI"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v["_OUI"], "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
if (v["_ProductClass"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.ProductClass"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v["_ProductClass"], "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
if (v["_ProductClass"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.ProductClass"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v["_ProductClass"], "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
if (v["_SerialNumber"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.SerialNumber"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v["_SerialNumber"], "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
if (v["_SerialNumber"] != null) {
|
||||
res.push([
|
||||
Path.parse("DeviceID.SerialNumber"),
|
||||
timestamp,
|
||||
{
|
||||
object: [timestamp, 0],
|
||||
writable: [timestamp, 0],
|
||||
value: [timestamp, [v["_SerialNumber"], "xsd:string"]]
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
delete device[k];
|
||||
}
|
||||
delete device[k];
|
||||
}
|
||||
}
|
||||
|
||||
storeParams(device, "", 0, 0, false, projectionTree);
|
||||
callback(null, res, loaded);
|
||||
});
|
||||
storeParams(device, "", 0, 0, false, projectionTree);
|
||||
return [res, loaded];
|
||||
}
|
||||
|
||||
export function saveDevice(
|
||||
export async function saveDevice(
|
||||
deviceId: string,
|
||||
deviceData: DeviceData,
|
||||
isNew: boolean,
|
||||
sessionTimestamp: number,
|
||||
callback
|
||||
): void {
|
||||
sessionTimestamp: number
|
||||
): Promise<void> {
|
||||
const update = { $set: {}, $unset: {}, $addToSet: {}, $pull: {} };
|
||||
|
||||
for (const diff of deviceData.timestamps.diff()) {
|
||||
@@ -737,7 +735,7 @@ export function saveDevice(
|
||||
if (!Object.keys(v).length) delete update[k];
|
||||
}
|
||||
|
||||
if (!Object.keys(update).length) return void callback();
|
||||
if (!Object.keys(update).length) return;
|
||||
|
||||
// Mongo doesn't allow $addToSet and $pull at the same time
|
||||
let update2;
|
||||
@@ -746,52 +744,41 @@ export function saveDevice(
|
||||
delete update["$pull"];
|
||||
}
|
||||
|
||||
devicesCollection.updateOne(
|
||||
{ _id: deviceId },
|
||||
update,
|
||||
{ upsert: isNew },
|
||||
(err, result) => {
|
||||
if (err || result.result.n !== 1) {
|
||||
return void callback(
|
||||
err || new Error(`Device ${deviceId} not found in database`)
|
||||
);
|
||||
}
|
||||
const result = await devicesCollection.updateOne({ _id: deviceId }, update, {
|
||||
upsert: isNew
|
||||
});
|
||||
|
||||
if (update2) {
|
||||
return void devicesCollection.updateOne(
|
||||
{ _id: deviceId },
|
||||
update2,
|
||||
callback
|
||||
);
|
||||
}
|
||||
if (result.result.n !== 1)
|
||||
throw new Error(`Device ${deviceId} not found in database`);
|
||||
|
||||
callback();
|
||||
}
|
||||
);
|
||||
if (update2) {
|
||||
await devicesCollection.updateOne({ _id: deviceId }, update2);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export function getFaults(deviceId, callback): void {
|
||||
faultsCollection
|
||||
export async function getFaults(
|
||||
deviceId
|
||||
): Promise<{ [channel: string]: SessionFault }> {
|
||||
const res = await faultsCollection
|
||||
.find({ _id: { $regex: `^${escapeRegExp(deviceId)}\\:` } })
|
||||
.toArray((err, res) => {
|
||||
if (err) return callback(err);
|
||||
.toArray();
|
||||
|
||||
const faults = {};
|
||||
for (const r of res) {
|
||||
const channel = r._id.slice(deviceId.length + 1);
|
||||
delete r._id;
|
||||
delete r.channel;
|
||||
delete r.device;
|
||||
r.timestamp = +r.timestamp;
|
||||
r.provisions = JSON.parse(r.provisions);
|
||||
faults[channel] = r;
|
||||
}
|
||||
const faults: { [channel: string]: SessionFault } = {};
|
||||
for (const r of res) {
|
||||
const channel = r._id.slice(deviceId.length + 1);
|
||||
delete r._id;
|
||||
delete r.channel;
|
||||
delete r.device;
|
||||
r.timestamp = +r.timestamp;
|
||||
r.provisions = JSON.parse(r.provisions);
|
||||
faults[channel] = r;
|
||||
}
|
||||
|
||||
return callback(err, faults);
|
||||
});
|
||||
return faults;
|
||||
}
|
||||
|
||||
export function saveFault(deviceId, channel, fault, callback): void {
|
||||
export async function saveFault(deviceId, channel, fault): Promise<void> {
|
||||
const id = `${deviceId}:${channel}`;
|
||||
fault = Object.assign({}, fault);
|
||||
fault._id = id;
|
||||
@@ -799,87 +786,79 @@ export function saveFault(deviceId, channel, fault, callback): void {
|
||||
fault.channel = channel;
|
||||
fault.timestamp = new Date(fault.timestamp);
|
||||
fault.provisions = JSON.stringify(fault.provisions);
|
||||
faultsCollection.replaceOne({ _id: id }, fault, { upsert: true }, callback);
|
||||
await faultsCollection.replaceOne({ _id: id }, fault, { upsert: true });
|
||||
}
|
||||
|
||||
export function deleteFault(deviceId, channel, callback): void {
|
||||
faultsCollection.deleteOne({ _id: `${deviceId}:${channel}` }, callback);
|
||||
export async function deleteFault(deviceId, channel): Promise<void> {
|
||||
await faultsCollection.deleteOne({ _id: `${deviceId}:${channel}` });
|
||||
}
|
||||
|
||||
export function getDueTasks(deviceId, timestamp, callback): void {
|
||||
export async function getDueTasks(
|
||||
deviceId,
|
||||
timestamp
|
||||
): Promise<[Task[], number]> {
|
||||
const cur = tasksCollection.find({ device: deviceId }).sort(["timestamp"]);
|
||||
const tasks = [];
|
||||
const tasks = [] as Task[];
|
||||
|
||||
let f;
|
||||
cur.next(
|
||||
(f = (err, task) => {
|
||||
if (err) return void callback(err);
|
||||
for await (const task of cur) {
|
||||
if (task.timestamp) task.timestamp = +task.timestamp;
|
||||
if (task.expiry) task.expiry = +task.expiry;
|
||||
if (task.timestamp >= timestamp) return [tasks, +task.timestamp];
|
||||
task._id = String(task._id);
|
||||
|
||||
if (!task) return void callback(null, tasks, null);
|
||||
tasks.push(task);
|
||||
|
||||
if (task.timestamp) task.timestamp = +task.timestamp;
|
||||
if (task.expiry) task.expiry = +task.expiry;
|
||||
if (task.timestamp >= timestamp)
|
||||
return void callback(null, tasks, +task.timestamp);
|
||||
task._id = String(task._id);
|
||||
// For API compatibility
|
||||
if (task.name === "download" && task.file) {
|
||||
let q;
|
||||
if (ObjectID.isValid(task.file))
|
||||
q = { _id: { $in: [task.file, new ObjectID(task.file)] } };
|
||||
else q = { _id: task.file };
|
||||
|
||||
tasks.push(task);
|
||||
const res = await filesCollection.find(q).toArray();
|
||||
|
||||
// For API compatibility
|
||||
if (task.name === "download" && task.file) {
|
||||
let q;
|
||||
if (ObjectID.isValid(task.file))
|
||||
q = { _id: { $in: [task.file, new ObjectID(task.file)] } };
|
||||
else q = { _id: task.file };
|
||||
if (res[0]) {
|
||||
if (!task.fileType) task.fileType = res[0].metadata.fileType;
|
||||
|
||||
filesCollection.find(q).toArray((err, res) => {
|
||||
if (err) return void callback(err);
|
||||
|
||||
if (res[0]) {
|
||||
if (!task.fileType) task.fileType = res[0].metadata.fileType;
|
||||
|
||||
if (!task.fileName)
|
||||
task.fileName = res[0].filename || res[0]._id.toString();
|
||||
}
|
||||
|
||||
cur.next(f);
|
||||
});
|
||||
} else {
|
||||
cur.next(f);
|
||||
if (!task.fileName)
|
||||
task.fileName = res[0].filename || res[0]._id.toString();
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
return [tasks, null];
|
||||
}
|
||||
|
||||
export function clearTasks(deviceId, taskIds, callback): void {
|
||||
tasksCollection.deleteMany(
|
||||
{ _id: { $in: taskIds.map(id => new ObjectID(id)) } },
|
||||
callback
|
||||
);
|
||||
export async function clearTasks(deviceId, taskIds): Promise<void> {
|
||||
await tasksCollection.deleteMany({
|
||||
_id: { $in: taskIds.map(id => new ObjectID(id)) }
|
||||
});
|
||||
}
|
||||
|
||||
export function getOperations(deviceId, callback): void {
|
||||
operationsCollection
|
||||
export async function getOperations(
|
||||
deviceId
|
||||
): Promise<{ [commandKey: string]: Operation }> {
|
||||
const res = await operationsCollection
|
||||
.find({ _id: { $regex: `^${escapeRegExp(deviceId)}\\:` } })
|
||||
.toArray((err, res) => {
|
||||
if (err) return void callback(err);
|
||||
.toArray();
|
||||
|
||||
const operations = {};
|
||||
for (const r of res) {
|
||||
const commandKey = r._id.slice(deviceId.length + 1);
|
||||
delete r._id;
|
||||
r.timestamp = +r.timestamp;
|
||||
if (r.args) r.args = JSON.parse(r.args);
|
||||
r.provisions = JSON.parse(r.provisions);
|
||||
r.retries = JSON.parse(r.retries);
|
||||
operations[commandKey] = r;
|
||||
}
|
||||
|
||||
callback(err, operations);
|
||||
});
|
||||
const operations: { [commandKey: string]: Operation } = {};
|
||||
for (const r of res) {
|
||||
const commandKey = r._id.slice(deviceId.length + 1);
|
||||
delete r._id;
|
||||
r.timestamp = +r.timestamp;
|
||||
if (r.args) r.args = JSON.parse(r.args);
|
||||
r.provisions = JSON.parse(r.provisions);
|
||||
r.retries = JSON.parse(r.retries);
|
||||
operations[commandKey] = r;
|
||||
}
|
||||
return operations;
|
||||
}
|
||||
|
||||
export function saveOperation(deviceId, commandKey, operation, callback): void {
|
||||
export async function saveOperation(
|
||||
deviceId,
|
||||
commandKey,
|
||||
operation
|
||||
): Promise<void> {
|
||||
const id = `${deviceId}:${commandKey}`;
|
||||
operation = Object.assign({}, operation);
|
||||
operation._id = id;
|
||||
@@ -887,75 +866,64 @@ export function saveOperation(deviceId, commandKey, operation, callback): void {
|
||||
operation.provisions = JSON.stringify(operation.provisions);
|
||||
operation.retries = JSON.stringify(operation.retries);
|
||||
operation.args = JSON.stringify(operation.args);
|
||||
operationsCollection.replaceOne(
|
||||
{ _id: id },
|
||||
operation,
|
||||
{ upsert: true },
|
||||
callback
|
||||
);
|
||||
}
|
||||
|
||||
export function deleteOperation(deviceId, commandKey, callback): void {
|
||||
operationsCollection.deleteOne(
|
||||
{ _id: `${deviceId}:${commandKey}` },
|
||||
callback
|
||||
);
|
||||
}
|
||||
|
||||
export function getPresets(callback): void {
|
||||
presetsCollection.find().toArray(callback);
|
||||
}
|
||||
|
||||
export function getObjects(callback): void {
|
||||
objectsCollection.find().toArray(callback);
|
||||
}
|
||||
|
||||
export function getProvisions(callback): void {
|
||||
provisionsCollection.find().toArray(callback);
|
||||
}
|
||||
|
||||
export function getVirtualParameters(callback): void {
|
||||
virtualParametersCollection.find().toArray(callback);
|
||||
}
|
||||
|
||||
export function getFiles(callback): void {
|
||||
filesCollection.find().toArray(callback);
|
||||
}
|
||||
|
||||
export function getConfig(callback): void {
|
||||
configCollection.find().toArray((err, res) => {
|
||||
if (err) return void callback(err);
|
||||
|
||||
callback(
|
||||
null,
|
||||
res.map(c => ({
|
||||
id: c["_id"],
|
||||
value: parse(c["value"])
|
||||
}))
|
||||
);
|
||||
await operationsCollection.replaceOne({ _id: id }, operation, {
|
||||
upsert: true
|
||||
});
|
||||
}
|
||||
|
||||
export function getPermissions(
|
||||
callback: (
|
||||
err?: Error,
|
||||
permissions?: {
|
||||
role: string;
|
||||
resource: string;
|
||||
access: number;
|
||||
filter: string;
|
||||
validate: string;
|
||||
}[]
|
||||
) => void
|
||||
): void {
|
||||
permissionsCollection.find().toArray(callback);
|
||||
export async function deleteOperation(deviceId, commandKey): Promise<void> {
|
||||
await operationsCollection.deleteOne({ _id: `${deviceId}:${commandKey}` });
|
||||
}
|
||||
|
||||
export function getUsers(
|
||||
callback: (
|
||||
err?: Error,
|
||||
users?: { _id: string; password: string; salt: string; roles: string }[]
|
||||
) => void
|
||||
): void {
|
||||
usersCollection.find().toArray(callback);
|
||||
export async function getPresets(): Promise<{}[]> {
|
||||
return presetsCollection.find().toArray();
|
||||
}
|
||||
|
||||
export async function getObjects(): Promise<{}[]> {
|
||||
return objectsCollection.find().toArray();
|
||||
}
|
||||
|
||||
export async function getProvisions(): Promise<{}[]> {
|
||||
return provisionsCollection.find().toArray();
|
||||
}
|
||||
|
||||
export async function getVirtualParameters(): Promise<{}[]> {
|
||||
return virtualParametersCollection.find().toArray();
|
||||
}
|
||||
|
||||
export function getFiles(): Promise<{}[]> {
|
||||
return filesCollection.find().toArray();
|
||||
}
|
||||
|
||||
export async function getConfig(): Promise<
|
||||
{ id: string; value: Expression }[]
|
||||
> {
|
||||
const res = await configCollection.find().toArray();
|
||||
return res.map(c => ({
|
||||
id: c["_id"],
|
||||
value: parse(c["value"])
|
||||
}));
|
||||
}
|
||||
|
||||
interface Permission {
|
||||
role: string;
|
||||
resource: string;
|
||||
access: number;
|
||||
filter: string;
|
||||
validate: string;
|
||||
}
|
||||
|
||||
export async function getPermissions(): Promise<Permission[]> {
|
||||
return permissionsCollection.find().toArray();
|
||||
}
|
||||
|
||||
interface User {
|
||||
_id: string;
|
||||
password: string;
|
||||
salt: string;
|
||||
roles: string;
|
||||
}
|
||||
|
||||
export async function getUsers(): Promise<User[]> {
|
||||
return usersCollection.find().toArray();
|
||||
}
|
||||
+70
-65
@@ -17,92 +17,97 @@
|
||||
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import { spawn } from "child_process";
|
||||
import { spawn, ChildProcess } from "child_process";
|
||||
import * as crypto from "crypto";
|
||||
import * as config from "./config";
|
||||
import { Fault } from "./types";
|
||||
|
||||
const TIMEOUT = +config.get("EXT_TIMEOUT");
|
||||
|
||||
const processes = {};
|
||||
const processes: { [script: string]: ChildProcess } = {};
|
||||
const jobs = new Map();
|
||||
|
||||
function messageHandler(message): void {
|
||||
const func = jobs.get(message[0]);
|
||||
if (func) {
|
||||
jobs.delete(message[0]);
|
||||
func(null, message[1], message[2]);
|
||||
func({ fault: message[1], value: message[2] });
|
||||
}
|
||||
}
|
||||
|
||||
export function run(args, callback): boolean {
|
||||
const scriptName = args[0];
|
||||
export function run(args): Promise<{ fault: Fault; value: any }> {
|
||||
return new Promise(resolve => {
|
||||
const scriptName = args[0];
|
||||
|
||||
const id = crypto.randomBytes(8).toString("hex");
|
||||
jobs.set(id, callback);
|
||||
const id = crypto.randomBytes(8).toString("hex");
|
||||
jobs.set(id, resolve);
|
||||
|
||||
if (!processes[scriptName]) {
|
||||
const p = spawn("bin/genieacs-ext", [scriptName], {
|
||||
stdio: ["inherit", "inherit", "inherit", "ipc"]
|
||||
});
|
||||
processes[scriptName] = p;
|
||||
if (!processes[scriptName]) {
|
||||
const p = spawn("bin/genieacs-ext", [scriptName], {
|
||||
stdio: ["inherit", "inherit", "inherit", "ipc"]
|
||||
});
|
||||
processes[scriptName] = p;
|
||||
|
||||
p.on("error", err => {
|
||||
if (processes[scriptName] === p) {
|
||||
if (jobs.delete(id))
|
||||
callback(null, { code: err.name, message: err.message });
|
||||
p.on("error", err => {
|
||||
if (processes[scriptName] === p) {
|
||||
if (jobs.delete(id)) {
|
||||
resolve({
|
||||
fault: { code: err.name, message: err.message },
|
||||
value: null
|
||||
});
|
||||
}
|
||||
|
||||
kill(processes[scriptName]);
|
||||
delete processes[scriptName];
|
||||
}
|
||||
});
|
||||
kill(processes[scriptName]);
|
||||
delete processes[scriptName];
|
||||
}
|
||||
});
|
||||
|
||||
p.on("disconnect", () => {
|
||||
if (processes[scriptName] === p) {
|
||||
kill(processes[scriptName]);
|
||||
delete processes[scriptName];
|
||||
}
|
||||
});
|
||||
p.on("disconnect", () => {
|
||||
if (processes[scriptName] === p) delete processes[scriptName];
|
||||
});
|
||||
|
||||
p.on("message", messageHandler);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
if (jobs.delete(id))
|
||||
callback(null, { code: "timeout", message: "Extension timed out" });
|
||||
}, TIMEOUT);
|
||||
|
||||
if (!processes[scriptName].connected) return false;
|
||||
|
||||
return processes[scriptName].send([id, args.slice(1)]);
|
||||
}
|
||||
|
||||
function kill(process, callback?: () => void): void {
|
||||
if (process.signalCode !== null || process.exitCode !== null) return;
|
||||
|
||||
const timeToKill = Date.now() + 5000;
|
||||
|
||||
process.kill();
|
||||
|
||||
const t = setInterval(() => {
|
||||
if (process.signalCode !== null || process.exitCode !== null) {
|
||||
clearInterval(t);
|
||||
if (callback) callback();
|
||||
} else if (Date.now() > timeToKill) {
|
||||
process.kill("SIGKILL");
|
||||
clearInterval(t);
|
||||
if (callback) callback();
|
||||
p.on("message", messageHandler);
|
||||
}
|
||||
}, 100);
|
||||
|
||||
setTimeout(() => {
|
||||
if (jobs.delete(id)) {
|
||||
resolve({
|
||||
fault: { code: "timeout", message: "Extension timed out" },
|
||||
value: null
|
||||
});
|
||||
}
|
||||
}, TIMEOUT);
|
||||
|
||||
if (!processes[scriptName].connected) return false;
|
||||
|
||||
return processes[scriptName].send([id, args.slice(1)]);
|
||||
});
|
||||
}
|
||||
|
||||
export function killAll(callback?): void {
|
||||
let counter = 1;
|
||||
for (const n in processes) {
|
||||
++counter;
|
||||
kill(processes[n], () => {
|
||||
if (--counter === 0 && callback) callback();
|
||||
});
|
||||
delete processes[n];
|
||||
}
|
||||
if (--counter === 0 && callback) callback();
|
||||
function kill(process: ChildProcess): Promise<void> {
|
||||
return new Promise(resolve => {
|
||||
const timeToKill = Date.now() + 5000;
|
||||
|
||||
process.kill();
|
||||
|
||||
const t = setInterval(() => {
|
||||
if (!process.connected) {
|
||||
clearInterval(t);
|
||||
resolve();
|
||||
} else if (Date.now() > timeToKill) {
|
||||
process.kill("SIGKILL");
|
||||
clearInterval(t);
|
||||
resolve();
|
||||
}
|
||||
}, 100);
|
||||
});
|
||||
}
|
||||
|
||||
export async function killAll(): Promise<void> {
|
||||
await Promise.all(
|
||||
Object.entries(processes).map(([k, p]) => {
|
||||
delete processes[k];
|
||||
return kill(p);
|
||||
})
|
||||
);
|
||||
}
|
||||
+309
-374
@@ -108,391 +108,326 @@ function flattenObject(src, prefix = "", dst = {}): {} {
|
||||
return dst;
|
||||
}
|
||||
|
||||
function refresh(callback): void {
|
||||
async function fetchPresets(): Promise<Preset[]> {
|
||||
const res = await db.getPresets();
|
||||
let objects = await db.getObjects();
|
||||
|
||||
objects = objects.map(obj => {
|
||||
// Flatten object
|
||||
obj = flattenObject(obj);
|
||||
|
||||
// If no keys are defined, consider all parameters as keys to keep the
|
||||
// same behavior from v1.0
|
||||
if (!obj["_keys"] || !obj["_keys"].length)
|
||||
obj["_keys"] = Object.keys(obj).filter(k => !k.startsWith("_"));
|
||||
|
||||
return obj;
|
||||
});
|
||||
|
||||
res.sort((a, b) => {
|
||||
if (a["weight"] === b["weight"])
|
||||
return a["_id"] > b["_id"] ? 1 : a["_id"] < b["_id"] ? -1 : 0;
|
||||
else return a["weight"] - b["weight"];
|
||||
});
|
||||
|
||||
const presets = [] as Preset[];
|
||||
for (const preset of res) {
|
||||
let schedule: { md5: string; duration: number; schedule: any } = null;
|
||||
if (preset["schedule"]) {
|
||||
const parts = preset["schedule"].trim().split(/\s+/);
|
||||
schedule = {
|
||||
md5: crypto
|
||||
.createHash("md5")
|
||||
.update(preset["schedule"])
|
||||
.digest("hex"),
|
||||
duration: null,
|
||||
schedule: null
|
||||
};
|
||||
|
||||
try {
|
||||
schedule.duration = +parts.shift() * 1000;
|
||||
schedule.schedule = scheduling.parseCron(parts.join(" "));
|
||||
} catch (err) {
|
||||
logger.warn({
|
||||
message: "Invalid preset schedule",
|
||||
preset: preset["_id"],
|
||||
schedule: preset["schedule"]
|
||||
});
|
||||
schedule.schedule = false;
|
||||
}
|
||||
}
|
||||
|
||||
const events = preset["events"] || {};
|
||||
let precondition;
|
||||
try {
|
||||
precondition = parse(preset["precondition"]);
|
||||
} catch (error) {
|
||||
precondition = mongoQueryToFilter(JSON.parse(preset["precondition"]));
|
||||
}
|
||||
|
||||
const _provisions = preset["provisions"] || [];
|
||||
|
||||
// Generate provisions from the old configuration format
|
||||
for (const c of preset["configurations"]) {
|
||||
switch (c.type) {
|
||||
case "age":
|
||||
_provisions.push(["refresh", c.name, c.age]);
|
||||
break;
|
||||
|
||||
case "value":
|
||||
_provisions.push(["value", c.name, c.value]);
|
||||
break;
|
||||
|
||||
case "add_tag":
|
||||
_provisions.push(["tag", c.tag, true]);
|
||||
break;
|
||||
|
||||
case "delete_tag":
|
||||
_provisions.push(["tag", c.tag, false]);
|
||||
break;
|
||||
|
||||
case "provision":
|
||||
_provisions.push([c.name].concat(c.args || []));
|
||||
break;
|
||||
|
||||
case "add_object":
|
||||
for (const obj of objects) {
|
||||
if (obj["_id"] === c.object) {
|
||||
const alias = obj["_keys"]
|
||||
.map(k => `${k}:${JSON.stringify(obj[k])}`)
|
||||
.join(",");
|
||||
const p = `${c.name}.[${alias}]`;
|
||||
_provisions.push(["instances", p, 1]);
|
||||
|
||||
for (const k in obj) {
|
||||
if (!k.startsWith("_") && !(obj["_keys"].indexOf(k) !== -1))
|
||||
_provisions.push(["value", `${p}.${k}`, obj[k]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "delete_object":
|
||||
for (const obj of objects) {
|
||||
if (obj["_id"] === c.object) {
|
||||
const alias = obj["_keys"]
|
||||
.map(k => `${k}:${JSON.stringify(obj[k])}`)
|
||||
.join(",");
|
||||
const p = `${c.name}.[${alias}]`;
|
||||
_provisions.push(["instances", p, 0]);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown configuration type ${c.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
presets.push({
|
||||
name: preset["_id"],
|
||||
channel: (preset["channel"] as string) || "default",
|
||||
schedule: schedule,
|
||||
events: events,
|
||||
precondition: precondition,
|
||||
provisions: _provisions
|
||||
});
|
||||
}
|
||||
|
||||
return presets;
|
||||
}
|
||||
|
||||
async function fetchProvisions(): Promise<{}> {
|
||||
const res = await db.getProvisions();
|
||||
|
||||
const provisions = {};
|
||||
for (const r of res) {
|
||||
provisions[r["_id"]] = {};
|
||||
provisions[r["_id"]].md5 = crypto
|
||||
.createHash("md5")
|
||||
.update(r["script"])
|
||||
.digest("hex");
|
||||
provisions[r["_id"]].script = new vm.Script(
|
||||
`"use strict";(function(){\n${r["script"]}\n})();`,
|
||||
{ filename: r["_id"], lineOffset: -1, timeout: 50 }
|
||||
);
|
||||
}
|
||||
|
||||
return provisions;
|
||||
}
|
||||
|
||||
async function fetchVirtualParameters(): Promise<{}> {
|
||||
const res = await db.getVirtualParameters();
|
||||
|
||||
const virtualParameters = {};
|
||||
for (const r of res) {
|
||||
virtualParameters[r["_id"]] = {};
|
||||
virtualParameters[r["_id"]].md5 = crypto
|
||||
.createHash("md5")
|
||||
.update(r["script"])
|
||||
.digest("hex");
|
||||
virtualParameters[r["_id"]].script = new vm.Script(
|
||||
`"use strict";(function(){\n${r["script"]}\n})();`,
|
||||
{ filename: r["_id"], lineOffset: -1, timeout: 50 }
|
||||
);
|
||||
}
|
||||
|
||||
return virtualParameters;
|
||||
}
|
||||
|
||||
async function fetchPermissions(): Promise<Permissions> {
|
||||
const perms = await db.getPermissions();
|
||||
const permissions: Permissions = {};
|
||||
|
||||
for (const p of perms) {
|
||||
if (!permissions[p.role]) permissions[p.role] = {};
|
||||
if (!permissions[p.role][p.access]) permissions[p.role][p.access] = {};
|
||||
|
||||
permissions[p.role][p.access][p.resource] = {
|
||||
access: p.access,
|
||||
filter: parse(p.filter || "true")
|
||||
};
|
||||
if (p.validate)
|
||||
permissions[p.role][p.access][p.resource].validate = parse(p.validate);
|
||||
}
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
async function fetchFiles(): Promise<{}> {
|
||||
const res = await db.getFiles();
|
||||
const files = {};
|
||||
|
||||
for (const r of res) {
|
||||
const id = r["filename"] || r["_id"].toString();
|
||||
files[id] = {};
|
||||
files[id].length = r["length"];
|
||||
files[id].md5 = r["md5"];
|
||||
files[id].contentType = r["contentType"];
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
async function fetchUsers(): Promise<{}> {
|
||||
const _users = await db.getUsers();
|
||||
const users = {};
|
||||
|
||||
for (const user of _users) {
|
||||
users[user._id] = {
|
||||
password: user.password,
|
||||
salt: user.salt,
|
||||
roles: user.roles.split(",").map(s => s.trim())
|
||||
};
|
||||
}
|
||||
|
||||
return users;
|
||||
}
|
||||
|
||||
async function fetchConfig(): Promise<[{}, {}]> {
|
||||
const conf = await db.getConfig();
|
||||
|
||||
conf.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0));
|
||||
|
||||
const ui = {
|
||||
filters: {},
|
||||
device: {},
|
||||
index: {},
|
||||
overview: { charts: {}, groups: {} }
|
||||
};
|
||||
const _config = {};
|
||||
|
||||
for (const c of conf) {
|
||||
// Evaluate expressions to simplify them
|
||||
const val = expression.evaluate(c.value);
|
||||
_config[c.id] = val;
|
||||
if (c.id.startsWith("ui.")) {
|
||||
const keys = c.id.split(".");
|
||||
// remove the first key(ui)
|
||||
keys.shift();
|
||||
let ref = ui;
|
||||
while (keys.length > 1) {
|
||||
const k = keys.shift();
|
||||
if (typeof ref[k] !== "object") ref[k] = {};
|
||||
ref = ref[k];
|
||||
}
|
||||
ref[keys[0]] = val;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Object.keys(ui["index"]).length) {
|
||||
ui["index"] = {
|
||||
"0": {
|
||||
label: "ID",
|
||||
parameter: ["PARAM", "DeviceID.ID"]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return [_config, ui];
|
||||
}
|
||||
|
||||
async function refresh(): Promise<void> {
|
||||
if (!nextRefresh) {
|
||||
return void setTimeout(() => {
|
||||
refresh(callback);
|
||||
}, 20);
|
||||
await new Promise(resolve => setTimeout(resolve, 20));
|
||||
await refresh();
|
||||
return;
|
||||
}
|
||||
|
||||
nextRefresh = 0;
|
||||
const now = Date.now();
|
||||
|
||||
cache.get("presets_hash", (err, dbHash) => {
|
||||
if (err) return void callback(err);
|
||||
const dbHash = await cache.get("presets_hash");
|
||||
|
||||
if (currentSnapshot && dbHash === currentSnapshot) {
|
||||
nextRefresh = now + (REFRESH - (now % REFRESH));
|
||||
return void callback();
|
||||
}
|
||||
|
||||
cache.lock("presets_hash_lock", 3, (err, unlockOrExtend) => {
|
||||
if (err) return void callback(err);
|
||||
|
||||
const promises = [];
|
||||
|
||||
promises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.getPresets((err, res) => {
|
||||
if (err) return void reject(err);
|
||||
|
||||
db.getObjects((err, objects) => {
|
||||
if (err) return void reject(err);
|
||||
|
||||
objects = objects.map(obj => {
|
||||
// Flatten object
|
||||
obj = flattenObject(obj);
|
||||
|
||||
// If no keys are defined, consider all parameters as keys to keep the
|
||||
// same behavior from v1.0
|
||||
if (!obj["_keys"] || !obj["_keys"].length) {
|
||||
obj["_keys"] = Object.keys(obj).filter(
|
||||
k => !k.startsWith("_")
|
||||
);
|
||||
}
|
||||
|
||||
return obj;
|
||||
});
|
||||
|
||||
res.sort((a, b) => {
|
||||
if (a.weight === b.weight) return a._id > b._id;
|
||||
else return a.weight - b.weight;
|
||||
});
|
||||
|
||||
const presets = [] as Preset[];
|
||||
for (const preset of res) {
|
||||
let schedule = null;
|
||||
if (preset.schedule) {
|
||||
const parts = preset.schedule.trim().split(/\s+/);
|
||||
schedule = {
|
||||
md5: crypto
|
||||
.createHash("md5")
|
||||
.update(preset.schedule)
|
||||
.digest("hex")
|
||||
};
|
||||
|
||||
try {
|
||||
schedule.duration = +parts.shift() * 1000;
|
||||
schedule.schedule = scheduling.parseCron(parts.join(" "));
|
||||
} catch (err) {
|
||||
logger.warn({
|
||||
message: "Invalid preset schedule",
|
||||
preset: preset._id,
|
||||
schedule: preset.schedule
|
||||
});
|
||||
schedule.schedule = false;
|
||||
}
|
||||
}
|
||||
|
||||
const events = preset.events || {};
|
||||
let precondition;
|
||||
try {
|
||||
precondition = parse(preset.precondition);
|
||||
} catch (error) {
|
||||
precondition = mongoQueryToFilter(
|
||||
JSON.parse(preset.precondition)
|
||||
);
|
||||
}
|
||||
|
||||
const _provisions = preset.provisions || [];
|
||||
|
||||
// Generate provisions from the old configuration format
|
||||
for (const c of preset.configurations) {
|
||||
switch (c.type) {
|
||||
case "age":
|
||||
_provisions.push(["refresh", c.name, c.age]);
|
||||
break;
|
||||
|
||||
case "value":
|
||||
_provisions.push(["value", c.name, c.value]);
|
||||
break;
|
||||
|
||||
case "add_tag":
|
||||
_provisions.push(["tag", c.tag, true]);
|
||||
break;
|
||||
|
||||
case "delete_tag":
|
||||
_provisions.push(["tag", c.tag, false]);
|
||||
break;
|
||||
|
||||
case "provision":
|
||||
_provisions.push([c.name].concat(c.args || []));
|
||||
break;
|
||||
|
||||
case "add_object":
|
||||
for (const obj of objects) {
|
||||
if (obj["_id"] === c.object) {
|
||||
const alias = obj["_keys"]
|
||||
.map(k => `${k}:${JSON.stringify(obj[k])}`)
|
||||
.join(",");
|
||||
const p = `${c.name}.[${alias}]`;
|
||||
_provisions.push(["instances", p, 1]);
|
||||
|
||||
for (const k in obj) {
|
||||
if (
|
||||
!k.startsWith("_") &&
|
||||
!(obj["_keys"].indexOf(k) !== -1)
|
||||
)
|
||||
_provisions.push(["value", `${p}.${k}`, obj[k]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "delete_object":
|
||||
for (const obj of objects) {
|
||||
if (obj["_id"] === c.object) {
|
||||
const alias = obj["_keys"]
|
||||
.map(k => `${k}:${JSON.stringify(obj[k])}`)
|
||||
.join(",");
|
||||
const p = `${c.name}.[${alias}]`;
|
||||
_provisions.push(["instances", p, 0]);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
return void reject(
|
||||
new Error(`Unknown configuration type ${c.type}`)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
presets.push({
|
||||
name: preset._id,
|
||||
channel: preset.channel || "default",
|
||||
schedule: schedule,
|
||||
events: events,
|
||||
precondition: precondition,
|
||||
provisions: _provisions
|
||||
});
|
||||
}
|
||||
|
||||
resolve(presets);
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
promises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.getProvisions((err, res) => {
|
||||
if (err) return void reject(err);
|
||||
|
||||
const provisions = {};
|
||||
for (const r of res) {
|
||||
provisions[r._id] = {};
|
||||
provisions[r._id].md5 = crypto
|
||||
.createHash("md5")
|
||||
.update(r.script)
|
||||
.digest("hex");
|
||||
provisions[r._id].script = new vm.Script(
|
||||
`"use strict";(function(){\n${r.script}\n})();`,
|
||||
{ filename: r._id, lineOffset: -1, timeout: 50 }
|
||||
);
|
||||
}
|
||||
|
||||
resolve(provisions);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
promises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.getVirtualParameters((err, res) => {
|
||||
if (err) return void reject(err);
|
||||
|
||||
const virtualParameters = {};
|
||||
for (const r of res) {
|
||||
virtualParameters[r._id] = {};
|
||||
virtualParameters[r._id].md5 = crypto
|
||||
.createHash("md5")
|
||||
.update(r.script)
|
||||
.digest("hex");
|
||||
virtualParameters[r._id].script = new vm.Script(
|
||||
`"use strict";(function(){\n${r.script}\n})();`,
|
||||
{ filename: r._id, lineOffset: -1, timeout: 50 }
|
||||
);
|
||||
}
|
||||
|
||||
resolve(virtualParameters);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
promises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.getFiles((err, res) => {
|
||||
if (err) return void reject(err);
|
||||
|
||||
const files = {};
|
||||
for (const r of res) {
|
||||
const id = r.filename || r._id.toString();
|
||||
files[id] = {};
|
||||
files[id].length = r.length;
|
||||
files[id].md5 = r.md5;
|
||||
files[id].contentType = r.contentType;
|
||||
}
|
||||
|
||||
resolve(files);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
promises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.getPermissions((err, perms) => {
|
||||
if (err) return void reject(err);
|
||||
const permissions: Permissions = {};
|
||||
for (const p of perms) {
|
||||
if (!permissions[p.role]) permissions[p.role] = {};
|
||||
if (!permissions[p.role][p.access])
|
||||
permissions[p.role][p.access] = {};
|
||||
|
||||
permissions[p.role][p.access][p.resource] = {
|
||||
access: p.access,
|
||||
filter: parse(p.filter || "true")
|
||||
};
|
||||
if (p.validate) {
|
||||
permissions[p.role][p.access][p.resource].validate = parse(
|
||||
p.validate
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
resolve(permissions);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
promises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.getUsers((err, _users) => {
|
||||
if (err) return void reject(err);
|
||||
const users = {};
|
||||
for (const user of _users) {
|
||||
users[user._id] = {
|
||||
password: user.password,
|
||||
salt: user.salt,
|
||||
roles: user.roles.split(",").map(s => s.trim())
|
||||
};
|
||||
}
|
||||
resolve(users);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
promises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
db.getConfig((err, conf) => {
|
||||
if (err) return void reject(err);
|
||||
|
||||
conf.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0));
|
||||
|
||||
const ui = {
|
||||
filters: {},
|
||||
device: {},
|
||||
index: {},
|
||||
overview: { charts: {}, groups: {} }
|
||||
};
|
||||
const _config = {};
|
||||
|
||||
for (const c of conf) {
|
||||
// Evaluate expressions to simplify them
|
||||
const val = expression.evaluate(c.value);
|
||||
_config[c.id] = val;
|
||||
if (c.id.startsWith("ui.")) {
|
||||
const keys = c.id.split(".");
|
||||
// remove the first key(ui)
|
||||
keys.shift();
|
||||
let ref = ui;
|
||||
while (keys.length > 1) {
|
||||
const k = keys.shift();
|
||||
if (typeof ref[k] !== "object") ref[k] = {};
|
||||
ref = ref[k];
|
||||
}
|
||||
ref[keys[0]] = val;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Object.keys(ui["index"]).length) {
|
||||
ui["index"] = {
|
||||
"0": {
|
||||
label: "ID",
|
||||
parameter: ["PARAM", "DeviceID.ID"]
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
resolve([_config, ui]);
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
Promise.all(promises)
|
||||
.then(res => {
|
||||
const snapshot = {
|
||||
presets: res[0],
|
||||
provisions: res[1],
|
||||
virtualParameters: res[2],
|
||||
files: res[3],
|
||||
permissions: res[4],
|
||||
users: res[5],
|
||||
config: res[6][0],
|
||||
ui: res[6][1]
|
||||
};
|
||||
|
||||
if (currentSnapshot) {
|
||||
const h = currentSnapshot;
|
||||
const s = snapshots.get(h);
|
||||
setTimeout(() => {
|
||||
if (snapshots.get(h) === s) snapshots.delete(h);
|
||||
}, EVICT_TIMEOUT).unref();
|
||||
}
|
||||
|
||||
currentSnapshot = computeHash(snapshot);
|
||||
snapshots.set(currentSnapshot, snapshot);
|
||||
cache.set("presets_hash", currentSnapshot, 300, err => {
|
||||
unlockOrExtend(0);
|
||||
if (err) return void callback(err);
|
||||
nextRefresh = now + (REFRESH - (now % REFRESH));
|
||||
callback();
|
||||
});
|
||||
})
|
||||
.catch(callback);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
interface GetCurrentSnapshotCallback {
|
||||
(err: null, currentSnapshot: string): void;
|
||||
(err: Error, currentSnapshot?: null): void;
|
||||
}
|
||||
|
||||
export function getCurrentSnapshot(callback: GetCurrentSnapshotCallback);
|
||||
export function getCurrentSnapshot(): Promise<string>;
|
||||
export function getCurrentSnapshot(
|
||||
callback?: GetCurrentSnapshotCallback
|
||||
): Promise<string> | void {
|
||||
if (Date.now() < nextRefresh) {
|
||||
if (callback) return callback(null, currentSnapshot);
|
||||
return Promise.resolve(currentSnapshot);
|
||||
if (currentSnapshot && dbHash === currentSnapshot) {
|
||||
nextRefresh = now + (REFRESH - (now % REFRESH));
|
||||
return;
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
return refresh(err => {
|
||||
callback(err, currentSnapshot);
|
||||
});
|
||||
const unlockOrExtend = await cache.lock("presets_hash_lock", 3);
|
||||
|
||||
const res = await Promise.all([
|
||||
fetchPresets(),
|
||||
fetchProvisions(),
|
||||
fetchVirtualParameters(),
|
||||
fetchFiles(),
|
||||
fetchPermissions(),
|
||||
fetchUsers(),
|
||||
fetchConfig()
|
||||
]);
|
||||
|
||||
const snapshot = {
|
||||
presets: res[0],
|
||||
provisions: res[1],
|
||||
virtualParameters: res[2],
|
||||
files: res[3],
|
||||
permissions: res[4],
|
||||
users: res[5],
|
||||
config: res[6][0],
|
||||
ui: res[6][1]
|
||||
};
|
||||
|
||||
if (currentSnapshot) {
|
||||
const h = currentSnapshot;
|
||||
const s = snapshots.get(h);
|
||||
setTimeout(() => {
|
||||
if (snapshots.get(h) === s) snapshots.delete(h);
|
||||
}, EVICT_TIMEOUT).unref();
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
refresh(err => {
|
||||
if (err) return void reject(err);
|
||||
resolve(currentSnapshot);
|
||||
});
|
||||
});
|
||||
currentSnapshot = computeHash(snapshot);
|
||||
snapshots.set(currentSnapshot, snapshot);
|
||||
await cache.set("presets_hash", currentSnapshot, 300);
|
||||
await unlockOrExtend(0);
|
||||
|
||||
nextRefresh = now + (REFRESH - (now % REFRESH));
|
||||
}
|
||||
|
||||
export async function getCurrentSnapshot(): Promise<string> {
|
||||
if (Date.now() > nextRefresh) await refresh();
|
||||
return currentSnapshot;
|
||||
}
|
||||
|
||||
export function hasSnapshot(hash): boolean {
|
||||
|
||||
+220
-145
@@ -91,24 +91,34 @@ export function listener(request, response): void {
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del("presets_hash", err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del("presets_hash")
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
} else if (request.method === "DELETE") {
|
||||
db.presetsCollection.deleteOne({ _id: presetName }, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del("presets_hash", err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del("presets_hash")
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
response.writeHead(405, { Allow: "PUT, DELETE" });
|
||||
@@ -128,24 +138,34 @@ export function listener(request, response): void {
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del("presets_hash", err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del("presets_hash")
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
} else if (request.method === "DELETE") {
|
||||
db.objectsCollection.deleteOne({ _id: objectName }, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del("presets_hash", err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del("presets_hash")
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
response.writeHead(405, { Allow: "PUT, DELETE" });
|
||||
@@ -176,24 +196,34 @@ export function listener(request, response): void {
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del("presets_hash", err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del("presets_hash")
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
} else if (request.method === "DELETE") {
|
||||
db.provisionsCollection.deleteOne({ _id: provisionName }, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del("presets_hash", err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del("presets_hash")
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
response.writeHead(405, { Allow: "PUT, DELETE" });
|
||||
@@ -224,12 +254,17 @@ export function listener(request, response): void {
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del("presets_hash", err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del("presets_hash")
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
} else if (request.method === "DELETE") {
|
||||
@@ -238,12 +273,17 @@ export function listener(request, response): void {
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del("presets_hash", err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del("presets_hash")
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
} else {
|
||||
@@ -258,7 +298,6 @@ export function listener(request, response): void {
|
||||
db.devicesCollection.updateOne(
|
||||
{ _id: deviceId },
|
||||
{ $addToSet: { _tags: tag } },
|
||||
{ safe: true },
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
response.writeHead(200);
|
||||
@@ -269,7 +308,6 @@ export function listener(request, response): void {
|
||||
db.devicesCollection.updateOne(
|
||||
{ _id: deviceId },
|
||||
{ $pull: { _tags: tag } },
|
||||
{ safe: true },
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
@@ -296,21 +334,31 @@ export function listener(request, response): void {
|
||||
return void db.tasksCollection.deleteOne({ _id: objId }, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del(`${deviceId}_tasks_faults_operations`, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del(`${deviceId}_tasks_faults_operations`)
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
cache.del(`${deviceId}_tasks_faults_operations`, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del(`${deviceId}_tasks_faults_operations`)
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
});
|
||||
} else {
|
||||
response.writeHead(405, { Allow: "DELETE" });
|
||||
@@ -324,83 +372,95 @@ export function listener(request, response): void {
|
||||
if (body.length) {
|
||||
const task = JSON.parse(body.toString());
|
||||
task.device = deviceId;
|
||||
apiFunctions.insertTasks(task, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
apiFunctions
|
||||
.insertTasks(task)
|
||||
.then(() => {
|
||||
cache
|
||||
.del(`${deviceId}_tasks_faults_operations`)
|
||||
.then(() => {
|
||||
if (urlParts.query.connection_request != null) {
|
||||
apiFunctions
|
||||
.connectionRequest(deviceId)
|
||||
.then(() => {
|
||||
const taskTimeout =
|
||||
(urlParts.query.timeout &&
|
||||
parseInt(urlParts.query.timeout as string)) ||
|
||||
config.get("DEVICE_ONLINE_THRESHOLD", deviceId);
|
||||
|
||||
cache.del(`${deviceId}_tasks_faults_operations`, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
apiFunctions
|
||||
.watchTask(deviceId, task._id, taskTimeout)
|
||||
.then(status => {
|
||||
if (status === "timeout") {
|
||||
response.writeHead(
|
||||
202,
|
||||
"Task queued but not processed",
|
||||
{
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
);
|
||||
response.end(JSON.stringify(task));
|
||||
} else if (status === "fault") {
|
||||
db.tasksCollection.findOne(
|
||||
{ _id: task._id },
|
||||
(err, task2) => {
|
||||
if (err)
|
||||
return void throwError(err, response);
|
||||
|
||||
if (urlParts.query.connection_request != null) {
|
||||
apiFunctions.connectionRequest(deviceId, err => {
|
||||
if (err) {
|
||||
response.writeHead(202, err.message, {
|
||||
"Content-Type": "application/json"
|
||||
});
|
||||
response.end(JSON.stringify(task));
|
||||
return;
|
||||
}
|
||||
|
||||
const taskTimeout =
|
||||
(urlParts.query.timeout &&
|
||||
parseInt(urlParts.query.timeout as string)) ||
|
||||
config.get("DEVICE_ONLINE_THRESHOLD", deviceId);
|
||||
|
||||
apiFunctions.watchTask(
|
||||
deviceId,
|
||||
task._id,
|
||||
taskTimeout,
|
||||
(err, status) => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
if (status === "timeout") {
|
||||
response.writeHead(
|
||||
202,
|
||||
"Task queued but not processed",
|
||||
{
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
);
|
||||
response.end(JSON.stringify(task));
|
||||
} else if (status === "fault") {
|
||||
db.tasksCollection.findOne(
|
||||
{ _id: task._id },
|
||||
(err, task2) => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(202, "Task faulted", {
|
||||
"Content-Type": "application/json"
|
||||
response.writeHead(202, "Task faulted", {
|
||||
"Content-Type": "application/json"
|
||||
});
|
||||
response.end(JSON.stringify(task2));
|
||||
}
|
||||
);
|
||||
} else {
|
||||
response.writeHead(200, {
|
||||
"Content-Type": "application/json"
|
||||
});
|
||||
response.end(JSON.stringify(task));
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
response.end(JSON.stringify(task2));
|
||||
}
|
||||
);
|
||||
} else {
|
||||
response.writeHead(200, {
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
response.writeHead(202, err.message, {
|
||||
"Content-Type": "application/json"
|
||||
});
|
||||
response.end(JSON.stringify(task));
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
} else {
|
||||
response.writeHead(202, {
|
||||
"Content-Type": "application/json"
|
||||
});
|
||||
response.end(JSON.stringify(task));
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
response.writeHead(202, {
|
||||
"Content-Type": "application/json"
|
||||
});
|
||||
response.end(JSON.stringify(task));
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
});
|
||||
} else if (urlParts.query.connection_request != null) {
|
||||
// No task, send connection request only
|
||||
apiFunctions.connectionRequest(deviceId, err => {
|
||||
if (err) {
|
||||
apiFunctions
|
||||
.connectionRequest(deviceId)
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
response.writeHead(504);
|
||||
response.end(`${err.name}: ${err.message}`);
|
||||
return;
|
||||
}
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
response.writeHead(400);
|
||||
response.end();
|
||||
@@ -438,12 +498,17 @@ export function listener(request, response): void {
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del(`${deviceId}_tasks_faults_operations`, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del(`${deviceId}_tasks_faults_operations`)
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -468,12 +533,17 @@ export function listener(request, response): void {
|
||||
err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
cache.del(`${deviceId}_tasks_faults_operations`, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
cache
|
||||
.del(`${deviceId}_tasks_faults_operations`)
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
@@ -547,12 +617,17 @@ export function listener(request, response): void {
|
||||
const deviceId = querystring.unescape(
|
||||
DELETE_DEVICE_REGEX.exec(urlParts.pathname)[1]
|
||||
);
|
||||
apiFunctions.deleteDevice(deviceId, err => {
|
||||
if (err) return void throwError(err, response);
|
||||
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
});
|
||||
apiFunctions
|
||||
.deleteDevice(deviceId)
|
||||
.then(() => {
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
})
|
||||
.catch(err => {
|
||||
setTimeout(() => {
|
||||
throwError(err, response);
|
||||
});
|
||||
});
|
||||
} else if (QUERY_REGEX.test(urlParts.pathname)) {
|
||||
let collectionName = QUERY_REGEX.exec(urlParts.pathname)[1];
|
||||
|
||||
|
||||
+71
-67
@@ -24,7 +24,7 @@ import * as extensions from "./extensions";
|
||||
import * as logger from "./logger";
|
||||
import * as scheduling from "./scheduling";
|
||||
import Path from "./common/path";
|
||||
import { Fault, SessionContext } from "./types";
|
||||
import { Fault, SessionContext, ScriptResult } from "./types";
|
||||
|
||||
// Used for throwing to exit user script and commit
|
||||
const COMMIT = Symbol();
|
||||
@@ -38,25 +38,35 @@ const context = vm.createContext();
|
||||
|
||||
let state;
|
||||
|
||||
const runningExtensions = new WeakMap();
|
||||
function runExtension(sessionContext, key, extCall, callback): void {
|
||||
const runningExtensions = new WeakMap<
|
||||
SessionContext,
|
||||
Map<string, Promise<Fault>>
|
||||
>();
|
||||
function runExtension(sessionContext, key, extCall): Promise<Fault> {
|
||||
let re = runningExtensions.get(sessionContext);
|
||||
if (!re) {
|
||||
re = {};
|
||||
re = new Map<string, Promise<Fault>>();
|
||||
runningExtensions.set(sessionContext, re);
|
||||
}
|
||||
|
||||
if (re[key]) {
|
||||
re[key].push(callback);
|
||||
} else {
|
||||
re[key] = [callback];
|
||||
extensions.run(extCall, (err, fault, res) => {
|
||||
const callbacks = re[key];
|
||||
delete re[key];
|
||||
if (!err && !fault) sessionContext.extensionsCache[key] = res;
|
||||
for (const c of callbacks) c(err, fault);
|
||||
});
|
||||
let prom = re.get(key);
|
||||
if (!prom) {
|
||||
re.set(
|
||||
key,
|
||||
(prom = new Promise((resolve, reject) => {
|
||||
extensions
|
||||
.run(extCall)
|
||||
.then(({ fault, value }) => {
|
||||
re.delete(key);
|
||||
if (!fault) sessionContext.extensionsCache[key] = value;
|
||||
resolve(fault);
|
||||
})
|
||||
.catch(reject);
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
return prom;
|
||||
}
|
||||
|
||||
class SandboxDate {
|
||||
@@ -343,15 +353,14 @@ function errorToFault(err: Error): Fault {
|
||||
return fault;
|
||||
}
|
||||
|
||||
export function run(
|
||||
export async function run(
|
||||
script: vm.Script,
|
||||
globals,
|
||||
globals: {},
|
||||
sessionContext: SessionContext,
|
||||
startRevision: number,
|
||||
maxRevision: number,
|
||||
callback,
|
||||
extCounter = 0
|
||||
): void {
|
||||
): Promise<ScriptResult> {
|
||||
state = {
|
||||
sessionContext: sessionContext,
|
||||
revision: startRevision,
|
||||
@@ -374,61 +383,56 @@ export function run(
|
||||
ret = script.runInContext(context, { displayErrors: false });
|
||||
status = 0;
|
||||
} catch (err) {
|
||||
if (err === COMMIT) status = 1;
|
||||
else if (err === EXT) status = 2;
|
||||
else return void callback(null, errorToFault(err));
|
||||
if (err === COMMIT) {
|
||||
status = 1;
|
||||
} else if (err === EXT) {
|
||||
status = 2;
|
||||
} else {
|
||||
return {
|
||||
fault: errorToFault(err),
|
||||
clear: null,
|
||||
declare: null,
|
||||
done: false,
|
||||
returnValue: null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const _state = state;
|
||||
const args = Array.from(arguments);
|
||||
let fault;
|
||||
|
||||
// Need to maintain a counter of ext() calls to avoid calling certain
|
||||
// functions (e.g. log()) multiple times as calling ext() results in
|
||||
// re-execution of script without revision being incremented.
|
||||
args[6] = 0 - (state.extCounter - extCounter);
|
||||
await Promise.all(
|
||||
Object.entries(_state.extensions).map(async ([k, v]) => {
|
||||
fault = (await runExtension(_state.sessionContext, k, v)) || fault;
|
||||
})
|
||||
);
|
||||
|
||||
let counter = 3;
|
||||
for (const key of Object.keys(_state.extensions)) {
|
||||
counter += 2;
|
||||
runExtension(
|
||||
_state.sessionContext,
|
||||
key,
|
||||
_state.extensions[key],
|
||||
(err, fault) => {
|
||||
if (err || fault) {
|
||||
if (counter & 1) callback(err, fault);
|
||||
return void (counter = 0);
|
||||
}
|
||||
if ((counter -= 2) === 1) {
|
||||
if (status === 2) {
|
||||
return void run.apply(null, args);
|
||||
} else {
|
||||
return void callback(
|
||||
null,
|
||||
null,
|
||||
_state.clear,
|
||||
_state.declarations,
|
||||
status === 0,
|
||||
ret
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fault) {
|
||||
return {
|
||||
fault: fault,
|
||||
clear: null,
|
||||
declare: null,
|
||||
done: false,
|
||||
returnValue: null
|
||||
};
|
||||
}
|
||||
|
||||
if (status === 2) {
|
||||
return run(
|
||||
script,
|
||||
globals,
|
||||
sessionContext,
|
||||
startRevision,
|
||||
maxRevision,
|
||||
extCounter - _state.extCounter
|
||||
);
|
||||
}
|
||||
|
||||
if ((counter -= 2) === 1) {
|
||||
if (status === 2) {
|
||||
run.apply(null, args);
|
||||
} else {
|
||||
callback(
|
||||
null,
|
||||
null,
|
||||
_state.clear,
|
||||
_state.declarations,
|
||||
status === 0,
|
||||
ret
|
||||
);
|
||||
}
|
||||
}
|
||||
return {
|
||||
fault: null,
|
||||
clear: _state.clear,
|
||||
declare: _state.declarations,
|
||||
done: status === 0,
|
||||
returnValue: ret
|
||||
};
|
||||
}
|
||||
+538
-595
File diff suppressed because it is too large.
Load diff
+5
-15
@@ -21,7 +21,6 @@ import { parseXml, Document, Element, ParserOptions } from "libxmljs";
|
||||
import * as config from "./config";
|
||||
import { version as VERSION } from "../package.json";
|
||||
import {
|
||||
CpeResponse,
|
||||
CpeGetResponse,
|
||||
CpeSetResponse,
|
||||
InformRequest,
|
||||
@@ -29,7 +28,9 @@ import {
|
||||
CpeRequest,
|
||||
FaultStruct,
|
||||
SpvFault,
|
||||
CpeFault
|
||||
CpeFault,
|
||||
SoapMessage,
|
||||
TransferCompleteRequest
|
||||
} from "./types";
|
||||
|
||||
const SERVER_NAME = `GenieACS/${VERSION}`;
|
||||
@@ -444,7 +445,7 @@ function GetRPCMethodsResponse(xml, methodResponse): void {
|
||||
for (const m of methodResponse.methodList) el.node("string").text(m);
|
||||
}
|
||||
|
||||
function TransferComplete(xml): AcsResponse {
|
||||
function TransferComplete(xml): TransferCompleteRequest {
|
||||
let commandKey, _faultStruct, startTime, completeTime;
|
||||
for (const c of xml.childNodes()) {
|
||||
switch (c.name()) {
|
||||
@@ -545,18 +546,7 @@ function fault(xml): CpeFault {
|
||||
return { faultCode, faultString, detail };
|
||||
}
|
||||
|
||||
export function request(
|
||||
data,
|
||||
cwmpVersion,
|
||||
warn
|
||||
): {
|
||||
id: string;
|
||||
cwmpVersion: string;
|
||||
sessionTimeout: number;
|
||||
cpeRequest?: CpeRequest;
|
||||
cpeFault?: CpeFault;
|
||||
cpeResponse?: CpeResponse;
|
||||
} {
|
||||
export function request(data, cwmpVersion, warn): SoapMessage {
|
||||
warnings = warn;
|
||||
|
||||
const rpc = {
|
||||
|
||||
+26
-2
@@ -16,6 +16,7 @@ export interface Fault {
|
||||
message: string;
|
||||
stack?: string;
|
||||
};
|
||||
timestamp?: number;
|
||||
}
|
||||
|
||||
export interface SessionFault extends Fault {
|
||||
@@ -255,14 +256,20 @@ export interface InformRequest extends CpeRequest {
|
||||
parameterList: [string, string | number | boolean, string][];
|
||||
}
|
||||
|
||||
export interface AcsResponse {
|
||||
name: string;
|
||||
export interface TransferCompleteRequest extends CpeRequest {
|
||||
name: "TransferComplete";
|
||||
commandKey?: string;
|
||||
faultStruct?: FaultStruct;
|
||||
startTime?: number;
|
||||
completeTime?: number;
|
||||
}
|
||||
|
||||
export interface AcsResponse {
|
||||
name: string;
|
||||
commandKey?: string;
|
||||
faultStruct?: FaultStruct;
|
||||
}
|
||||
|
||||
export interface QueryOptions {
|
||||
projection?: any;
|
||||
skip?: number;
|
||||
@@ -308,3 +315,20 @@ export type PermissionSet = {
|
||||
filter: Expression;
|
||||
};
|
||||
}[];
|
||||
|
||||
export interface SoapMessage {
|
||||
id: string;
|
||||
cwmpVersion: string;
|
||||
sessionTimeout: number;
|
||||
cpeRequest?: CpeRequest;
|
||||
cpeFault?: CpeFault;
|
||||
cpeResponse?: CpeResponse;
|
||||
}
|
||||
|
||||
export interface ScriptResult {
|
||||
fault: Fault;
|
||||
clear: Clear[];
|
||||
declare: Declaration[];
|
||||
done: boolean;
|
||||
returnValue: any;
|
||||
}
|
||||
+100
-145
@@ -11,144 +11,110 @@ import {
|
||||
deleteDevice
|
||||
} from "../api-functions";
|
||||
|
||||
function deleteFault(id): Promise<void> {
|
||||
async function deleteFault(id): Promise<void> {
|
||||
const deviceId = id.split(":", 1)[0];
|
||||
const channel = id.slice(deviceId.length + 1);
|
||||
return new Promise((resolve, reject) => {
|
||||
Promise.all([
|
||||
db.deleteFault(id),
|
||||
channel.startsWith("task_")
|
||||
? db.deleteTask(new ObjectID(channel.slice(5)))
|
||||
: null
|
||||
])
|
||||
.then(() => {
|
||||
del(`${deviceId}_tasks_faults_operations`, err => {
|
||||
err ? reject(err) : resolve();
|
||||
});
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
|
||||
await Promise.all([
|
||||
db.deleteFault(id),
|
||||
channel.startsWith("task_")
|
||||
? db.deleteTask(new ObjectID(channel.slice(5)))
|
||||
: null
|
||||
]);
|
||||
|
||||
await del(`${deviceId}_tasks_faults_operations`);
|
||||
}
|
||||
|
||||
export function deleteResource(resource, id): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let promise;
|
||||
export async function deleteResource(resource, id): Promise<void> {
|
||||
switch (resource) {
|
||||
case "devices":
|
||||
await deleteDevice(id);
|
||||
break;
|
||||
|
||||
switch (resource) {
|
||||
case "devices":
|
||||
promise = new Promise((res, rej) => {
|
||||
deleteDevice(id, err => {
|
||||
if (err) return void rej(err);
|
||||
res();
|
||||
});
|
||||
});
|
||||
break;
|
||||
case "files":
|
||||
await db.deleteFile(id);
|
||||
break;
|
||||
|
||||
case "files":
|
||||
promise = db.deleteFile(id);
|
||||
break;
|
||||
case "faults":
|
||||
await deleteFault(id);
|
||||
break;
|
||||
|
||||
case "faults":
|
||||
promise = deleteFault(id);
|
||||
break;
|
||||
case "provisions":
|
||||
await db.deleteProvision(id);
|
||||
break;
|
||||
|
||||
case "provisions":
|
||||
promise = db.deleteProvision(id);
|
||||
break;
|
||||
case "presets":
|
||||
await db.deletePreset(id);
|
||||
break;
|
||||
|
||||
case "presets":
|
||||
promise = db.deletePreset(id);
|
||||
break;
|
||||
case "virtualParameters":
|
||||
await db.deleteVirtualParameter(id);
|
||||
break;
|
||||
|
||||
case "virtualParameters":
|
||||
promise = db.deleteVirtualParameter(id);
|
||||
break;
|
||||
case "config":
|
||||
await db.deleteConfig(id);
|
||||
break;
|
||||
|
||||
case "config":
|
||||
promise = db.deleteConfig(id);
|
||||
break;
|
||||
case "permissions":
|
||||
await db.deletePermission(id);
|
||||
break;
|
||||
|
||||
case "permissions":
|
||||
promise = db.deletePermission(id);
|
||||
break;
|
||||
case "users":
|
||||
await db.deleteUser(id);
|
||||
break;
|
||||
}
|
||||
|
||||
case "users":
|
||||
promise = db.deleteUser(id);
|
||||
break;
|
||||
}
|
||||
|
||||
promise
|
||||
.then(() => {
|
||||
del("presets_hash", err => {
|
||||
if (err) return void reject(err);
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
await del("presets_hash");
|
||||
}
|
||||
|
||||
export function postTasks(
|
||||
export async function postTasks(
|
||||
deviceId,
|
||||
tasks,
|
||||
timeout
|
||||
): Promise<{ connectionRequest: string; tasks: any[] }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
for (const task of tasks) task.device = deviceId;
|
||||
for (const task of tasks) task.device = deviceId;
|
||||
|
||||
insertTasks(tasks, err => {
|
||||
if (err) return void reject(err);
|
||||
const statuses = tasks.map(t => {
|
||||
return { _id: t._id, status: "pending" };
|
||||
});
|
||||
del(`${deviceId}_tasks_faults_operations`, err => {
|
||||
if (err) return void reject(err);
|
||||
connectionRequest(deviceId, err => {
|
||||
if (err) {
|
||||
return void resolve({
|
||||
connectionRequest: err.message,
|
||||
tasks: statuses
|
||||
});
|
||||
}
|
||||
|
||||
const sample = tasks[tasks.length - 1];
|
||||
|
||||
// Waiting for session to finish or timeout
|
||||
watchTask(deviceId, sample._id, timeout, err => {
|
||||
if (err) return void reject(err);
|
||||
|
||||
const promises2 = [];
|
||||
for (const s of statuses) {
|
||||
promises2.push(db.query("tasks", ["=", ["PARAM", "_id"], s._id]));
|
||||
promises2.push(
|
||||
db.query("faults", [
|
||||
"=",
|
||||
["PARAM", "_id"],
|
||||
`${deviceId}:task_${s._id}`
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
Promise.all(promises2)
|
||||
.then(res => {
|
||||
for (const [i, r] of statuses.entries()) {
|
||||
if (res[i * 2].length === 0) {
|
||||
r.status = "done";
|
||||
} else if (res[i * 2 + 1].length === 1) {
|
||||
r.status = "fault";
|
||||
r.fault = res[i * 2 + 1][0];
|
||||
}
|
||||
db.deleteTask(r._id);
|
||||
}
|
||||
|
||||
resolve({ connectionRequest: "OK", tasks: statuses });
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
tasks = await insertTasks(tasks);
|
||||
const statuses = tasks.map(t => {
|
||||
return { _id: t._id, status: "pending" };
|
||||
});
|
||||
|
||||
await del(`${deviceId}_tasks_faults_operations`);
|
||||
|
||||
try {
|
||||
await connectionRequest(deviceId);
|
||||
} catch (err) {
|
||||
return {
|
||||
connectionRequest: err.message,
|
||||
tasks: statuses
|
||||
};
|
||||
}
|
||||
|
||||
const sample = tasks[tasks.length - 1];
|
||||
|
||||
// Waiting for session to finish or timeout
|
||||
await watchTask(deviceId, sample._id, timeout);
|
||||
|
||||
const promises = [];
|
||||
for (const s of statuses) {
|
||||
promises.push(db.query("tasks", ["=", ["PARAM", "_id"], s._id]));
|
||||
promises.push(
|
||||
db.query("faults", ["=", ["PARAM", "_id"], `${deviceId}:task_${s._id}`])
|
||||
);
|
||||
}
|
||||
|
||||
const res = await Promise.all(promises);
|
||||
for (const [i, r] of statuses.entries()) {
|
||||
if (res[i * 2].length === 0) {
|
||||
r.status = "done";
|
||||
} else if (res[i * 2 + 1].length === 1) {
|
||||
r.status = "fault";
|
||||
r.fault = res[i * 2 + 1][0];
|
||||
}
|
||||
db.deleteTask(r._id);
|
||||
}
|
||||
|
||||
return { connectionRequest: "OK", tasks: statuses };
|
||||
}
|
||||
|
||||
interface PingResponse {
|
||||
@@ -184,35 +150,24 @@ export function ping(host): Promise<PingResponse> {
|
||||
});
|
||||
}
|
||||
|
||||
export function putResource(resource, id, data): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let promise;
|
||||
export async function putResource(resource, id, data): Promise<void> {
|
||||
if (resource === "presets") {
|
||||
await db.putPreset(id, data);
|
||||
} else if (resource === "provisions") {
|
||||
await db.putProvision(id, data);
|
||||
} else if (resource === "virtualParameters") {
|
||||
await db.putVirtualParameter(id, data);
|
||||
} else if (resource === "config") {
|
||||
await db.putConfig(id, data);
|
||||
} else if (resource === "permissions") {
|
||||
await db.putPermission(id, data);
|
||||
} else if (resource === "users") {
|
||||
delete data.password;
|
||||
delete data.salt;
|
||||
await db.putUser(id, data);
|
||||
}
|
||||
|
||||
if (resource === "presets") {
|
||||
promise = db.putPreset(id, data);
|
||||
} else if (resource === "provisions") {
|
||||
promise = db.putProvision(id, data);
|
||||
} else if (resource === "virtualParameters") {
|
||||
promise = db.putVirtualParameter(id, data);
|
||||
} else if (resource === "config") {
|
||||
promise = db.putConfig(id, data);
|
||||
} else if (resource === "permissions") {
|
||||
promise = db.putPermission(id, data);
|
||||
} else if (resource === "users") {
|
||||
delete data.password;
|
||||
delete data.salt;
|
||||
promise = db.putUser(id, data);
|
||||
}
|
||||
|
||||
promise
|
||||
.then(() => {
|
||||
del("presets_hash", err => {
|
||||
if (err) return void reject(err);
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
await del("presets_hash");
|
||||
}
|
||||
|
||||
export function authSimple(snapshot, username, password): Promise<string[]> {
|
||||
|
||||
+1
-10
@@ -546,16 +546,7 @@ router.put("/users/:id/password", async (ctx, next) => {
|
||||
const password = await hashPassword(newPassword, salt);
|
||||
await db.putUser(username, { password, salt });
|
||||
|
||||
function delCache(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
del("presets_hash", err => {
|
||||
if (err) return void reject(err);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
await delCache();
|
||||
await del("presets_hash");
|
||||
|
||||
logger.accessInfo(log);
|
||||
ctx.body = "";
|
||||
|
||||
Reference in new issue
Block a user