|
@@ -4,11 +4,14 @@ import (
|
|
"city_chips/internal/model"
|
|
"city_chips/internal/model"
|
|
"city_chips/internal/service"
|
|
"city_chips/internal/service"
|
|
"city_chips/pkg/helper/resp"
|
|
"city_chips/pkg/helper/resp"
|
|
|
|
+ "encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
- "github.com/gin-gonic/gin"
|
|
|
|
- "github.com/spf13/viper"
|
|
|
|
"math/rand"
|
|
"math/rand"
|
|
|
|
+ "strconv"
|
|
"time"
|
|
"time"
|
|
|
|
+
|
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
|
+ "github.com/spf13/viper"
|
|
)
|
|
)
|
|
|
|
|
|
type IlluminatingHandler struct {
|
|
type IlluminatingHandler struct {
|
|
@@ -29,6 +32,7 @@ func NewIlluminatingHandler(
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// 照明系统
|
|
func (h *IlluminatingHandler) GetIlluminating(ctx *gin.Context) {
|
|
func (h *IlluminatingHandler) GetIlluminating(ctx *gin.Context) {
|
|
m := make(map[string]any)
|
|
m := make(map[string]any)
|
|
runAnaly := make(map[string]any)
|
|
runAnaly := make(map[string]any)
|
|
@@ -67,3 +71,59 @@ func (h *IlluminatingHandler) GetIlluminating(ctx *gin.Context) {
|
|
m["EventList"] = eventList //事件列表
|
|
m["EventList"] = eventList //事件列表
|
|
resp.HandleSuccess(ctx, m)
|
|
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["name"] = "设备一"
|
|
|
|
+ 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)
|
|
|
|
+}
|