-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
310 lines (267 loc) · 9.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Nest application to export Data nest to console or to influxdb.
package main
//go:generate echo Go Generate!
//go:generate ./command/bindata.sh
//go:generate ./command/bindata-assetfs.sh
import (
"context"
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"reflect"
"strconv"
"time"
_ "net/http/pprof"
"github.com/nicksnyder/go-i18n/i18n"
nest "github.com/patrickalin/nest-api-go"
"github.com/patrickalin/nest-client-go/assembly"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
//configName name of the config file and log file
const (
configNameFile = "config"
logFile = "nest.log"
)
// Configuration is the structure of the config YAML file
//use http://mervine.net/json2struct
type configuration struct {
consoleActivated bool
hTTPActivated bool
historyActivated bool
hTTPPort string
hTTPSPort string
influxDBActivated bool
influxDBDatabase string
influxDBPassword string
influxDBServer string
influxDBServerPort string
influxDBUsername string
logLevel string
nestAccessToken string
nestURL string
refreshTimer time.Duration
mock bool
language string
translateFunc i18n.TranslateFunc
dev bool
}
var (
//Version of the code, fill in in compile.sh -ldflags "-X main.Version=`cat VERSION`"
Version = "No Version Provided"
//logger
log = logrus.New()
)
func init() {
log.Formatter = new(logrus.JSONFormatter)
err := os.Remove(logFile)
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
log.Error("Failed to log to file, using default stderr")
return
}
log.Out = file
}
func main() {
//Create context
logDebug(funcName(), "Create context", "")
myContext, cancel := context.WithCancel(context.Background())
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh)
go func() {
select {
case i := <-signalCh:
logDebug(funcName(), "Receive interrupt", i.String())
cancel()
return
}
}()
logrus.WithFields(logrus.Fields{
"time": time.Now().Format(time.RFC850),
"version": Version,
"config": configNameFile,
"fct": funcName(),
}).Info("Nest API")
//Read configuration from config file
config, err := readConfig(configNameFile)
if err != nil {
logWarn(funcName(), "Config file not loaded error we use flag and default value", os.Args[0])
config.language = "en-us"
config.influxDBActivated = false
config.hTTPActivated = true
config.hTTPPort = ":1111"
config.hTTPSPort = ":1112"
config.consoleActivated = true
config.refreshTimer = time.Duration(60) * time.Second
config.nestURL = "https://api.nest.com/api/skydata/"
config.logLevel = "debug"
config.mock = true
config.dev = false
}
//Read flags
logDebug(funcName(), "Get flag from command line", "")
levelF := flag.String("debug", "", "panic,fatal,error,warning,info,debug")
tokenF := flag.String("token", "", "yourtoken")
develF := flag.String("devel", "", "true,false")
mockF := flag.String("mock", "", "true,false")
flag.Parse()
if *levelF != "" {
config.logLevel = *levelF
}
if *tokenF != "" {
config.nestAccessToken = *tokenF
}
if *develF != "" {
config.dev, err = strconv.ParseBool(*develF)
checkErr(err, funcName(), "error convert string to bol", "")
}
if *mockF != "" {
config.mock, err = strconv.ParseBool(*mockF)
checkErr(err, funcName(), "error convert string to bol", "")
}
// Set Level log
level, err := logrus.ParseLevel(config.logLevel)
checkErr(err, funcName(), "Error parse level", "")
log.Level = level
logInfo(funcName(), "Level log", config.logLevel)
// Context
ctxsch := context.Context(myContext)
channels := make(map[string]chan nest.Nest)
// Traduction
i18n.ParseTranslationFileBytes("lang/en-us.all.json", readFile("lang/en-us.all.json", config.dev))
checkErr(err, funcName(), "Error read language file check in config.yaml if dev=false", "")
i18n.ParseTranslationFileBytes("lang/fr.all.json", readFile("lang/fr.all.json", config.dev))
checkErr(err, funcName(), "Error read language file check in config.yaml if dev=false", "")
translateFunc, err := i18n.Tfunc(config.language)
checkErr(err, funcName(), "Problem with loading translate file", "")
// Console initialisation
if config.consoleActivated {
channels["console"] = make(chan nest.Nest)
c, err := createConsole(channels["console"], translateFunc, config.dev)
checkErr(err, funcName(), "Error with initConsol", "")
c.listen(context.Background())
}
// InfluxDB initialisation
if config.influxDBActivated {
channels["influxdb"] = make(chan nest.Nest)
c, err := initClient(channels["influxdb"], config.influxDBServer, config.influxDBServerPort, config.influxDBUsername, config.influxDBPassword, config.influxDBDatabase)
checkErr(err, funcName(), "Error with initClientInfluxDB", "")
c.listen(context.Background())
}
// WebServer initialisation
var httpServ *httpServer
if config.hTTPActivated {
channels["store"] = make(chan nest.Nest)
st, err := createStore(channels["store"])
checkErr(err, funcName(), "Error with history create store", "")
st.listen(context.Background())
channels["web"] = make(chan nest.Nest)
httpServ, err = createWebServer(channels["web"], config.hTTPPort, config.hTTPSPort, translateFunc, config.dev, st)
checkErr(err, funcName(), "Error with initWebServer", "")
httpServ.listen(context.Background())
}
// get nest JSON and parse information in nest Go Structure
mynest := nest.New(config.nestURL, config.nestAccessToken, config.mock, log)
//Call scheduler
schedule(ctxsch, mynest, channels, config.refreshTimer)
//If signal to close the program
<-myContext.Done()
if httpServ.httpServ != nil {
logDebug(funcName(), "Shutting down webserver", "")
err := httpServ.httpServ.Shutdown(myContext)
checkErr(err, funcName(), "Impossible to shutdown context", "")
}
logrus.WithFields(logrus.Fields{
"fct": "main.main",
}).Debug("Terminated see nest.log")
}
// The scheduler executes each time "collect"
func schedule(myContext context.Context, mynest nest.Nest, channels map[string]chan nest.Nest, refreshTime time.Duration) {
ticker := time.NewTicker(refreshTime)
logDebug(funcName(), "Create scheduler", refreshTime.String())
collect(mynest, channels)
for {
select {
case <-ticker.C:
collect(mynest, channels)
case <-myContext.Done():
logDebug(funcName(), "Stoping ticker", "")
ticker.Stop()
for _, v := range channels {
close(v)
}
return
}
}
}
//Principal function which one loops each Time Variable
func collect(mynest nest.Nest, channels map[string]chan nest.Nest) {
logDebug(funcName(), "Parse informations from API nest", "")
mynest.Refresh()
//send message on each channels
for _, v := range channels {
v <- mynest
}
}
// ReadConfig read config from config.json with the package viper
func readConfig(configName string) (configuration, error) {
var conf configuration
viper.SetConfigName(configName)
viper.AddConfigPath(".")
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
checkErr(err, funcName(), "Fielpaths", "")
dir = dir + "/" + configName
if err := viper.ReadInConfig(); err != nil {
logWarn(funcName(), "Error loading the config file", dir)
return conf, err
}
logInfo(funcName(), "The config file loaded", dir)
//TODO#16 find to simplify this section
conf.nestURL = viper.GetString("NestURL")
conf.nestAccessToken = viper.GetString("NestAccessToken")
conf.influxDBDatabase = viper.GetString("InfluxDBDatabase")
conf.influxDBPassword = viper.GetString("InfluxDBPassword")
conf.influxDBServer = viper.GetString("InfluxDBServer")
conf.influxDBServerPort = viper.GetString("InfluxDBServerPort")
conf.influxDBUsername = viper.GetString("InfluxDBUsername")
conf.consoleActivated = viper.GetBool("ConsoleActivated")
conf.influxDBActivated = viper.GetBool("InfluxDBActivated")
conf.historyActivated = viper.GetBool("historyActivated")
conf.refreshTimer = time.Duration(viper.GetInt("RefreshTimer")) * time.Second
conf.hTTPActivated = viper.GetBool("HTTPActivated")
conf.hTTPPort = viper.GetString("HTTPPort")
conf.hTTPSPort = viper.GetString("hTTPSPort")
conf.logLevel = viper.GetString("LogLevel")
conf.mock = viper.GetBool("mock")
conf.language = viper.GetString("language")
conf.dev = viper.GetBool("dev")
// Check if one value of the structure is empty
v := reflect.ValueOf(conf)
values := make([]interface{}, v.NumField())
for i := 0; i < v.NumField(); i++ {
values[i] = v.Field(i)
//TODO#16
//v.Field(i).SetString(viper.GetString(v.Type().Field(i).Name))
if values[i] == "" {
return conf, fmt.Errorf("Check if the key " + v.Type().Field(i).Name + " is present in the file " + dir)
}
}
if token := os.Getenv("nestAccessToken"); token != "" {
conf.nestAccessToken = token
}
return conf, nil
}
//Read file and return []byte
func readFile(fileName string, dev bool) []byte {
if dev {
fileByte, err := ioutil.ReadFile(fileName)
checkErr(err, funcName(), "Error reading the file", fileName)
return fileByte
}
fileByte, err := assembly.Asset(fileName)
checkErr(err, funcName(), "Error reading the file", fileName)
return fileByte
}