2021-12-10 01:14:34 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2021-12-12 04:48:55 +00:00
|
|
|
"sync"
|
2021-12-10 01:14:34 +00:00
|
|
|
|
|
|
|
"git.godopu.net/lab/etri-smartfarm-poc-controller-serial/puserial"
|
|
|
|
"github.com/rjeczalik/notify"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ctx context.Context
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
var ch_discover chan notify.EventInfo
|
|
|
|
var _managerObj *_manager
|
2021-12-10 10:26:13 +00:00
|
|
|
var registerHandleFunc func(e Event)
|
|
|
|
var removeHandleFunc func(e Event)
|
2021-12-10 01:14:34 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
ctx, cancel = context.WithCancel(context.Background())
|
|
|
|
ch_discover = make(chan notify.EventInfo)
|
2021-12-10 06:45:28 +00:00
|
|
|
devWithUUID := map[string]*_device{}
|
|
|
|
devWithIface := map[interface{}]*_device{}
|
|
|
|
chanForSync := map[string]chan map[string]interface{}{}
|
|
|
|
|
2021-12-10 01:14:34 +00:00
|
|
|
_managerObj = &_manager{
|
2021-12-10 06:45:28 +00:00
|
|
|
devicesWithUUID: devWithUUID,
|
|
|
|
devicesWithIface: devWithIface,
|
2021-12-10 08:07:20 +00:00
|
|
|
chanForSync: chanForSync,
|
2021-12-12 06:18:56 +00:00
|
|
|
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &sync.Mutex{}},
|
|
|
|
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
2021-12-10 06:45:28 +00:00
|
|
|
|
2021-12-10 08:07:20 +00:00
|
|
|
registerHandleFunc = nil
|
|
|
|
removeHandleFunc = nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 10:26:13 +00:00
|
|
|
func AddRegisterHandleFunc(h func(e Event)) {
|
2021-12-10 08:07:20 +00:00
|
|
|
registerHandleFunc = h
|
|
|
|
}
|
|
|
|
|
2021-12-10 10:26:13 +00:00
|
|
|
func AddRemoveHandleFunc(h func(e Event)) {
|
2021-12-10 08:07:20 +00:00
|
|
|
removeHandleFunc = h
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Close() {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:45:28 +00:00
|
|
|
func Sync(key string, param map[string]interface{}) {
|
|
|
|
_managerObj.onSync(key, param)
|
|
|
|
}
|
2021-12-10 01:14:34 +00:00
|
|
|
|
2021-12-10 08:07:20 +00:00
|
|
|
func AddRecvListener(h EventHandler) {
|
|
|
|
_managerObj.addRecvListener(h)
|
|
|
|
}
|
|
|
|
|
2021-12-12 01:08:11 +00:00
|
|
|
// func SetDevicePropsToSync(uuid string, propsToSync []string) error {
|
|
|
|
// device, ok := _managerObj.devicesWithUUID[uuid]
|
|
|
|
// if !ok {
|
|
|
|
// return errors.New("device not found")
|
|
|
|
// }
|
|
|
|
// device.propsToSync = propsToSync
|
|
|
|
// return nil
|
|
|
|
// }
|
2021-12-10 01:14:34 +00:00
|
|
|
|
|
|
|
func Run() error {
|
2021-12-16 02:54:57 +00:00
|
|
|
ifaces, err := puserial.InitDevice()
|
2021-12-10 01:14:34 +00:00
|
|
|
if err != nil {
|
2021-12-16 02:54:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, e := range ifaces {
|
2021-12-16 04:30:07 +00:00
|
|
|
go _managerObj.onAdded(e)
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go puserial.WatchNewDevice(ctx, ch_discover)
|
|
|
|
|
|
|
|
for {
|
|
|
|
e, ok := <-ch_discover
|
|
|
|
if !ok {
|
|
|
|
log.Println("manager exit")
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-10 08:07:20 +00:00
|
|
|
switch e.Event() {
|
|
|
|
case notify.Create:
|
2021-12-16 04:30:07 +00:00
|
|
|
go _managerObj.onAdded(e.Path())
|
2021-12-10 08:07:20 +00:00
|
|
|
// case notify.Remove:
|
|
|
|
// log.Println("USB Disconnected!!")
|
|
|
|
// _managerObj.onAdded(e.Path())
|
|
|
|
}
|
|
|
|
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
|
|
|
}
|