Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
5fa75efee5 | |||
652d936d83 | |||
0f1e5e610e | |||
2a77d039a0 |
2
cli.go
2
cli.go
@ -27,7 +27,7 @@ func init() {
|
||||
devicesWithUUID: devWithUUID,
|
||||
devicesWithIface: devWithIface,
|
||||
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},
|
||||
}
|
||||
|
||||
|
28
listener.go
28
listener.go
@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@ -11,6 +12,7 @@ type SyncHandler struct {
|
||||
devices map[string]*_device
|
||||
mutex *sync.Mutex
|
||||
chanForSync map[string]chan map[string]interface{}
|
||||
states map[string]map[string]interface{}
|
||||
}
|
||||
|
||||
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" {
|
||||
continue
|
||||
}
|
||||
srcV, ok := value.(int)
|
||||
if !ok {
|
||||
srcV = int(value.(float64))
|
||||
|
||||
if reflect.TypeOf(value).String() == "string" {
|
||||
dstV, ok := dst[key].(string)
|
||||
if !ok || value != dstV {
|
||||
return false
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
dstV, ok := dst[key].(int)
|
||||
srcV, ok := value.(float64)
|
||||
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 {
|
||||
@ -59,6 +70,7 @@ func (sh *SyncHandler) Handle(e Event) {
|
||||
origin[key] = value
|
||||
}
|
||||
|
||||
sh.states[device.UUID] = origin
|
||||
// props := []string{"fan", "light", "servo"}
|
||||
|
||||
err := encoder.Encode(origin)
|
||||
@ -80,7 +92,7 @@ func (sh *SyncHandler) Handle(e Event) {
|
||||
sh.mutex.Unlock()
|
||||
|
||||
for state := range chanForSync {
|
||||
if compareMap(origin, state) {
|
||||
if compareMap(sh.states[device.UUID], state) {
|
||||
sh.mutex.Lock()
|
||||
close(sh.chanForSync[device.IfaceName])
|
||||
delete(sh.chanForSync, device.IfaceName)
|
||||
@ -88,8 +100,8 @@ func (sh *SyncHandler) Handle(e Event) {
|
||||
return
|
||||
}
|
||||
log.Println("wrong: ", state)
|
||||
log.Println("resend: ", origin)
|
||||
err := encoder.Encode(origin)
|
||||
log.Println("resend: ", sh.states[device.UUID])
|
||||
err := encoder.Encode(sh.states[device.UUID])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user