-
Notifications
You must be signed in to change notification settings - Fork 0
/
pizero-workshop.js
200 lines (194 loc) · 5.91 KB
/
pizero-workshop.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// @ts-check
"use strict";
require("date-utils");
const Omron2jcieBu01 = require("omron2jcieBu01.js");
const Csv = require("csv.js");
const Machinist = require("machinist.js");
const Ambient = require("ambient.js");
const WebAgent = require("omron-iot-sensor-web-agent");
const SpeechAgent = require("omron-iot-sensor-speech-agent");
const pattern = require("pattern.js");
const logDirectory = "/home/pi/pizero-workshop/log";
const fs = require("fs");
function isExistFile(file) {
try {
fs.statSync(file);
return true;
} catch (err) {
if (err.code === "ENOENT") return false;
}
}
const configFile = isExistFile("/boot/setting/config.js")
? "/boot/setting/config.js"
: "./setting/config.js";
console.log('config_file: "' + configFile + '"');
/**
* @typedef {{csvFilename: string}} CsvConfig
* @typedef {{
* machinistApiKey: string,
* machinistAgent: string,
* machinistBatchQuantity: number
* }} MachinistConfig
* @typedef {{
* ambientChannelId: string,
* ambientUserId: string,
* ambientWriteKey: string,
* ambientReadKey: string,
* ambientBatchQuantity: number
* }} AmbientConfig
* @typedef {{webAgent: Object}} WebAgentConfig
* @typedef {{speechAgent: Object}} SpeechAgentConfig
* @type {Array<
* {
* intervalMillisec: number,
* omron2jcieBu01Name: string,
* omron2jcieBu01Address: string
* } & Partial<CsvConfig>
* & Partial<MachinistConfig>
* & Partial<AmbientConfig>
* & Partial<WebAgentConfig>
* & Partial<SpeechAgentConfig>
* >}
*/
const config = require(configFile);
config &&
config.forEach(param => {
const omron2jcieBu01 =
param.omron2jcieBu01Name &&
param.omron2jcieBu01Address &&
new Omron2jcieBu01({
name: param.omron2jcieBu01Name,
address: param.omron2jcieBu01Address
});
const csv =
param.csvFilename &&
new Csv({
path: `${logDirectory}/${param.csvFilename}`,
date: true,
dateFormat: "YYYY-MM-DD HH24:MI:SS",
order: [
"temperature",
"relativeHumidity",
"barometricPressure",
"ambientLight",
"soundNoise",
"eTVOC",
"eCO2"
]
});
const machinist =
param.machinistApiKey &&
param.machinistAgent &&
param.machinistBatchQuantity &&
new Machinist({
agent: param.machinistAgent,
apiKey: param.machinistApiKey,
batchQuantity: param.machinistBatchQuantity,
format: datas => {
let metrics = [];
datas.forEach(data => {
let timestamp = Math.floor(data.timestamp / 1000);
metrics.push({
name: "温度",
namespace: "2JCIE-BU",
data_point: { value: data.temperature, timestamp: timestamp }
});
metrics.push({
name: "湿度",
namespace: "2JCIE-BU",
data_point: { value: data.relativeHumidity, timestamp: timestamp }
});
metrics.push({
name: "大気圧",
namespace: "2JCIE-BU",
data_point: {
value: data.barometricPressure,
timestamp: timestamp
}
});
metrics.push({
name: "騒音",
namespace: "2JCIE-BU",
data_point: { value: data.soundNoise, timestamp: timestamp }
});
metrics.push({
name: "照度",
namespace: "2JCIE-BU",
data_point: { value: data.ambientLight, timestamp: timestamp }
});
metrics.push({
name: "総揮発性有機化合物濃度",
namespace: "2JCIE-BU",
data_point: { value: data.eTVOC, timestamp: timestamp }
});
metrics.push({
name: "二酸化炭素",
namespace: "2JCIE-BU",
data_point: { value: data.eCO2, timestamp: timestamp }
});
});
return { agent: param.machinistAgent, metrics: metrics };
}
});
const ambient =
param.ambientChannelId &&
param.ambientWriteKey &&
new Ambient({
channelId: param.ambientChannelId,
writeKey: param.ambientWriteKey,
readKey: param.ambientReadKey,
userId: param.ambientUserId,
batchQuantity: param.ambientBatchQuantity,
format: datas =>
datas.map(data => {
return {
created: new Date(data.timestamp).toFormat(
"YYYY-MM-DD HH24:MI:SS"
),
d1: data.temperature,
d2: data.relativeHumidity,
d3: data.barometricPressure,
d4: data.ambientLight,
d5: data.soundNoise,
d6: data.eTVOC,
d7: data.eCO2
};
})
});
const webAgent = param.webAgent && WebAgent(param.webAgent);
const speechAgent = param.speechAgent && SpeechAgent(param.speechAgent);
if (
omron2jcieBu01 &&
(webAgent || speechAgent || csv || machinist || ambient)
) {
console.log(
[
"sensorRecords pattern start with:",
omron2jcieBu01 ? "omron2jcieBu01" : "",
webAgent ? "webAgent" : "",
speechAgent ? "speechAgent" : "",
csv ? "csv" : "",
machinist ? "machinist" : "",
ambient ? "ambient" : ""
].join(" ")
);
// call pattern function //
pattern.sensorRecords({
loopInterval: param.intervalMillisec,
sensor: omron2jcieBu01,
records: [webAgent, speechAgent, csv, machinist, ambient].filter(
record => record
)
});
} else {
console.log(
"sensorRecords pattern coud not start with this uncomplete setting."
);
}
});
console.log(
"All sensorRecords pattern starter is finished, and wait loop start!"
);
(function waitLoop() {
setTimeout(waitLoop, 1000);
})();