From 85ad71c9adcdb09b96ca2de8c391fe875cbbdf54 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Mon, 4 Nov 2019 14:29:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E7=89=88=E5=89=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command.go | 15 +++++++++++++-- main.go | 3 +++ operator.go | 8 +++++--- sensor.go | 3 ++- serialport.go | 2 ++ worker.go | 21 ++++++++++++++------- 6 files changed, 39 insertions(+), 13 deletions(-) diff --git a/command.go b/command.go index 5c992a3..5787846 100644 --- a/command.go +++ b/command.go @@ -4,12 +4,23 @@ import "time" // Command 命令相关 type Command struct { - commands string + commands CommandType commandTime time.Time } +// CommandType 命令 +type CommandType string + +const ( + CMDDoNothing = "不做任何操作" + CMDChaoShengBoQingXi = "超声波清洗" + CMDQingXiGuanFangShui = "清洗罐放水" +) + // CreateCommand 生成一个命令 -func CreateCommand() *Command { +func CreateCommand(cmdtype CommandType) *Command { c := &Command{} + c.commands = cmdtype + c.commandTime = time.Now() return c } diff --git a/main.go b/main.go index 71ecc5b..0d5295c 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,9 @@ import ( func (worker *Worker) OperateHandler(cxt *gin.Context) { //log.Println(worker.Sensor()) + cmd := CommandType(cxt.PostForm("command")) + worker.SendCommand(cmd) + log.Println("SendCommand: ", cmd) } func (worker *Worker) StatusHandler(cxt *gin.Context) { diff --git a/operator.go b/operator.go index 29ad2d3..77e9403 100644 --- a/operator.go +++ b/operator.go @@ -14,10 +14,10 @@ const ( // YM-01-X-03 UFTreatedWater OperatorFlag = 0b0000100000000000 // bit11 UF 超滤膜净水出水阀 1开,0关 UFRawWater OperatorFlag = 0b0000010000000000 // bit10 UF超滤膜原水进水阀 1开,0关 - // YM-01-X-01 + // CirculatingTankWashWater YM-01-X-01 CirculatingTankWashWater OperatorFlag = 0b0000001000000000 // bit9 循环罐洗进水电动球阀 1开,0关 UFPositiveFlushingWaterOutlet OperatorFlag = 0b0000000100000000 // bit8 UF超滤膜正冲浓水出口电磁阀 1开,0关 - // YV-02-02-1-X-06 + // CleaningTankExhaust YV-02-02-1-X-06 CleaningTankExhaust OperatorFlag = 0b0000000010000000 // bit7 清洗罐排气电磁阀 1开,0关 DPFCompactCylinderControlB OperatorFlag = 0b0000000001000000 // bit6 DPF压紧气缸控制电磁阀B 1开,0关 DPFCompactCylinderControlA OperatorFlag = 0b0000000000100000 // bit5 DPF压紧气缸控制电磁阀A 1开,0关 @@ -32,7 +32,8 @@ const ( // ChaoShengBoQingXi func ChaoShengBoQingXi(worker *Worker) { - for sensor := worker.Sensor(); (20.0/1024.0)*float64(sensor.SP02) < 10.0; sensor = worker.Sensor() { // TODO: 预设值未知 + // TODO: 预设值未知 + for sensor := worker.Sensor(); (20.0/1024.0)*float64(sensor.SP02) < 10.0; sensor = worker.Sensor() { } @@ -71,6 +72,7 @@ func ChaoShengBoQingXi(worker *Worker) { worker.Write(OperatorOption(flag)) for { + // TODO: 定时时间 15000230 if time.Now().Unix() >= 15000230 { break } diff --git a/sensor.go b/sensor.go index 994eece..b621f89 100644 --- a/sensor.go +++ b/sensor.go @@ -17,10 +17,11 @@ type Sensor struct { func NewSensor(buf []byte) *Sensor { if len(buf) == 14 { - sensor := &Sensor{} if buf[0] == byte(0xaa) && buf[1] == byte(0x55) { + sensor := &Sensor{} + sensor.SP01 = binary.BigEndian.Uint16(buf[2:4]) sensor.SP02 = binary.BigEndian.Uint16(buf[4:6]) sensor.LT01 = binary.BigEndian.Uint16(buf[6:8]) diff --git a/serialport.go b/serialport.go index c4aeb91..a3f3da8 100644 --- a/serialport.go +++ b/serialport.go @@ -35,6 +35,8 @@ func NewSerialPort() *SerialPort { sp.port1 = "/dev/pts/3" sp.port2 = "/dev/pts/4" + // sp.port = "/dev/pts/3" + sp.baud = 9600 sp.windowsRWLock = &sync.Mutex{} diff --git a/worker.go b/worker.go index 4295266..4d0e145 100644 --- a/worker.go +++ b/worker.go @@ -8,15 +8,14 @@ import ( ) // Register 操作注册表 -var Register map[string]func(worker *Worker) +var Register map[CommandType]func(worker *Worker) // init 初始化 func init() { - Register = make(map[string]func(worker *Worker)) + Register = make(map[CommandType]func(worker *Worker)) - Register["超声波清洗"] = ChaoShengBoQingXi - - Register["清洗罐放水"] = QingXiGuanFangShui + Register[CMDChaoShengBoQingXi] = ChaoShengBoQingXi + Register[CMDQingXiGuanFangShui] = QingXiGuanFangShui } // Worker 接收命令 @@ -63,7 +62,7 @@ func NewWorker() *Worker { w.isStop = 0 w.waitGroup = &sync.WaitGroup{} - w.command = CreateCommand() + w.command = CreateCommand(CMDDoNothing) w.commandLock = new(sync.Mutex) w.readlogsLock = &sync.Mutex{} @@ -105,6 +104,15 @@ func (worker *Worker) Read() (result []byte) { return result } +// SendCommand 发送任务 +func (worker *Worker) SendCommand(cmdtype CommandType) { + worker.commandLock.Lock() + worker.command = CreateCommand(cmdtype) + worker.isOperating = true + worker.commandLock.Unlock() + log.Println("SendCommand: ", worker.command, worker.command.commandTime) +} + func (worker *Worker) operator(wait *sync.WaitGroup) { defer wait.Done() @@ -119,7 +127,6 @@ func (worker *Worker) operator(wait *sync.WaitGroup) { worker.commandLock.Lock() if worker.isOperating { - if now.Sub(worker.command.commandTime).Seconds() >= 5 { // TODO: 操作 if operate, ok := Register[worker.command.commands]; ok {