etri-smartfarm-poc-controll.../examples/main.go
2021-12-12 10:08:11 +09:00

52 lines
995 B
Go

package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
manager "git.godopu.net/lab/etri-smartfarm-poc-controller-serial"
)
func main() {
manager.AddRecvListener(manager.NewEventHandler(func(e manager.Event) {
fmt.Println("RECV: ", e.Params())
}))
manager.AddRegisterHandleFunc(func(e manager.Event) {
param := e.Params()
fmt.Println(param["uuid"].(string), " is registered!! ] ", param["sname"])
})
manager.AddRemoveHandleFunc(func(e manager.Event) {
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)
b, _, _ := reader.ReadLine()
cmd := string(b)
fmt.Println("cmd: ", cmd)
if cmd == "exit" {
return
}
tkns := strings.Split(cmd, " ")
key := tkns[0]
value, err := strconv.Atoi(tkns[2])
if err != nil {
continue
}
param[tkns[1]] = value
manager.Sync(key, param)
}
}