123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- package handler
- import (
- "city_chips/internal/model"
- "city_chips/internal/service"
- "city_chips/pkg/helper/resp"
- "encoding/json"
- "fmt"
- "go.uber.org/zap"
- "strconv"
- "strings"
- "time"
- "github.com/gin-gonic/gin"
- "github.com/spf13/viper"
- )
- type IlluminatingHandler struct {
- *Handler
- illuminatingService service.IlluminatingService
- conf *viper.Viper
- }
- func NewIlluminatingHandler(
- handler *Handler,
- illuminatingService service.IlluminatingService,
- conf *viper.Viper,
- ) *IlluminatingHandler {
- return &IlluminatingHandler{
- Handler: handler,
- illuminatingService: illuminatingService,
- conf: conf,
- }
- }
- // GetIlluminating 照明系统
- func (h *IlluminatingHandler) GetIlluminating(ctx *gin.Context) {
- m := make(map[string]any)
- var alarmstatistics model.AlarmStatistics
- var alarmCount model.AlarmCount
- var statis model.IlluminatingStatistics
- 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
- }
- 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")
- state := ctx.PostForm("state")
- if len(id) == 0 || len(state) == 0 {
- resp.HandleError(ctx, 1201, "缺少必要参数", nil)
- return
- }
- resp.HandleSuccess(ctx, id)
- }
- // GetStatistics 获取灯光统计数据
- func (h *IlluminatingHandler) GetStatistics(ctx *gin.Context) {
- var statis model.IlluminatingStatistics
- 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
- }
- 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) {
- currentPageStr := ctx.PostForm("currentPage")
- 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, gateway_id, pageSize, query, "")
- if err != nil {
- h.logger.Error("获取统计信息失败", zap.Any("err", err))
- resp.HandleError(ctx, 1201, "获取统计信息失败", nil)
- return
- }
- 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))
- }
|