-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudwatch-publisher.js
124 lines (113 loc) · 3.54 KB
/
cloudwatch-publisher.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
const debug = require('debug')('webmon:cloudwatchpublisher')
const AWS = require('aws-sdk')
AWS.config.update(AWS.config.loadFromPath('./secret_config/aws-config.json'))
/**
* class to collect values to publish as a statistics set
* computes the min/max/average
*/
class MetricsSet {
constructor () {
this.count = 0
this.min = Number.POSITIVE_INFINITY
this.max = Number.NEGATIVE_INFINITY
this.sum = 0
}
addMetric (value) {
this.count++
this.min = Math.min(this.min, value)
this.max = Math.max(this.max, value)
this.sum += value
}
getStatisticsValues () {
debug('getStatisticsValues returning %O', this)
return {
Maximum: this.max,
Minimum: this.min,
SampleCount: this.count,
Sum: this.sum
}
}
}
/**
* Class to publish sensor readings to Amazon Cloudwatch as custom metrics
*/
class CloudwatchPublisher {
/**
* constructor
*/
constructor () {
debug('constructing')
const PUBLISH_INTERVAL_MSEC = 5 * 60 * 1000
this.pendingReadings = {}
this.cloudwatch = new AWS.CloudWatch()
// this.sendReadings() // (send immendiately for testing only)
setInterval(this.sendReadings.bind(this), PUBLISH_INTERVAL_MSEC)
}
/**
* consume a sensor reading
* event - {object} sensor event
*/
newEvent (event) {
// debug('newEvent() %O', event)
if (event.pm2_5) {
// air quality
if (!this.pendingReadings['airquality/pm2.5']) {
this.pendingReadings['airquality/pm2.5'] = new MetricsSet()
}
this.pendingReadings['airquality/pm2.5'].addMetric(event.pm2_5)
}
if (event.power0_W) {
// power meter
if (!this.pendingReadings['electricity/power']) {
this.pendingReadings['electricity/power'] = new MetricsSet()
}
this.pendingReadings['electricity/power'].addMetric(event.power0_W)
}
if (event.kwh_day_total) {
// total electricity usage for a complete day
if (!this.pendingReadings['electricity/kwh_day_total']) {
this.pendingReadings['electricity/kwh_day_total'] = new MetricsSet()
}
this.pendingReadings['electricity/kwh_day_total'].addMetric(event.kwh_day_total)
}
if (event.temperature_C) {
// temperature
if (!this.pendingReadings['temperature/' + event.sensorName]) {
this.pendingReadings['temperature/' + event.sensorName] = new MetricsSet()
}
this.pendingReadings['temperature/' + event.sensorName].addMetric(event.temperature_C)
}
if (event.humidity) {
// humidity
if (!this.pendingReadings['humidity/' + event.sensorName]) {
this.pendingReadings['humidity/' + event.sensorName] = new MetricsSet()
}
this.pendingReadings['humidity/' + event.sensorName].addMetric(event.humidity)
}
}
/**
* Send readings to Amazon CloudWatch
*/
sendReadings () {
debug('sendReadings(): pendingReadings: %O', this.pendingReadings)
for (const metricName in this.pendingReadings) {
const metricSet = this.pendingReadings[metricName]
const putMetricParams = {
Namespace: 'house',
MetricData: [
{
MetricName: metricName,
StatisticValues: metricSet.getStatisticsValues(),
StorageResolution: 60
}
]
}
this.cloudwatch.putMetricData(putMetricParams, (err, data) => {
if (err) debug('Error putting data to CloudWatch: ' + err, err.stack)
else debug('Put data to CloudWatch OK: ' + JSON.stringify(data))
})
}
this.pendingReadings = {}
}
}
module.exports = CloudwatchPublisher