forked from bofh69/FlyLight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
114 lines (93 loc) · 2.79 KB
/
server.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
//
// # FlyLight
//
// A server program to control a lamp.
//
var webServer = require("./webServer.js");
var cfgname = "config";
if(process.argv.length > 2) {
cfgname = process.argv[2];
}
var config = require("./" + cfgname);
var lamp = require("./" + config.lamp.driver + "Controller");
function createWeatherFilter(cfg) {
var filter = {};
filter.cfg = cfg;
filter.accept = function(dir, wind) {
dir = parseFloat(dir);
wind = parseFloat(wind);
if(cfg.dirMin < 0) {
if((dir > cfg.dirMax) &&
(dir <(cfg.dirMin+360))) {
console.log("Wrong direction", dir, "(" + cfg.dirMin + ", " + cfg.dirMax +")");
return false;
}
} else {
if(dir < cfg.dirMin || dir > cfg.dirMax) {
console.log("Wrong direction", dir, "(" + cfg.dirMin + ", " + cfg.dirMax +")");
return false;
}
}
console.log("Windspeed", wind, "(" + cfg.windMin + ", " + cfg.windMax +")");
if((wind < cfg.windMin) || (wind > cfg.windMax)) {
console.log("Wrong windspeed", wind, "(" + cfg.windMin + ", " + cfg.windMax +")");
return false;
}
return true;
};
return filter;
}
function createHistory(filter) {
var result = {};
result.history = [];
result.filter = filter;
result.addSample = function(dir, wind) {
var result = this.filter.accept(dir, wind);
this.history.push(result);
if(this.history.length > config.maxHistory) this.history.shift();
};
result.isOk = function() {
return this.history.reduce(function(current, prev) {
if(!prev || !current) return false;
return true;
}, true);
};
return result;
}
function pollServer(cfg, history) {
var reader = require("./" + cfg.reader + "Reader");
reader.pollServer(cfg, function(wd) {
console.log("Station name: ", wd.name);
console.log("Current direction: ", wd.dir);
console.log("Current avg: ", wd.avgSpeed);
history.addSample(wd.dir, wd.avgSpeed);
});
}
function initPoller(cfg, history) {
pollServer(cfg, history);
setInterval(function() {pollServer(cfg, history)}, 60*1000);
}
function updateLamp(history) {
if(history.isOk()) {
lamp.setColour("green", config.lamp.greenColour);
} else {
lamp.setColour("red", config.lamp.redColour);
}
}
///////////////////////////////////////////////////////////////////////////////
lamp.setDestination(config.lamp);
lamp.setColour("yellow", config.lamp.yellowColour);
var history = [];
config.windMeeters.forEach(function (cfg) {
var hist = createHistory(createWeatherFilter(cfg));
history.push(hist);
initPoller(cfg, hist);
});
var aggHistory = {};
aggHistory.isOk = function() {
return history.reduce(function(o, n) {
return o || n.isOk();
}, false)
};
setInterval(function() {updateLamp(aggHistory)}, 60*1000);
webServer.init(config, aggHistory);