Files
go-telldus-matter/main.go
2025-11-21 18:12:55 +01:00

90 lines
2.2 KiB
Go

package main
import (
"fmt"
"log"
"net"
"git.k6n.net/mats/go-telldus-matter/telldus"
"github.com/tom-code/gomat"
)
const (
fabricID = 0x100
adminUser = 5
deviceID = 10
matterIP = "192.168.1.100" // Replace with actual Matter device IP
matterPort = 5540
)
var fabric *gomat.Fabric
func main() {
// Initialize Telldus
telldus.Init()
defer telldus.Close()
// Register callbacks
telldus.RegisterDeviceEvent(deviceEventHandler)
telldus.RegisterSensorEvent(sensorEventHandler)
// Initialize Matter certificate manager
cm := gomat.NewFileCertManager(fabricID)
cm.Load()
fabric = gomat.NewFabric(fabricID, cm)
// Keep the program running
fmt.Println("Telldus-Matter bridge running. Press Ctrl+C to exit.")
select {}
}
func deviceEventHandler(deviceId, method int, data string, callbackId int) {
fmt.Printf("Device event: ID=%d, Method=%d, Data=%s\n", deviceId, method, data)
// Example: If a Telldus device is turned on, turn on a Matter device
if method == telldus.MethodTurnOn {
sendMatterCommand("on")
}
}
func sensorEventHandler(protocol, model string, id, dataType int, value string, timestamp, callbackId int) {
fmt.Printf("Sensor event: Protocol=%s, Model=%s, ID=%d, Type=%d, Value=%s, Timestamp=%d\n",
protocol, model, id, dataType, value, timestamp)
// Example: Log sensor data, or react to temperature, etc.
}
func sendMatterCommand(command string) {
secureChannel, err := gomat.StartSecureChannel(net.ParseIP(matterIP), matterPort, 55555)
if err != nil {
log.Printf("Failed to start secure channel: %v", err)
return
}
defer secureChannel.Close()
secureChannel, err = gomat.SigmaExchange(fabric, adminUser, deviceID, secureChannel)
if err != nil {
log.Printf("Failed to exchange sigma: %v", err)
return
}
var invokeCommand []byte
switch command {
case "on":
// invokeCommand = gomat.EncodeInvokeCommand(1, 6, 1, []byte{})
invokeCommand = []byte{} // Placeholder
case "off":
// invokeCommand = gomat.EncodeInvokeCommand(1, 6, 0, []byte{})
invokeCommand = []byte{} // Placeholder
}
secureChannel.Send(invokeCommand)
resp, err := secureChannel.Receive()
if err != nil {
log.Printf("Failed to receive response: %v", err)
return
}
fmt.Printf("Matter command response: %v\n", resp)
}