Compare commits

...

5 Commits
v0.1.7 ... main

Author SHA1 Message Date
5fa75efee5 commit 2021-12-16]17:46:11 2021-12-16 17:46:13 +09:00
652d936d83 commit 2021-12-16]17:42:53 2021-12-16 17:42:57 +09:00
0f1e5e610e commit 2021-12-16]17:34:06 2021-12-16 17:34:08 +09:00
2a77d039a0 commit 2021-12-16]15:57:46 2021-12-16 15:57:48 +09:00
ce91a11c5a commit 2021-12-16]13:30:04 2021-12-16 13:30:07 +09:00
2 changed files with 23 additions and 11 deletions

6
cli.go
View File

@ -27,7 +27,7 @@ func init() {
devicesWithUUID: devWithUUID, devicesWithUUID: devWithUUID,
devicesWithIface: devWithIface, devicesWithIface: devWithIface,
chanForSync: chanForSync, chanForSync: chanForSync,
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &sync.Mutex{}}, SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &sync.Mutex{}, states: map[string]map[string]interface{}{}},
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync}, RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
} }
@ -71,7 +71,7 @@ func Run() error {
} }
for _, e := range ifaces { for _, e := range ifaces {
_managerObj.onAdded(e) go _managerObj.onAdded(e)
} }
go puserial.WatchNewDevice(ctx, ch_discover) go puserial.WatchNewDevice(ctx, ch_discover)
@ -84,7 +84,7 @@ func Run() error {
} }
switch e.Event() { switch e.Event() {
case notify.Create: case notify.Create:
_managerObj.onAdded(e.Path()) go _managerObj.onAdded(e.Path())
// case notify.Remove: // case notify.Remove:
// log.Println("USB Disconnected!!") // log.Println("USB Disconnected!!")
// _managerObj.onAdded(e.Path()) // _managerObj.onAdded(e.Path())

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"reflect"
"sync" "sync"
) )
@ -11,6 +12,7 @@ type SyncHandler struct {
devices map[string]*_device devices map[string]*_device
mutex *sync.Mutex mutex *sync.Mutex
chanForSync map[string]chan map[string]interface{} chanForSync map[string]chan map[string]interface{}
states map[string]map[string]interface{}
} }
func compareMap(src map[string]interface{}, dst map[string]interface{}) bool { func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
@ -19,14 +21,23 @@ func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
if key == "code" { if key == "code" {
continue continue
} }
srcV, ok := value.(int)
if !ok { if reflect.TypeOf(value).String() == "string" {
srcV = int(value.(float64)) dstV, ok := dst[key].(string)
if !ok || value != dstV {
return false
}
continue
} }
dstV, ok := dst[key].(int) srcV, ok := value.(float64)
if !ok { if !ok {
dstV = int(dst[key].(float64)) srcV = float64(value.(int))
}
dstV, ok := dst[key].(float64)
if !ok {
dstV = float64(dst[key].(int))
} }
if srcV != dstV { if srcV != dstV {
@ -59,6 +70,7 @@ func (sh *SyncHandler) Handle(e Event) {
origin[key] = value origin[key] = value
} }
sh.states[device.UUID] = origin
// props := []string{"fan", "light", "servo"} // props := []string{"fan", "light", "servo"}
err := encoder.Encode(origin) err := encoder.Encode(origin)
@ -80,7 +92,7 @@ func (sh *SyncHandler) Handle(e Event) {
sh.mutex.Unlock() sh.mutex.Unlock()
for state := range chanForSync { for state := range chanForSync {
if compareMap(origin, state) { if compareMap(sh.states[device.UUID], state) {
sh.mutex.Lock() sh.mutex.Lock()
close(sh.chanForSync[device.IfaceName]) close(sh.chanForSync[device.IfaceName])
delete(sh.chanForSync, device.IfaceName) delete(sh.chanForSync, device.IfaceName)
@ -88,8 +100,8 @@ func (sh *SyncHandler) Handle(e Event) {
return return
} }
log.Println("wrong: ", state) log.Println("wrong: ", state)
log.Println("resend: ", origin) log.Println("resend: ", sh.states[device.UUID])
err := encoder.Encode(origin) err := encoder.Encode(sh.states[device.UUID])
if err != nil { if err != nil {
return return
} }