diff --git a/helper.go b/helper.go index 214cd06..2d98574 100644 --- a/helper.go +++ b/helper.go @@ -34,6 +34,7 @@ func GetDevices(svc *d.DynamoDB) (*d.ScanOutput, error) { proj := expression.NamesList( expression.Name("device"), + expression.Name("group"), expression.Name("name")) expr, _ := expression.NewBuilder().WithProjection(proj).Build() diff --git a/html/home.html b/html/home.html index e23237f..63726f7 100644 --- a/html/home.html +++ b/html/home.html @@ -6,204 +6,106 @@ Life Around - - - - - - - - - - - - - - - + + -
- -
+
+
+ {{ message }} +
+ +
+
+
+
+

+ {{ group }} +

+
+
+
+
+
+
+
รดน้ำด้วย!
+

{{ device.info.name }}

+

{{ device.fromNow }}

+ {{ device.info.sensor }} + เปลี่ยน Battery +
+
+

Temperature ºC

+

{{ device.data.temperature }}

+
+
+

ความชื้นอากาศ %

+

{{ device.data.humidity }}

+
+
+

ความชื้นของดิน %

+

{{ device.data.moisture }}

+
+
+ +
+
+
+
+
- - + - - + + diff --git a/main.go b/main.go index 22df734..c00a639 100644 --- a/main.go +++ b/main.go @@ -12,28 +12,19 @@ import ( "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" - "github.com/flosch/pongo2" "github.com/labstack/echo" "github.com/labstack/echo/middleware" ) -var tplHome = pongo2.Must(pongo2.FromFile("html/home.html")) var sess *session.Session var err error var svc *dynamodb.DynamoDB -// func indexHandler(w http.ResponseWriter, r *http.Request) { func indexHandler(c echo.Context) error { - // Execute the template per HTTP request - out, err := tplHome.Execute(pongo2.Context{"query": "dddd"}) - if err != nil { - return err - } - return c.HTML(http.StatusOK, out) + return c.File("html/home.html") } func healthCheckHandler(c echo.Context) error { - // A very simple health check. return c.JSON(http.StatusOK, `{"alive": true}`) } @@ -49,9 +40,10 @@ func overallHandler(c echo.Context) error { } data, _ := device.History(svc, 1) one := data[0] + one.Group = device.Group one.Info.Name = device.Name one.Info.Model = device.Model - result = append(result, data[0]) + result = append(result, one) } return c.JSON(http.StatusOK, result) } diff --git a/model.go b/model.go index 7091a05..cf7c782 100644 --- a/model.go +++ b/model.go @@ -8,6 +8,7 @@ import ( // 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"` } @@ -33,6 +34,7 @@ type SensorData struct { // 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"` @@ -47,6 +49,7 @@ func (d Device) History(svc *dynamodb.DynamoDB, limit int64) ([]Item, error) { } 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