Skip to content

Instantly share code, notes, and snippets.

@mogeta
Created June 9, 2018 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogeta/6cd955c6a0479a80b1558399dd7d301d to your computer and use it in GitHub Desktop.
Save mogeta/6cd955c6a0479a80b1558399dd7d301d to your computer and use it in GitHub Desktop.
lambdaからfirebase
package main
import (
"context"
"firebase.google.com/go"
"google.golang.org/api/option"
"log"
"time"
"github.com/aws/aws-lambda-go/lambda"
)
type iratto struct {
Type string
Created time.Time
}
func main() {
lambda.Start(hello)
}
func hello(event IotButtonEvent) {
ctx := context.Background()
opt := option.WithCredentialsFile("./firebase_secret_key.json")
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
log.Fatalln(err)
}
client, err := app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
_, _, err = client.Collection("users").Add(ctx, iratto{
Type: event.DeviceEvent.ButtonClicked.ClickType,
Created: time.Now(),
})
if err != nil {
log.Fatalf("Failed adding alovelace: %v", err)
}
}
package main
import "time"
type IotButtonEvent struct {
DeviceInfo struct {
DeviceID string `json:"deviceId"`
Type string `json:"type"`
RemainingLife float64 `json:"remainingLife"`
Attributes struct {
ProjectRegion string `json:"projectRegion"`
ProjectName string `json:"projectName"`
PlacementName string `json:"placementName"`
DeviceTemplateName string `json:"deviceTemplateName"`
} `json:"attributes"`
} `json:"deviceInfo"`
DeviceEvent struct {
ButtonClicked struct {
ClickType string `json:"clickType"`
ReportedTime time.Time `json:"reportedTime"`
} `json:"buttonClicked"`
} `json:"deviceEvent"`
PlacementInfo struct {
ProjectName string `json:"projectName"`
PlacementName string `json:"placementName"`
Attributes struct {
Key string `json:"key"`
} `json:"attributes"`
Devices struct {
SampleRequest string `json:"Sample-Request"`
} `json:"devices"`
} `json:"placementInfo"`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment