You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5 KiB

7 years ago
package main
7 years ago
import (
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
7 years ago
// Device struct for store device data from iot-item table
type Device struct {
Device string `json:"device"`
Name string `json:"name"`
7 years ago
Model string `json:"model"`
7 years ago
}
// ItemInfo - create structs to hold info about new item
7 years ago
type ItemInfo struct {
Firmware string `json:"firmware"`
Sensor string `json:"sensor"`
Model string `json:"model"`
7 years ago
Name string `json:"name"`
7 years ago
}
// SensorData to store all sensor's data
type SensorData struct {
7 years ago
Battery int `json:"battery"`
Light int `json:"light"`
Conductivity float32 `json:"conductivity"`
Moisture float32 `json:"moisture"`
Temperature float32 `json:"temperature"`
7 years ago
Humidity float32 `json:"humidity"`
7 years ago
}
// Item is an item to generalize every IoT devices
7 years ago
type Item struct {
Device string `json:"device"`
Timestamp string `json:"timestamp"`
Info ItemInfo `json:"info"`
Data SensorData `json:"data"`
7 years ago
}
7 years ago
// History returns data in []Item
7 years ago
func (d Device) History(svc *dynamodb.DynamoDB, limit int64) ([]Item, error) {
result, err := GetHistory(svc, d.Device, limit)
var ev []Item
if err != nil {
return ev, err
}
for _, i := range result.Items {
item := Item{}
err = dynamodbattribute.UnmarshalMap(i, &item)
7 years ago
item.Info.Name = d.Name
item.Info.Model = d.Model
7 years ago
if err != nil {
return ev, err
}
ev = append(ev, item)
}
return ev, nil
}