改版前

This commit is contained in:
huangsimin 2019-11-04 14:29:08 +08:00
parent f76f86bd8e
commit 85ad71c9ad
6 changed files with 39 additions and 13 deletions

View File

@ -4,12 +4,23 @@ import "time"
// Command 命令相关 // Command 命令相关
type Command struct { type Command struct {
commands string commands CommandType
commandTime time.Time commandTime time.Time
} }
// CommandType 命令
type CommandType string
const (
CMDDoNothing = "不做任何操作"
CMDChaoShengBoQingXi = "超声波清洗"
CMDQingXiGuanFangShui = "清洗罐放水"
)
// CreateCommand 生成一个命令 // CreateCommand 生成一个命令
func CreateCommand() *Command { func CreateCommand(cmdtype CommandType) *Command {
c := &Command{} c := &Command{}
c.commands = cmdtype
c.commandTime = time.Now()
return c return c
} }

View File

@ -11,6 +11,9 @@ import (
func (worker *Worker) OperateHandler(cxt *gin.Context) { func (worker *Worker) OperateHandler(cxt *gin.Context) {
//log.Println(worker.Sensor()) //log.Println(worker.Sensor())
cmd := CommandType(cxt.PostForm("command"))
worker.SendCommand(cmd)
log.Println("SendCommand: ", cmd)
} }
func (worker *Worker) StatusHandler(cxt *gin.Context) { func (worker *Worker) StatusHandler(cxt *gin.Context) {

View File

@ -14,10 +14,10 @@ const (
// YM-01-X-03 // YM-01-X-03
UFTreatedWater OperatorFlag = 0b0000100000000000 // bit11 UF 超滤膜净水出水阀 1开0关 UFTreatedWater OperatorFlag = 0b0000100000000000 // bit11 UF 超滤膜净水出水阀 1开0关
UFRawWater OperatorFlag = 0b0000010000000000 // bit10 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关 CirculatingTankWashWater OperatorFlag = 0b0000001000000000 // bit9 循环罐洗进水电动球阀 1开0关
UFPositiveFlushingWaterOutlet OperatorFlag = 0b0000000100000000 // bit8 UF超滤膜正冲浓水出口电磁阀 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关 CleaningTankExhaust OperatorFlag = 0b0000000010000000 // bit7 清洗罐排气电磁阀 1开0关
DPFCompactCylinderControlB OperatorFlag = 0b0000000001000000 // bit6 DPF压紧气缸控制电磁阀B 1开0关 DPFCompactCylinderControlB OperatorFlag = 0b0000000001000000 // bit6 DPF压紧气缸控制电磁阀B 1开0关
DPFCompactCylinderControlA OperatorFlag = 0b0000000000100000 // bit5 DPF压紧气缸控制电磁阀A 1开0关 DPFCompactCylinderControlA OperatorFlag = 0b0000000000100000 // bit5 DPF压紧气缸控制电磁阀A 1开0关
@ -32,7 +32,8 @@ const (
// ChaoShengBoQingXi // ChaoShengBoQingXi
func ChaoShengBoQingXi(worker *Worker) { 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)) worker.Write(OperatorOption(flag))
for { for {
// TODO: 定时时间 15000230
if time.Now().Unix() >= 15000230 { if time.Now().Unix() >= 15000230 {
break break
} }

View File

@ -17,10 +17,11 @@ type Sensor struct {
func NewSensor(buf []byte) *Sensor { func NewSensor(buf []byte) *Sensor {
if len(buf) == 14 { if len(buf) == 14 {
sensor := &Sensor{}
if buf[0] == byte(0xaa) && buf[1] == byte(0x55) { if buf[0] == byte(0xaa) && buf[1] == byte(0x55) {
sensor := &Sensor{}
sensor.SP01 = binary.BigEndian.Uint16(buf[2:4]) sensor.SP01 = binary.BigEndian.Uint16(buf[2:4])
sensor.SP02 = binary.BigEndian.Uint16(buf[4:6]) sensor.SP02 = binary.BigEndian.Uint16(buf[4:6])
sensor.LT01 = binary.BigEndian.Uint16(buf[6:8]) sensor.LT01 = binary.BigEndian.Uint16(buf[6:8])

View File

@ -35,6 +35,8 @@ func NewSerialPort() *SerialPort {
sp.port1 = "/dev/pts/3" sp.port1 = "/dev/pts/3"
sp.port2 = "/dev/pts/4" sp.port2 = "/dev/pts/4"
// sp.port = "/dev/pts/3"
sp.baud = 9600 sp.baud = 9600
sp.windowsRWLock = &sync.Mutex{} sp.windowsRWLock = &sync.Mutex{}

View File

@ -8,15 +8,14 @@ import (
) )
// Register 操作注册表 // Register 操作注册表
var Register map[string]func(worker *Worker) var Register map[CommandType]func(worker *Worker)
// init 初始化 // init 初始化
func init() { func init() {
Register = make(map[string]func(worker *Worker)) Register = make(map[CommandType]func(worker *Worker))
Register["超声波清洗"] = ChaoShengBoQingXi Register[CMDChaoShengBoQingXi] = ChaoShengBoQingXi
Register[CMDQingXiGuanFangShui] = QingXiGuanFangShui
Register["清洗罐放水"] = QingXiGuanFangShui
} }
// Worker 接收命令 // Worker 接收命令
@ -63,7 +62,7 @@ func NewWorker() *Worker {
w.isStop = 0 w.isStop = 0
w.waitGroup = &sync.WaitGroup{} w.waitGroup = &sync.WaitGroup{}
w.command = CreateCommand() w.command = CreateCommand(CMDDoNothing)
w.commandLock = new(sync.Mutex) w.commandLock = new(sync.Mutex)
w.readlogsLock = &sync.Mutex{} w.readlogsLock = &sync.Mutex{}
@ -105,6 +104,15 @@ func (worker *Worker) Read() (result []byte) {
return result 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) { func (worker *Worker) operator(wait *sync.WaitGroup) {
defer wait.Done() defer wait.Done()
@ -119,7 +127,6 @@ func (worker *Worker) operator(wait *sync.WaitGroup) {
worker.commandLock.Lock() worker.commandLock.Lock()
if worker.isOperating { if worker.isOperating {
if now.Sub(worker.command.commandTime).Seconds() >= 5 { if now.Sub(worker.command.commandTime).Seconds() >= 5 {
// TODO: 操作 // TODO: 操作
if operate, ok := Register[worker.command.commands]; ok { if operate, ok := Register[worker.command.commands]; ok {