Remove UI dependance on NBI
This commit is contained in:
1 parent
520cc43972
commit
d46d133878
5 files changed
+285
-341
No files matched your search
+106
-8
@@ -1,9 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
import { MongoClient } from "mongodb";
|
||||
import { MongoClient, GridFSBucket } from "mongodb";
|
||||
import * as config from "../config";
|
||||
import * as mongodbFunctions from "./mongodb-functions";
|
||||
import * as expression from "../common/expression";
|
||||
import { preProcessPreset } from "./mongodb-functions";
|
||||
|
||||
const CACHE_TTL = 300000;
|
||||
|
||||
@@ -187,14 +188,23 @@ export function count(resource, filter) {
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteConfig(id) {
|
||||
export function updateDeviceTags(deviceId, tags) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const add = [];
|
||||
const pull = [];
|
||||
|
||||
for (const [tag, onOff] of Object.entries(tags)) {
|
||||
if (onOff) add.push(tag);
|
||||
else pull.push(tag);
|
||||
}
|
||||
getClient()
|
||||
.then(client => {
|
||||
const collection = client
|
||||
.db()
|
||||
.collection(RESOURCE_COLLECTION["config"] || "config");
|
||||
collection.deleteOne({ _id: id }, err => {
|
||||
const collection = client.db().collection("devices");
|
||||
|
||||
const object = {};
|
||||
if (add && add.length) object["$addToSet"] = { _tags: { $each: add } };
|
||||
if (pull && pull.length) object["$pullAll"] = { _tags: pull };
|
||||
collection.updateOne({ _id: deviceId }, object, err => {
|
||||
if (err) return void reject(err);
|
||||
resolve();
|
||||
});
|
||||
@@ -203,13 +213,13 @@ export function deleteConfig(id) {
|
||||
});
|
||||
}
|
||||
|
||||
export function putConfig(id, object) {
|
||||
function putResource(resource, id, object) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient()
|
||||
.then(client => {
|
||||
const collection = client
|
||||
.db()
|
||||
.collection(RESOURCE_COLLECTION["config"] || "config");
|
||||
.collection(RESOURCE_COLLECTION[resource] || resource);
|
||||
collection.replaceOne({ _id: id }, object, { upsert: true }, err => {
|
||||
if (err) return void reject(err);
|
||||
resolve();
|
||||
@@ -219,6 +229,94 @@ export function putConfig(id, object) {
|
||||
});
|
||||
}
|
||||
|
||||
function deleteResource(resource, id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient()
|
||||
.then(client => {
|
||||
const collection = client
|
||||
.db()
|
||||
.collection(RESOURCE_COLLECTION[resource] || resource);
|
||||
collection.deleteOne({ _id: id }, err => {
|
||||
if (err) return void reject(err);
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
export function putPreset(id, object) {
|
||||
object = preProcessPreset(object);
|
||||
return putResource("presets", id, object);
|
||||
}
|
||||
|
||||
export function deletePreset(id) {
|
||||
return deleteResource("presets", id);
|
||||
}
|
||||
|
||||
export function putProvision(id, object) {
|
||||
return putResource("provisions", id, object);
|
||||
}
|
||||
|
||||
export function deleteProvision(id) {
|
||||
return deleteResource("provisions", id);
|
||||
}
|
||||
|
||||
export function putVirtualParameter(id, object) {
|
||||
return putResource("virtualParameters", id, object);
|
||||
}
|
||||
|
||||
export function deleteVirtualParameter(id) {
|
||||
return deleteResource("virtualParameters", id);
|
||||
}
|
||||
|
||||
export function putConfig(id, object) {
|
||||
return putResource("config", id, object);
|
||||
}
|
||||
|
||||
export function deleteConfig(id) {
|
||||
return deleteResource("config", id);
|
||||
}
|
||||
|
||||
export function putFile(filename, metadata, contentStream) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient()
|
||||
.then(client => {
|
||||
const bucket = new GridFSBucket(client.db());
|
||||
const uploadStream = bucket.openUploadStreamWithId(filename, filename, {
|
||||
metadata: metadata
|
||||
});
|
||||
uploadStream.on("error", reject);
|
||||
contentStream.on("error", reject);
|
||||
uploadStream.on("finish", resolve);
|
||||
contentStream.pipe(uploadStream);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteFile(filename) {
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient()
|
||||
.then(client => {
|
||||
const bucket = new GridFSBucket(client.db());
|
||||
bucket.delete(filename, err => {
|
||||
if (err) return void reject(err);
|
||||
resolve();
|
||||
});
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteFault(id) {
|
||||
return deleteResource("faults", id);
|
||||
}
|
||||
|
||||
export function deleteTask(id) {
|
||||
return deleteResource("tasks", id);
|
||||
}
|
||||
|
||||
export function disconnect() {
|
||||
if (_clientPromise) {
|
||||
_clientPromise.then(client => {
|
||||
|
||||
Reference in new issue
Block a user