etri-smartfarm-poc-controll.../cli.go

72 lines
1.5 KiB
Go

package manager
import (
"context"
"errors"
"log"
"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
func init() {
ctx, cancel = context.WithCancel(context.Background())
ch_discover = make(chan notify.EventInfo)
devWithUUID := map[string]*_device{}
devWithIface := map[interface{}]*_device{}
chanForSync := map[string]chan map[string]interface{}{}
_managerObj = &_manager{
devicesWithUUID: devWithUUID,
devicesWithIface: devWithIface,
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync},
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
}
}
func Close() {
cancel()
}
func Sync(key string, param map[string]interface{}) {
_managerObj.onSync(key, param)
}
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
}
func Run() error {
iface, err := puserial.InitDevice()
if err != nil {
if err.Error() != "USB Not found" {
return err
}
} else {
// _managerObj.onAdd(iface)
_managerObj.onAdded(iface)
}
go puserial.WatchNewDevice(ctx, ch_discover)
for {
e, ok := <-ch_discover
if !ok {
log.Println("manager exit")
return nil
}
_managerObj.onAdded(e.Path())
}
}