commit 873ecb87db38a92c90dac028ea7a5307ca4a517e Author: Bram Prieshof Date: Sun Oct 15 19:12:06 2023 +0200 inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9fb2dcb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +GoMqttIR_* \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1c3f829 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +#!/usr/bin/make +.DEFAULT_GOAL := list + +list: #Shows this list + @echo The following options are available + @grep '^[^#[:space:]].*:' Makefile | grep -v := | grep \# | sed -e "s/:[^|]*#/: /g" + +all: prepair amd64_build arm64_build arm_build #Build for all targets (excludes native) + +native: prepair native_build #Build for "Native" target +amd64: prepair amd64_build #Build for amd64 target +arm64: prepair arm64_build #Build for arm64 target +arm: prepair arm_build #Build for arm target + +prepair: + go get + go install + +native_build: + go build -o GoMqttIR_Native + +amd64_build: + env GOARCH=amd64 go build -o GoMqttIR_amd64 + +arm64_build: + env GOARCH=arm64 go build -o GoMqttIR_arm64 + +arm_build: + env GOARCH=arm go build -o GoMqttIR_arm + +clean: # Run cleanup + go clean + rm -f GoMqttIR_Native GoMqttIR_amd64 GoMqttIR_arm64 GoMqttIR_arm \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..a21fac5 --- /dev/null +++ b/Readme.md @@ -0,0 +1,3 @@ +# GoMqttIR + +an ir-ctl command wrapper to bridge mqtt with an ir blaster written in golang \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9513eb4 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module brammp.nl/GoMqttIR + +go 1.21.1 + +require github.com/eclipse/paho.mqtt.golang v1.4.3 +require github.com/gorilla/websocket v1.5.0 +require golang.org/x/sync v0.4.0 +require golang.org/x/net v0.17.0 \ No newline at end of file diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..8333065 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQNCyp70xik= +github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= +golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= +golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= +golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= diff --git a/main.go b/main.go new file mode 100644 index 0000000..a76e98b --- /dev/null +++ b/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + mqtt "github.com/eclipse/paho.mqtt.golang" + "time" + "os/exec" + "os" + "flag" +) + +//mqtt Event listener +var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) { + fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic()) + RecievedPayload := string(msg.Payload()) + cmd := exec.Command("ir-ctl","-S" + RecievedPayload) + out, err := cmd.Output() + if err != nil { + fmt.Println("could not run command: ", err) + } + fmt.Println("Output: ", string(out)) +} + +var connectHandler mqtt.OnConnectHandler = func(client mqtt.Client) { + fmt.Println("Connected") +} + +var connectLostHandler mqtt.ConnectionLostHandler = func(client mqtt.Client, err error) { + fmt.Printf("Connect lost: %v", err) +} + +func main() { + //Set variables + var DeviceName string + var broker = "192.168.1.4" + var port = 1883 + var CoreTopic = "KodiIR" + //Get the hostname as sane default for mqtt topic/device id + hostname, err := os.Hostname() + if err != nil { + panic(err) + } + //innput flag -DeviceName + flag.StringVar(&DeviceName, "DeviceName", hostname, "The name that will be used as the topic an mqtt DeviceID") + flag.Parse() + //Set Mqtt Client options + opts := mqtt.NewClientOptions() + opts.AddBroker(fmt.Sprintf("tcp://%s:%d", broker, port)) + opts.SetClientID(DeviceName) + opts.SetDefaultPublishHandler(messagePubHandler) + opts.SetWill(CoreTopic + "/" + DeviceName + "/status", "Offline", 0, true) + opts.OnConnect = connectHandler + opts.OnConnectionLost = connectLostHandler + + client := mqtt.NewClient(opts) + if token := client.Connect(); token.Wait() && token.Error() != nil { + panic(token.Error()) + } + //Subscribe to command toptic + client.Subscribe(CoreTopic + "/" + DeviceName + "/cmd", 1, nil).Wait() + //Publish connect msg + client.Publish(CoreTopic + "/" + DeviceName + "/status", 0, true, "Online") + //Print status + fmt.Printf("Mqtt subscribed") + //Loop so the program keeps running + for { + time.Sleep(time.Hour) + } + + //Publish disconnect msg + client.Publish(CoreTopic + "/" + DeviceName + "/status", 0, true, "Offline") + client.Disconnect(250) +} \ No newline at end of file diff --git a/mqttir.service b/mqttir.service new file mode 100644 index 0000000..ac7cd5e --- /dev/null +++ b/mqttir.service @@ -0,0 +1,11 @@ +[Unit] +Description=IR over MQTT controller +After=multi-user.target + + +[Service] +Type=simple +Restart=always +ExecStart=/opt/GoMqttIR_ +[Install] +WantedBy=multi-user.target