separate code
This commit is contained in:
128
src/udi/main.go
128
src/udi/main.go
@ -1,150 +1,30 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "log"
|
import "log"
|
||||||
import "fmt"
|
|
||||||
import "os"
|
import "os"
|
||||||
import "os/signal"
|
import "os/signal"
|
||||||
import "strings"
|
|
||||||
// import "time"
|
|
||||||
import MQTT "github.com/eclipse/paho.mqtt.golang"
|
|
||||||
import "github.com/google/uuid"
|
|
||||||
import "crypto/tls"
|
|
||||||
import um "udi/mqtt"
|
import um "udi/mqtt"
|
||||||
|
|
||||||
type Message struct {
|
|
||||||
topic string
|
|
||||||
payload []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
var messageInputChannel chan Message = make(chan Message, 100)
|
|
||||||
var messageOutputChannel chan Message = make(chan Message, 100)
|
|
||||||
|
|
||||||
|
|
||||||
func onMessageReceived(client MQTT.Client, message MQTT.Message) {
|
|
||||||
// log.Printf("Message received, topic: %s, payload: %s\n", message.Topic(), message.Payload())
|
|
||||||
m := Message {
|
|
||||||
topic: message.Topic(),
|
|
||||||
payload: message.Payload(),
|
|
||||||
}
|
|
||||||
select {
|
|
||||||
case messageInputChannel <- m:
|
|
||||||
{}
|
|
||||||
//log.Println("Message sent to channel")
|
|
||||||
default:
|
|
||||||
log.Println("Channel full, message lost")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func onConnectionLost(client MQTT.Client, error error) {
|
|
||||||
log.Printf("Connection lost, error %s", error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func onReconnecting(client MQTT.Client, clientOpts *MQTT.ClientOptions) {
|
|
||||||
log.Println("Oops, connection lost, already reconnecting ...")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func onConnect(client MQTT.Client) {
|
|
||||||
topicsStr := os.Getenv("MQTT_SUBSCRIBE_TOPICS")
|
|
||||||
if topicsStr == "" {
|
|
||||||
log.Fatal("No topics given, set env var MQTT_SUBSCRIBE_TOPICS")
|
|
||||||
}
|
|
||||||
topics := strings.Split(topicsStr, ",")
|
|
||||||
for _, topic := range topics {
|
|
||||||
if token := client.Subscribe(topic, 0, onMessageReceived); token.Wait() && token.Error() != nil {
|
|
||||||
log.Fatalf("Unable to subscribe to topic %s, error %s", topic, token.Error())
|
|
||||||
}
|
|
||||||
log.Printf("Successfully subscribed to topic %s", topic)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func inputDispatcher() {
|
func inputDispatcher() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case message := <- messageInputChannel:
|
case message := <- um.InputChannel:
|
||||||
log.Printf("Message arrived in inputDispatcher, topic: %s, payload: %s\n", message.topic, message.payload)
|
log.Printf("Message arrived in inputDispatcher, topic: %s, payload: %s\n", message.Topic, message.Payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func outputDispatcher(client MQTT.Client) {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case message := <- messageOutputChannel:
|
|
||||||
log.Printf("Message arrived in outputDispatcher, topic: %s, payload: %s\n", message.topic, message.payload)
|
|
||||||
if token := client.Publish(message.topic, 0, false, message.payload); token.Wait() && token.Error() != nil {
|
|
||||||
log.Printf("Unable to publish, error %s", token.Error())
|
|
||||||
}
|
|
||||||
log.Println("Successfully published")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func startMqttClient() MQTT.Client {
|
|
||||||
broker := os.Getenv("MQTT_BROKER")
|
|
||||||
if broker == "" {
|
|
||||||
log.Fatal("No broker given, set env var MQTT_BROKER")
|
|
||||||
}
|
|
||||||
|
|
||||||
prefix := "UDI"
|
|
||||||
uuid := uuid.New()
|
|
||||||
clientId := fmt.Sprintf("%s-%s", prefix, uuid)
|
|
||||||
|
|
||||||
opts := MQTT.NewClientOptions().
|
|
||||||
AddBroker(broker).
|
|
||||||
SetClientID(clientId).
|
|
||||||
SetConnectionLostHandler(onConnectionLost).
|
|
||||||
SetOnConnectHandler(onConnect).
|
|
||||||
SetReconnectingHandler(onReconnecting).
|
|
||||||
SetConnectRetry(true)
|
|
||||||
|
|
||||||
username := os.Getenv("MQTT_USERNAME")
|
|
||||||
if username != "" {
|
|
||||||
opts.SetUsername(username)
|
|
||||||
}
|
|
||||||
|
|
||||||
password := os.Getenv("MQTT_PASSWORD")
|
|
||||||
if password != "" {
|
|
||||||
opts.SetPassword(password)
|
|
||||||
}
|
|
||||||
|
|
||||||
enableTls := os.Getenv("MQTT_ENABLE_TLS")
|
|
||||||
if enableTls == "true" {
|
|
||||||
log.Println("Enableing TLS connection")
|
|
||||||
tlsConfig := &tls.Config {
|
|
||||||
InsecureSkipVerify: true,
|
|
||||||
}
|
|
||||||
opts.SetTLSConfig(tlsConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Println("Trying to connect to broker")
|
|
||||||
client := MQTT.NewClient(opts)
|
|
||||||
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
|
||||||
log.Fatalf("Unable to connect to broker %s, error %s", broker, token.Error())
|
|
||||||
}
|
|
||||||
log.Printf("Successfully connected to broker %s", broker)
|
|
||||||
|
|
||||||
return client
|
|
||||||
}
|
|
||||||
|
|
||||||
func stopMqttClient(client MQTT.Client) {
|
|
||||||
log.Println("Disconnecting from broker")
|
|
||||||
client.Disconnect(250)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.SetPrefix("UDI: ")
|
log.SetPrefix("UDI: ")
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
|
||||||
|
|
||||||
log.Println("UDI starting")
|
log.Println("UDI starting")
|
||||||
um.TestMqtt()
|
|
||||||
|
|
||||||
go inputDispatcher()
|
go inputDispatcher()
|
||||||
|
|
||||||
mqttClient := startMqttClient()
|
um.StartMqttClient()
|
||||||
defer stopMqttClient(mqttClient)
|
defer um.StopMqttClient()
|
||||||
|
|
||||||
go outputDispatcher(mqttClient)
|
|
||||||
|
|
||||||
log.Println("UDI running")
|
log.Println("UDI running")
|
||||||
|
|
||||||
|
@ -1,9 +1,126 @@
|
|||||||
package mqtt
|
package mqtt
|
||||||
|
|
||||||
import "log"
|
import "log"
|
||||||
|
import "fmt"
|
||||||
|
import "os"
|
||||||
|
import "strings"
|
||||||
|
// import "time"
|
||||||
|
import MQTT "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
import "github.com/google/uuid"
|
||||||
|
import "crypto/tls"
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
func TestMqtt() {
|
Topic string
|
||||||
log.Println("test mqtt")
|
Payload []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
var InputChannel chan Message = make(chan Message, 100)
|
||||||
|
var OutputChannel chan Message = make(chan Message, 100)
|
||||||
|
|
||||||
|
var mqttClient MQTT.Client
|
||||||
|
|
||||||
|
func onMessageReceived(client MQTT.Client, message MQTT.Message) {
|
||||||
|
// log.Printf("Message received, topic: %s, payload: %s\n", message.Topic(), message.Payload())
|
||||||
|
m := Message {
|
||||||
|
Topic: message.Topic(),
|
||||||
|
Payload: message.Payload(),
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case InputChannel <- m:
|
||||||
|
{}
|
||||||
|
//log.Println("Message sent to channel")
|
||||||
|
default:
|
||||||
|
log.Println("Channel full, message lost")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func onConnectionLost(client MQTT.Client, error error) {
|
||||||
|
log.Printf("Connection lost, error %s", error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func onReconnecting(client MQTT.Client, clientOpts *MQTT.ClientOptions) {
|
||||||
|
log.Println("Oops, connection lost, already reconnecting ...")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func onConnect(client MQTT.Client) {
|
||||||
|
topicsStr := os.Getenv("MQTT_SUBSCRIBE_TOPICS")
|
||||||
|
if topicsStr == "" {
|
||||||
|
log.Fatal("No topics given, set env var MQTT_SUBSCRIBE_TOPICS")
|
||||||
|
}
|
||||||
|
topics := strings.Split(topicsStr, ",")
|
||||||
|
for _, topic := range topics {
|
||||||
|
if token := client.Subscribe(topic, 0, onMessageReceived); token.Wait() && token.Error() != nil {
|
||||||
|
log.Fatalf("Unable to subscribe to topic %s, error %s", topic, token.Error())
|
||||||
|
}
|
||||||
|
log.Printf("Successfully subscribed to topic %s", topic)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func outputDispatcher(client MQTT.Client) {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case message := <- OutputChannel:
|
||||||
|
log.Printf("Message arrived in outputDispatcher, topic: %s, payload: %s\n", message.Topic, message.Payload)
|
||||||
|
if token := client.Publish(message.Topic, 0, false, message.Payload); token.Wait() && token.Error() != nil {
|
||||||
|
log.Printf("Unable to publish, error %s", token.Error())
|
||||||
|
}
|
||||||
|
log.Println("Successfully published")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func StartMqttClient() {
|
||||||
|
broker := os.Getenv("MQTT_BROKER")
|
||||||
|
if broker == "" {
|
||||||
|
log.Fatal("No broker given, set env var MQTT_BROKER")
|
||||||
|
}
|
||||||
|
|
||||||
|
prefix := "UDI"
|
||||||
|
uuid := uuid.New()
|
||||||
|
clientId := fmt.Sprintf("%s-%s", prefix, uuid)
|
||||||
|
|
||||||
|
opts := MQTT.NewClientOptions().
|
||||||
|
AddBroker(broker).
|
||||||
|
SetClientID(clientId).
|
||||||
|
SetConnectionLostHandler(onConnectionLost).
|
||||||
|
SetOnConnectHandler(onConnect).
|
||||||
|
SetReconnectingHandler(onReconnecting).
|
||||||
|
SetConnectRetry(true)
|
||||||
|
|
||||||
|
username := os.Getenv("MQTT_USERNAME")
|
||||||
|
if username != "" {
|
||||||
|
opts.SetUsername(username)
|
||||||
|
}
|
||||||
|
|
||||||
|
password := os.Getenv("MQTT_PASSWORD")
|
||||||
|
if password != "" {
|
||||||
|
opts.SetPassword(password)
|
||||||
|
}
|
||||||
|
|
||||||
|
enableTls := os.Getenv("MQTT_ENABLE_TLS")
|
||||||
|
if enableTls == "true" {
|
||||||
|
log.Println("Enableing TLS connection")
|
||||||
|
tlsConfig := &tls.Config {
|
||||||
|
InsecureSkipVerify: true,
|
||||||
|
}
|
||||||
|
opts.SetTLSConfig(tlsConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Trying to connect to broker")
|
||||||
|
mqttClient = MQTT.NewClient(opts)
|
||||||
|
if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
|
||||||
|
log.Fatalf("Unable to connect to broker %s, error %s", broker, token.Error())
|
||||||
|
}
|
||||||
|
log.Printf("Successfully connected to broker %s", broker)
|
||||||
|
|
||||||
|
go outputDispatcher(mqttClient)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func StopMqttClient() {
|
||||||
|
log.Println("Disconnecting from broker")
|
||||||
|
mqttClient.Disconnect(250)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
src/udi/udi
BIN
src/udi/udi
Binary file not shown.
Reference in New Issue
Block a user