2020-08-26 09:54:26 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-02 09:02:30 +02:00
|
|
|
"mqttListener/config"
|
|
|
|
"mqttListener/env"
|
2020-08-30 07:16:50 +02:00
|
|
|
"mqttListener/models"
|
2020-08-26 09:54:26 +02:00
|
|
|
"mqttListener/mqtt"
|
|
|
|
|
2020-08-30 07:16:50 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-08-26 09:54:26 +02:00
|
|
|
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2020-08-30 07:16:50 +02:00
|
|
|
|
|
|
|
"net/http"
|
2020-08-26 09:54:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
|
|
|
c := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
2020-09-02 09:02:30 +02:00
|
|
|
config, err := config.NewConfig("config.toml")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2020-08-26 09:54:26 +02:00
|
|
|
|
|
|
|
// DEBUG Config
|
2020-09-02 09:02:30 +02:00
|
|
|
fmt.Println("BrokerAddress = ", config.BrokerAddress)
|
2020-08-26 09:54:26 +02:00
|
|
|
|
2020-08-30 07:16:50 +02:00
|
|
|
db, err := models.NewDB("simple.sqlite")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2020-09-02 09:02:30 +02:00
|
|
|
env := &env.Env{db, config}
|
2020-08-30 07:16:50 +02:00
|
|
|
|
2020-09-02 09:02:30 +02:00
|
|
|
mqtt.Setup(env)
|
2020-08-26 09:54:26 +02:00
|
|
|
mqtt.Connect()
|
|
|
|
go mqtt.Listen()
|
|
|
|
|
2020-09-02 09:02:30 +02:00
|
|
|
http.HandleFunc("/devices", env.DevicesIndex)
|
2020-08-30 07:16:50 +02:00
|
|
|
go http.ListenAndServe(":3000", nil)
|
2020-08-26 09:54:26 +02:00
|
|
|
|
2020-08-30 07:16:50 +02:00
|
|
|
fmt.Println("awaiting signal")
|
2020-08-26 09:54:26 +02:00
|
|
|
<-c // wait for SIGTERM
|
2020-08-30 07:16:50 +02:00
|
|
|
fmt.Println("exiting")
|
2020-08-26 09:54:26 +02:00
|
|
|
|
|
|
|
mqtt.Disconnect()
|
|
|
|
}
|