This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
105 lines (95 loc) · 2.63 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/hemtjanst/bibliotek/client"
"github.com/hemtjanst/bibliotek/device"
"github.com/hemtjanst/bibliotek/feature"
"github.com/hemtjanst/bibliotek/transport/mqtt"
)
var (
timeout = flag.Int("timeout", 120, "Minutes after which we think the robot is done")
startTopic = flag.String("start.topic", "remote/robovac/KEY_AUTO", "LIRCd topic to start the vacuum")
startPress = flag.String("start.press", "200", "Milliseconds to hold down the start button")
stopTopic = flag.String("stop.topic", "remote/robovac/KEY_HOME", "LIRCd topic to stop the vacuum")
stopPress = flag.String("stop.press", "5000", "Milliseconds to hold down the stop button")
manufacturer = flag.String("robot.manufacturer", "Eufy", "Vacuum manufacturer")
name = flag.String("robot.name", "RoboVac", "Vacuum name")
model = flag.String("robot.model", "11", "Vacuum model")
serial = flag.String("robot.serial-number", "undefined", "Vacuum serial number")
)
type handler struct {
devices []*device.Device
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Parameters:\n\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
}
mcfg := mqtt.MustFlags(flag.String, flag.Bool)
flag.Parse()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
m, err := mqtt.New(ctx, mcfg())
if err != nil {
panic(err)
}
robot, _ := client.NewDevice(&device.Info{
Topic: "robot/vacuum",
Name: *name,
Model: *model,
Type: "switch",
Manufacturer: *manufacturer,
SerialNumber: *serial,
Features: map[string]*feature.Info{
"on": &feature.Info{}},
}, m)
ft := robot.Feature("on")
on, _ := ft.OnSet()
var t *time.Timer
loop:
for {
select {
case sig := <-quit:
log.Printf("Received signal: %s, proceeding to shutdown", sig)
break loop
// Publish after every interval has elapsed
case msg, open := <-on:
if !open {
break loop
}
switch msg {
case "1":
if t != nil {
t.Stop()
}
m.Publish(*startTopic, []byte(*startPress), false)
ft.Update("1")
log.Print("Turned on robot")
t = time.AfterFunc(time.Duration(*timeout)*time.Minute,
func() {
ft.Update("0")
log.Print("Timeout expired, setting switch to off")
})
default:
if t != nil {
t.Stop()
}
m.Publish(*stopTopic, []byte(*stopPress), false)
ft.Update("0")
log.Print("Turned off robot")
}
}
}
cancel()
os.Exit(0)
}