Include notification and accessList in CSV
This commit is contained in:
1 parent
96add7efe2
commit
6733cef5dd
2 files changed
+48
-23
No files matched your search
+42
-21
@@ -285,19 +285,30 @@ function parseDate(d): number | string {
|
|||||||
return isNaN(n) ? "" + d : n;
|
return isNaN(n) ? "" + d : n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface FlatAttributes {
|
||||||
|
object?: boolean;
|
||||||
|
objectTimestamp?: number;
|
||||||
|
writable?: boolean;
|
||||||
|
writableTimestamp?: number;
|
||||||
|
value?: [string | number | boolean, string];
|
||||||
|
valueTimestamp?: number;
|
||||||
|
notification?: number;
|
||||||
|
notificationTimestamp?: number;
|
||||||
|
accessList?: string[];
|
||||||
|
accessListTimestamp?: number;
|
||||||
|
}
|
||||||
|
|
||||||
interface FlatDevice {
|
interface FlatDevice {
|
||||||
[param: string]: {
|
[param: string]: FlatAttributes;
|
||||||
object: boolean;
|
|
||||||
objectTimestamp: number;
|
|
||||||
writable: boolean;
|
|
||||||
writableTimestamp: number;
|
|
||||||
value: [string | number | boolean, string];
|
|
||||||
valueTimestamp: number;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function flattenDevice(device): FlatDevice {
|
export function flattenDevice(device): FlatDevice {
|
||||||
function recursive(input, root, output, timestamp): void {
|
function recursive(
|
||||||
|
input,
|
||||||
|
root: string,
|
||||||
|
output: FlatDevice,
|
||||||
|
timestamp: number
|
||||||
|
): void {
|
||||||
for (const [name, tree] of Object.entries(input)) {
|
for (const [name, tree] of Object.entries(input)) {
|
||||||
if (!root) {
|
if (!root) {
|
||||||
if (name === "_lastInform") {
|
if (name === "_lastInform") {
|
||||||
@@ -338,7 +349,7 @@ export function flattenDevice(device): FlatDevice {
|
|||||||
};
|
};
|
||||||
} else if (name === "_id") {
|
} else if (name === "_id") {
|
||||||
output["DeviceID.ID"] = {
|
output["DeviceID.ID"] = {
|
||||||
value: [tree, "xsd:string"],
|
value: [tree as string, "xsd:string"],
|
||||||
valueTimestamp: timestamp,
|
valueTimestamp: timestamp,
|
||||||
writable: false,
|
writable: false,
|
||||||
writableTimestamp: timestamp,
|
writableTimestamp: timestamp,
|
||||||
@@ -407,34 +418,44 @@ export function flattenDevice(device): FlatDevice {
|
|||||||
else if (+input["_timestamp"] > timestamp)
|
else if (+input["_timestamp"] > timestamp)
|
||||||
childrenTimestamp = +input["_timestamp"];
|
childrenTimestamp = +input["_timestamp"];
|
||||||
|
|
||||||
const attrs = {};
|
const attrs: FlatAttributes = {};
|
||||||
if (tree["_value"] != null) {
|
if (tree["_value"] != null) {
|
||||||
attrs["value"] = [
|
attrs.value = [
|
||||||
tree["_value"] instanceof Date ? +tree["_value"] : tree["_value"],
|
tree["_value"] instanceof Date ? +tree["_value"] : tree["_value"],
|
||||||
tree["_type"]
|
tree["_type"]
|
||||||
];
|
];
|
||||||
attrs["valueTimestamp"] = +(tree["_timestamp"] || childrenTimestamp);
|
attrs.valueTimestamp = +(tree["_timestamp"] || childrenTimestamp);
|
||||||
attrs["object"] = false;
|
attrs.object = false;
|
||||||
attrs["objectTimestamp"] = childrenTimestamp;
|
attrs.objectTimestamp = childrenTimestamp;
|
||||||
} else if (tree["_object"] != null) {
|
} else if (tree["_object"] != null) {
|
||||||
attrs["object"] = tree["_object"];
|
attrs.object = tree["_object"];
|
||||||
attrs["objectTimestamp"] = childrenTimestamp;
|
attrs.objectTimestamp = childrenTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tree["_writable"] != null) {
|
if (tree["_writable"] != null) {
|
||||||
attrs["writable"] = tree["_writable"];
|
attrs.writable = tree["_writable"];
|
||||||
attrs["writableTimestamp"] = childrenTimestamp;
|
attrs.writableTimestamp = childrenTimestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tree["_notification"] != null) {
|
||||||
|
attrs.notification = tree["_notification"];
|
||||||
|
attrs.notificationTimestamp = +tree["_attributesTimestamp"] || 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tree["_accessList"] != null) {
|
||||||
|
attrs.accessList = tree["_accessList"];
|
||||||
|
attrs.accessListTimestamp = +tree["_attributesTimestamp"] || 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const r = root ? `${root}.${name}` : name;
|
const r = root ? `${root}.${name}` : name;
|
||||||
output[r] = attrs;
|
output[r] = attrs;
|
||||||
|
|
||||||
if (attrs["object"] || tree["object"] == null)
|
if (attrs.object || tree["object"] == null)
|
||||||
recursive(tree, r, output, childrenTimestamp);
|
recursive(tree, r, output, childrenTimestamp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const newDevice = {};
|
const newDevice: FlatDevice = {};
|
||||||
const timestamp = new Date(device["_lastInform"] || 1).getTime();
|
const timestamp = new Date(device["_lastInform"] || 1).getTime();
|
||||||
recursive(device, "", newDevice, timestamp);
|
recursive(device, "", newDevice, timestamp);
|
||||||
return newDevice;
|
return newDevice;
|
||||||
|
|||||||
+6
-2
@@ -99,7 +99,7 @@ router.get(`/devices/:id.csv`, async ctx => {
|
|||||||
|
|
||||||
ctx.body = new stream.PassThrough();
|
ctx.body = new stream.PassThrough();
|
||||||
ctx.body.write(
|
ctx.body.write(
|
||||||
"Parameter,Object,Object timestamp,Writable,Writable timestamp,Value,Value type,Value timestamp\n"
|
"Parameter,Object,Object timestamp,Writable,Writable timestamp,Value,Value type,Value timestamp,Notification,Notification timestamp,Access list,Access list timestamp\n"
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const k of Object.keys(res[0]).sort()) {
|
for (const k of Object.keys(res[0]).sort()) {
|
||||||
@@ -112,7 +112,11 @@ router.get(`/devices/:id.csv`, async ctx => {
|
|||||||
p.writableTimestamp,
|
p.writableTimestamp,
|
||||||
p.value != null ? `"${p.value[0].toString().replace(/"/g, '""')}"` : "",
|
p.value != null ? `"${p.value[0].toString().replace(/"/g, '""')}"` : "",
|
||||||
p.value != null ? p.value[1] : "",
|
p.value != null ? p.value[1] : "",
|
||||||
p.valueTimestamp
|
p.valueTimestamp,
|
||||||
|
p.notification,
|
||||||
|
p.notificationTimestamp,
|
||||||
|
p.accessList ? p.accessList.join(", ") : "",
|
||||||
|
p.accessListTimestamp
|
||||||
];
|
];
|
||||||
ctx.body.write(row.map(r => (r != null ? r : "")).join(",") + "\n");
|
ctx.body.write(row.map(r => (r != null ? r : "")).join(",") + "\n");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user