From b061243c3828df28a94cef14c65c8ad301d77c7d Mon Sep 17 00:00:00 2001 From: Dan Esparza Date: Fri, 4 Aug 2017 21:33:45 -0400 Subject: [PATCH] Add cloud activity logging --- data/activity.go | 7 ++++++ sensordata/collect_linux_arm.go | 43 +++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/data/activity.go b/data/activity.go index cc821b9..2782f42 100644 --- a/data/activity.go +++ b/data/activity.go @@ -31,6 +31,13 @@ type Activity struct { Type EventType `json:"eventtype"` } +// CloudActivity represents a single activity event in the cloud +type CloudActivity struct { + Timestamp time.Time `json:"timestamp"` + Type EventType `json:"eventtype"` + DeviceID string `json:"deviceId"` +} + // ActivityDB is the BoltDB database for activity information type ActivityDB struct { Database string diff --git a/sensordata/collect_linux_arm.go b/sensordata/collect_linux_arm.go index e4db65d..f96979f 100644 --- a/sensordata/collect_linux_arm.go +++ b/sensordata/collect_linux_arm.go @@ -1,9 +1,12 @@ package sensordata import ( + "bytes" "context" + "encoding/json" "fmt" "log" + "net/http" "os" "time" @@ -189,7 +192,9 @@ func CollectAndProcess(ctx context.Context) { timeStart = time.Now() // Track the activity: - activityDB.Add(data.Activity{Type: data.ApplianceRunning}) + newActivity := data.Activity{Type: data.ApplianceRunning} + activityDB.Add(newActivity) + trackActivity(newActivity, configDB) } /********************************* @@ -205,7 +210,9 @@ func CollectAndProcess(ctx context.Context) { runningTime := time.Since(timeStart) // Track the activity: - activityDB.Add(data.Activity{Type: data.ApplianceStopped}) + newActivity := data.Activity{Type: data.ApplianceStopped} + activityDB.Add(newActivity) + trackActivity(newActivity, configDB) // Send a Pushover message err := sendPushoverNotification(configDB, int(runningTime.Minutes())) @@ -229,13 +236,35 @@ func resetPin(pin embd.DigitalPin) { pin.Close() } -// Track the activity -func trackActivity(activity data.Activity) error { - // Track the activity in the database +// Track the activity in the cloud +func trackActivity(activity data.Activity, c data.ConfigDB) error { + url := "https://api.appliance-monitor.com/v1/activity" - // Track the activity in the cloud + // Get the deviceID: + deviceID, _ := c.Get("deviceID") - return nil + // Serialize to JSON + cloudActivity := data.CloudActivity{ + DeviceID: deviceID.Value, + Timestamp: activity.Timestamp, + Type: activity.Type} + + jsonStr, err := json.Marshal(cloudActivity) + if err != nil { + log.Printf("[WARN] Problem marshalling cloud activity message: %v\n", err) + } + + // Create a request + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) + + // Set our headers + req.Header.Set("Content-Type", "application/json; charset=UTF-8") + + // Execute the request + client := &http.Client{} + _, err = client.Do(req) + + return err } // Send a pushover notification