-
Notifications
You must be signed in to change notification settings - Fork 10
/
test-connection.go
183 lines (141 loc) · 4.81 KB
/
test-connection.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
180
181
182
183
// +build ignore
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
zigbee "github.com/ninjasphere/go-zigbee"
"github.com/ninjasphere/go-zigbee/nwkmgr"
"github.com/ninjasphere/go-zigbee/gateway"
)
const (
hostname = "beaglebone.local"
otasrvrPort = 2525
gatewayPort = 2541
nwkmgrPort = 2540
)
func main() {
log.Println("Starting")
_, err := zigbee.ConnectToOtaServer(hostname, otasrvrPort)
if err != nil {
// handle error
log.Printf("Error connecting otasrvr %s", err)
}
nwkmgrConn, err := zigbee.ConnectToNwkMgrServer(hostname, nwkmgrPort)
if err != nil {
// handle error
log.Printf("Error connecting nwkmgr %s", err)
}
gatewayConn, err := zigbee.ConnectToGatewayServer(hostname, gatewayPort)
if err != nil {
// handle error
log.Printf("Error connecting nwkmgr %s", err)
}
//gatewayConn.pendingResponses = make(map[uint8]*pendingGatewayResponse)
//command := &nwkmgr.NwkZigbeeNwkInfoReq{}
response := &nwkmgr.NwkGetLocalDeviceInfoCnf{}
err = nwkmgrConn.SendCommand(&nwkmgr.NwkGetLocalDeviceInfoReq{}, response)
if err != nil {
log.Fatalf("Failed to get local device info: %s", err)
}
log.Println("Local device info: ")
outJSON(response)
joinTime := uint32(30)
permitJoinRequest := &nwkmgr.NwkSetPermitJoinReq{
PermitJoinTime: &joinTime,
PermitJoin: nwkmgr.NwkPermitJoinTypeT_PERMIT_ALL.Enum(),
}
permitJoinResponse := &nwkmgr.NwkZigbeeGenericCnf{}
err = nwkmgrConn.SendCommand(permitJoinRequest, permitJoinResponse)
if err != nil {
log.Fatalf("Failed to enable joining: %s", err)
}
if permitJoinResponse.Status.String() != "STATUS_SUCCESS" {
log.Fatalf("Failed to enable joining: %s", permitJoinResponse.Status)
}
log.Println("Permit join response: ")
outJSON(permitJoinResponse)
deviceListResponse := &nwkmgr.NwkGetDeviceListCnf{}
err = nwkmgrConn.SendCommand(&nwkmgr.NwkGetDeviceListReq{}, deviceListResponse)
if err != nil {
log.Fatalf("Failed to get device list: %s", err)
}
log.Printf("Found %d device(s): ", len(deviceListResponse.DeviceList))
outJSON(deviceListResponse)
for _, device := range deviceListResponse.DeviceList {
log.Printf("Got device : %d", device.IeeeAddress)
for _, endpoint := range device.SimpleDescList {
log.Printf("Got endpoint : %d", endpoint.EndpointId)
if containsUInt32(endpoint.InputClusters, 0x06) {
log.Printf("This endpoint has on/off cluster")
toggleRequest := &gateway.DevSetOnOffStateReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: device.IeeeAddress,
},
State: gateway.GwOnOffStateT_TOGGLE_STATE.Enum(),
}
powerRequest := &gateway.DevGetPowerReq{
DstAddress: &gateway.GwAddressStructT{
AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
IeeeAddr: device.IeeeAddress,
},
}
for i := 0; i < 3; i++ {
log.Println("Toggling on/off device")
confirmation := &gateway.GwZigbeeGenericCnf{}
err = gatewayConn.SendCommand(toggleRequest, confirmation)
if err != nil {
log.Fatalf("Failed to toggle device: ", err)
}
log.Printf("Got on/off confirmation")
if confirmation.Status.String() != "STATUS_SUCCESS" {
log.Fatalf("Failed to request the device to toggle. Status:%s", confirmation.Status.String())
}
response := &gateway.GwZigbeeGenericRspInd{}
err = gatewayConn.WaitForSequenceResponse(confirmation.SequenceNumber, response)
if err != nil {
log.Fatalf("Failed to get on/off response: ", err)
}
log.Printf("Got toggle response from device! Status: %s", response.Status.String())
//time.Sleep(10000 * time.Millisecond)
confirmation = &gateway.GwZigbeeGenericCnf{}
err = gatewayConn.SendCommand(powerRequest, confirmation)
if err != nil {
log.Fatalf("Failed to request power: ", err)
}
log.Printf("Got power request confirmation")
if confirmation.Status.String() != "STATUS_SUCCESS" {
log.Fatalf("Failed to request the power. Status:%s", confirmation.Status.String())
}
powerResponse := &gateway.DevGetPowerRspInd{}
err = gatewayConn.WaitForSequenceResponse(confirmation.SequenceNumber, powerResponse)
if err != nil {
log.Fatalf("Failed to get power response: ", err)
}
// log.Printf("Got power level from device! Status: %s", powerResponse.Status.String())
outJSON(powerResponse)
}
}
}
//GwOnOffStateT_TOGGLE_STATE
}
//time.Sleep(2000 * time.Millisecond)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
log.Infof("Got signal:", <-c)
}
func containsUInt32(hackstack []uint32, needle uint32) bool {
for _, cluster := range hackstack {
if cluster == needle {
return true
}
}
return false
}
func outJSON(thing interface{}) {
jsonOut, _ := json.Marshal(thing)
log.Printf("%s", jsonOut)
}