2021-12-09 05:04:56 +00:00
|
|
|
package manager
|
2021-12-09 02:35:44 +00:00
|
|
|
|
|
|
|
import (
|
2021-12-10 01:14:34 +00:00
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
2021-12-10 06:45:28 +00:00
|
|
|
"fmt"
|
2021-12-10 08:07:20 +00:00
|
|
|
"io"
|
2021-12-10 01:14:34 +00:00
|
|
|
"log"
|
2021-12-09 02:35:44 +00:00
|
|
|
|
2021-12-10 01:14:34 +00:00
|
|
|
"github.com/jacobsa/go-serial/serial"
|
2021-12-09 02:35:44 +00:00
|
|
|
)
|
|
|
|
|
2021-12-10 01:14:34 +00:00
|
|
|
type _manager struct {
|
2021-12-10 06:45:28 +00:00
|
|
|
devicesWithUUID map[string]*_device
|
|
|
|
devicesWithIface map[interface{}]*_device
|
2021-12-10 08:07:20 +00:00
|
|
|
chanForSync map[string]chan map[string]interface{}
|
|
|
|
RegisterListener EventHandler
|
2021-12-10 06:45:28 +00:00
|
|
|
SyncListener EventHandler
|
|
|
|
RecvListener EventHandler
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
2021-12-09 02:35:44 +00:00
|
|
|
|
2021-12-10 06:45:28 +00:00
|
|
|
func (m *_manager) onRegistered(dev *_device) {
|
2021-12-10 08:07:20 +00:00
|
|
|
// if m.RegisterListener != nil {
|
|
|
|
// m.RegisterListener.Handle(&EventStruct{key: })
|
|
|
|
// }
|
|
|
|
if registerHandleFunc != nil {
|
2021-12-10 10:26:13 +00:00
|
|
|
param := map[string]interface{}{}
|
|
|
|
param["uuid"] = dev.UUID
|
|
|
|
param["sname"] = dev.Sname
|
|
|
|
|
|
|
|
registerHandleFunc(&EventStruct{key: dev.UUID, params: param})
|
2021-12-10 08:07:20 +00:00
|
|
|
}
|
2021-12-10 06:45:28 +00:00
|
|
|
go recv(dev.Iface, m)
|
2021-12-10 08:07:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *_manager) onRemoved(port io.Reader) {
|
|
|
|
dev := _managerObj.devicesWithIface[port]
|
|
|
|
delete(m.devicesWithUUID, dev.UUID)
|
|
|
|
delete(m.devicesWithIface, port)
|
|
|
|
ch, ok := m.chanForSync[dev.UUID]
|
|
|
|
if ok {
|
|
|
|
close(ch)
|
|
|
|
delete(m.chanForSync, dev.UUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if removeHandleFunc != nil {
|
2021-12-10 10:26:13 +00:00
|
|
|
param := map[string]interface{}{}
|
|
|
|
param["uuid"] = dev.UUID
|
|
|
|
|
|
|
|
removeHandleFunc(&EventStruct{key: dev.UUID, params: param})
|
2021-12-10 08:07:20 +00:00
|
|
|
}
|
|
|
|
// log.Println(m.devicesWithUUID)
|
|
|
|
// log.Println(m.devicesWithIface)
|
2021-12-10 06:45:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *_manager) onAdded(iface string) {
|
2021-12-10 01:14:34 +00:00
|
|
|
err := ChangePermission(iface)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-12-09 02:35:44 +00:00
|
|
|
|
2021-12-10 01:14:34 +00:00
|
|
|
// Set up options.
|
|
|
|
options := serial.OpenOptions{
|
|
|
|
PortName: iface,
|
|
|
|
BaudRate: 9600,
|
|
|
|
DataBits: 8,
|
|
|
|
StopBits: 1,
|
|
|
|
MinimumReadSize: 16,
|
2021-12-09 02:35:44 +00:00
|
|
|
}
|
2021-12-10 01:14:34 +00:00
|
|
|
|
|
|
|
// Open the port.
|
|
|
|
go func() {
|
|
|
|
port, err := serial.Open(options)
|
|
|
|
if err != nil {
|
2021-12-10 06:45:28 +00:00
|
|
|
log.Printf("serial.Open: %v", err)
|
|
|
|
return
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reader := bufio.NewReader(port)
|
2021-12-10 06:45:28 +00:00
|
|
|
encoder := json.NewEncoder(port)
|
2021-12-10 01:14:34 +00:00
|
|
|
|
2021-12-10 06:45:28 +00:00
|
|
|
sndMsg := map[string]interface{}{}
|
|
|
|
sndMsg["code"] = 100
|
2021-12-10 01:14:34 +00:00
|
|
|
|
2021-12-10 06:45:28 +00:00
|
|
|
for {
|
|
|
|
b, _, _ := reader.ReadLine()
|
|
|
|
rcvMsg := map[string]interface{}{}
|
|
|
|
err := json.Unmarshal(b, &rcvMsg)
|
2021-12-10 01:14:34 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:45:28 +00:00
|
|
|
code, ok := rcvMsg["code"].(float64)
|
|
|
|
if ok && code == 100 {
|
|
|
|
newDevice := &_device{
|
|
|
|
UUID: rcvMsg["uuid"].(string),
|
2021-12-10 01:14:34 +00:00
|
|
|
IfaceName: iface,
|
|
|
|
Iface: port,
|
2021-12-10 10:26:13 +00:00
|
|
|
Sname: rcvMsg["sname"].(string),
|
2021-12-10 06:45:28 +00:00
|
|
|
states: map[string]interface{}{},
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
2021-12-10 06:45:28 +00:00
|
|
|
m.devicesWithUUID[rcvMsg["uuid"].(string)] = newDevice
|
|
|
|
m.devicesWithIface[port] = newDevice
|
|
|
|
|
|
|
|
m.onRegistered(newDevice)
|
|
|
|
fmt.Println("onAdded sub-routine is died")
|
|
|
|
return
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
2021-12-10 06:45:28 +00:00
|
|
|
encoder.Encode(sndMsg)
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-12-10 06:45:28 +00:00
|
|
|
fmt.Println("onAdded main-routine is died")
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 06:45:28 +00:00
|
|
|
func (m *_manager) onSync(key interface{}, params map[string]interface{}) {
|
|
|
|
if m.SyncListener != nil {
|
|
|
|
m.SyncListener.Handle(&EventStruct{key: key, params: params})
|
|
|
|
}
|
2021-12-10 01:14:34 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 06:45:28 +00:00
|
|
|
func (m *_manager) onRecv(key interface{}, params map[string]interface{}) {
|
|
|
|
if m.RecvListener != nil {
|
|
|
|
m.RecvListener.Handle(&EventStruct{key: key, params: params})
|
|
|
|
}
|
2021-12-09 02:35:44 +00:00
|
|
|
}
|
2021-12-10 08:07:20 +00:00
|
|
|
|
|
|
|
func (m *_manager) addRecvListener(h EventHandler) {
|
2021-12-12 06:18:56 +00:00
|
|
|
m.RecvListener = &RecvHandler{next: h, devices: m.devicesWithIface, chanForSync: m.chanForSync}
|
2021-12-10 08:07:20 +00:00
|
|
|
}
|