Browse Source

Add /all API

master
sipp11 7 years ago
parent
commit
24d049941b
  1. 23
      model.go
  2. 70
      read_item.go

23
model.go

@ -1,5 +1,10 @@
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"`
@ -18,6 +23,7 @@ type ItemFloraData struct {
Conductivity float32 `json:"conductivity"`
Moisture float32 `json:"moisture"`
Temperature float32 `json:"temperature"`
Humidity float32 `json:"humidity"`
}
type Item struct {
@ -26,3 +32,20 @@ type Item struct {
Info ItemInfo `json:"info"`
Data ItemFloraData `json:"data"`
}
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)
if err != nil {
return ev, err
}
ev = append(ev, item)
}
return ev, nil
}

70
read_item.go

@ -1,15 +1,25 @@
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/aws/aws-sdk-go/aws"
"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/gorilla/mux"
)
var tplHome = pongo2.Must(pongo2.FromFile("home.html"))
var sess *session.Session
var err error
var svc *dynamodb.DynamoDB
func indexHandler(w http.ResponseWriter, r *http.Request) {
// Execute the template per HTTP request
@ -29,46 +39,50 @@ func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, `{"alive": true}`)
}
func main() {
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
// sess, err := session.NewSession(&aws.Config{
// Region: aws.String("ap-southeast-1")},
// )
func overallHandler(w http.ResponseWriter, r *http.Request) {
// Create DynamoDB client
/* svc := dynamodb.New(sess)
devices, err := GetDevices(svc)
devices, _ := GetDevices(svc)
var result []Item
for _, i := range devices.Items {
device := Device{}
err := dynamodbattribute.UnmarshalMap(i, &device)
if err != nil {
fmt.Println("Got error unmarshalling:")
fmt.Println("Device error unmarshalling:")
fmt.Println(err.Error())
os.Exit(1)
}
for _, i := range devices.Items {
device := Device{}
err := dynamodbattribute.UnmarshalMap(i, &device)
fmt.Printf(":: Name: %+v (%+v)\n", device.Name, device.Device)
data, _ := device.History(svc, 1)
result = append(result, data[0])
}
b, _ := json.Marshal(result)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, string(b))
}
result, err := GetHistory(svc, device.Device, 2)
// Make the DynamoDB Query API call
// fmt.Printf("result count item: %+v|%+v|", result.Count, result.ScannedCount)
for _, i := range result.Items {
item := Item{}
func oneDeviceHandler(w http.ResponseWriter, r *http.Request) {
err = dynamodbattribute.UnmarshalMap(i, &item)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
}
func main() {
// Initialize a session in us-west-2 that the SDK will use to load
// credentials from the shared credentials file ~/.aws/credentials.
sess, err = session.NewSession(&aws.Config{
Region: aws.String("ap-southeast-1")},
)
if err != nil {
fmt.Println("Got error unmarshalling:")
fmt.Println("aws error ")
fmt.Println(err.Error())
os.Exit(1)
}
// fmt.Println("Found item:")
fmt.Println(" > Timestamp: ", item.Timestamp,
"Data == Temp: ", item.Data.Temperature,
" | moisure: ", item.Data.Moisture)
}
} */
// Create DynamoDB client
svc = dynamodb.New(sess)
// func main() {
// session, err := r.Connect(r.ConnectOpts{
@ -91,6 +105,8 @@ func main() {
// r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
// r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
r.HandleFunc("/health", healthCheckHandler)
r.HandleFunc("/id/{id}", oneDeviceHandler)
r.HandleFunc("/all", overallHandler)
r.HandleFunc("/", indexHandler)
log.Fatal(http.ListenAndServe(":8000", r))
}

Loading…
Cancel
Save