diff --git a/cli.go b/cli.go index 81fecf4..10f474f 100644 --- a/cli.go +++ b/cli.go @@ -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()) + } + } } diff --git a/communication.go b/communication.go index 0ef8a48..ad05cce 100644 --- a/communication.go +++ b/communication.go @@ -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 } } diff --git a/examples/main.go b/examples/main.go index c0cf7a0..ad80cf4 100644 --- a/examples/main.go +++ b/examples/main.go @@ -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) diff --git a/interface.go b/interface.go index 92f0d32..9f024b6 100644 --- a/interface.go +++ b/interface.go @@ -15,3 +15,7 @@ type Receiver interface { type Syncronizer interface { onSync(key interface{}, params map[string]interface{}) } + + + + diff --git a/listener.go b/listener.go index 131e179..2fe4e14 100644 --- a/listener.go +++ b/listener.go @@ -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) } diff --git a/manager.go b/manager.go index 74e244b..0fc35af 100644 --- a/manager.go +++ b/manager.go @@ -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} +} diff --git a/puserial/serial.go b/puserial/serial.go index 155aed4..a1ccc07 100644 --- a/puserial/serial.go +++ b/puserial/serial.go @@ -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) diff --git a/structure.go b/structure.go index 0e29c27..27a90e1 100644 --- a/structure.go +++ b/structure.go @@ -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} +}