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.
62 lines
1.6 KiB
62 lines
1.6 KiB
package main |
|
|
|
import ( |
|
"github.com/aws/aws-sdk-go/service/dynamodb" |
|
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" |
|
) |
|
|
|
// Device struct for store device data from iot-item table |
|
type Device struct { |
|
Device string `json:"device"` |
|
Group string `json:"group"` |
|
Name string `json:"name"` |
|
Model string `json:"model"` |
|
} |
|
|
|
// ItemInfo - create structs to hold info about new item |
|
type ItemInfo struct { |
|
Firmware string `json:"firmware"` |
|
Sensor string `json:"sensor"` |
|
Model string `json:"model"` |
|
Name string `json:"name"` |
|
} |
|
|
|
// SensorData to store all sensor's data |
|
type SensorData struct { |
|
Battery int `json:"battery"` |
|
Light int `json:"light"` |
|
Conductivity float32 `json:"conductivity"` |
|
Moisture float32 `json:"moisture"` |
|
Temperature float32 `json:"temperature"` |
|
Humidity float32 `json:"humidity"` |
|
} |
|
|
|
// Item is an item to generalize every IoT devices |
|
type Item struct { |
|
Device string `json:"device"` |
|
Group string `json:"group"` |
|
Timestamp string `json:"timestamp"` |
|
Info ItemInfo `json:"info"` |
|
Data SensorData `json:"data"` |
|
} |
|
|
|
// History returns data in []Item |
|
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{} |
|
item.Group = d.Group |
|
err = dynamodbattribute.UnmarshalMap(i, &item) |
|
item.Info.Name = d.Name |
|
item.Info.Model = d.Model |
|
if err != nil { |
|
return ev, err |
|
} |
|
ev = append(ev, item) |
|
} |
|
return ev, nil |
|
}
|
|
|