|
@@ -7,8 +7,8 @@ import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"go.uber.org/zap"
|
|
|
- "math/rand"
|
|
|
"strconv"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
@@ -33,90 +33,116 @@ func NewIlluminatingHandler(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-// 照明系统
|
|
|
+// GetIlluminating 照明系统
|
|
|
func (h *IlluminatingHandler) GetIlluminating(ctx *gin.Context) {
|
|
|
m := make(map[string]any)
|
|
|
- runAnaly := make(map[string]any)
|
|
|
- var illuminatingDevice []model.IlluminatingDevice
|
|
|
- var eventList []model.EventList
|
|
|
- var runAnalyse model.RunAnalyse
|
|
|
- for i := 0; i < 20; i++ {
|
|
|
- device := model.IlluminatingDevice{
|
|
|
- Id: i + 1,
|
|
|
- DeviceName: model.GetRandomItem(model.IlluminatingDeviceNames),
|
|
|
- SwitchStatus: rand.Intn(2),
|
|
|
- OnlinePresence: rand.Intn(2),
|
|
|
- }
|
|
|
- list := model.EventList{
|
|
|
- Id: i + 1,
|
|
|
- DeviceName: model.GetRandomItem(model.LightingEvents),
|
|
|
- DeviceStatus: rand.Intn(2),
|
|
|
- EventDate: time.Now().Format("2006-01-02 15:04:05"),
|
|
|
- }
|
|
|
- eventList = append(eventList, list)
|
|
|
- illuminatingDevice = append(illuminatingDevice, device)
|
|
|
- }
|
|
|
- for i := 0; i < 7; i++ {
|
|
|
- date := fmt.Sprintf("2025-5-%v", i+1)
|
|
|
- runAnalyse.FaultCount = rand.Intn(100)
|
|
|
- runAnalyse.OpenCount = rand.Intn(100)
|
|
|
- runAnalyse.OffCount = rand.Intn(100)
|
|
|
- runAnaly[date] = runAnalyse
|
|
|
- }
|
|
|
- m["IlluminatingDevice"] = illuminatingDevice //设备列表
|
|
|
- m["DeviceCount"] = rand.Intn(100) //设备总数
|
|
|
- m["OpenCount"] = rand.Intn(100) //开启数量
|
|
|
- m["OffCount"] = rand.Intn(100) //关闭数量
|
|
|
- m["FaultCount"] = rand.Intn(100) //故障数量
|
|
|
- m["RunAnalyse"] = runAnaly //运行分析
|
|
|
- m["EventList"] = eventList //事件列表
|
|
|
- resp.HandleSuccess(ctx, m)
|
|
|
-}
|
|
|
+ var alarmstatistics model.AlarmStatistics
|
|
|
+ var alarmCount model.AlarmCount
|
|
|
+ var statis model.IlluminatingStatistics
|
|
|
|
|
|
-// 获取灯光状态
|
|
|
-func (h *IlluminatingHandler) GetLightingstatus(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("id")
|
|
|
- if len(stationNo) == 0 {
|
|
|
- resp.HandleError(ctx, 1201, "缺少必要参数", nil)
|
|
|
+ now := time.Now()
|
|
|
+ sevenDaysAgo := now.AddDate(0, 0, -7).Format("2006-01-02")
|
|
|
+ format := now.Format("2006-01-02")
|
|
|
+ alarmStatistics, err := h.illuminatingService.GetAlarmStatistics(sevenDaysAgo, format)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("获取告警信息统计失败", zap.Any("err", err))
|
|
|
+ resp.HandleError(ctx, 1204, "获取告警信息统计失败", nil)
|
|
|
return
|
|
|
}
|
|
|
- var response Response
|
|
|
- for conn {
|
|
|
- select {
|
|
|
- case <-notify:
|
|
|
- conn = false
|
|
|
- fmt.Println("断开连接")
|
|
|
- return
|
|
|
- default:
|
|
|
- // 模拟数据
|
|
|
- data := make(map[string]any)
|
|
|
- response.Code = 200
|
|
|
- response.RequestId = strconv.Itoa(rand.Intn(1000))
|
|
|
- response.Msg = "success"
|
|
|
- data["设备名称"] = model.GetRandomItem(model.IlluminatingDeviceNames)
|
|
|
- data["state"] = rand.Intn(2) // 1开 0关 2故障
|
|
|
- response.Data = data
|
|
|
- res, _ := json.Marshal(&response)
|
|
|
- fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
|
- ctx.Writer.Flush()
|
|
|
- time.Sleep(10 * time.Second)
|
|
|
- }
|
|
|
+ err = json.Unmarshal(alarmStatistics, &alarmstatistics)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("json反序列化失败")
|
|
|
+ resp.HandleError(ctx, 1205, "json序列化失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ alarm, err := h.illuminatingService.GetAlarm(1, 20)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("获取告警信息失败", zap.Any("err", err))
|
|
|
+ resp.HandleError(ctx, 1203, "获取告警信息失败", nil)
|
|
|
+ return
|
|
|
}
|
|
|
+ err = json.Unmarshal(alarm, &alarmCount)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("json反序列化失败")
|
|
|
+ resp.HandleError(ctx, 1205, "json序列化失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ statistics, err := h.illuminatingService.GetStatistics()
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("获取统计信息失败", zap.Any("err", err))
|
|
|
+ resp.HandleError(ctx, 1201, "获取统计信息失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = json.Unmarshal(statistics, &statis)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("json反序列化失败")
|
|
|
+ resp.HandleError(ctx, 1202, "json反序列化失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for i, _ := range alarmstatistics.TimeStatistics {
|
|
|
+ s := alarmstatistics.TimeStatistics[i].TimeDay.Format("2006-01-02 15:04:05")
|
|
|
+ alarmstatistics.TimeStatistics[i].Time = s
|
|
|
+ }
|
|
|
+ baseecic, err := h.illuminatingService.GetBaseecic(1, 0, -1, 0, 20, "", "")
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("获取 basement_ecic 列表失败", zap.Any("err", err))
|
|
|
+ resp.HandleError(ctx, 1202, "获取 basement_ecic 列表失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ m["IlluminatingDevice"] = baseecic.Devices //设备列表
|
|
|
+ m["DeviceCount"] = statis.DeviceCount //设备总数
|
|
|
+ m["GatewayCount"] = statis.GatewayCount //网关数量
|
|
|
+ m["OffCount"] = statis.StrategyCount //策略总数
|
|
|
+ m["AlarmCount"] = statis.AlarmCount //报警数量
|
|
|
+ m["RunAnalyse"] = alarmstatistics.TimeStatistics //运行分析
|
|
|
+ m["EventList"] = alarmCount //报警事件
|
|
|
+ resp.HandleSuccess(ctx, m)
|
|
|
}
|
|
|
|
|
|
+// 获取灯光状态
|
|
|
+//func (h *IlluminatingHandler) GetLightingstatus(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("id")
|
|
|
+// if len(stationNo) == 0 {
|
|
|
+// resp.HandleError(ctx, 1201, "缺少必要参数", nil)
|
|
|
+// return
|
|
|
+// }
|
|
|
+// var response Response
|
|
|
+// for conn {
|
|
|
+// select {
|
|
|
+// case <-notify:
|
|
|
+// conn = false
|
|
|
+// fmt.Println("断开连接")
|
|
|
+// return
|
|
|
+// default:
|
|
|
+// // 模拟数据
|
|
|
+// data := make(map[string]any)
|
|
|
+// response.Code = 200
|
|
|
+// response.RequestId = strconv.Itoa(rand.Intn(1000))
|
|
|
+// response.Msg = "success"
|
|
|
+// data["设备名称"] = model.GetRandomItem(model.IlluminatingDeviceNames)
|
|
|
+// data["state"] = rand.Intn(2) // 1开 0关 2故障
|
|
|
+// response.Data = data
|
|
|
+// res, _ := json.Marshal(&response)
|
|
|
+// fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
|
+// ctx.Writer.Flush()
|
|
|
+// time.Sleep(10 * time.Second)
|
|
|
+// }
|
|
|
+// }
|
|
|
+//}
|
|
|
+
|
|
|
// 修改灯光状态
|
|
|
func (h *IlluminatingHandler) UpdataLightingStatus(ctx *gin.Context) {
|
|
|
id := ctx.PostForm("id")
|
|
@@ -144,6 +170,13 @@ func (h *IlluminatingHandler) GetStatistics(ctx *gin.Context) {
|
|
|
resp.HandleError(ctx, 1202, "json反序列化失败", nil)
|
|
|
return
|
|
|
}
|
|
|
+ alarm, err := h.illuminatingService.GetAlarm(1, 20)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("获取告警信息失败", zap.Any("err", err))
|
|
|
+ resp.HandleError(ctx, 1203, "获取告警信息失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ fmt.Println("报警信息统计", string(alarm))
|
|
|
resp.HandleSuccess(ctx, statis)
|
|
|
}
|
|
|
func (h *IlluminatingHandler) GetBaseecic(ctx *gin.Context) {
|
|
@@ -151,18 +184,18 @@ func (h *IlluminatingHandler) GetBaseecic(ctx *gin.Context) {
|
|
|
currentPage, err := strconv.Atoi(currentPageStr)
|
|
|
device_type_idStr := ctx.PostForm("device_type_id")
|
|
|
device_type_id, err := strconv.Atoi(device_type_idStr)
|
|
|
-
|
|
|
devices_enabledStr := ctx.PostForm("devices_enabled")
|
|
|
devices_enabled, err := strconv.Atoi(devices_enabledStr)
|
|
|
pageSizeStr := ctx.PostForm("pageSize")
|
|
|
+ gatewayStr := ctx.PostForm("gateway_id")
|
|
|
+ gateway_id, err := strconv.Atoi(gatewayStr)
|
|
|
pageSize, err := strconv.Atoi(pageSizeStr)
|
|
|
if err != nil {
|
|
|
resp.HandleError(ctx, 1203, "参数错误", err)
|
|
|
return
|
|
|
}
|
|
|
query := ctx.PostForm("query")
|
|
|
-
|
|
|
- baseecic, err := h.illuminatingService.GetBaseecic(currentPage, device_type_id, devices_enabled, pageSize, query)
|
|
|
+ baseecic, err := h.illuminatingService.GetBaseecic(currentPage, device_type_id, devices_enabled, gateway_id, pageSize, query, "")
|
|
|
if err != nil {
|
|
|
h.logger.Error("获取统计信息失败", zap.Any("err", err))
|
|
|
resp.HandleError(ctx, 1201, "获取统计信息失败", nil)
|
|
@@ -171,3 +204,142 @@ func (h *IlluminatingHandler) GetBaseecic(ctx *gin.Context) {
|
|
|
|
|
|
resp.HandleSuccess(ctx, baseecic)
|
|
|
}
|
|
|
+
|
|
|
+// GetgatewayFind 获取网关信息
|
|
|
+func (h *IlluminatingHandler) GetgatewayFind(ctx *gin.Context) {
|
|
|
+ gateway, err := h.illuminatingService.GetgatewayFind()
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("获取统计信息失败", zap.Any("err", err))
|
|
|
+ resp.HandleError(ctx, 1201, "获取统计信息失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.HandleSuccess(ctx, gateway)
|
|
|
+ h.logger.Info("获取统计信息成功", zap.Any("control", gateway))
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// DevicesControl 设备控制
|
|
|
+func (h *IlluminatingHandler) DevicesControl(ctx *gin.Context) {
|
|
|
+ agreement_json := ctx.PostForm("agreement_json")
|
|
|
+ request_id := ctx.PostForm("request_id")
|
|
|
+ udid := ctx.PostForm("udid")
|
|
|
+ source, err := strconv.Atoi(ctx.PostForm("source"))
|
|
|
+ if err != nil {
|
|
|
+ resp.HandleError(ctx, 1203, "参数错误", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ control, err := h.illuminatingService.DevicesControl(agreement_json, request_id, udid, source)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("控制灯光设备失败", zap.Any("err", err))
|
|
|
+ resp.HandleError(ctx, 1201, "控制灯光设备失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.HandleSuccess(ctx, control)
|
|
|
+ h.logger.Info("控制灯光设备成功", zap.Any("control", control))
|
|
|
+}
|
|
|
+
|
|
|
+// GetLightingstatus 获取设备信息详情
|
|
|
+func (h *IlluminatingHandler) GetLightingstatus(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
|
|
|
+ m := make(map[string]any)
|
|
|
+ // 使用 map[string]interface{} 来接收未知结构的 JSON 对象
|
|
|
+ var data map[string]any
|
|
|
+ devices_udid := ctx.Query("devices_udid")
|
|
|
+ for conn {
|
|
|
+ select {
|
|
|
+ case <-notify:
|
|
|
+ conn = false
|
|
|
+ fmt.Println("断开连接")
|
|
|
+ return
|
|
|
+ default:
|
|
|
+ baseecic, err := h.illuminatingService.GetBaseecic(1, 0, -1, 0, 1, "", devices_udid)
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ m["设备名称"] = baseecic.Devices[0].DevicesName
|
|
|
+ m["state"] = baseecic.Devices[0].GatewayStatus
|
|
|
+ err = json.Unmarshal([]byte(baseecic.Devices[0].DevicesJsonObject), &data)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("json反序列化失败")
|
|
|
+ response.Code = 1202
|
|
|
+ response.Msg = "json反序列化失败"
|
|
|
+ response.Data = nil
|
|
|
+ res, _ := json.Marshal(&response)
|
|
|
+ fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
|
+ conn = false
|
|
|
+ }
|
|
|
+
|
|
|
+ deviceTags := make(map[string]int)
|
|
|
+ // 遍历所有 key
|
|
|
+ for key, value := range data {
|
|
|
+ // 检查是否是 name 开头
|
|
|
+ if strings.HasPrefix(key, "name") {
|
|
|
+ // 尝试将 value 转为字符串
|
|
|
+ name, ok := value.(string)
|
|
|
+ if !ok {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 提取序号,比如 name1 -> 1
|
|
|
+ indexStr := strings.TrimPrefix(key, "name")
|
|
|
+ index, err := strconv.Atoi(indexStr)
|
|
|
+ if err != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 构造对应的 tag 键名
|
|
|
+ tagKey := fmt.Sprintf("tag%d", index)
|
|
|
+ // 获取 tag 的值并转为 int
|
|
|
+ if tagValue, ok := data[tagKey].(float64); ok {
|
|
|
+ deviceTags[name] = int(tagValue)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for k, v := range deviceTags {
|
|
|
+ m[k] = v
|
|
|
+ }
|
|
|
+ response.Code = 200
|
|
|
+ response.Msg = "获取设备信息成功"
|
|
|
+ response.Data = m
|
|
|
+ res, _ := json.Marshal(&response)
|
|
|
+ fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
|
+ ctx.Writer.Flush()
|
|
|
+ time.Sleep(10 * time.Second)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// GetDeviceOrgid 获取照明定时策略
|
|
|
+func (h *IlluminatingHandler) GetDeviceOrgid(ctx *gin.Context) {
|
|
|
+ currentPage := ctx.Query("currentPage")
|
|
|
+ pageSize := ctx.Query("pageSize")
|
|
|
+ searchText := ctx.Query("searchText")
|
|
|
+ var response model.DeviceOrgid
|
|
|
+ control, err := h.illuminatingService.GetDeviceOrgid(currentPage, pageSize, searchText)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("获取照明定时策略失败", zap.Any("err", err))
|
|
|
+ resp.HandleError(ctx, 1201, "获取照明定时策略失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = json.Unmarshal(control, &response)
|
|
|
+ if err != nil {
|
|
|
+ h.logger.Error("json反序列化失败")
|
|
|
+ resp.HandleError(ctx, 1202, "json反序列化失败", nil)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.HandleSuccess(ctx, response)
|
|
|
+ h.logger.Info("获取照明定时策略成功", zap.Any("control", response))
|
|
|
+}
|