commit 2021-12-10]17:07:17

This commit is contained in:
Godopu 2021-12-10 17:07:20 +09:00
parent 8934cbfa4c
commit 2763715d2c
8 changed files with 94 additions and 6 deletions

26
cli.go
View File

@ -13,6 +13,8 @@ var ctx context.Context
var cancel context.CancelFunc
var ch_discover chan notify.EventInfo
var _managerObj *_manager
var registerHandleFunc func(uuid string)
var removeHandleFunc func(uuid string)
func init() {
ctx, cancel = context.WithCancel(context.Background())
@ -24,10 +26,21 @@ func init() {
_managerObj = &_manager{
devicesWithUUID: devWithUUID,
devicesWithIface: devWithIface,
chanForSync: chanForSync,
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync},
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
}
registerHandleFunc = nil
removeHandleFunc = nil
}
func AddRegisterHandleFunc(h func(uuid string)) {
registerHandleFunc = h
}
func AddRemoveHandleFunc(h func(uuid string)) {
removeHandleFunc = h
}
func Close() {
@ -38,6 +51,10 @@ func Sync(key string, param map[string]interface{}) {
_managerObj.onSync(key, param)
}
func AddRecvListener(h EventHandler) {
_managerObj.addRecvListener(h)
}
func SetDevicePropsToSync(uuid string, propsToSync []string) error {
device, ok := _managerObj.devicesWithUUID[uuid]
if !ok {
@ -66,6 +83,13 @@ func Run() error {
log.Println("manager exit")
return nil
}
_managerObj.onAdded(e.Path())
switch e.Event() {
case notify.Create:
_managerObj.onAdded(e.Path())
// case notify.Remove:
// log.Println("USB Disconnected!!")
// _managerObj.onAdded(e.Path())
}
}
}

View File

@ -15,6 +15,7 @@ func recv(port io.Reader, r Receiver) {
if err != nil {
if err == io.EOF {
log.Println("USB is disconnected")
_managerObj.onRemoved(port)
return
}
}

View File

@ -11,8 +11,19 @@ import (
)
func main() {
go manager.Run()
manager.AddRecvListener(manager.NewEventHandler(func(e manager.Event) {
fmt.Println("RECV: ", e.Params())
}))
manager.AddRegisterHandleFunc(func(dname string) {
fmt.Println(dname, " is registered!!")
})
manager.AddRemoveHandleFunc(func(dname string) {
fmt.Println(dname, " is removed!!")
})
go manager.Run()
defer manager.Close()
for {
fmt.Print("> ")
reader := bufio.NewReader(os.Stdin)

View File

@ -15,3 +15,7 @@ type Receiver interface {
type Syncronizer interface {
onSync(key interface{}, params map[string]interface{})
}

View File

@ -85,6 +85,7 @@ func (sh *SyncHandler) Handle(e Event) {
type RecvHandler struct {
devices map[interface{}]*_device
chanForSync map[string]chan map[string]interface{}
next EventHandler
}
func (rh *RecvHandler) Handle(e Event) {
@ -95,14 +96,19 @@ func (rh *RecvHandler) Handle(e Event) {
param := e.Params()
code, _ := param["code"].(float64)
if int(code) != 100 {
device.states = param
if int(code) == 100 {
return
}
device.states = param
channel, ok := rh.chanForSync[device.IfaceName]
if ok {
channel <- device.states
}
if rh.next != nil {
rh.next.Handle(e)
}
// fmt.Println("recv] ", device.states)
}

View File

@ -4,6 +4,7 @@ import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"github.com/jacobsa/go-serial/serial"
@ -12,13 +13,37 @@ import (
type _manager struct {
devicesWithUUID map[string]*_device
devicesWithIface map[interface{}]*_device
chanForSync map[string]chan map[string]interface{}
RegisterListener EventHandler
SyncListener EventHandler
RecvListener EventHandler
}
func (m *_manager) onRegistered(dev *_device) {
// if m.RegisterListener != nil {
// m.RegisterListener.Handle(&EventStruct{key: })
// }
if registerHandleFunc != nil {
registerHandleFunc(dev.UUID)
}
go recv(dev.Iface, m)
log.Println("> End onRegistered")
}
func (m *_manager) onRemoved(port io.Reader) {
dev := _managerObj.devicesWithIface[port]
delete(m.devicesWithUUID, dev.UUID)
delete(m.devicesWithIface, port)
ch, ok := m.chanForSync[dev.UUID]
if ok {
close(ch)
delete(m.chanForSync, dev.UUID)
}
if removeHandleFunc != nil {
removeHandleFunc(dev.UUID)
}
// log.Println(m.devicesWithUUID)
// log.Println(m.devicesWithIface)
}
func (m *_manager) onAdded(iface string) {
@ -92,3 +117,7 @@ func (m *_manager) onRecv(key interface{}, params map[string]interface{}) {
m.RecvListener.Handle(&EventStruct{key: key, params: params})
}
}
func (m *_manager) addRecvListener(h EventHandler) {
m.RecvListener = &RecvHandler{next: h, devices: m.devicesWithIface, chanForSync: m.chanForSync}
}

View File

@ -29,7 +29,7 @@ func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) er
defer close(ch_discover)
filter := make(chan notify.EventInfo, 1)
if err := notify.Watch("/dev", filter, notify.Create, notify.Remove); err != nil {
if err := notify.Watch("/dev", filter, notify.Create); err != nil {
return err
}
defer notify.Stop(filter)

View File

@ -13,3 +13,16 @@ func (es *EventStruct) Params() map[string]interface{} {
func (es *EventStruct) Key() interface{} {
return es.key
}
type EventHandlerStruct struct {
EventHandler
HandleFunc func(e Event)
}
func (ehs *EventHandlerStruct) Handle(e Event) {
ehs.HandleFunc(e)
}
func NewEventHandler(h func(e Event)) *EventHandlerStruct {
return &EventHandlerStruct{HandleFunc: h}
}