device_zkteco_controller.go
3.5 KB
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
package controllers
import (
"fmt"
"github.com/beego/beego/v2/server/web/context"
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/allied-lib/gateway/allied_creation_user"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"strings"
"time"
)
type DeviceZKTecoController struct {
beego.BaseController
}
func (controller *DeviceZKTecoController) PostCdata() {
body := controller.Ctx.Input.RequestBody
sn := controller.Ctx.Input.Query("SN")
//table := controller.Ctx.Input.Query("table")
bodyList := strings.Split(string(body), "\t")
data := &domain.DeviceZkTeco{
Sn: sn,
}
if len(bodyList) > 2 {
data.UserNo = bodyList[0]
//转成时间格式
mTime, err := time.ParseInLocation("2006-01-02 15:04:05", bodyList[1], time.Local)
if err == nil {
data.ActionTime = mTime
//mBytes, _ := json.Marshal(data)
//redis.GetRedis().LPush(domain.TaskDeviceZkTecoReport(), mBytes)
domainService.SendDeviceZkTecoReportJob(data)
} else {
data.ActionTime = time.Now()
domainService.SendDeviceZkTecoReportJob(data)
log.Logger.Debug(err.Error() + "data:" + bodyList[1])
}
}
RedirectToUserModule(controller.Ctx, false)
controller.Response(data, nil)
}
func (controller *DeviceZKTecoController) GetCdata() {
//sn := controller.Ctx.Input.Query("SN")
controller.Ctx.WriteString("OK")
}
func (controller *DeviceZKTecoController) GetRequest() {
//controller.Ctx.WriteString("C:11:DATA\tQUERY\tUSERINFO\tPIN=10086")
//controller.Ctx.WriteString("C:7EceWxtHB6:DATA QUERY USERINFO PIN=3")
//controller.Ctx.WriteString("OK")
RedirectToUserModule(controller.Ctx, true)
}
func (controller *DeviceZKTecoController) Ping() {
controller.Ctx.WriteString("OK")
}
func RedirectToUserModule(ctx *context.Context, useResult bool) {
var svr = allied_creation_user.NewHttpLibAlliedCreationUser(constant.ALLIED_CREATION_USER_HOST)
data := ctx.Input.RequestBody
uri := ctx.Request.URL.Path
sn := ctx.Input.Query("SN")
table := ""
cmd := uri
switch uri {
case "/zkteco/iclock/getrequest": // 获取下行命令
cmd = "getrequest"
case "/zkteco/iclock/cdata": // 上报数据
cmd = "cdata"
table = ctx.Input.Query("table")
}
param := allied_creation_user.ReqTerminalReport{
TerminalType: "zkteco",
TerminalId: sn,
Command: cmd,
CompanyId: int64(constant.MANUFACTURE_DEFAULT_COMPANYID),
OrgId: int64(constant.MANUFACTURE_DEFAULT_ORGID),
Table: table,
Content: string(data),
}
if !useResult {
log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 成功", sn, cmd, string(data)))
go func() {
defer func() {
if p := recover(); p != nil {
log.Logger.Error(fmt.Sprintf("%v", p))
}
}()
svr.TerminalReport(param)
}()
return
}
response, err := svr.TerminalReport(param)
if err != nil {
log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 应答:%v 错误:%v", sn, cmd, string(data), response.Response, err.Error()))
ctx.WriteString("OK")
return
}
log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v 应答:%v 成功", sn, cmd, string(data), response.Response))
ctx.WriteString(response.Response)
}