// "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 (
"context"
"errors"
"log"
"git.godopu.net/lab/etri-smartfarm-poc-controller-serial/puserial"
@ -55,14 +54,14 @@ func AddRecvListener(h EventHandler) {
_managerObj.addRecvListener(h)
}
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 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()

View File

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

View File

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

View File

@ -3,12 +3,11 @@ package manager
import "io"
type _device struct {
UUID string
IfaceName string
Sname string
Iface io.ReadWriter
states map[string]interface{}
propsToSync []string
UUID string
IfaceName string
Sname string
Iface io.ReadWriter
states map[string]interface{}
}
type RecvEvent struct {

View File

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