-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (138 loc) · 3.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"os"
"encoding/json"
"fmt"
"net"
"strings"
mqtt "github.com/eclipse/paho.mqtt.golang"
yaml "gopkg.in/yaml.v2"
defaults "github.com/creasty/defaults"
)
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)
}
var messagePubHandler mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
}
func main() {
fmt.Println("Starting!")
fmt.Println("Reading config file")
var systemConfigFile = "/etc/telldusmqtt.conf"
var config Config
ReadConfig(systemConfigFile, &config)
var broker = config.MqttServer.BrokerHost
var port = config.MqttServer.BrokerPort
fmt.Printf("Config host: %s:%s\n", broker, port)
var username = config.MqttServer.Username
var password = config.MqttServer.Password
var clientId = config.MqttServer.ClientId
var eventSocket = config.TelldusBridge.TelldusEventsSocket
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("tcp://%s:%s", broker, port))
opts.SetClientID(clientId)
opts.SetUsername(username)
opts.SetPassword(password)
opts.SetDefaultPublishHandler(messagePubHandler)
opts.OnConnect = connectHandler
opts.OnConnectionLost = connectLostHandler
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
panic(token.Error())
}
c, err := net.Dial("unix", eventSocket)
if err != nil {
panic(err.Error())
}
for {
// Read line
buf := make([]byte, 512)
nr, err := c.Read(buf)
if err != nil {
return
}
data := buf[0:nr]
fmt.Printf("Received: %v\n", string(data))
_, paramstrings, _ := splitTelldus(string(data))
params := getParams(paramstrings)
if params["protocol"] == "arctech" {
singleString := params["house"] + "-" + params["unit"] + "-" + params["group"] + "-" + params["method"]
send(client, singleString)
}
jsonString, err := json.Marshal(params)
if err != nil {
panic(err)
}
// Send message
send(client, string(jsonString))
}
}
func send(client mqtt.Client, message string) {
// Send message
fmt.Printf("Sending '%v'\n", message)
token := client.Publish("telldus/event", 0, false, message)
token.Wait()
}
func sub(client mqtt.Client) {
topic := "#"
token := client.Subscribe(topic, 1, nil)
token.Wait()
fmt.Printf("Subscribed to topic: %s", topic)
}
func splitTelldus(message string) (string, []string, string) {
fields := strings.Split(message, ";")
var header string
var paramstrings []string
var rest string
header = fields[0]
if len(fields) > 1 {
paramstrings = fields[1:len(fields)-1]
}
if len(fields) > 1 {
rest = fields[len(fields)-1]
}
return header, paramstrings, rest
}
func getParams(paramFields []string) map[string]string {
params := make(map[string]string)
for _, i := range paramFields {
kv := strings.Split(i, ":")
key, val := kv[0], kv[1]
params[key] = val
}
return params
}
type Config struct {
MqttServer struct {
BrokerHost string `yaml:"brokerhost"`
BrokerPort string `yaml:"brokerport" default:"1883"`
Username string `yaml:"username"`
Password string `yaml:"password"`
ClientId string `yaml:"clientid" default:"telldusbridge"`
} `yaml:"mqtt"`
TelldusBridge struct {
TelldusEventsSocket string `yaml:"eventsocket" default:"/tmp/TelldusEvents"`
MqttEventTopic string `yaml:"mqtttopic" "telldus/event"`
} `yaml:"events"`
}
func ReadConfig(filename string, config *Config) {
defaults.Set(config)
f, err := os.Open(filename)
if err != nil {
printError(err)
}
defer f.Close()
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&config)
if err != nil {
printError(err)
}
fmt.Println(config)
}
func printError(err error) {
fmt.Println(err)
os.Exit(2)
}