This repository has been archived by the owner on Nov 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
st-monitor.js
98 lines (78 loc) · 1.97 KB
/
st-monitor.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
use Web-Application
show collections
print("")
var _keys = Object.keys(db.logs.findOne())
print("Keys:")
print(_keys)
print("")
var _names = db.logs.distinct("name")
var _types = db.logs.distinct("type")
print("Names:")
print(_names)
print("")
print("Types:")
print(_types)
print("")
function notSeen(minutes) {
var seen = []
_names.forEach(function(name) {
// {"type": {$ne: "temperature"}}
var createdDate = new Date(ISODate().getTime() - (1000 * 60 * minutes))
var newest = db.logs.find({ $and: [ {"name": name},{"created": {$gte: createdDate }} ] }).sort({"created":-1}).limit(1).toArray()[0]
if (newest != undefined) {
seen.push(newest)
//printjson(newest)
}
});
var seenNames = seen.map(function(obj) {
return obj.name
});
var result = []
_names.forEach(function(name) {
if(!seenNames.includes(name)) {
item = db.logs.find({"name": name}).sort({"created":-1}).limit(1).toArray()[0]
result.push(item)
}
});
return result
}
var _minutes=1440
print("")
print("*** Device NOT Seen ("+_minutes+" min.) ***")
print("")
notSeen(_minutes).forEach(function(item){
print(item.name+" : "+item.value)
});
print("")
print("*** Motion NOT Seen ("+_minutes+" min.) ***")
print("")
notSeen(_minutes).forEach(function(item){
if(item.type == "motion") {
print(item.name+" : "+item.value)
}
});
function batteryReplace(percent) {
var seen = []
_names.forEach(function(name) {
var newest = db.logs.find({ $and: [ {"name": name},{"type": "battery"}]}).sort({"created":-1}).limit(1).toArray()[0]
if (newest != undefined) {
seen.push(newest)
//printjson(newest)
}
});
var result = []
seen.forEach(function(item) {
if(parseInt(item.value) <= percent) {
result.push(item)
}
});
return result
}
var _percent=40
print("")
print("*** Battery Low (<="+_percent+"%) ***")
print("")
batteryReplace(_percent).forEach(function(item){
print(item.name + " : " + item.value)
});
print("");