commit 2021-12-10]17:07:17
This commit is contained in:
parent
8934cbfa4c
commit
2763715d2c
26
cli.go
26
cli.go
@ -13,6 +13,8 @@ var ctx context.Context
|
|||||||
var cancel context.CancelFunc
|
var cancel context.CancelFunc
|
||||||
var ch_discover chan notify.EventInfo
|
var ch_discover chan notify.EventInfo
|
||||||
var _managerObj *_manager
|
var _managerObj *_manager
|
||||||
|
var registerHandleFunc func(uuid string)
|
||||||
|
var removeHandleFunc func(uuid string)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
ctx, cancel = context.WithCancel(context.Background())
|
ctx, cancel = context.WithCancel(context.Background())
|
||||||
@ -24,10 +26,21 @@ func init() {
|
|||||||
_managerObj = &_manager{
|
_managerObj = &_manager{
|
||||||
devicesWithUUID: devWithUUID,
|
devicesWithUUID: devWithUUID,
|
||||||
devicesWithIface: devWithIface,
|
devicesWithIface: devWithIface,
|
||||||
|
chanForSync: chanForSync,
|
||||||
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync},
|
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync},
|
||||||
RecvListener: &RecvHandler{devices: devWithIface, 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() {
|
func Close() {
|
||||||
@ -38,6 +51,10 @@ func Sync(key string, param map[string]interface{}) {
|
|||||||
_managerObj.onSync(key, param)
|
_managerObj.onSync(key, param)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func AddRecvListener(h EventHandler) {
|
||||||
|
_managerObj.addRecvListener(h)
|
||||||
|
}
|
||||||
|
|
||||||
func SetDevicePropsToSync(uuid string, propsToSync []string) error {
|
func SetDevicePropsToSync(uuid string, propsToSync []string) error {
|
||||||
device, ok := _managerObj.devicesWithUUID[uuid]
|
device, ok := _managerObj.devicesWithUUID[uuid]
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -66,6 +83,13 @@ func Run() error {
|
|||||||
log.Println("manager exit")
|
log.Println("manager exit")
|
||||||
return nil
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ func recv(port io.Reader, r Receiver) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
log.Println("USB is disconnected")
|
log.Println("USB is disconnected")
|
||||||
|
_managerObj.onRemoved(port)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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 {
|
for {
|
||||||
fmt.Print("> ")
|
fmt.Print("> ")
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
@ -15,3 +15,7 @@ type Receiver interface {
|
|||||||
type Syncronizer interface {
|
type Syncronizer interface {
|
||||||
onSync(key interface{}, params map[string]interface{})
|
onSync(key interface{}, params map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
10
listener.go
10
listener.go
@ -85,6 +85,7 @@ func (sh *SyncHandler) Handle(e Event) {
|
|||||||
type RecvHandler struct {
|
type RecvHandler struct {
|
||||||
devices map[interface{}]*_device
|
devices map[interface{}]*_device
|
||||||
chanForSync map[string]chan map[string]interface{}
|
chanForSync map[string]chan map[string]interface{}
|
||||||
|
next EventHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rh *RecvHandler) Handle(e Event) {
|
func (rh *RecvHandler) Handle(e Event) {
|
||||||
@ -95,14 +96,19 @@ func (rh *RecvHandler) Handle(e Event) {
|
|||||||
|
|
||||||
param := e.Params()
|
param := e.Params()
|
||||||
code, _ := param["code"].(float64)
|
code, _ := param["code"].(float64)
|
||||||
if int(code) != 100 {
|
if int(code) == 100 {
|
||||||
device.states = param
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
device.states = param
|
||||||
channel, ok := rh.chanForSync[device.IfaceName]
|
channel, ok := rh.chanForSync[device.IfaceName]
|
||||||
if ok {
|
if ok {
|
||||||
channel <- device.states
|
channel <- device.states
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if rh.next != nil {
|
||||||
|
rh.next.Handle(e)
|
||||||
|
}
|
||||||
|
|
||||||
// fmt.Println("recv] ", device.states)
|
// fmt.Println("recv] ", device.states)
|
||||||
}
|
}
|
||||||
|
31
manager.go
31
manager.go
@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"github.com/jacobsa/go-serial/serial"
|
"github.com/jacobsa/go-serial/serial"
|
||||||
@ -12,13 +13,37 @@ import (
|
|||||||
type _manager struct {
|
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{}
|
||||||
|
RegisterListener EventHandler
|
||||||
SyncListener EventHandler
|
SyncListener EventHandler
|
||||||
RecvListener EventHandler
|
RecvListener EventHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *_manager) onRegistered(dev *_device) {
|
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)
|
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) {
|
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})
|
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}
|
||||||
|
}
|
||||||
|
@ -29,7 +29,7 @@ func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) er
|
|||||||
defer close(ch_discover)
|
defer close(ch_discover)
|
||||||
|
|
||||||
filter := make(chan notify.EventInfo, 1)
|
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
|
return err
|
||||||
}
|
}
|
||||||
defer notify.Stop(filter)
|
defer notify.Stop(filter)
|
||||||
|
13
structure.go
13
structure.go
@ -13,3 +13,16 @@ func (es *EventStruct) Params() map[string]interface{} {
|
|||||||
func (es *EventStruct) Key() interface{} {
|
func (es *EventStruct) Key() interface{} {
|
||||||
return es.key
|
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}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user