diff --git a/cli.go b/cli.go index fb1d423..c5f4dc3 100644 --- a/cli.go +++ b/cli.go @@ -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() diff --git a/examples/main.go b/examples/main.go index 6855e0e..b3f42cc 100644 --- a/examples/main.go +++ b/examples/main.go @@ -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) } } diff --git a/listener.go b/listener.go index 2fe4e14..a798ae6 100644 --- a/listener.go +++ b/listener.go @@ -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) diff --git a/model.go b/model.go index ebbac2f..ac197e6 100644 --- a/model.go +++ b/model.go @@ -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 { diff --git a/puserial/serial.go b/puserial/serial.go index a1ccc07..702f5ec 100644 --- a/puserial/serial.go +++ b/puserial/serial.go @@ -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)