-
Notifications
You must be signed in to change notification settings - Fork 1
/
ModelController.js
125 lines (105 loc) · 3.86 KB
/
ModelController.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
const merge = require('deepmerge');
const util = require('./utilities.js');
const Register = require('./Register.js');
const ModelDataClient = require('./ModelDataClient.js');
const path = require('path');
const fs = require('fs');
const { deflateSync } = require('zlib');
const { defaultMaxListeners } = require('stream');
const CONFIG_FILE = path.join(__dirname, 'config.json');
var modelConfig = {}
// const modelDataClient = new ModelDataClient(false, this.setData);
// modelDataClient.callbacks.incomingDataListener = setData;
exports.getConfiguration = function () {
return modelConfig;
}
exports.setConfiguration = function (newConfig) {
// console.log(config);
modelConfig.views=newConfig.views;
modelConfig.containers=newConfig.containers;
modelConfig.name = newConfig.name;
//modelConfig = merge(modelConfig, newConfig, {arrayMerge : combineMerge});
// console.log(config);
util.saveJsonFile(CONFIG_FILE, modelConfig);
}
exports.getData = function () {
return modelConfig.data;
}
exports.getReferenceByName = (name) => {
return modelConfig.data.findIndex(datum => datum.name == name);
}
exports.setModelConfig = function (newModelConfig){
if(!newModelConfig.views){
newModelConfig.views = []
}
if(!newModelConfig.containers){
newModelConfig.containers = []
}
modelConfig = newModelConfig
}
exports.setData = function(dataPackets) {
return new Promise((resolve, reject) => {
let errors = [];
for (const dataPacket of dataPackets) {
try {
// console.log(dataPacket);
// console.log(dataPacket.index);
let datum = modelConfig.data[dataPacket.index];
// console.log(datum);
// console.log(modelConfig);
if (datum.type === "register" || datum.type === "dpr") {
const dataWritePromise = Register.write(
datum.device,
datum.name,
dataPacket.value
);
dataWritePromise.then((fulfilledResult) => {
// console.log(`successfully wrote ${dataPacket.value} to ${datum.device} ${datum.name}`);
// data write was successful, so update the shadow in modelConfig
datum.value = dataPacket.value;
}, (rejectedResult) => {
// TODO: error handling
console.error(rejectedResult);
});
}
else if(datum.type === "user-only"){
resolve({})
return;
}
else {
errors.push(`data type ${datum.type} is not supported`);
}
}
catch (error) {
console.error(error);
}
}
if (errors && errors.length) {
console.log("set data failed")
reject({errors});
}
else {
// TODO: what to return on success?
resolve({})
}
});
};
// merge arrays together instead of concatenating them when merging objects
// taken from the deepmerge documentation
const combineMerge = (target, source, options) => {
const destination = target.slice()
source.forEach((item, index) => {
if (typeof destination[index] === 'undefined') {
destination[index] = options.cloneUnlessOtherwiseSpecified(item, options)
} else if (options.isMergeableObject(item)) {
destination[index] = merge(target[index], item, options)
} else if (target.indexOf(item) === -1) {
destination.push(item)
}
})
return destination
}
if (fs.existsSync('./ui.json')) {
exports.setModelConfig(util.loadJsonFile('./ui.json'));
}