Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
5fa75efee5 | |||
652d936d83 |
2
cli.go
2
cli.go
@ -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},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,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 {
|
||||||
@ -69,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)
|
||||||
@ -90,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)
|
||||||
@ -98,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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user