174 lines
2.8 KiB
Go
174 lines
2.8 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// Worker 接收命令
|
||
|
type Worker struct {
|
||
|
commandLock *sync.Mutex
|
||
|
command *Command
|
||
|
|
||
|
port *SerialPort // 串口相关
|
||
|
|
||
|
readlogsLock *sync.Mutex
|
||
|
readlogs []Log
|
||
|
|
||
|
writelogsLock *sync.Mutex
|
||
|
isOperating bool
|
||
|
writelogs []Log
|
||
|
|
||
|
isStop int
|
||
|
waitGroup *sync.WaitGroup
|
||
|
}
|
||
|
|
||
|
// Log 日志
|
||
|
type Log struct {
|
||
|
Data []byte
|
||
|
Time time.Time
|
||
|
}
|
||
|
|
||
|
// NewWorker 创建一个工人
|
||
|
func NewWorker() *Worker {
|
||
|
w := &Worker{}
|
||
|
|
||
|
w.isStop = 0
|
||
|
w.waitGroup = &sync.WaitGroup{}
|
||
|
|
||
|
w.command = NewCommand()
|
||
|
w.commandLock = new(sync.Mutex)
|
||
|
|
||
|
w.readlogsLock = &sync.Mutex{}
|
||
|
w.readlogs = make([]Log, 0, 1000)
|
||
|
|
||
|
w.writelogsLock = &sync.Mutex{}
|
||
|
w.writelogs = make([]Log, 0, 1000)
|
||
|
w.isOperating = false
|
||
|
|
||
|
return w
|
||
|
}
|
||
|
|
||
|
func (worker *Worker) write(data []byte) {
|
||
|
worker.writelogsLock.Lock()
|
||
|
worker.isOperating = true
|
||
|
worker.writelogs = append(worker.writelogs, Log{data, time.Now()})
|
||
|
if len(worker.writelogs) >= 1000 {
|
||
|
worker.writelogs = worker.writelogs[500:1000]
|
||
|
}
|
||
|
worker.writelogsLock.Unlock()
|
||
|
}
|
||
|
|
||
|
// read 如果没有读到数据为nil
|
||
|
func (worker *Worker) read() (result []byte) {
|
||
|
|
||
|
worker.readlogsLock.Lock()
|
||
|
if len(worker.readlogs) > 0 {
|
||
|
copy(worker.readlogs[len(worker.readlogs)-1].Data, result)
|
||
|
}
|
||
|
worker.writelogsLock.Unlock()
|
||
|
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func (worker *Worker) operator(wait *sync.WaitGroup) {
|
||
|
defer wait.Done()
|
||
|
|
||
|
if CurrentOS == "linux" {
|
||
|
|
||
|
for {
|
||
|
|
||
|
if worker.isStop > 0 {
|
||
|
break
|
||
|
}
|
||
|
|
||
|
now := time.Now()
|
||
|
worker.writelogsLock.Lock()
|
||
|
if worker.isOperating {
|
||
|
l := worker.writelogs[len(worker.writelogs)-1]
|
||
|
if now.Sub(l.Time).Seconds() >= 5 {
|
||
|
// TODO: 操作
|
||
|
worker.isOperating = false
|
||
|
}
|
||
|
|
||
|
}
|
||
|
worker.writelogsLock.Unlock()
|
||
|
}
|
||
|
|
||
|
} else { // Windows
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func (worker *Worker) status(wait *sync.WaitGroup) {
|
||
|
defer wait.Done()
|
||
|
|
||
|
if CurrentOS == "linux" {
|
||
|
|
||
|
for {
|
||
|
|
||
|
if worker.isStop > 0 {
|
||
|
break
|
||
|
}
|
||
|
|
||
|
var buf []byte
|
||
|
n, err := worker.port.linuxRPort.Read(buf)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
} else {
|
||
|
if n > 0 {
|
||
|
|
||
|
worker.readlogsLock.Lock()
|
||
|
worker.readlogs = append(worker.readlogs, Log{buf, time.Now()})
|
||
|
if len(worker.readlogs) >= 1000 {
|
||
|
worker.readlogs = worker.readlogs[500:1000]
|
||
|
}
|
||
|
|
||
|
log.Println("data size ", n)
|
||
|
log.Panicln("data: ", buf)
|
||
|
worker.readlogsLock.Unlock()
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
time.Sleep(time.Millisecond * 50)
|
||
|
}
|
||
|
|
||
|
} else { // windows
|
||
|
|
||
|
for {
|
||
|
|
||
|
var buf []byte
|
||
|
n, err := worker.port.windowsRWPort.Read(buf)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
} else {
|
||
|
if n > 0 {
|
||
|
|
||
|
log.Println("data size ", n)
|
||
|
log.Panicln("data: ", buf)
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
time.Sleep(time.Millisecond * 50)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
// Run 运行
|
||
|
func (worker *Worker) Run() {
|
||
|
|
||
|
sp := NewSerialPort()
|
||
|
sp.OpenPort()
|
||
|
worker.port = sp
|
||
|
|
||
|
worker.waitGroup.Add(1)
|
||
|
go worker.status(worker.waitGroup)
|
||
|
|
||
|
worker.waitGroup.Wait()
|
||
|
}
|