illuminating.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package handler
  2. import (
  3. "city_chips/internal/model"
  4. "city_chips/internal/service"
  5. "city_chips/pkg/helper/resp"
  6. "encoding/json"
  7. "fmt"
  8. "go.uber.org/zap"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/gin-gonic/gin"
  13. "github.com/spf13/viper"
  14. )
  15. type IlluminatingHandler struct {
  16. *Handler
  17. illuminatingService service.IlluminatingService
  18. conf *viper.Viper
  19. }
  20. func NewIlluminatingHandler(
  21. handler *Handler,
  22. illuminatingService service.IlluminatingService,
  23. conf *viper.Viper,
  24. ) *IlluminatingHandler {
  25. return &IlluminatingHandler{
  26. Handler: handler,
  27. illuminatingService: illuminatingService,
  28. conf: conf,
  29. }
  30. }
  31. // GetIlluminating 照明系统
  32. func (h *IlluminatingHandler) GetIlluminating(ctx *gin.Context) {
  33. m := make(map[string]any)
  34. var alarmstatistics model.AlarmStatistics
  35. var alarmCount model.AlarmCount
  36. var statis model.IlluminatingStatistics
  37. now := time.Now()
  38. sevenDaysAgo := now.AddDate(0, 0, -7).Format("2006-01-02")
  39. format := now.Format("2006-01-02")
  40. alarmStatistics, err := h.illuminatingService.GetAlarmStatistics(sevenDaysAgo, format)
  41. if err != nil {
  42. h.logger.Error("获取告警信息统计失败", zap.Any("err", err))
  43. resp.HandleError(ctx, 1204, "获取告警信息统计失败", nil)
  44. return
  45. }
  46. err = json.Unmarshal(alarmStatistics, &alarmstatistics)
  47. if err != nil {
  48. h.logger.Error("json反序列化失败")
  49. resp.HandleError(ctx, 1205, "json序列化失败", nil)
  50. return
  51. }
  52. alarm, err := h.illuminatingService.GetAlarm(1, 20)
  53. if err != nil {
  54. h.logger.Error("获取告警信息失败", zap.Any("err", err))
  55. resp.HandleError(ctx, 1203, "获取告警信息失败", nil)
  56. return
  57. }
  58. err = json.Unmarshal(alarm, &alarmCount)
  59. if err != nil {
  60. h.logger.Error("json反序列化失败")
  61. resp.HandleError(ctx, 1205, "json序列化失败", nil)
  62. return
  63. }
  64. statistics, err := h.illuminatingService.GetStatistics()
  65. if err != nil {
  66. h.logger.Error("获取统计信息失败", zap.Any("err", err))
  67. resp.HandleError(ctx, 1201, "获取统计信息失败", nil)
  68. return
  69. }
  70. err = json.Unmarshal(statistics, &statis)
  71. if err != nil {
  72. h.logger.Error("json反序列化失败")
  73. resp.HandleError(ctx, 1202, "json反序列化失败", nil)
  74. return
  75. }
  76. for i, _ := range alarmstatistics.TimeStatistics {
  77. s := alarmstatistics.TimeStatistics[i].TimeDay.Format("2006-01-02 15:04:05")
  78. alarmstatistics.TimeStatistics[i].Time = s
  79. }
  80. baseecic, err := h.illuminatingService.GetBaseecic(1, 0, -1, 0, 20, "", "")
  81. if err != nil {
  82. h.logger.Error("获取 basement_ecic 列表失败", zap.Any("err", err))
  83. resp.HandleError(ctx, 1202, "获取 basement_ecic 列表失败", nil)
  84. return
  85. }
  86. m["IlluminatingDevice"] = baseecic.Devices //设备列表
  87. m["DeviceCount"] = statis.DeviceCount //设备总数
  88. m["GatewayCount"] = statis.GatewayCount //网关数量
  89. m["OffCount"] = statis.StrategyCount //策略总数
  90. m["AlarmCount"] = statis.AlarmCount //报警数量
  91. m["RunAnalyse"] = alarmstatistics.TimeStatistics //运行分析
  92. m["EventList"] = alarmCount //报警事件
  93. resp.HandleSuccess(ctx, m)
  94. }
  95. // 获取灯光状态
  96. //func (h *IlluminatingHandler) GetLightingstatus(ctx *gin.Context) {
  97. // // 设置响应头
  98. // ctx.Header("Content-Type", "text/event-stream")
  99. // ctx.Header("Cache-Control", "no-cache")
  100. // ctx.Header("Connection", "keep-alive")
  101. // // 监听客户端断开连接
  102. // conn := true
  103. // notify := ctx.Writer.CloseNotify()
  104. // type Response struct {
  105. // RequestId string `protobuf:"bytes,1,opt,name=requestId,proto3" json:"requestId,omitempty"`
  106. // Code int32 `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
  107. // Msg string `protobuf:"bytes,3,opt,name=msg,proto3" json:"msg,omitempty"`
  108. // Data any `json:"data"`
  109. // }
  110. // stationNo := ctx.Query("id")
  111. // if len(stationNo) == 0 {
  112. // resp.HandleError(ctx, 1201, "缺少必要参数", nil)
  113. // return
  114. // }
  115. // var response Response
  116. // for conn {
  117. // select {
  118. // case <-notify:
  119. // conn = false
  120. // fmt.Println("断开连接")
  121. // return
  122. // default:
  123. // // 模拟数据
  124. // data := make(map[string]any)
  125. // response.Code = 200
  126. // response.RequestId = strconv.Itoa(rand.Intn(1000))
  127. // response.Msg = "success"
  128. // data["设备名称"] = model.GetRandomItem(model.IlluminatingDeviceNames)
  129. // data["state"] = rand.Intn(2) // 1开 0关 2故障
  130. // response.Data = data
  131. // res, _ := json.Marshal(&response)
  132. // fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  133. // ctx.Writer.Flush()
  134. // time.Sleep(10 * time.Second)
  135. // }
  136. // }
  137. //}
  138. // 修改灯光状态
  139. func (h *IlluminatingHandler) UpdataLightingStatus(ctx *gin.Context) {
  140. id := ctx.PostForm("id")
  141. state := ctx.PostForm("state")
  142. if len(id) == 0 || len(state) == 0 {
  143. resp.HandleError(ctx, 1201, "缺少必要参数", nil)
  144. return
  145. }
  146. resp.HandleSuccess(ctx, id)
  147. }
  148. // GetStatistics 获取灯光统计数据
  149. func (h *IlluminatingHandler) GetStatistics(ctx *gin.Context) {
  150. var statis model.IlluminatingStatistics
  151. statistics, err := h.illuminatingService.GetStatistics()
  152. if err != nil {
  153. h.logger.Error("获取统计信息失败", zap.Any("err", err))
  154. resp.HandleError(ctx, 1201, "获取统计信息失败", nil)
  155. return
  156. }
  157. err = json.Unmarshal(statistics, &statis)
  158. if err != nil {
  159. h.logger.Error("json反序列化失败")
  160. resp.HandleError(ctx, 1202, "json反序列化失败", nil)
  161. return
  162. }
  163. alarm, err := h.illuminatingService.GetAlarm(1, 20)
  164. if err != nil {
  165. h.logger.Error("获取告警信息失败", zap.Any("err", err))
  166. resp.HandleError(ctx, 1203, "获取告警信息失败", nil)
  167. return
  168. }
  169. fmt.Println("报警信息统计", string(alarm))
  170. resp.HandleSuccess(ctx, statis)
  171. }
  172. func (h *IlluminatingHandler) GetBaseecic(ctx *gin.Context) {
  173. currentPageStr := ctx.PostForm("currentPage")
  174. currentPage, err := strconv.Atoi(currentPageStr)
  175. device_type_idStr := ctx.PostForm("device_type_id")
  176. device_type_id, err := strconv.Atoi(device_type_idStr)
  177. devices_enabledStr := ctx.PostForm("devices_enabled")
  178. devices_enabled, err := strconv.Atoi(devices_enabledStr)
  179. pageSizeStr := ctx.PostForm("pageSize")
  180. gatewayStr := ctx.PostForm("gateway_id")
  181. gateway_id, err := strconv.Atoi(gatewayStr)
  182. pageSize, err := strconv.Atoi(pageSizeStr)
  183. if err != nil {
  184. resp.HandleError(ctx, 1203, "参数错误", err)
  185. return
  186. }
  187. query := ctx.PostForm("query")
  188. baseecic, err := h.illuminatingService.GetBaseecic(currentPage, device_type_id, devices_enabled, gateway_id, pageSize, query, "")
  189. if err != nil {
  190. h.logger.Error("获取统计信息失败", zap.Any("err", err))
  191. resp.HandleError(ctx, 1201, "获取统计信息失败", nil)
  192. return
  193. }
  194. resp.HandleSuccess(ctx, baseecic)
  195. }
  196. // GetgatewayFind 获取网关信息
  197. func (h *IlluminatingHandler) GetgatewayFind(ctx *gin.Context) {
  198. gateway, err := h.illuminatingService.GetgatewayFind()
  199. if err != nil {
  200. h.logger.Error("获取统计信息失败", zap.Any("err", err))
  201. resp.HandleError(ctx, 1201, "获取统计信息失败", nil)
  202. return
  203. }
  204. resp.HandleSuccess(ctx, gateway)
  205. h.logger.Info("获取统计信息成功", zap.Any("control", gateway))
  206. }
  207. // DevicesControl 设备控制
  208. func (h *IlluminatingHandler) DevicesControl(ctx *gin.Context) {
  209. agreement_json := ctx.PostForm("agreement_json")
  210. request_id := ctx.PostForm("request_id")
  211. udid := ctx.PostForm("udid")
  212. source, err := strconv.Atoi(ctx.PostForm("source"))
  213. if err != nil {
  214. resp.HandleError(ctx, 1203, "参数错误", err)
  215. return
  216. }
  217. control, err := h.illuminatingService.DevicesControl(agreement_json, request_id, udid, source)
  218. if err != nil {
  219. h.logger.Error("控制灯光设备失败", zap.Any("err", err))
  220. resp.HandleError(ctx, 1201, "控制灯光设备失败", nil)
  221. return
  222. }
  223. resp.HandleSuccess(ctx, control)
  224. h.logger.Info("控制灯光设备成功", zap.Any("control", control))
  225. }
  226. // GetLightingstatus 获取设备信息详情
  227. func (h *IlluminatingHandler) GetLightingstatus(ctx *gin.Context) {
  228. // 设置响应头
  229. ctx.Header("Content-Type", "text/event-stream")
  230. ctx.Header("Cache-Control", "no-cache")
  231. ctx.Header("Connection", "keep-alive")
  232. // 监听客户端断开连接
  233. conn := true
  234. notify := ctx.Writer.CloseNotify()
  235. var response model.Response
  236. m := make(map[string]any)
  237. // 使用 map[string]interface{} 来接收未知结构的 JSON 对象
  238. var data map[string]any
  239. devices_udid := ctx.Query("devices_udid")
  240. for conn {
  241. select {
  242. case <-notify:
  243. conn = false
  244. fmt.Println("断开连接")
  245. return
  246. default:
  247. baseecic, err := h.illuminatingService.GetBaseecic(1, 0, -1, 0, 1, "", devices_udid)
  248. if err != nil {
  249. h.logger.Error("获取设备信息详情失败")
  250. response.Code = 1203
  251. response.Msg = "获取设备信息详情失败"
  252. response.Data = nil
  253. res, _ := json.Marshal(&response)
  254. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  255. ctx.Writer.Flush()
  256. conn = false
  257. return
  258. }
  259. m["设备名称"] = baseecic.Devices[0].DevicesName
  260. m["state"] = baseecic.Devices[0].GatewayStatus
  261. err = json.Unmarshal([]byte(baseecic.Devices[0].DevicesJsonObject), &data)
  262. if err != nil {
  263. h.logger.Error("json反序列化失败")
  264. response.Code = 1202
  265. response.Msg = "json反序列化失败"
  266. response.Data = nil
  267. res, _ := json.Marshal(&response)
  268. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  269. conn = false
  270. }
  271. deviceTags := make(map[string]int)
  272. // 遍历所有 key
  273. for key, value := range data {
  274. // 检查是否是 name 开头
  275. if strings.HasPrefix(key, "name") {
  276. // 尝试将 value 转为字符串
  277. name, ok := value.(string)
  278. if !ok {
  279. continue
  280. }
  281. // 提取序号,比如 name1 -> 1
  282. indexStr := strings.TrimPrefix(key, "name")
  283. index, err := strconv.Atoi(indexStr)
  284. if err != nil {
  285. continue
  286. }
  287. // 构造对应的 tag 键名
  288. tagKey := fmt.Sprintf("tag%d", index)
  289. // 获取 tag 的值并转为 int
  290. if tagValue, ok := data[tagKey].(float64); ok {
  291. deviceTags[name] = int(tagValue)
  292. }
  293. }
  294. }
  295. for k, v := range deviceTags {
  296. m[k] = v
  297. }
  298. response.Code = 200
  299. response.Msg = "获取设备信息成功"
  300. response.Data = m
  301. res, _ := json.Marshal(&response)
  302. fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
  303. ctx.Writer.Flush()
  304. time.Sleep(10 * time.Second)
  305. }
  306. }
  307. }
  308. // GetDeviceOrgid 获取照明定时策略
  309. func (h *IlluminatingHandler) GetDeviceOrgid(ctx *gin.Context) {
  310. currentPage := ctx.Query("currentPage")
  311. pageSize := ctx.Query("pageSize")
  312. searchText := ctx.Query("searchText")
  313. var response model.DeviceOrgid
  314. control, err := h.illuminatingService.GetDeviceOrgid(currentPage, pageSize, searchText)
  315. if err != nil {
  316. h.logger.Error("获取照明定时策略失败", zap.Any("err", err))
  317. resp.HandleError(ctx, 1201, "获取照明定时策略失败", nil)
  318. return
  319. }
  320. err = json.Unmarshal(control, &response)
  321. if err != nil {
  322. h.logger.Error("json反序列化失败")
  323. resp.HandleError(ctx, 1202, "json反序列化失败", nil)
  324. return
  325. }
  326. resp.HandleSuccess(ctx, response)
  327. h.logger.Info("获取照明定时策略成功", zap.Any("control", response))
  328. }