@@ -2,46 +2,50 @@ package main
22
33import (
44 "encoding/json"
5+ "flag"
56 "fmt"
67 "io"
7- "log"
88 "os"
99
10- "github.com/joho/godotenv"
1110 "github.com/kuzemkon/aws-iot-device-sdk-go/device"
11+ "github.com/sirupsen/logrus"
1212 "periph.io/x/periph/conn/i2c/i2creg"
1313 "periph.io/x/periph/experimental/devices/mcp9808"
1414 "periph.io/x/periph/host"
1515)
1616
17- type Shadow struct {
18- State struct {
19- Reported struct {
20- Temp int `json:"temp"`
21- } `json:"reported"`
22- } `json:"state"`
23- Version int
24- }
17+ var thingName string
18+ var endpoint string
19+ var privateKey string
20+ var cert string
21+ var rootCA string
22+ var logFile string
23+ var log = logrus .New ()
24+
25+ func init () {
26+ flag .StringVar (& thingName , "thing" , "" , "Set this to the AWS IoT thing name" )
27+ flag .StringVar (& endpoint , "endpoint" , "" , "Set this to the AWS IoT endpoint" )
28+ flag .StringVar (& privateKey , "privatekey" , "" , "This must be a full path to the AWS IoT thing private key .pem.key file" )
29+ flag .StringVar (& cert , "cert" , "" , "This must be a full path to the AWS IoT thing cert .pem.crt file" )
30+ flag .StringVar (& rootCA , "rootca" , "" , "This must be a full path to the AWS IoT thing root-CA .crt file" )
31+ flag .StringVar (& logFile , "logfile" , "" , "Set this to the full path for the log file" )
32+ flag .Parse ()
33+
34+ if thingName == "" || endpoint == "" || privateKey == "" || cert == "" || rootCA == "" || logFile == "" {
35+ flag .PrintDefaults ()
36+ log .Panic ("missing required flag" )
37+ }
2538
26- func initEnv () error {
27- home , err := os .UserHomeDir ( )
39+ // Initilize logging
40+ file , err := os .OpenFile ( logFile , os . O_RDWR | os . O_CREATE | os . O_APPEND , 0666 )
2841 if err != nil {
29- return err
42+ log . Info ( "error opening file: %v" , err )
3043 }
31- err = godotenv .Load (fmt .Sprintf ("%s/.env" , home ))
32- return err
33- }
3444
35- func initThing () (* device.Thing , error ) {
36- thingName := device .ThingName (os .Getenv ("THING_NAME" ))
37- endpoint := os .Getenv ("ENDPOINT" )
38- keyPair := device.KeyPair {
39- PrivateKeyPath : os .Getenv ("PRIVATE_KEY_PATH" ),
40- CertificatePath : os .Getenv ("CERT_PATH" ),
41- CACertificatePath : os .Getenv ("ROOT_CA_PATH" ),
42- }
45+ mw := io .MultiWriter (os .Stdout , file )
4346
44- return device .NewThing (keyPair , endpoint , thingName )
47+ log .SetFormatter (& logrus.JSONFormatter {})
48+ log .SetOutput (mw )
4549}
4650
4751func readTemp () int {
@@ -73,32 +77,32 @@ func readTemp() int {
7377 return int (measurement .Fahrenheit ())
7478}
7579
76- func main () {
77- err := initEnv ()
78- if err != nil {
79- panic (err )
80- }
80+ type Shadow struct {
81+ State struct {
82+ Reported struct {
83+ Temp int `json:"temp"`
84+ } `json:"reported"`
85+ } `json:"state"`
86+ Version int
87+ }
8188
82- logPath := os .Getenv ("LOG_FILE_PATH" )
83- f , err := os .OpenFile (logPath , os .O_RDWR | os .O_CREATE | os .O_APPEND , 0666 )
84- if err != nil {
85- log .Fatalf ("error opening file: %v" , err )
89+ func main () {
90+ // Initilize a new Thing
91+ keyPair := device.KeyPair {
92+ PrivateKeyPath : privateKey ,
93+ CertificatePath : cert ,
94+ CACertificatePath : rootCA ,
8695 }
87- defer f .Close ()
8896
89- mw := io .MultiWriter (os .Stdout , f )
90- log .SetOutput (mw )
91-
92- // Initilize a new Thing
93- thing , err := initThing ()
97+ thing , err := device .NewThing (keyPair , endpoint , device .ThingName (thingName ))
9498 if err != nil {
95- panic (err )
99+ log . Panic (err )
96100 }
97101
98102 // Subscribe to shadow
99103 shadowChan , err := thing .SubscribeForThingShadowChanges ()
100104 if err != nil {
101- panic (err )
105+ log . Panic (err )
102106 }
103107
104108 // Read temperature from sensor
@@ -108,20 +112,23 @@ func main() {
108112 // Update thing shadow
109113 err = thing .UpdateThingShadow (device .Shadow (shadow ))
110114 if err != nil {
111- panic (err )
115+ log . Panic (err )
112116 }
113117
114118 updatedShadow , ok := <- shadowChan
115119 if ! ok {
116- panic ("Failed to read from shadow channel" )
120+ log . Panic ("Failed to read from shadow channel" )
117121 }
118122
119123 unmarshaledUpdatedShadow := & Shadow {}
120124
121125 err = json .Unmarshal (updatedShadow , unmarshaledUpdatedShadow )
122126 if err != nil {
123- panic (err )
127+ log . Panic (err )
124128 }
125129
126- log .Printf ("Temp: %v Version: %v" , unmarshaledUpdatedShadow .State .Reported .Temp , unmarshaledUpdatedShadow .Version )
130+ log .WithFields (logrus.Fields {
131+ "temperature" : unmarshaledUpdatedShadow .State .Reported .Temp ,
132+ "version" : unmarshaledUpdatedShadow .Version ,
133+ }).Info ("Updated Shadow" )
127134}
0 commit comments