-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger.go
137 lines (125 loc) · 3.41 KB
/
trigger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package irain
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/nextabc-lab/edgex-go"
"github.com/nextabc-lab/edgex-go/extra"
"github.com/parkingwang/go-wg26"
sock "github.com/yoojia/go-socket"
"go.uber.org/zap"
"io"
"os"
)
//
// Author: 陈哈哈 [email protected]
//
const FrameCardEventLength = 10
// 等待刷卡数据循环
func ReceiveLoop(ctx edgex.Context, trigger edgex.Trigger, boardAddr byte, cli *sock.Client, shutdown <-chan os.Signal) error {
log := ctx.Log()
process := func(msg *IrMessage) {
if FrameCardEventLength != len(msg.Payload) {
log.Debug("只处理刷卡类型事件,忽略")
return
}
ctx.LogIfVerbose(func(log *zap.SugaredLogger) {
log.Debug("艾润发送事件码: " + hex.EncodeToString(msg.Payload))
})
event := parseCardEvent(msg.Payload, boardAddr)
log.Debugf("接收到控制器事件, DoorId: %d, Card: %s, EventType: %s", event.DoorId, event.CardNO, event.Type)
data, err := json.Marshal(event)
if nil != err {
log.Error("JSON序列化错误", err)
return
}
if err := trigger.PublishEvent(
makeBoardId(byte(event.BoardId)),
makeMajorId(int(event.DoorId)),
directName(event.Direct),
data,
trigger.GenerateEventId()); nil != err {
log.Error("触发事件出错: ", err)
return
}
}
// 读数据循环
message := new(IrMessage)
for {
select {
case <-shutdown:
log.Debug("接收到系统终止信号")
return nil
default:
err := cli.ReadWith(func(in io.Reader) error {
if ok, err := ReadMessage(in, message); ok {
process(message)
return nil
} else if err == ErrUnknownMessage {
log.Debugf("接收到非艾润数据格式数据: DATA= %v", in)
return nil
} else {
return err
}
})
if nil != err && !sock.IsNetTempErr(err) {
ctx.LogIfVerbose(func(log *zap.SugaredLogger) {
log.Error("读取监控数据出错: " + err.Error())
})
log.Debug("正在重新连接")
if err := cli.Reconnect(); nil != err {
log.Error("重连失败: ", err)
}
}
}
}
}
func parseCardEvent(data []byte, boardAddr byte) extra.CardEvent {
doorId := byte(0)
switch data[9] & 0xF0 {
case 0x10:
doorId = 1
case 0x20:
doorId = 2
case 0x30:
doorId = 3
case 0x40:
doorId = 4
}
return extra.CardEvent{
SerialNum: uint32(boardAddr),
BoardId: uint32(boardAddr),
DoorId: doorId,
Direct: extra.DirectIn,
CardNO: wg26.ParseFromWg26([3]byte{data[0], data[1], data[2]}).CardSN,
Type: extra.TypeCard,
State: "OPEN",
Index: 0,
}
}
// 创建TriggerNode消息函数
func FuncTriggerProperties(boardAddr byte, doorCount int) func() edgex.MainNodeProperties {
deviceOf := func(doorId int, directName string) *edgex.VirtualNodeProperties {
return &edgex.VirtualNodeProperties{
BoardId: makeBoardId(boardAddr),
MajorId: makeMajorId(doorId),
MinorId: directName,
DeviceType: "reader",
Description: fmt.Sprintf("艾润#%d/%d号门/%s/Reader", boardAddr, doorId, directName),
Virtual: true,
}
}
return func() edgex.MainNodeProperties {
nodes := make([]*edgex.VirtualNodeProperties, doorCount*2)
for d := 0; d < doorCount; d++ {
nodes[d*2] = deviceOf(d+1, directName(extra.DirectIn))
nodes[d*2+1] = deviceOf(d+1, directName(extra.DirectOut))
}
return edgex.MainNodeProperties{
NodeType: edgex.NodeTypeTrigger,
Vendor: VendorName,
ConnDriver: DriverName,
VirtualNodes: nodes,
}
}
}