Skip to content

Commit

Permalink
Add cloud activity logging
Browse files Browse the repository at this point in the history
  • Loading branch information
danesparza committed Aug 5, 2017
1 parent e6df65e commit b061243
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
7 changes: 7 additions & 0 deletions data/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 36 additions & 7 deletions sensordata/collect_linux_arm.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package sensordata

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -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)
}

/*********************************
Expand All @@ -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()))
Expand All @@ -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
Expand Down

0 comments on commit b061243

Please sign in to comment.