Compare commits

..

No commits in common. "main" and "v0.1.3" have entirely different histories.
main ... v0.1.3

3 changed files with 26 additions and 58 deletions

18
cli.go
View File

@ -3,7 +3,6 @@ package manager
import (
"context"
"log"
"sync"
"git.godopu.net/lab/etri-smartfarm-poc-controller-serial/puserial"
"github.com/rjeczalik/notify"
@ -27,7 +26,7 @@ func init() {
devicesWithUUID: devWithUUID,
devicesWithIface: devWithIface,
chanForSync: chanForSync,
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync, mutex: &sync.Mutex{}, states: map[string]map[string]interface{}{}},
SyncListener: &SyncHandler{devices: devWithUUID, chanForSync: chanForSync},
RecvListener: &RecvHandler{devices: devWithIface, chanForSync: chanForSync},
}
@ -65,13 +64,14 @@ func AddRecvListener(h EventHandler) {
// }
func Run() error {
ifaces, err := puserial.InitDevice()
iface, err := puserial.InitDevice()
if err != nil {
return err
}
for _, e := range ifaces {
go _managerObj.onAdded(e)
if err.Error() != "USB Not found" {
return err
}
} else {
// _managerObj.onAdd(iface)
_managerObj.onAdded(iface)
}
go puserial.WatchNewDevice(ctx, ch_discover)
@ -84,7 +84,7 @@ func Run() error {
}
switch e.Event() {
case notify.Create:
go _managerObj.onAdded(e.Path())
_managerObj.onAdded(e.Path())
// case notify.Remove:
// log.Println("USB Disconnected!!")
// _managerObj.onAdded(e.Path())

View File

@ -4,15 +4,11 @@ import (
"encoding/json"
"fmt"
"log"
"reflect"
"sync"
)
type SyncHandler struct {
devices map[string]*_device
mutex *sync.Mutex
chanForSync map[string]chan map[string]interface{}
states map[string]map[string]interface{}
}
func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
@ -21,23 +17,14 @@ func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
if key == "code" {
continue
}
if reflect.TypeOf(value).String() == "string" {
dstV, ok := dst[key].(string)
if !ok || value != dstV {
return false
}
continue
srcV, ok := value.(int)
if !ok {
srcV = int(value.(float64))
}
srcV, ok := value.(float64)
dstV, ok := dst[key].(int)
if !ok {
srcV = float64(value.(int))
}
dstV, ok := dst[key].(float64)
if !ok {
dstV = float64(dst[key].(int))
dstV = int(dst[key].(float64))
}
if srcV != dstV {
@ -50,6 +37,7 @@ func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
}
func (sh *SyncHandler) Handle(e Event) {
device, ok := sh.devices[e.Key().(string)]
// fmt.Println("sync] ", device.IfaceName)
// fmt.Println(sh.devices)
@ -70,7 +58,6 @@ func (sh *SyncHandler) Handle(e Event) {
origin[key] = value
}
sh.states[device.UUID] = origin
// props := []string{"fan", "light", "servo"}
err := encoder.Encode(origin)
@ -78,35 +65,21 @@ func (sh *SyncHandler) Handle(e Event) {
return
}
_, ok := sh.chanForSync[device.IfaceName]
if ok {
sh.mutex.Lock()
close(sh.chanForSync[device.IfaceName])
delete(sh.chanForSync, device.IfaceName)
sh.mutex.Unlock()
}
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()
sh.chanForSync[device.IfaceName] = make(chan map[string]interface{})
for state := range sh.chanForSync[device.IfaceName] {
if compareMap(origin, state) {
log.Println("Same!!")
close(sh.chanForSync[device.IfaceName])
delete(sh.chanForSync, device.IfaceName)
sh.mutex.Unlock()
return
}
log.Println("wrong: ", state)
log.Println("resend: ", sh.states[device.UUID])
err := encoder.Encode(sh.states[device.UUID])
log.Println("resend: ", origin)
err := encoder.Encode(origin)
if err != nil {
return
}
}
}()
}
@ -117,11 +90,6 @@ type RecvHandler struct {
}
func (rh *RecvHandler) Handle(e Event) {
defer func() {
if r := recover(); r != nil {
fmt.Println("panic recover - ", r)
}
}()
device, ok := rh.devices[e.Key()]
if !ok {
return

View File

@ -2,6 +2,7 @@ package puserial
import (
"context"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
@ -10,20 +11,19 @@ import (
"github.com/rjeczalik/notify"
)
func InitDevice() ([]string, error) {
func InitDevice() (string, error) {
fs, err := ioutil.ReadDir("/dev")
if err != nil {
return nil, err
return "", err
}
var result []string = nil
for _, f := range fs {
if strings.Contains(f.Name(), "ttyACM") {
result = append(result, filepath.Join("/dev", f.Name()))
return filepath.Join("/dev", f.Name()), nil
}
}
return result, nil
return "", errors.New("USB Not found")
}
func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) error {