// "servo": 0,

This commit is contained in:
Godopu 2021-12-12 10:08:11 +09:00
parent e0c59f23cc
commit 29a7d73791
5 changed files with 36 additions and 38 deletions

17
cli.go
View File

@ -2,7 +2,6 @@ package manager
import ( import (
"context" "context"
"errors"
"log" "log"
"git.godopu.net/lab/etri-smartfarm-poc-controller-serial/puserial" "git.godopu.net/lab/etri-smartfarm-poc-controller-serial/puserial"
@ -55,14 +54,14 @@ func AddRecvListener(h EventHandler) {
_managerObj.addRecvListener(h) _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 {
return errors.New("device not found") // return errors.New("device not found")
} // }
device.propsToSync = propsToSync // device.propsToSync = propsToSync
return nil // return nil
} // }
func Run() error { func Run() error {
iface, err := puserial.InitDevice() iface, err := puserial.InitDevice()

View File

@ -24,8 +24,11 @@ func main() {
param := e.Params() param := e.Params()
fmt.Println(param["uuid"].(string), " is removed!!") fmt.Println(param["uuid"].(string), " is removed!!")
}) })
go manager.Run() go manager.Run()
defer manager.Close() defer manager.Close()
param := map[string]interface{}{}
for { for {
fmt.Print("> ") fmt.Print("> ")
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
@ -34,9 +37,6 @@ func main() {
fmt.Println("cmd: ", cmd) fmt.Println("cmd: ", cmd)
if cmd == "exit" { if cmd == "exit" {
return return
} else if cmd == "setup" {
manager.SetDevicePropsToSync("DEVICE-A-UUID", []string{"fan", "servo"})
continue
} }
tkns := strings.Split(cmd, " ") tkns := strings.Split(cmd, " ")
@ -45,9 +45,7 @@ func main() {
if err != nil { if err != nil {
continue continue
} }
param := map[string]interface{}{ param[tkns[1]] = value
tkns[1]: value,
}
manager.Sync(key, param) manager.Sync(key, param)
} }
} }

View File

@ -11,22 +11,28 @@ type SyncHandler struct {
chanForSync map[string]chan map[string]interface{} chanForSync map[string]chan map[string]interface{}
} }
func compareMap(src map[string]interface{}, dst map[string]interface{}, props []string) bool { func compareMap(src map[string]interface{}, dst map[string]interface{}) bool {
for _, prop := range props {
srcV, ok := src[prop].(int) for key, value := range src {
if key == "code" {
continue
}
srcV, ok := value.(int)
if !ok { if !ok {
srcV = int(src[prop].(float64)) srcV = int(value.(float64))
} }
dstV, ok := dst[prop].(int) dstV, ok := dst[key].(int)
if !ok { if !ok {
dstV = int(dst[prop].(float64)) dstV = int(dst[key].(float64))
} }
if srcV != dstV { if srcV != dstV {
fmt.Println("diff ", prop, "] ", srcV, " vs ", dstV) fmt.Println("diff ", key, "] ", srcV, " vs ", dstV)
return false return false
} }
} }
return true return true
} }
@ -48,16 +54,11 @@ func (sh *SyncHandler) Handle(e Event) {
encoder := json.NewEncoder(device.Iface) encoder := json.NewEncoder(device.Iface)
origin := map[string]interface{}{} origin := map[string]interface{}{}
origin["code"] = 200 origin["code"] = 200
props := device.propsToSync for key, value := range params {
origin[key] = value
}
// props := []string{"fan", "light", "servo"} // props := []string{"fan", "light", "servo"}
for _, pname := range props {
prop, ok := params[pname]
if !ok {
origin[pname] = device.states[pname]
} else {
origin[pname] = prop
}
}
err := encoder.Encode(origin) err := encoder.Encode(origin)
if err != nil { if err != nil {
@ -66,7 +67,7 @@ func (sh *SyncHandler) Handle(e Event) {
sh.chanForSync[device.IfaceName] = make(chan map[string]interface{}) sh.chanForSync[device.IfaceName] = make(chan map[string]interface{})
for state := range sh.chanForSync[device.IfaceName] { for state := range sh.chanForSync[device.IfaceName] {
if compareMap(origin, state, props) { if compareMap(origin, state) {
log.Println("Same!!") log.Println("Same!!")
close(sh.chanForSync[device.IfaceName]) close(sh.chanForSync[device.IfaceName])
delete(sh.chanForSync, device.IfaceName) delete(sh.chanForSync, device.IfaceName)

View File

@ -8,7 +8,6 @@ type _device struct {
Sname string Sname string
Iface io.ReadWriter Iface io.ReadWriter
states map[string]interface{} states map[string]interface{}
propsToSync []string
} }
type RecvEvent struct { type RecvEvent struct {

View File

@ -25,6 +25,7 @@ func InitDevice() (string, error) {
return "", errors.New("USB Not found") return "", errors.New("USB Not found")
} }
func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) error { func WatchNewDevice(ctx context.Context, ch_discover chan<- notify.EventInfo) error {
defer close(ch_discover) defer close(ch_discover)