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{} devWithUUID := map[string]*_device{}
devWithIface := map[interface{}]*_device{} devWithIface := map[interface{}]*_device{}
chanForSync := map[string]chan map[string]interface{}{} chanForSync := map[string]chan map[string]interface{}{}
var mutex = sync.Mutex{}
_managerObj = &_manager{ _managerObj = &_manager{
devicesWithUUID: devWithUUID, devicesWithUUID: devWithUUID,
devicesWithIface: devWithIface, devicesWithIface: devWithIface,
chanForSync: chanForSync, chanForSync: chanForSync,
mutex: &mutex, SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &sync.Mutex{}},
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &mutex}, RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync, mutex: &sync.Mutex{}},
} }
registerHandleFunc = nil 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) { func (sh *SyncHandler) Handle(e Event) {
device, ok := sh.devices[e.Key().(string)] device, ok := sh.devices[e.Key().(string)]
// fmt.Println("sync] ", device.IfaceName) // fmt.Println("sync] ", device.IfaceName)
// fmt.Println(sh.devices) // fmt.Println(sh.devices)
@ -72,18 +71,20 @@ func (sh *SyncHandler) Handle(e Event) {
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)
sh.mutex.Lock() sh.mutex.Unlock()
} }
chanForSync := make(chan map[string]interface{}) 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 { for state := range chanForSync {
if compareMap(origin, state) { if compareMap(origin, 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)
sh.mutex.Lock() sh.mutex.Unlock()
return return
} }
log.Println("wrong: ", state) log.Println("wrong: ", state)
@ -93,18 +94,22 @@ func (sh *SyncHandler) Handle(e Event) {
return return
} }
} }
log.Println("sync routine is died")
}() }()
} }
type RecvHandler struct { type RecvHandler struct {
devices map[interface{}]*_device devices map[interface{}]*_device
mutex *sync.Mutex
chanForSync map[string]chan map[string]interface{} chanForSync map[string]chan map[string]interface{}
next EventHandler next EventHandler
} }
func (rh *RecvHandler) Handle(e Event) { func (rh *RecvHandler) Handle(e Event) {
defer func() {
if r := recover(); r != nil {
fmt.Println("panic recover - ", r)
}
}()
device, ok := rh.devices[e.Key()] device, ok := rh.devices[e.Key()]
if !ok { if !ok {
return return
@ -117,9 +122,7 @@ func (rh *RecvHandler) Handle(e Event) {
} }
device.states = param device.states = param
rh.mutex.Lock()
channel, ok := rh.chanForSync[device.IfaceName] channel, ok := rh.chanForSync[device.IfaceName]
rh.mutex.Unlock()
if ok { if ok {
channel <- device.states channel <- device.states
} }

View File

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