123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package handler
- import (
- "city_chips/internal/model"
- "city_chips/internal/service"
- "city_chips/pkg/helper/resp"
- "math/rand"
- "time"
- "github.com/spf13/viper"
- "github.com/gin-gonic/gin"
- )
- type IntelligentBuildingControlHandler struct {
- *Handler
- intelligentBuildingControlService service.IntelligentBuildingControlService
- conf *viper.Viper
- }
- func NewIntelligentBuildingControlHandler(
- handler *Handler,
- intelligentBuildingControlService service.IntelligentBuildingControlService,
- conf *viper.Viper,
- ) *IntelligentBuildingControlHandler {
- return &IntelligentBuildingControlHandler{
- Handler: handler,
- intelligentBuildingControlService: intelligentBuildingControlService,
- conf: conf,
- }
- }
- // 智能楼宇控制系统
- // 示例:创建一个报警记录
- func generateAlarm() model.AlarmList {
- return model.AlarmList{
- Id: rand.Intn(100),
- Name: model.GetRandomItem(model.DeviceNames),
- State: rand.Intn(2), // 假设0为正常,1为报警
- Date: time.Now().Format("2006-01-02 15:04:05"),
- Location: model.GetRandomItem(model.Locations),
- AlarmContent: model.GetRandomItem(model.AlarmContents),
- }
- }
- func (h *IntelligentBuildingControlHandler) GetIntelligentBuildingControl(ctx *gin.Context) {
- m := make(map[string]any)
- var device []model.AlarmList
- for i := 0; i < 20; i++ {
- alarm := generateAlarm()
- device = append(device, alarm)
- }
- count, err := h.intelligentBuildingControlService.DeviceCount()
- if err != nil {
- resp.HandleError(ctx, 1201, "查询设备总数失败", nil)
- return
- }
- m["DeviceCount"] = count //设备总数
- m["StopState"] = rand.Intn(100) //停止状态
- m["RunState"] = rand.Intn(100) //运行状态
- m["FaultState"] = rand.Intn(1000) //故障状态
- m["DeviceList"] = device //设备列表
- resp.HandleSuccess(ctx, m)
- }
- // GetPoint 获取点位数据
- func (h *IntelligentBuildingControlHandler) GetPoint(ctx *gin.Context) {
- pointName := ctx.PostForm("pointName")
- deviceType := ctx.PostForm("deviceType")
- building := ctx.PostForm("building")
- floor := ctx.PostForm("floor")
- section := ctx.PostForm("section")
- device_name := ctx.PostForm("deviceName")
- conds := make(map[string]any)
- if pointName != "" {
- conds["point_name"] = pointName
- }
- if deviceType != "" {
- conds["device_type"] = deviceType
- }
- if building != "" {
- conds["building"] = building
- }
- if floor != "" {
- conds["floor"] = floor
- }
- if section != "" {
- conds["section"] = section
- }
- if device_name != "" {
- conds["device_name"] = device_name
- }
- baseUrl := h.conf.GetString("obix.baseUrl")
- println("obix baseUrl:", baseUrl)
- points, err := h.intelligentBuildingControlService.GetPoint(conds)
- if err != nil {
- resp.HandleError(ctx, 1201, "查询点位失败", nil)
- return
- }
- resp.HandleSuccess(ctx, points)
- }
- // GetPointType 获取点位类型
- func (h *IntelligentBuildingControlHandler) GetPointType(ctx *gin.Context) {
- points, err := h.intelligentBuildingControlService.GetPointType()
- if err != nil {
- resp.HandleError(ctx, 1201, "查询点位类型失败", nil)
- return
- }
- resp.HandleSuccess(ctx, points)
- }
- func (h *IntelligentBuildingControlHandler) GetDeviceType(ctx *gin.Context) {
- points, err := h.intelligentBuildingControlService.GetPointType()
- if err != nil {
- resp.HandleError(ctx, 1201, "查询设备类型失败", nil)
- return
- }
- m := make(map[string]string)
- for _, v := range points {
- m[v] = model.DeviceType[v]
- }
- resp.HandleSuccess(ctx, m)
- }
|