-
-
Save pwood/cd95044873a1f35ca0a76d9f185a6758 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"github.com/shimmeringbee/da" | |
"github.com/shimmeringbee/da/capabilities" | |
"github.com/shimmeringbee/zda" | |
"github.com/shimmeringbee/zigbee" | |
"github.com/shimmeringbee/zstack" | |
"go.bug.st/serial.v1" | |
"io/ioutil" | |
"log" | |
"os" | |
"strings" | |
"time" | |
) | |
func main() { | |
home, _ := os.UserHomeDir() | |
configDirectory := strings.Join([]string{home, ".config", "shimmeringbee", "testapp"}, string(os.PathSeparator)) | |
_ = os.MkdirAll(configDirectory, 0600) | |
var nc zigbee.NetworkConfiguration | |
configFile := configDirectory + string(os.PathSeparator) + "network.json" | |
_, err := os.Stat(configFile) | |
if os.IsNotExist(err) { | |
nc, _ = zigbee.GenerateNetworkConfiguration() | |
fmt.Printf("New Config: PAN: %d\n", nc.PANID) | |
data, _ := json.Marshal(nc) | |
ioutil.WriteFile(configFile, data, 0600) | |
} else { | |
configRaw, _ := ioutil.ReadFile(configFile) | |
json.Unmarshal(configRaw, &nc) | |
fmt.Printf("Old Config: PAN: %d\n", nc.PANID) | |
} | |
mode := &serial.Mode{ | |
BaudRate: 115200, | |
} | |
port, err := serial.Open("COM5", mode) | |
if err != nil { | |
log.Fatal(err) | |
} | |
port.SetRTS(true) | |
z := zstack.New(port) | |
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) | |
defer cancel() | |
fmt.Printf("Initialising zStack\n") | |
err = z.Initialise(ctx, nc) | |
if err != nil { | |
fmt.Printf("failed to initialise adapter: %+v", err) | |
} | |
fmt.Printf("Starting zda\n") | |
zda := zda.New(z) | |
err = zda.Start() | |
if err != nil { | |
fmt.Printf("failed to start zigbee device abstraction: %+v\n", err) | |
} | |
fmt.Printf("Enabling Device Discovery\n") | |
err = zda.Capability(capabilities.DeviceDiscoveryFlag).(capabilities.DeviceDiscovery).Enable(context.Background(), zda.Self(), 10*time.Minute) | |
if err != nil { | |
fmt.Printf("failed to enable device discovery: %+v\n", err) | |
} | |
//go func() { | |
// state := true | |
// | |
// for { | |
// time.Sleep(3 * time.Second) | |
// | |
// for _, device := range zda.Devices() { | |
// ctx, done := context.WithTimeout(context.Background(), 100 * time.Millisecond) | |
// | |
// if device.HasCapability(capabilities.OnOffFlag) { | |
// oo := zda.Capability(capabilities.OnOffFlag).(capabilities.OnOff) | |
// | |
// currentState, _ := oo.State(ctx, device) | |
// fmt.Printf("%s: State: %s\n", device.Identifier,currentState) | |
// | |
// if state { | |
// oo.On(ctx, device) | |
// } else { | |
// oo.Off(ctx, device) | |
// } | |
// } | |
// | |
// done() | |
// } | |
// | |
// state = !state | |
// } | |
//}() | |
fmt.Printf("Main loop\n") | |
for { | |
ctx := context.Background() | |
event, err := zda.ReadEvent(ctx) | |
if err != nil { | |
return | |
} | |
switch e := event.(type) { | |
case da.DeviceAdded: | |
fmt.Printf("Device Added: %+v\n", e) | |
case da.DeviceRemoved: | |
fmt.Printf("Device Removed: %+v\n", e) | |
case da.DeviceStateChange: | |
fmt.Printf("Device State Change: %+v\n", e) | |
case capabilities.EnumerateDeviceStart: | |
fmt.Printf("Enumeration of Device Started: %+v\n", e) | |
case capabilities.EnumerateDeviceSuccess: | |
fmt.Printf("Enumeration of Device Successful: %+v\n", e) | |
if e.Device.HasCapability(capabilities.LocalDebugFlag) { | |
e.Gateway.Capability(capabilities.LocalDebugFlag).(capabilities.LocalDebug).Start(ctx, e.Device) | |
} | |
case capabilities.EnumerateDeviceFailure: | |
fmt.Printf("Enumeration of Device Failure: %+v\n", e) | |
case capabilities.DeviceDiscoveryStatus: | |
fmt.Printf("Device Discovery Status: %+v\n", e) | |
case capabilities.DeviceDiscoveryEnabled: | |
fmt.Printf("Device Discovery Enabled: %+v\n", e) | |
case capabilities.DeviceDiscoveryDisabled: | |
fmt.Printf("Device Discovery Disabled: %+v\n", e) | |
case capabilities.LocalDebugSuccess: | |
json, err := json.MarshalIndent(e.Debug, "", "\t") | |
fmt.Printf("Err: %+v\n", err) | |
fmt.Printf("Local Debug:\n%s\n", json) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment