Ver Fonte

门禁系统

huangyan há 2 meses atrás
pai
commit
dc7471995c

BIN
cmd/server/city_chips


+ 9 - 5
config/local.yml

@@ -27,11 +27,12 @@ log:
   compress: true               # 是否压缩
 #立方停车http协议(Ver2.1.2)
 lifang:
-  baseUrl:
-    Login: "/Login" #账号登录接口
-    GetCarCodes: "/GetCarCodes" #模糊查询车牌号接口
-    GetCarInfo: "/GetCarInfo" #查询车牌号信息接口
-    GetParkingLotInfo: "/GetParkingLotInfo" #查询车场信息接口
+  baseUrl: "http://[ip]:[port]/[路径]"
+  Login: "/Login" #账号登录接口
+  GetCarCodes: "/GetCarCodes" #模糊查询车牌号接口
+  GetCarInfo: "/GetCarInfo" #查询车牌号信息接口
+  GetParkingLotInfo: "/GetParkingLotInfo" #查询车场信息接口
+
 #海康威视
 hikvision:
   Ip: ""
@@ -39,6 +40,7 @@ hikvision:
   AppKey: ""
   Secret: ""
   IsHttps: false
+  pageSize: 9999
   api:
     nodesByParams: "/api/irds/v2/region/nodesByParams" #根据查询条件查询区域列表信息,主要用于区域信息查询过滤。相对V1接口,支持级联场景的区域查询
     deviceResource: "/api/irds/v2/deviceResource/resources" #根据资源类型分页获取资源列表,主要用于资源信息的全量同步。
@@ -46,6 +48,8 @@ hikvision:
     previewURLs: "/api/video/v2/cameras/previewURLs"#获取监控点预览取流URLv2
     controlling: "/api/video/v1/ptzs/controlling" #根据监控点编号进行云台操作接口
     visitorInfo: "/api/v1/visitorInfo/count" #获取今日访客信息包含:今日来访总人数(已签离人数,未签离人数),预约人数
+    doorSearch: "/api/resource/v2/door/search" #查询门禁设备列表接口
+    doorStates: "/api/acs/v1/door/states" #查询门禁设备状态接口
 #会议系统
 conference:
   pageSize: 9999

+ 88 - 0
internal/handler/accesscontrol.go

@@ -4,10 +4,12 @@ import (
 	"city_chips/internal/model"
 	"city_chips/internal/service"
 	"city_chips/pkg/helper/resp"
+	"encoding/json"
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/spf13/viper"
 	"math/rand"
+	"strconv"
 	"time"
 )
 
@@ -58,3 +60,89 @@ func (h *AccessControlHandler) GetAccessControl(ctx *gin.Context) {
 	m["DeviceList"] = device           //设备列表
 	resp.HandleSuccess(ctx, m)
 }
+
+// GetParkingLotInfo 查询车场信息
+func (h *AccessControlHandler) GetParkingLotInfo(ctx *gin.Context) {
+	// 设置响应头
+	ctx.Header("Content-Type", "text/event-stream")
+	ctx.Header("Cache-Control", "no-cache")
+	ctx.Header("Connection", "keep-alive")
+	// 监听客户端断开连接
+	conn := true
+	notify := ctx.Writer.CloseNotify()
+	type Response struct {
+		RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
+		Code      int32  `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
+		Msg       string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
+		Data      any    `json:"data"`
+	}
+	stationNo := ctx.Query("stationNo")
+	//GetParkingLotInfo := h.conf.GetString("lifang.baseurl") + h.conf.GetString("lifang.GetParkingLotInfo")
+	m := make(map[string]any)
+	m["stationNo"] = stationNo
+	moni := make(map[string]any)
+	moni["stationNo"] = stationNo
+	var response Response
+	for conn {
+		select {
+		case <-notify:
+			conn = false
+			fmt.Println("断开连接")
+			return
+		default:
+			//request, err := h.accessControlService.SendRequest(http.DefaultClient, "POST", GetParkingLotInfo, m)
+			//if err != nil {
+			//	resp.HandleError(ctx, 1203, "获取停车场信息失败", err)
+			//	return
+			//}
+			var parkingLot model.ParkingLot
+			//if gjson.Get(string(request), "resCode").Int() == 0 {
+			//	err = json.Unmarshal(request, &parkingLot)
+			//	if err != nil {
+			//		h.logger.Error("json序列化失败")
+			//		response.Code = 1203
+			//		response.Msg = "json序列化失败"
+			//		response.Data = nil
+			//		res, _ := json.Marshal(&response)
+			//		fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			//		ctx.Writer.Flush()
+			//		return
+			//	}
+			//} else {
+			//	response.Code = 1203
+			//	response.Msg = "获取停车场信息失败"
+			//	response.Data = string(request)
+			//	res, _ := json.Marshal(&response)
+			//	fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			//	ctx.Writer.Flush()
+			//	return
+			//}
+			parkingLot.ResCode = 0
+			parkingLot.ResMsg = "查询成功"
+			parkingLot.TotalNum = rand.Intn(100)
+			parkingLot.TotalStopNum = rand.Intn(100)
+			parkingLot.TotalRemainNum = rand.Intn(100)
+			parkingLot.ParkID = strconv.Itoa(rand.Intn(100))
+			parkingLot.ParkName = "测试停车场"
+			parkingLot.ChargeRuleDesc = "2元/小时"
+			for i := 0; i < 3; i++ {
+				sprintf := fmt.Sprintf("设备%v", i+1)
+				info := model.ParkingLotInfo{
+					ParkingLotId:   rand.Intn(100),
+					ParkingLotName: sprintf,
+					TotalNum:       rand.Intn(100),
+					TotalStopNum:   rand.Intn(100),
+					TotalRemainNum: rand.Intn(100),
+				}
+				parkingLot.ParkingLotInfo = append(parkingLot.ParkingLotInfo, info)
+			}
+			response.Code = 200
+			response.Msg = "查询成功"
+			response.Data = parkingLot
+			res, _ := json.Marshal(&response)
+			fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			ctx.Writer.Flush()
+			time.Sleep(10 * time.Second)
+		}
+	}
+}

+ 166 - 1
internal/handler/hikvision.go

@@ -4,6 +4,7 @@ import (
 	"city_chips/internal/model"
 	"city_chips/internal/service"
 	"city_chips/pkg/helper/resp"
+	"city_chips/pkg/helper/uuid"
 	"encoding/json"
 	"fmt"
 	"math/rand"
@@ -141,7 +142,7 @@ func (h *HikvisionHandler) GetElectronicInspections(ctx *gin.Context) {
 	resp.HandleSuccess(ctx, m)
 }
 
-// 访客系统
+// GetVisitor 访客系统
 func (h *HikvisionHandler) GetVisitor(ctx *gin.Context) {
 	m := make(map[string]any)
 	PrevailingTrends := make(map[string]any)
@@ -397,3 +398,167 @@ func (h *HikvisionHandler) VisitorInfoCount(c *gin.Context) {
 	}
 	resp.HandleSuccess(c, hikvision.Data)
 }
+
+// GetDoorSearch 查询门禁点列表v2
+func (h *HikvisionHandler) GetDoorSearch(ctx *gin.Context) {
+	// 设置响应头
+	ctx.Header("Content-Type", "text/event-stream")
+	ctx.Header("Cache-Control", "no-cache")
+	ctx.Header("Connection", "keep-alive")
+	// 监听客户端断开连接
+	conn := true
+	notify := ctx.Writer.CloseNotify()
+
+	var response model.Response
+	var doorlist []model.DoorList
+	m := make(map[string]string)
+	m["pageNo"] = "1"
+	m["pageSize"] = "1"
+
+	for conn {
+		select {
+		case <-notify:
+			conn = false
+			fmt.Println("断开连接")
+			return
+		default:
+			//hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.visitorInfo"), m, 15)
+			//if err != nil {
+			//	h.logger.Error("获取门禁点列表失败")
+			//	response.Code = 1203
+			//	response.Msg = "获取门禁点列表失败"
+			//	response.Data = nil
+			//	res, _ := json.Marshal(&response)
+			//	fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			//	ctx.Writer.Flush()
+			//	return
+			//}
+			//if hikvision.Code != "0" {
+			//	response.Code = 1203
+			//	response.Msg = "获取门禁点列表失败"
+			//	response.Data = nil
+			//	res, _ := json.Marshal(&response)
+			//	fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			//	ctx.Writer.Flush()
+			//  conn = false
+			//	return
+			//}
+			for i := 0; i < 3; i++ {
+				genUUID := uuid.GenUUID()
+				name := fmt.Sprintf("资源:%v", i+1)
+
+				list := model.DoorList{
+					IndexCode:       genUUID,
+					ResourceType:    "door",
+					Name:            name,
+					DoorNo:          genUUID,
+					ChannelNo:       genUUID,
+					ParentIndexCode: genUUID,
+					ControlOneId:    genUUID,
+					ControlTwoId:    genUUID,
+					ReaderInId:      genUUID,
+					ReaderOutId:     genUUID,
+					DoorSerial:      i + 1,
+					TreatyType:      genUUID,
+					RegionIndexCode: genUUID,
+					RegionPath:      genUUID,
+					CreateTime:      time.Now().Format("2006-01-02 15:04:05"),
+					UpdateTime:      time.Now().Format("2006-01-02 15:04:05"),
+					Description:     genUUID,
+					ChannelType:     genUUID,
+					RegionName:      genUUID,
+					RegionPathName:  genUUID,
+					InstallLocation: genUUID,
+				}
+				doorlist = append(doorlist, list)
+			}
+			doorResp := model.DoorResp{
+				Code: "0",
+				Msg:  "SUCCESS",
+				Data: struct {
+					Total    int              `json:"total"`
+					PageNo   int              `json:"pageNo"`
+					PageSize int              `json:"pageSize"`
+					List     []model.DoorList `json:"list"`
+				}{Total: 3, PageNo: 1, PageSize: 1, List: doorlist},
+			}
+			response.Code = 200
+			response.Msg = "获取门禁点列表成功"
+			response.Data = doorResp.Data
+			res, _ := json.Marshal(&response)
+			fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			ctx.Writer.Flush()
+			time.Sleep(10 * time.Second)
+		}
+	}
+}
+
+// DoControl 控制门禁
+func (h *HikvisionHandler) DoControl(ctx *gin.Context) {
+	// 设置响应头
+	ctx.Header("Content-Type", "text/event-stream")
+	ctx.Header("Cache-Control", "no-cache")
+	ctx.Header("Connection", "keep-alive")
+	// 监听客户端断开连接
+	conn := true
+	notify := ctx.Writer.CloseNotify()
+	doorIndexCodes := ctx.Query("doorIndexCodes")
+	controlType := ctx.Query("controlType")
+	if len(doorIndexCodes) <= 0 || len(controlType) <= 0 {
+		resp.HandleError(ctx, 1203, "设备编码不能为空", nil)
+		return
+	}
+	fmt.Println("doorIndexCodes:", doorIndexCodes)
+	m := make(map[string]string)
+	m["doorIndexCodes"] = doorIndexCodes
+	m["controlType"] = controlType
+	var response model.Response
+	for conn {
+		select {
+		case <-notify:
+			conn = false
+			fmt.Println("断开连接")
+			return
+		default:
+			//hikvision, err := h.hikvisionService.Hikvision(h.conf.GetString("hikvision.api.control"), m, 15)
+			//if err != nil {
+			//	h.logger.Error("控制门禁失败")
+			//	response.Code = 1203
+			//	response.Msg = "控制门禁失败"
+			//	response.Data = nil
+			//	res, _ := json.Marshal(&response)
+			//	fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			//	ctx.Writer.Flush()
+			//	conn = false
+			//	return
+			//}
+			//if hikvision.Code != "0" {
+			//	response.Code = 1203
+			//	response.Msg = "控制门禁失败"
+			//	response.Data = nil
+			//	res, _ := json.Marshal(&response)
+			//	fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			//	ctx.Writer.Flush()
+			//	conn = false
+			//	return
+			//}
+			genUUID := uuid.GenUUID()
+			doControl := model.DoControl{
+				Code: "0",
+				Msg:  "SUCCESS",
+				Data: []model.DoControlData{{
+					DoorIndexCode:     genUUID,
+					ControlResultCode: 0,
+					ControlResultDesc: "success",
+				}},
+			}
+			response.Code = 200
+			response.Msg = "控制门禁成功"
+			response.Data = doControl.Data
+			res, _ := json.Marshal(&response)
+			fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
+			ctx.Writer.Flush()
+			time.Sleep(10 * time.Second)
+		}
+	}
+}

+ 18 - 0
internal/model/accesscontrol.go

@@ -10,6 +10,24 @@ type DeviceRanking struct {
 	DeviceName string `json:"DeviceName"`
 	Value      int    `json:"Value"`
 }
+type ParkingLot struct {
+	ResCode        int              `json:"resCode"`
+	ResMsg         string           `json:"resMsg"`
+	TotalNum       int              `json:"totalNum"`
+	TotalStopNum   int              `json:"totalStopNum"`
+	TotalRemainNum int              `json:"totalRemainNum"`
+	ParkID         string           `json:"parkID"`
+	ParkName       string           `json:"parkName"`
+	ChargeRuleDesc string           `json:"chargeRuleDesc"`
+	ParkingLotInfo []ParkingLotInfo `json:"parkingLotInfo"`
+}
+type ParkingLotInfo struct {
+	ParkingLotId   int    `json:"parkingLotId"`
+	ParkingLotName string `json:"parkingLotName"`
+	TotalNum       int    `json:"totalNum"`
+	TotalStopNum   int    `json:"totalStopNum"`
+	TotalRemainNum int    `json:"totalRemainNum"`
+}
 
 func (m *AccessControl) TableName() string {
 	return "access_control"

+ 64 - 0
internal/model/hikvision.go

@@ -37,6 +37,70 @@ type AlarmList struct {
 	State        int    `json:"State"`        //状态
 	Date         string `json:"Date"`         //时间
 }
+type Door struct {
+	Name             string   `json:"name"`
+	RegionIndexCodes []string `json:"regionIndexCodes"`
+	IsSubRegion      bool     `json:"isSubRegion"`
+	PageNo           int      `json:"pageNo"`
+	PageSize         int      `json:"pageSize"`
+	AuthCodes        []string `json:"authCodes"`
+	Expressions      []struct {
+		Key      string   `json:"key"`
+		Operator int      `json:"operator"`
+		Values   []string `json:"values"`
+	} `json:"expressions"`
+	OrderBy   string `json:"orderBy"`
+	OrderType string `json:"orderType"`
+}
+type DoorResp struct {
+	Code string `json:"code"`
+	Msg  string `json:"msg"`
+	Data struct {
+		Total    int        `json:"total"`
+		PageNo   int        `json:"pageNo"`
+		PageSize int        `json:"pageSize"`
+		List     []DoorList `json:"list"`
+	} `json:"data"`
+}
+type DoorList struct {
+	IndexCode       string `json:"indexCode"`
+	ResourceType    string `json:"resourceType"`
+	Name            string `json:"name"`
+	DoorNo          string `json:"doorNo"`
+	ChannelNo       string `json:"channelNo"`
+	ParentIndexCode string `json:"parentIndexCode"`
+	ControlOneId    string `json:"controlOneId"`
+	ControlTwoId    string `json:"controlTwoId"`
+	ReaderInId      string `json:"readerInId"`
+	ReaderOutId     string `json:"readerOutId"`
+	DoorSerial      int    `json:"doorSerial"`
+	TreatyType      string `json:"treatyType"`
+	RegionIndexCode string `json:"regionIndexCode"`
+	RegionPath      string `json:"regionPath"`
+	CreateTime      string `json:"createTime"`
+	UpdateTime      string `json:"updateTime"`
+	Description     string `json:"description"`
+	ChannelType     string `json:"channelType"`
+	RegionName      string `json:"regionName"`
+	RegionPathName  string `json:"regionPathName"`
+	InstallLocation string `json:"installLocation"`
+}
+type Response struct {
+	RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
+	Code      int32  `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
+	Msg       string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
+	Data      any    `json:"data"`
+}
+type DoControl struct {
+	Code string          `json:"code"`
+	Msg  string          `json:"msg"`
+	Data []DoControlData `json:"data"`
+}
+type DoControlData struct {
+	DoorIndexCode     string `json:"doorIndexCode"`
+	ControlResultCode int    `json:"controlResultCode"`
+	ControlResultDesc string `json:"controlResultDesc"`
+}
 
 func (m *Hikvision) TableName() string {
 	return "hikvision"

+ 3 - 0
internal/server/http.go

@@ -31,6 +31,7 @@ func NewServerHTTP(
 	Access := r.Group("/Access")
 	{
 		Access.GET("/count", accessHandler.GetAccessControl)
+		Access.GET("/getParkingLotInfo", accessHandler.GetParkingLotInfo)
 	}
 	//海康威视
 	Hikvision := r.Group("/Hikvision")
@@ -44,6 +45,8 @@ func NewServerHTTP(
 		Hikvision.GET("/visitor", hikvision.GetVisitor)
 		Hikvision.GET("/passenger", hikvision.GetPassenger)
 		Hikvision.GET("/access", hikvision.GetAccess)
+		Hikvision.GET("/getDoorSearch", hikvision.GetDoorSearch)
+		Hikvision.GET("/doControl", hikvision.DoControl)
 	}
 	//会议系统
 	Conference := r.Group("/Conference")

+ 38 - 0
internal/service/accesscontrol.go

@@ -1,12 +1,18 @@
 package service
 
 import (
+	"bytes"
 	"city_chips/internal/model"
 	"city_chips/internal/repository"
+	"encoding/json"
+	"fmt"
+	"io"
+	"net/http"
 )
 
 type AccessControlService interface {
 	GetAccessControl(id int64) (*model.AccessControl, error)
+	SendRequest(client HTTPClient, method, url string, body interface{}) ([]byte, error)
 }
 
 func NewAccessControlService(service *Service, accessControlRepository repository.AccessControlRepository) AccessControlService {
@@ -21,6 +27,38 @@ type accessControlService struct {
 	accessControlRepository repository.AccessControlRepository
 }
 
+func (s *accessControlService) SendRequest(client HTTPClient, method, url string, body interface{}) ([]byte, error) {
+	var requestBody io.Reader
+
+	if body != nil {
+		jsonBytes, err := json.Marshal(body)
+		if err != nil {
+			return nil, err
+		}
+		requestBody = bytes.NewBuffer(jsonBytes)
+	}
+	req, err := http.NewRequest(method, url, requestBody)
+	if err != nil {
+		return nil, err
+	}
+	resp, err := client.Do(req)
+	if err != nil {
+		return nil, err
+	}
+	defer resp.Body.Close()
+
+	responseBody, err := io.ReadAll(resp.Body)
+	if err != nil {
+		return nil, err
+	}
+
+	if resp.StatusCode >= 400 {
+		return nil, fmt.Errorf("bad status: %s", resp.Status)
+	}
+
+	return responseBody, nil
+}
+
 func (s *accessControlService) GetAccessControl(id int64) (*model.AccessControl, error) {
 	return s.accessControlRepository.GetAccessControl(id)
 }