device_zkteco_controller.go 4.2 KB
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/infrastructure/utils"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
	"strings"
	"time"
	"unicode/utf8"
)

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,
	}
	table := controller.Ctx.Input.Query("table")
	if len(bodyList) > 2 && table == "ATTLOG" {
		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")
	case "/zkteco/iclock/devicecmd":
		cmd = "devicecmd"
	}
	content := string(data)
	if !utf8.Valid(data) {
		utf8Content, _ := utils.GbkToUtf8(data) //ConvertToString(content, "gbk", "utf-8")
		content = string(utf8Content)
	}
	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:      content,
	}
	if !useResult {
		log.Logger.Debug(fmt.Sprintf("命令透传(sn:%v)-命令:%v 命令(内容):%v  成功", sn, cmd, content))
		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))
	gbkData, _ := utils.Utf8ToGbk([]byte(response.Response))
	ctx.Output.Header("Content-Type", "text/plain;charset=gbk")
	ack := string(gbkData)
	ctx.WriteString(ack)
}

func (controller *DeviceZKTecoController) DeviceCmd() {
	//sn := controller.Ctx.Input.Query("SN")
	RedirectToUserModule(controller.Ctx, false)
	controller.Ctx.WriteString("OK")
}