const EventBus = require("../event-bus")
const config = require("../config")
const AWS = require ("aws-sdk")

class Monitor {
	constructor(){
		AWS.config.update (
			new AWS.Config({
 				accessKeyId: config.reporterId,
  				secretAccessKey: config.reporterKey,
  				region: "eu-west-1"
			})
		)
		this.cloudwatch = new AWS.CloudWatch ();
		this.eventHandler = eventHandler
	}
}

const eventHandler = function(event){
	let name = event.name + (event.detail ? ':'+event.detail : '') 
	monitor.cloudwatch.putMetricData({
	MetricData : [{
		MetricName : name,
		StorageResolution: 60,
		Timestamp : event.timestamp,
		Value : 1.0
	}],
	Namespace : "Alarm"
	},(err,data)=>{
		if(err) console.error(err)
	})
}

setInterval(()=>{eventHandler({name:"heartbeat"})},60*1000);

const monitor = new Monitor()

EventBus.register({
        needs: {
                alarmState: monitor.eventHandler,
                strategyState: monitor.eventHandler,
                arm: monitor.eventHandler,
                disarm: monitor.eventHandler,
                armed: monitor.eventHandler,
                disarmed: monitor.eventHandler,
                intruder: monitor.eventHandler,
                bedtime: monitor.eventHandler,
		movement: monitor.eventHandler		
        }
})

module.exports = new Monitor()

@@ -1,12 +1,13 @@
+%0A
 const EventB
@@ -425,14 +425,53 @@
 let 
-n
+request = %7B%0A%09%09MetricData : %5B%7B%0A%09%09%09MetricN
 ame 
-=
+:
  eve
@@ -500,12 +500,14 @@
 l ? 
-':'+
+%22:%22 + 
 even
@@ -524,84 +524,11 @@
  '')
- 
+,
 %0A%09
-monitor.cloudwatch.putMetricData(%7B%0A%09MetricData : %5B%7B%0A%09%09MetricName : name,%0A
 %09%09St
@@ -550,16 +550,17 @@
 : 60,%0A%09%09
+%09
 Timestam
@@ -563,16 +563,25 @@
 stamp : 
+new Date(
 event.ti
@@ -586,15 +586,12 @@
 time
-stamp
+)
 ,%0A
+%09
 %09%09Va
@@ -604,13 +604,15 @@
 1.0%0A
+%09
 %09%7D%5D,%0A
+%09
 %09Nam
@@ -630,16 +630,58 @@
 larm%22%0A%09%7D
+%0A%09monitor.cloudwatch.putMetricData(request
 ,(err,da
@@ -770,16 +770,32 @@
 artbeat%22
+,time:Date.now()
 %7D)%7D,60*1

fix timestamp