Compare commits
No commits in common. "main" and "release" have entirely different histories.
17
sauna.go
17
sauna.go
@ -33,7 +33,7 @@ var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Me
|
|||||||
}
|
}
|
||||||
|
|
||||||
var saunaHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
var saunaHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
|
||||||
logger.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
|
log.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
|
||||||
sauna_mutex.Lock()
|
sauna_mutex.Lock()
|
||||||
err := json.Unmarshal(msg.Payload(), &sauna_temperature)
|
err := json.Unmarshal(msg.Payload(), &sauna_temperature)
|
||||||
sauna_temperature.Time = time.Now()
|
sauna_temperature.Time = time.Now()
|
||||||
@ -49,7 +49,6 @@ var saunaHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message
|
|||||||
|
|
||||||
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
|
var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) {
|
||||||
logger.Println("Connected")
|
logger.Println("Connected")
|
||||||
subscribe(client)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
|
var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) {
|
||||||
@ -78,32 +77,24 @@ func http_endpoint_sauna(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect() mqtt.Client {
|
func Start() {
|
||||||
// MQTT connection
|
// MQTT connection
|
||||||
opts := mqtt.NewClientOptions()
|
opts := mqtt.NewClientOptions()
|
||||||
opts.AddBroker("tcp://p5:1883")
|
opts.AddBroker("tcp://nuc:1883")
|
||||||
opts.SetClientID("apiservice/sauna/" + strconv.Itoa(rand.Int()))
|
opts.SetClientID("apiservice/sauna/" + strconv.Itoa(rand.Int()))
|
||||||
opts.SetDefaultPublishHandler(messagePubHandler)
|
opts.SetDefaultPublishHandler(messagePubHandler)
|
||||||
opts.OnConnect = connectHandler
|
opts.OnConnect = connectHandler
|
||||||
opts.OnConnectionLost = connectLostHandler
|
opts.OnConnectionLost = connectLostHandler
|
||||||
client := mqtt.NewClient(opts)
|
client := mqtt.NewClient(opts)
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
||||||
logger.Panic(token.Error())
|
panic(token.Error())
|
||||||
}
|
}
|
||||||
return client
|
|
||||||
}
|
|
||||||
|
|
||||||
func subscribe(client mqtt.Client) {
|
|
||||||
// MQTT subscribtion
|
// MQTT subscribtion
|
||||||
topic := "sauna/temperature"
|
topic := "sauna/temperature"
|
||||||
token := client.Subscribe(topic, 1, saunaHandler)
|
token := client.Subscribe(topic, 1, saunaHandler)
|
||||||
token.Wait()
|
token.Wait()
|
||||||
logger.Printf("Subscribed to topic %s", topic)
|
logger.Printf("Subscribed to topic %s", topic)
|
||||||
}
|
|
||||||
|
|
||||||
func Start() {
|
|
||||||
client := connect()
|
|
||||||
subscribe(client)
|
|
||||||
|
|
||||||
http.HandleFunc("/api/sauna", http_endpoint_sauna)
|
http.HandleFunc("/api/sauna", http_endpoint_sauna)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user