Browse Source

Support Docker; Support toggle

master
maze 2 years ago
parent
commit
7447c3ea4f
3 changed files with 48 additions and 5 deletions
  1. +8
    -0
      Dockerfile
  2. +37
    -5
      main.go
  3. +3
    -0
      rfproxy.conf

+ 8
- 0
Dockerfile View File

@ -0,0 +1,8 @@
FROM golang:latest
RUN curl -fsSL -o /usr/local/bin/dep https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 && chmod +x /usr/local/bin/dep
RUN mkdir /go/src/rfproxy
ADD . /go/src/rfproxy/
WORKDIR /go/src/rfproxy
RUN GOPATH=/go dep ensure -vendor-only
RUN GOPATH=/go go build -o /app/main .
CMD ["/app/main", "-config", "/etc/rfproxy/rfproxy.conf"]

+ 37
- 5
main.go View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"encoding/json"
"flag"
"io/ioutil"
@ -30,6 +31,8 @@ var (
Delay string
Filter map[string]int
}
Toggle map[string]string
}
delay = time.Second * 5
@ -39,6 +42,8 @@ func main() {
configFile := flag.String("config", "rfproxy.conf", "config file")
flag.Parse()
dockerStuff()
b, err := ioutil.ReadFile(*configFile)
if err != nil {
panic(err)
@ -78,7 +83,13 @@ func main() {
opts.SetUsername(config.Server.Username)
opts.SetPassword(config.Server.Password)
opts.SetCleanSession(true)
opts.SetConnectTimeout(time.Second * 10)
opts.SetAutoReconnect(true)
opts.SetPingTimeout(time.Second * 15)
opts.SetWriteTimeout(time.Second * 15)
opts.OnConnect = func(c mqtt.Client) {
log.Println("connected")
log.Printf("topic %s: subscribe", config.Sensor.Sub)
if token := c.Subscribe(config.Sensor.Sub, 0, onMessageReceived); token.Wait() && token.Error() != nil {
panic(token.Error())
}
@ -115,12 +126,21 @@ func onMessageReceived(client mqtt.Client, message mqtt.Message) {
RfReceived struct {
Sync, Low, High int
Data string
RfKey string
RfKey interface{}
} `json:"RfReceived"`
}
if err := json.Unmarshal(message.Payload(), &payload); err != nil {
log.Printf("topic %s: invalid payload: %v", message.Topic(), err)
log.Printf("!! %s: invalid payload: %v", message.Topic(), err)
return
}
log.Printf("<- %s: payload: %+v", message.Topic(), payload.RfReceived)
if topic, ok := config.Toggle[payload.RfReceived.Data]; ok {
log.Printf("-> %s: toggle", topic)
if token := client.Publish(topic, 0, false, "toggle"); token.Wait() && token.Error() != nil {
log.Printf("!! %s: %v", topic, token.Error())
}
return
}
@ -151,15 +171,27 @@ func onMessageReceived(client mqtt.Client, message mqtt.Message) {
}
}
log.Printf("topic %s: payload: %+v", message.Topic(), payload.RfReceived)
log.Printf("topic %s: %s", config.Sensor.Pub, config.Sensor.On)
log.Printf("-> %s: %s", config.Sensor.Pub, config.Sensor.On)
if token := client.Publish(config.Sensor.Pub, 0, false, config.Sensor.On); token.Wait() && token.Error() != nil {
panic(token.Error())
}
<-time.After(delay)
log.Printf("topic %s: %s", config.Sensor.Pub, config.Sensor.Off)
log.Printf("-> %s: %s", config.Sensor.Pub, config.Sensor.Off)
if token := client.Publish(config.Sensor.Pub, 0, false, config.Sensor.Off); token.Wait() && token.Error() != nil {
panic(token.Error())
}
}
func dockerStuff() {
b, err := ioutil.ReadFile("/proc/1/cgroup")
if err != nil {
return
}
if !bytes.Contains(b, []byte(":/docker/")) {
return
}
log.SetFlags(0)
log.Println("detected docker")
}

+ 3
- 0
rfproxy.conf View File

@ -11,3 +11,6 @@ on = "on"
off = "off"
delay = "5s"
filter = {sync_ge = 11500, sync_lt = 11600, low_ge = 350, low_lt = 390}
[toggle]

Loading…
Cancel
Save