commit 2021-12-12]15:18:52

This commit is contained in:
Godopu 2021-12-12 15:18:56 +09:00
parent a0ff2d6f52
commit ab08e7ab15
3 changed files with 15 additions and 16 deletions

6
cli.go
View File

@ -22,15 +22,13 @@ func init() {
devWithUUID := map[string]*_device{}
devWithIface := map[interface{}]*_device{}
chanForSync := map[string]chan map[string]interface{}{}
var mutex = sync.Mutex{}
_managerObj = &_manager{
devicesWithUUID: devWithUUID,
devicesWithIface: devWithIface,
chanForSync: chanForSync,
mutex: &mutex,
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &mutex},
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync, mutex: &sync.Mutex{}},
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &sync.Mutex{}},
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
}
registerHandleFunc = nil

View File

@ -39,7 +39,6 @@ func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
}
func (sh *SyncHandler) Handle(e Event) {
device, ok := sh.devices[e.Key().(string)]
// fmt.Println("sync] ", device.IfaceName)
// fmt.Println(sh.devices)
@ -72,18 +71,20 @@ func (sh *SyncHandler) Handle(e Event) {
sh.mutex.Lock()
close(sh.chanForSync[device.IfaceName])
delete(sh.chanForSync, device.IfaceName)
sh.mutex.Lock()
sh.mutex.Unlock()
}
chanForSync := make(chan map[string]interface{})
sh.chanForSync[device.IfaceName] = make(chan map[string]interface{})
sh.mutex.Lock()
sh.chanForSync[device.IfaceName] = chanForSync
sh.mutex.Unlock()
for state := range chanForSync {
if compareMap(origin, state) {
sh.mutex.Lock()
close(sh.chanForSync[device.IfaceName])
delete(sh.chanForSync, device.IfaceName)
sh.mutex.Lock()
sh.mutex.Unlock()
return
}
log.Println("wrong: ", state)
@ -93,18 +94,22 @@ func (sh *SyncHandler) Handle(e Event) {
return
}
}
log.Println("sync routine is died")
}()
}
type RecvHandler struct {
devices map[interface{}]*_device
mutex *sync.Mutex
chanForSync map[string]chan map[string]interface{}
next EventHandler
}
func (rh *RecvHandler) Handle(e Event) {
defer func() {
if r := recover(); r != nil {
fmt.Println("panic recover - ", r)
}
}()
device, ok := rh.devices[e.Key()]
if !ok {
return
@ -117,9 +122,7 @@ func (rh *RecvHandler) Handle(e Event) {
}
device.states = param
rh.mutex.Lock()
channel, ok := rh.chanForSync[device.IfaceName]
rh.mutex.Unlock()
if ok {
channel <- device.states
}

View File

@ -6,7 +6,6 @@ import (
"fmt"
"io"
"log"
"sync"
"github.com/jacobsa/go-serial/serial"
)
@ -15,7 +14,6 @@ type _manager struct {
devicesWithUUID map[string]*_device
devicesWithIface map[interface{}]*_device
chanForSync map[string]chan map[string]interface{}
mutex *sync.Mutex
RegisterListener EventHandler
SyncListener EventHandler
RecvListener EventHandler
@ -129,5 +127,5 @@ func (m *_manager) onRecv(key interface{}, params map[string]interface{}) {
}
func (m *_manager) addRecvListener(h EventHandler) {
m.RecvListener = &RecvHandler{next: h, devices: m.devicesWithIface, chanForSync: m.chanForSync, mutex: m.mutex}
m.RecvListener = &RecvHandler{next: h, devices: m.devicesWithIface, chanForSync: m.chanForSync}
}