From 78a9924c635008b34a598f97d0fef90b8a480132 Mon Sep 17 00:00:00 2001 From: sipp11 Date: Wed, 21 Mar 2018 18:42:06 +0900 Subject: [PATCH] Alpha --- .gitignore | 4 ++ helper.go | 24 +++++++ html/home.html | 135 ++++++++++++++++++++++++++++++++++++---- read_item.go => main.go | 79 ++++++----------------- model.go | 4 ++ 5 files changed, 174 insertions(+), 72 deletions(-) create mode 100644 .gitignore rename read_item.go => main.go (52%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..903cb24 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store + +helper +my-iot diff --git a/helper.go b/helper.go index dadac0e..214cd06 100644 --- a/helper.go +++ b/helper.go @@ -3,9 +3,32 @@ package main import ( "github.com/aws/aws-sdk-go/aws" d "github.com/aws/aws-sdk-go/service/dynamodb" + da "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" "github.com/aws/aws-sdk-go/service/dynamodb/expression" ) +// 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 +} + // GetDevices is to get all devices we know func GetDevices(svc *d.DynamoDB) (*d.ScanOutput, error) { @@ -31,6 +54,7 @@ func GetHistory(svc *d.DynamoDB, deviceID string, limit int64) (*d.QueryOutput, qProj := expression.NamesList( expression.Name("device"), expression.Name("timestamp"), + expression.Name("info"), expression.Name("data")) qBuilder := expression.NewBuilder().WithKeyCondition(keyCond).WithProjection(qProj) diff --git a/html/home.html b/html/home.html index 74bd0a1..5d53757 100644 --- a/html/home.html +++ b/html/home.html @@ -27,17 +27,38 @@