-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevent.js
166 lines (149 loc) · 4.22 KB
/
event.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
// Copyrigth (C) Pascal Martin, 2014.
//
// NAME
//
// event - a module to route event distribution and storage.
//
// SYNOPSYS
//
// This module receives events and distributes them as configured.
// All events are stored locally, and removed after some time.
//
// Events may also be formatted as syslog entries.
//
// The main benefit of using syslog is to take advantage of the syslog
// redirections capabilities, more specifically the ability to redirect
// logging to a remote server for storage.
//
// DESCRIPTION
//
// var event = require('./event');
//
// event.configure (config, options);
//
// Initialize the event module from the configuration.
// This method can be called as often as necessary (typically
// when the user configuration has changed).
//
// event.record (data);
//
// Record one new event.
//
// event.find (filter, callback);
//
// Return a list of events matching the given filter. See the
// documentation of NeDB for the filter syntax.
//
// The callback function is called with one parameter, which is
// a JavaScript structure designed to be sent to a web client.
//
// event.latest ();
//
// Return the ID of the most recent event.
//
// USER CONFIGURATION
//
// event.syslog Enable (true) or disable (false) recording of events
// through syslog.
//
// event.cleanup Delete events older than this number of days. Events
// are kept indefinitely if not defined or 0.
//
var path = require('./path');
function errorLog (text) {
console.error ('[ERROR] Event: '+text);
}
var syslog = null;
try {
syslog = require('node-syslog');
syslog.init("sprinkler", syslog.LOG_ODELAY, syslog.LOG_USER);
}
catch (err) {
errorLog ('cannot initialize node-syslog');
}
var syslog_enabled = false;
var latestDate = new Date(0);
var latestSequence = 1;
var latestId = null;
const oneDay = 86400000;
var cleanup = 0;
// load up the database
var nedb = require('nedb');
var db = new nedb({ filename: path.events(), autoload: true });
exports.configure = function (config, options) {
if (config.event) {
if (config.event.syslog) {
if (syslog) {
syslog_enabled = true;
}
}
if (config.event.cleanup) {
cleanup = config.event.cleanup;
}
}
}
exports.record = function (data) {
data.timestamp = new Date();
if (data.timestamp > latestDate) {
latestSequence = 1;
latestDate = data.timestamp;
} else {
latestSequence += 1;
}
data.sequence = latestSequence;
db.insert(data, function (err, newDoc) {
if(err){
errorLog('Database insert error: '+err);
return;
}
latestId = newDoc._id;
});
if (syslog_enabled) {
description = '';
if (data.zone!=null) {
description = ' zone '+data.zone;
}
if (data.program) {
if (data.adjustment) {
description = ' program '+data.program+' (weather adjustment: '+data.adjustment+'%)';
} else {
description = ' program '+data.program;
}
}
if (data.parent) {
parent = ' (program '+data.parent+')';
} else {
parent = '';
}
syslog.log(syslog.LOG_INFO, data.action+description+parent);
}
// Cleanup old events.
if (cleanup) {
if (latestSequence === 1) {
var old = new Date (latestDate.getTime() - (cleanup * oneDay));
db.remove ({timestamp: {$lt:old}}, {multi:true});
}
}
}
exports.find = function (filter, callback) {
// Finding all the history entries matching the specified filter.
if (filter == null) filter = {};
db.find(filter, function (err, docs) {
if(err){
errorLog('cannot use filter "'+filter+'": '+err);
callback({status: 'error', msg:err.message});
} else {
// The history is sorted most recent first.
docs.sort(function (a, b) {
if (b.timestamp.getTime() == a.timestamp.getTime()) {
return b.sequence - a.sequence;
}
return b.timestamp - a.timestamp;
});
callback({status: 'ok', history:docs});
}
});
}
exports.latest = function () {
return latestId;
}