-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontrolcfg.js
137 lines (129 loc) · 4.2 KB
/
controlcfg.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
'use strict';
function nowString() { return new Date().toISOString(); }
// var maxData = 200; // number of data points displayed per trace
var maxData = 40; // number of data points displayed per trace
var pumpPin = 5; // single pin number to turn pump motor on and off
var sensorReadRate = 120000;//milliseconds -- 2 minutes
var sensorPins = ['A0', 'A1', 'A2', 'A3', 'A4', 'A5'];
var valvePins = [8, 9, 10, 11, 2, 3, 4];
// Template for a trace to associate with a (Plotly) plot
var dataTemplate = {
x: [],
y: [],
stream: {
maxpoints: maxData
}
};
/***
* Make sure there are enough configured sensors and valves to support the
* number of log traces being configured
*
* @param {integer} traceCount The number of configured trace logs
* @return {undefined}
*/
function checkConfiguredHardware(traceCount) {
module.exports.controller.nTraces = Math.min(
traceCount,
sensorPins.length,
valvePins.length,
module.exports.controller.off.length,
module.exports.controller.on.length
);
}// ./function checkConfiguredHardware(traceCount)
/***
* Create an array of (trace) entries for a plot
*
* @param {array of string} tokens The tokens to associate with the traces
* @return {array of Object}
*/
function buildPlotData(tokens) {
var i, trace, plot;
// console.log(nowString(), 'start buildPlotData');//trace
// console.log(nowString(), 'template:', dataTemplate);//DEBUG
// console.log(nowString(), 'tokens:', tokens);//DEBUG
// Limit the number of configured traces to the number of available sensors
// and valves
checkConfiguredHardware(tokens.length);
console.log(nowString(), 'number of traces to configure:',
module.exports.controller.nTraces
);//DEBUG
plot = [];
for (i = 0; i < module.exports.controller.nTraces; i += 1) {
trace = JSON.parse(JSON.stringify(dataTemplate));
trace.stream.token = tokens[i];
plot.push(trace);
}
return plot;
}
/***
* Create a moisture sensor configuration object
*
* @param {integer} index The index for pin to use for the sensor
* @return {Object}
*/
function sensorConfig(index) {
return {
pin: sensorPins[index],
freq: sensorReadRate,
id: 'Moisture Sensor ' + index
};
}// ./function buildSensorConfig(index)
/***
* Create valve (solenoid) control
*
* @param {integer} index The index for the pin to use to control the valve
* @return {Object}
*/
function valveConfig(index) {
return {
pin: valvePins[index],
id: "Solenoid " + index,
type: "NC"
};
}// ./function valveConfig(index)
module.exports = {
controller: {
tooDryValue: 700, // Greater than this is OK
flowTime: 10000, // time for out of range correction event (ms) - 10 seconds
hardwareDelay: 1000, // minimum delay (ms) between slave hardware setting
// operations (that change power usage)
// off: [0, 2, 4, 6, 8, 10], // graph values per trace and state
// on: [1, 3, 5, 7, 8, 11],
off: [0, 1, 2, 3, 4, 5, 6],
on: [0.9, 1.9, 2.9, 3.9, 4.9, 5.9],
// Should have a 'momentum' factor somewhere: delay a second watering long
// enough that the results of the first have reached the sensors
pumpConfig: {
pin: pumpPin,
type: "NO", //Normally Open contacts: engerize to turn on
id: "Water Pump"
}
},
plotly: {
graphOptions: {
fileopt: 'extend',
filename: 'Tomatoes',
title: 'Tomato Watering Schedule',
xaxis: {
title: 'Date/Time'
},
yaxis: {
title: 'On/Off'
}
}
},
buildPlotData: buildPlotData,
sensorConfig: sensorConfig,
valveConfig: valveConfig
};
/*
sensorReadRate The time (milliseconds) between reads of the controlling
sensor value. This should be the longest time that the measured value
could be 'out of band' before being noticed. Only 'should', because
processing being done for other sensors could increase this time. The
event can not trigger until all current processing as finished.
graphOptions an Object containing styling options like axis information
and titles for the graph.
multipleStreams Expected success streamstatus value on successfull plot
initialization with multiple traces.
*/