123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- package v1
- import (
- "context"
- "fmt"
- "git.getensh.com/common/gopkgs/logger"
- "git.getensh.com/common/gopkgs/tasker/httptasker"
- "github.com/gin-gonic/gin"
- "github.com/tidwall/gjson"
- "go.uber.org/zap"
- "io/ioutil"
- "net/http"
- param_v1 "property-device-gateway/param/v1"
- "property-device-gateway/parser"
- "property-device-gateway/pb"
- pb_v1 "property-device-gateway/pb/v1"
- "strconv"
- "strings"
- "time"
- )
- const (
- PersonRegersterTaskName = "person/create"
- PersonUpdateTaskName = "person/update"
- PersonDelTaskName = "person/delete"
- PhotoRegisterByUrlTaskName = "face/createByUrl"
- TakePhotoRegisterTaskName = "face/takeImg"
- PhotoDelTaskName = "face/delete"
- DeviceRebootTaskName = "restartDevice"
- DeviceOpenTaskName = "device/openDoorControl"
- PhotoUpdateByUrlTaskName = "face"
- )
- type PersonRegesterTask struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- Person PersonRegesterData `json:"person"`
- }
- type PersonRegesterData struct {
- Id string `json:"id"`
- Name string `json:"name"`
- IdCardNum string `json:"idcardNum"`
- IdNumber string `json:"iDNumber"`
- FacePermission int `json:"facePermission"`
- IdCardPermission int `json:"idCardPermission"`
- FaceAndCardPermission int `json:"faceAndCardPermission"`
- IDPermission int `json:"iDPermission"`
- Tag string `json:"tag"`
- Phone string `json:"phone"`
- }
- type PersonUpdateTask struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- Person PersonUpdateData `json:"person"`
- }
- type PersonUpdateData struct {
- Id string `json:"id"`
- Name string `json:"name"`
- IdCardNum string `json:"idcardNum"`
- IdNumber string `json:"iDNumber"`
- FacePermission string `json:"facePermission"`
- IdCardPermission string `json:"idCardPermission"`
- FaceAndCardPermission string `json:"faceAndCardPermission"`
- IDPermission string `json:"iDPermission"`
- Tag string `json:"tag"`
- Phone string `json:"phone"`
- }
- type PersonDelTask struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- Id string `json:"id"`
- }
- type PhoneRegisterTask struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- PhotoRegisterData
- }
- type PhotoRegisterData struct {
- PersonId string `json:"personId"`
- FaceId string `json:"faceId"`
- ImgUrl string `json:"imgUrl"`
- }
- type PhotoUpdateTask struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- PhotoUpdateData
- }
- type PhotoUpdateData struct {
- PersonId string `json:"personId"`
- FaceId string `json:"faceId"`
- Url string `json:"url"`
- }
- type PhoneDelTask struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- FaceId string `json:"faceId"`
- }
- type TaskPhotoRegister struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- PersonId string `json:"personId"`
- ImgUrl string `json:"imgUrl"`
- FaceId string `json:"faceId"`
- }
- type DeviceRebootTask struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- }
- type DeviceOpenTask struct {
- TaskNo string `json:"taskNo"`
- InterfaceName string `json:"interfaceName"`
- Result bool `json:"result"`
- // 1:开门 2:串口 3:韦根 4:表示自定义文字弹
- OType int32 `json:"type"`
- }
- func getYufanTask(param string, cmdId int64) interface{} {
- taskNoStr := fmt.Sprintf("\"taskNo\":\"%d\"", cmdId)
- param = strings.ReplaceAll(param, `"taskNo":""`, taskNoStr)
- taskName := gjson.GetBytes([]byte(param), "interfaceName").String()
- switch taskName {
- case PersonRegersterTaskName:
- ret := PersonRegesterTask{}
- json.Unmarshal([]byte(param), &ret)
- return ret
- case PersonDelTaskName:
- ret := PersonDelTask{}
- json.Unmarshal([]byte(param), &ret)
- return ret
- case PhotoRegisterByUrlTaskName:
- ret := TaskPhotoRegister{}
- json.Unmarshal([]byte(param), &ret)
- return ret
- case PhotoUpdateByUrlTaskName:
- ret := PhotoUpdateTask{}
- json.Unmarshal([]byte(param), &ret)
- return ret
- }
- return nil
- }
- //
- // @Summary 宇泛心跳
- // @Description 宇泛心跳
- // @Tags 宇泛
- // @Accept json
- // @Produce json
- // @Failure 500 {object} base.HTTPError
- // @Router /api/yufan/heartbeat [post]
- func (c *Controller) YuFanHeartBeat(ctx *gin.Context) {
- // 解析参数
- body, err := ioutil.ReadAll(ctx.Request.Body)
- resp := param_v1.YuFanHeartBeatResponse{Result: false}
- if err != nil {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- fmt.Printf("心跳body:%s\n", body)
- defer ctx.Request.Body.Close()
- sn := gjson.GetBytes(body, "deviceKey").String()
- if sn == "" {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- handleServiceTask := func() error {
- mreq := pb_v1.GateOnlineRequest{Sn: sn, Protocol: GateProtocolYufanHttpV1}
- mreply, err := pb.Device.GateOnline(context.Background(), &mreq)
- if err != nil {
- s, _ := json.MarshalToString(mreq)
- logger.Error("func",
- zap.String("call", "pb.Device.GateOnline"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- }
- // 查看是否有命令需要
- if mreply.HasTask {
- resp.Result = true
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, handleServiceTask)
- }
- //
- // @Summary 宇泛获取任务
- // @Description 宇泛获取任务
- // @Tags 宇泛
- // @Accept json
- // @Produce json
- // @Failure 500 {object} base.HTTPError
- // @Router /api/yufan/task [post]
- func (c *Controller) YuFanTaskGet(ctx *gin.Context) {
- body, err := ioutil.ReadAll(ctx.Request.Body)
- resp := param_v1.YuFanHeartBeatResponse{Result: false}
- if err != nil {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- fmt.Printf("任务body:%s\n", body)
- defer ctx.Request.Body.Close()
- sn := gjson.GetBytes(body, "deviceKey").String()
- if sn == "" {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- handleServiceTask := func() error {
- mreq := pb_v1.GateCommandUseRequest{Sn: sn, Protocol: GateProtocolYufanHttpV1}
- mreply, err := pb.Device.GateCommandUse(context.Background(), &mreq)
- if err != nil {
- s, _ := json.MarshalToString(mreq)
- logger.Error("func",
- zap.String("call", "pb.Device.GateCommandUse"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- params := ""
- if params == "" {
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- yufanTask := getYufanTask(params, mreply.Id)
- if yufanTask == nil {
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- ctx.JSON(http.StatusOK, yufanTask)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, handleServiceTask)
- }
- func resultParse(info gjson.Result) (int64, string, string) {
- result := info.Get("result").Int()
- code := info.Get("code").String()
- msg := info.Get("msg").String()
- return result, code, msg
- }
- //
- // @Summary 宇泛任务结果
- // @Description 宇泛获取任务结果
- // @Tags 宇泛
- // @Accept json
- // @Produce json
- // @Failure 500 {object} base.HTTPError
- // @Router /api/yufan/task_result [post]
- func (c *Controller) YuFanTaskResult(ctx *gin.Context) {
- body, err := ioutil.ReadAll(ctx.Request.Body)
- resp := param_v1.YuFanHeartBeatResponse{Result: false}
- if err != nil {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- fmt.Printf("任务结果body:%s\n", body)
- defer ctx.Request.Body.Close()
- sn := gjson.GetBytes(body, "deviceKey").String()
- cmdId := gjson.GetBytes(body, "taskNo").String()
- info := gjson.GetBytes(body, "result")
- if sn == "" {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- handleServiceTask := func() error {
- result, code, msg := resultParse(info)
- // todo 服务
- fmt.Printf("结果:%v, %v,%v,%v\n", cmdId, result, code, msg)
- cmdIdInt, _ := strconv.ParseInt(cmdId, 10, 64)
- mreq := pb_v1.GateCommandResultRequest{
- Id: cmdIdInt,
- //CmdCode: int32(SaiboCommandToLocaolM[int32(req.CmdCode)]),
- ResultStatus: 1,
- ResultDesc: "",
- Sn: sn,
- Protocol: GateProtocolYufanHttpV1,
- Content: string(body),
- }
- // todo 测试是否有code
- if result != 1 {
- mreq.ResultStatus = 2
- mreq.ResultDesc = msg
- }
- mreply, err := pb.Device.GateCommandResult(context.Background(), &mreq)
- if err != nil {
- s, _ := json.MarshalToString(mreq)
- logger.Error("func",
- zap.String("call", "pb.Device.GateCommandResult"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- if mreply.HasTask {
- resp.Result = false
- }
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, handleServiceTask)
- }
- //
- // @Summary 宇泛照片回调
- // @Description 宇泛照片回调
- // @Tags 宇泛
- // @Accept json
- // @Produce json
- // @Failure 500 {object} base.HTTPError
- // @Router /api/yufan/photo [post]
- func (c *Controller) YuFanPhotoCallback(ctx *gin.Context) {
- body, err := ioutil.ReadAll(ctx.Request.Body)
- resp := param_v1.CallbackResponse{Result: 1, Success: true}
- if err != nil {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- fmt.Printf("照片回调body:%s\n", body)
- defer ctx.Request.Body.Close()
- deviceId := gjson.GetBytes(body, "deviceKey").String()
- uid := gjson.GetBytes(body, "personId").String()
- path := gjson.GetBytes(body, "newImgPath").String()
- faceId := gjson.GetBytes(body, "faceId").String()
- if deviceId == "" || uid == "" || faceId == "" || path == "" {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- handleServiceTask := func() error {
- // todo 服务更新照片
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, handleServiceTask)
- }
- //
- // @Summary 宇泛识别结果回调
- // @Description 宇泛识别结果回调
- // @Tags 宇泛
- // @Accept json
- // @Produce json
- // @Failure 500 {object} base.HTTPError
- // @Router /api/yufan/result [post]
- func (c *Controller) YuFanResultCallback(ctx *gin.Context) {
- body, err := ioutil.ReadAll(ctx.Request.Body)
- resp := param_v1.CallbackResponse{Result: 1, Success: true}
- if err != nil {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- fmt.Printf("识别结果回调body:%s\n", body)
- defer ctx.Request.Body.Close()
- sn := gjson.GetBytes(body, "deviceKey").String()
- personId := gjson.GetBytes(body, "personId").String()
- idcardNum := gjson.GetBytes(body, "idcardNum").String()
- model := gjson.GetBytes(body, "model").String()
- identifyType := gjson.GetBytes(body, "identifyType").String()
- mstime := gjson.GetBytes(body, "time").String()
- mstimeInt, _ := strconv.ParseInt(mstime, 10, 64)
- t := time.UnixMilli(mstimeInt)
- if sn == "" {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- if identifyType != "1" {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- if model != "0" && model != "3" {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- codeVal := personId
- codeType := 1
- uid := int64(0)
- uid, _ = strconv.ParseInt(personId, 10, 64)
- openType := int32(3)
- if model == "3" {
- codeVal = idcardNum
- codeType = 2
- openType = 2
- }
- // 检查白名单
- mreq := pb_v1.GateWhiteMatchRequest{
- CodeVal: codeVal,
- CodeType: int32(codeType),
- Sn: sn,
- Protocol: GateProtocolYufanHttpV1,
- }
- mreply, err := pb.Device.GateWhiteMatch(context.Background(), &mreq)
- if err != nil {
- s, _ := json.MarshalToString(mreq)
- logger.Error("func",
- zap.String("call", "pb.Device.GateWhiteMatch"),
- zap.String("params", s),
- zap.String("error", err.Error()))
- ctx.JSON(http.StatusOK, resp)
- return
- }
- if mreply.DeviceId == 0 {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- handleServiceTask := func() error {
- fmt.Printf("%v,%v,%v,%v\n", personId, idcardNum, model, identifyType)
- recordReq := pb_v1.GateRecordAddRequest{
- DeviceId: mreply.DeviceId,
- Location: mreply.Location,
- Direction: mreply.Direction,
- HouseholdUser: mreply.Name,
- HouseholdIdNumber: mreply.IdNumber,
- HouseholdHousename: mreply.HouseName,
- CardNumber: mreply.CardNumber,
- CardOwner: mreply.Name,
- Online: 2,
- GardenId: mreply.GardenId,
- // 1 访客 2 非访客
- IsVisitor: 2,
- // 开门时间
- OpenTime: t.Unix(),
- HouseholdUid: uid,
- VisitorPhone: "vphone",
- VisitorName: "vname",
- Sn: sn,
- Protocol: GateProtocolYufanHttpV1,
- OpenType: openType,
- }
- _, _ = pb.Device.GateRecordAdd(context.Background(), &recordReq)
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, handleServiceTask)
- }
- func checkQCode(codeStr string, gateKey string) (string, int32, string, string, int64) {
- loc, _ := time.LoadLocation("Local")
- visitor := int32(2)
- vname, vphone := "", ""
- vid := int64(0)
- text := Rc4Decrypt(codeStr, gateKey)
- if text == "" {
- return "", 0, "", "", 0
- }
- length := len(text)
- array := strings.Split(text[1:length-1], ",")
- uid := array[0]
- if len(array) < 4 {
- return "", 0, "", "", 0
- }
- subArray := strings.Split(array[0], "-")
- if len(subArray) > 3 {
- uid = subArray[0]
- visitor = 1
- vphone = subArray[1]
- vname = subArray[2]
- vid, _ = strconv.ParseInt(subArray[3], 10, 64)
- }
- start, _ := time.ParseInLocation("20060102150405", array[2], loc)
- end, _ := time.ParseInLocation("20060102150405", array[3], loc)
- now := time.Now()
- if now.Unix() < start.Unix() || now.Unix() > end.Unix() {
- return "", 0, "", "", 0
- }
- return uid, visitor, vname, vphone, vid
- }
- func whiteCheck(sn string, protocol int32, codeVal string, codeType int32) (*pb_v1.GateWhiteMatchReply, error) {
- mreq := pb_v1.GateWhiteMatchRequest{
- Sn: sn,
- Protocol: protocol,
- CodeVal: codeVal,
- CodeType: codeType,
- }
- return pb.Device.GateWhiteMatch(context.Background(), &mreq)
- }
- //
- // @Summary 二维码回调
- // @Description 二维码回调
- // @Tags 宇泛
- // @Accept json
- // @Produce json
- // @Failure 500 {object} base.HTTPError
- // @Router /api/yufan/qcode [post]
- func (c *Controller) YufanQcodeCallback(ctx *gin.Context) {
- body, err := ioutil.ReadAll(ctx.Request.Body)
- resp := param_v1.QcodeCallbackResponse{TttsModeContent: "自定义语音输出", DisplayModContent: "界面自定义显示", IsOpenRelay: 0}
- if err != nil {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- fmt.Printf("识别结果回调body:%s\n", body)
- defer ctx.Request.Body.Close()
- sn := gjson.GetBytes(body, "deviceKey").String()
- qrdata := gjson.GetBytes(body, "QRdata").String()
- uidStr, visitor, vname, vphone, vid := checkQCode(qrdata, parser.Conf.GateKey)
- if uidStr == "" {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- uid, _ := strconv.ParseInt(uidStr, 10, 64)
- whiteInfo, err := whiteCheck(sn, GateProtocolYufanHttpV1, uidStr, 1)
- if err != nil || whiteInfo.Status != 1 {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- now := time.Now()
- if visitor == 1 {
- vreq := pb_v1.GateVisitorCheckRequest{
- Id: vid,
- OpenTime: now.Unix(),
- DeviceId: whiteInfo.DeviceId,
- }
- _, err := pb.Device.GateVisitorCheck(context.Background(), &vreq)
- if err != nil {
- ctx.JSON(http.StatusOK, resp)
- return
- }
- }
- handleServiceTask := func() error {
- recordReq := pb_v1.GateRecordAddRequest{
- Sn: sn,
- Protocol: GateProtocolSaiboMqttV1,
- OpenTime: now.Unix(),
- HouseholdUid: uid,
- Location: whiteInfo.Location,
- Direction: whiteInfo.Direction,
- HouseholdIdNumber: whiteInfo.IdNumber,
- HouseholdHousename: whiteInfo.HouseName,
- DeviceId: whiteInfo.DeviceId,
- HouseholdUser: whiteInfo.Name,
- IsVisitor: visitor,
- GardenId: whiteInfo.GardenId,
- OpenType: 1,
- CardNumber: "",
- VisitorPhone: vphone,
- VisitorName: vname,
- }
- _, _ = pb.Device.GateRecordAdd(context.Background(), &recordReq)
- resp.IsOpenRelay = 1
- ctx.JSON(http.StatusOK, resp)
- return nil
- }
- // 执行任务
- httptasker.Exec(ctx, handleServiceTask)
- }
|