forked from skburgart/go-vacbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmpp.go
73 lines (61 loc) · 1.28 KB
/
xmpp.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
package vacbot
import (
"crypto/tls"
"fmt"
"log"
"time"
xmpp "github.com/mattn/go-xmpp"
)
const (
XMPP_URL = "msg-%s.ecouser.net:5223"
)
type VacbotXMPP struct {
client *xmpp.Client
from string
to string
}
func NewVacbotXMPP(userId, userAccessToken, deviceJID string) *VacbotXMPP {
xmppPassword := fmt.Sprintf("0/%s/%s", config.Resource, userAccessToken)
xmppOpts := xmpp.Options{
Host: getXmppUrl(),
User: fmt.Sprintf("%s@%s", userId, config.Realm),
Password: xmppPassword,
NoTLS: true,
Debug: false,
Session: true,
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
xmppClient, err := xmppOpts.NewClient()
if err != nil {
log.Fatal(err)
}
vx := &VacbotXMPP{
client: xmppClient,
from: xmppClient.JID(),
to: deviceJID,
}
go vx.pinger()
return vx
}
func (vx *VacbotXMPP) issueCommand(command string) {
_, err := vx.client.RawInformationQuery(vx.from, vx.to, "1", xmpp.IQTypeSet, "com:ctl", command)
if err != nil {
log.Fatal(err)
}
}
func (vx *VacbotXMPP) pinger() {
for _ = range time.Tick(30 * time.Second) {
vx.ping()
}
}
func (vx *VacbotXMPP) ping() {
err := vx.client.PingC2S(vx.from, vx.to)
if err != nil {
log.Fatal(err)
}
}
func getXmppUrl() string {
return fmt.Sprintf(XMPP_URL, config.Continent)
}