Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
5fa75efee5 | |||
652d936d83 | |||
0f1e5e610e | |||
2a77d039a0 | |||
ce91a11c5a | |||
37705d6fe5 | |||
ab08e7ab15 | |||
a0ff2d6f52 | |||
31b00fb912 |
18
cli.go
18
cli.go
@ -3,6 +3,7 @@ package manager
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"git.godopu.net/lab/etri-smartfarm-poc-controller-serial/puserial"
|
"git.godopu.net/lab/etri-smartfarm-poc-controller-serial/puserial"
|
||||||
"github.com/rjeczalik/notify"
|
"github.com/rjeczalik/notify"
|
||||||
@ -26,7 +27,7 @@ func init() {
|
|||||||
devicesWithUUID: devWithUUID,
|
devicesWithUUID: devWithUUID,
|
||||||
devicesWithIface: devWithIface,
|
devicesWithIface: devWithIface,
|
||||||
chanForSync: chanForSync,
|
chanForSync: chanForSync,
|
||||||
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync},
|
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &sync.Mutex{}, states: map[string]map[string]interface{}{}},
|
||||||
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
|
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,14 +65,13 @@ func AddRecvListener(h EventHandler) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
func Run() error {
|
func Run() error {
|
||||||
iface, err := puserial.InitDevice()
|
ifaces, err := puserial.InitDevice()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() != "USB Not found" {
|
return err
|
||||||
return err
|
}
|
||||||
}
|
|
||||||
} else {
|
for _, e := range ifaces {
|
||||||
// _managerObj.onAdd(iface)
|
go _managerObj.onAdded(e)
|
||||||
_managerObj.onAdded(iface)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
go puserial.WatchNewDevice(ctx, ch_discover)
|
go puserial.WatchNewDevice(ctx, ch_discover)
|
||||||
@ -84,7 +84,7 @@ func Run() error {
|
|||||||
}
|
}
|
||||||
switch e.Event() {
|
switch e.Event() {
|
||||||
case notify.Create:
|
case notify.Create:
|
||||||
_managerObj.onAdded(e.Path())
|
go _managerObj.onAdded(e.Path())
|
||||||
// case notify.Remove:
|
// case notify.Remove:
|
||||||
// log.Println("USB Disconnected!!")
|
// log.Println("USB Disconnected!!")
|
||||||
// _managerObj.onAdded(e.Path())
|
// _managerObj.onAdded(e.Path())
|
||||||
|
51
listener.go
51
listener.go
@ -4,11 +4,15 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"reflect"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SyncHandler struct {
|
type SyncHandler struct {
|
||||||
devices map[string]*_device
|
devices map[string]*_device
|
||||||
|
mutex *sync.Mutex
|
||||||
chanForSync map[string]chan map[string]interface{}
|
chanForSync map[string]chan map[string]interface{}
|
||||||
|
states map[string]map[string]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
|
func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
|
||||||
@ -17,14 +21,23 @@ func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
|
|||||||
if key == "code" {
|
if key == "code" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
srcV, ok := value.(int)
|
|
||||||
if !ok {
|
if reflect.TypeOf(value).String() == "string" {
|
||||||
srcV = int(value.(float64))
|
dstV, ok := dst[key].(string)
|
||||||
|
if !ok || value != dstV {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
dstV, ok := dst[key].(int)
|
srcV, ok := value.(float64)
|
||||||
if !ok {
|
if !ok {
|
||||||
dstV = int(dst[key].(float64))
|
srcV = float64(value.(int))
|
||||||
|
}
|
||||||
|
|
||||||
|
dstV, ok := dst[key].(float64)
|
||||||
|
if !ok {
|
||||||
|
dstV = float64(dst[key].(int))
|
||||||
}
|
}
|
||||||
|
|
||||||
if srcV != dstV {
|
if srcV != dstV {
|
||||||
@ -37,7 +50,6 @@ func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sh *SyncHandler) Handle(e Event) {
|
func (sh *SyncHandler) Handle(e Event) {
|
||||||
|
|
||||||
device, ok := sh.devices[e.Key().(string)]
|
device, ok := sh.devices[e.Key().(string)]
|
||||||
// fmt.Println("sync] ", device.IfaceName)
|
// fmt.Println("sync] ", device.IfaceName)
|
||||||
// fmt.Println(sh.devices)
|
// fmt.Println(sh.devices)
|
||||||
@ -58,6 +70,7 @@ func (sh *SyncHandler) Handle(e Event) {
|
|||||||
origin[key] = value
|
origin[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sh.states[device.UUID] = origin
|
||||||
// props := []string{"fan", "light", "servo"}
|
// props := []string{"fan", "light", "servo"}
|
||||||
|
|
||||||
err := encoder.Encode(origin)
|
err := encoder.Encode(origin)
|
||||||
@ -67,24 +80,33 @@ func (sh *SyncHandler) Handle(e Event) {
|
|||||||
|
|
||||||
_, ok := sh.chanForSync[device.IfaceName]
|
_, ok := sh.chanForSync[device.IfaceName]
|
||||||
if ok {
|
if ok {
|
||||||
|
sh.mutex.Lock()
|
||||||
close(sh.chanForSync[device.IfaceName])
|
close(sh.chanForSync[device.IfaceName])
|
||||||
|
delete(sh.chanForSync, device.IfaceName)
|
||||||
|
sh.mutex.Unlock()
|
||||||
}
|
}
|
||||||
chanForSync := make(chan map[string]interface{})
|
|
||||||
sh.chanForSync[device.IfaceName] = make(chan map[string]interface{})
|
|
||||||
for state := range chanForSync {
|
|
||||||
|
|
||||||
if compareMap(origin, state) {
|
chanForSync := make(chan map[string]interface{})
|
||||||
|
sh.mutex.Lock()
|
||||||
|
sh.chanForSync[device.IfaceName] = chanForSync
|
||||||
|
sh.mutex.Unlock()
|
||||||
|
|
||||||
|
for state := range chanForSync {
|
||||||
|
if compareMap(sh.states[device.UUID], state) {
|
||||||
|
sh.mutex.Lock()
|
||||||
close(sh.chanForSync[device.IfaceName])
|
close(sh.chanForSync[device.IfaceName])
|
||||||
delete(sh.chanForSync, device.IfaceName)
|
delete(sh.chanForSync, device.IfaceName)
|
||||||
|
sh.mutex.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println("wrong: ", state)
|
log.Println("wrong: ", state)
|
||||||
log.Println("resend: ", origin)
|
log.Println("resend: ", sh.states[device.UUID])
|
||||||
err := encoder.Encode(origin)
|
err := encoder.Encode(sh.states[device.UUID])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,6 +117,11 @@ type RecvHandler struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rh *RecvHandler) Handle(e Event) {
|
func (rh *RecvHandler) Handle(e Event) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
fmt.Println("panic recover - ", r)
|
||||||
|
}
|
||||||
|
}()
|
||||||
device, ok := rh.devices[e.Key()]
|
device, ok := rh.devices[e.Key()]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
|
@ -2,7 +2,6 @@ package puserial
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -11,19 +10,20 @@ import (
|
|||||||
"github.com/rjeczalik/notify"
|
"github.com/rjeczalik/notify"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitDevice() (string, error) {
|
func InitDevice() ([]string, error) {
|
||||||
fs, err := ioutil.ReadDir("/dev")
|
fs, err := ioutil.ReadDir("/dev")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var result []string = nil
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
if strings.Contains(f.Name(), "ttyACM") {
|
if strings.Contains(f.Name(), "ttyACM") {
|
||||||
return filepath.Join("/dev", f.Name()), nil
|
result = append(result, filepath.Join("/dev", f.Name()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", errors.New("USB Not found")
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) error {
|
func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user