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.

74 lines
2.1 KiB

7 years ago
package main
import (
"github.com/aws/aws-sdk-go/aws"
d "github.com/aws/aws-sdk-go/service/dynamodb"
7 years ago
da "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
7 years ago
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
7 years ago
// GetDevice is to get a device from id
func GetDevice(svc *d.DynamoDB, deviceID string) (Device, error) {
var device Device
input := &d.GetItemInput{
Key: map[string]*d.AttributeValue{
"device": {
S: aws.String(deviceID),
},
},
TableName: aws.String("iot-item"),
}
result, err := svc.GetItem(input)
if err != nil {
return device, err
}
err = da.UnmarshalMap(result.Item, &device)
if err != nil {
return device, err
}
return device, nil
}
7 years ago
// GetDevices is to get all devices we know
func GetDevices(svc *d.DynamoDB) (*d.ScanOutput, error) {
7 years ago
proj := expression.NamesList(
expression.Name("device"),
expression.Name("name"))
expr, _ := expression.NewBuilder().WithProjection(proj).Build()
// Build the query input parameters
params := &d.ScanInput{
7 years ago
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
ProjectionExpression: expr.Projection(),
TableName: aws.String("iot-item"),
}
return svc.Scan(params)
}
// GetHistory is a query history for any device
func GetHistory(svc *d.DynamoDB, deviceID string, limit int64) (*d.QueryOutput, error) {
7 years ago
keyCond := expression.Key("device").Equal(expression.Value(deviceID))
qProj := expression.NamesList(
expression.Name("device"),
expression.Name("timestamp"),
7 years ago
expression.Name("info"),
expression.Name("data"))
7 years ago
qBuilder := expression.NewBuilder().WithKeyCondition(keyCond).WithProjection(qProj)
qExpression, _ := qBuilder.Build()
queryInput := &d.QueryInput{
7 years ago
KeyConditionExpression: qExpression.KeyCondition(),
ProjectionExpression: qExpression.Projection(),
ExpressionAttributeNames: qExpression.Names(),
ExpressionAttributeValues: qExpression.Values(),
ScanIndexForward: aws.Bool(false),
TableName: aws.String("iot-logger"),
Limit: aws.Int64(limit),
}
return svc.Query(queryInput)
}