|
@@ -1,12 +1,15 @@
|
|
package handler
|
|
package handler
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "city_chips/internal/model"
|
|
"city_chips/internal/service"
|
|
"city_chips/internal/service"
|
|
"city_chips/pkg/helper/resp"
|
|
"city_chips/pkg/helper/resp"
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"fmt"
|
|
"fmt"
|
|
|
|
+ "go.uber.org/zap"
|
|
"math/rand"
|
|
"math/rand"
|
|
"strconv"
|
|
"strconv"
|
|
|
|
+ "strings"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
@@ -52,27 +55,201 @@ func (h *EnergyHandler) GetEnergy(ctx *gin.Context) {
|
|
LastYearCarbonEmissions[name] = rand.Intn(1000)
|
|
LastYearCarbonEmissions[name] = rand.Intn(1000)
|
|
YearCarbonEmissions[name] = rand.Intn(1000)
|
|
YearCarbonEmissions[name] = rand.Intn(1000)
|
|
}
|
|
}
|
|
- m["PowerConsumption"] = rand.Intn(1000) //今日实时电耗
|
|
|
|
- m["WaterConsumption"] = rand.Intn(1000) //今日实时水耗
|
|
|
|
- m["YesterdayConsumption"] = rand.Intn(1000) //昨日电耗
|
|
|
|
- m["MonthElectricityConsumption"] = rand.Intn(1000) //本月电耗
|
|
|
|
- m["YesterdayWaterConsumption"] = rand.Intn(1000) //昨日水耗
|
|
|
|
- m["MonthWaterConsumption"] = rand.Intn(1000) //本月水耗
|
|
|
|
- m["OnedayPowerConsumption"] = OnedayPowerConsumption //本月水耗
|
|
|
|
- m["OnedayWaterConsumption"] = OnedayWaterConsumption //本月水耗
|
|
|
|
- m["EnergyCount"] = rand.Intn(10000) //能源总量
|
|
|
|
- m["EnergyIntensity"] = rand.Intn(1000) //能耗强度
|
|
|
|
- m["CarbonEmissions"] = rand.Intn(1000) //碳排总量
|
|
|
|
- m["CarbonIsntensity"] = rand.Intn(1000) //碳排强度
|
|
|
|
- m["LastYearEnergyConsumption"] = LastYearEnergyConsumption //去年能耗
|
|
|
|
- m["YearEnergyConsumption"] = YearEnergyConsumption //今年能耗
|
|
|
|
- m["LastYearCarbonEmissions"] = LastYearCarbonEmissions //去年碳排
|
|
|
|
- m["YearCarbonEmissions"] = YearCarbonEmissions //今年碳排
|
|
|
|
|
|
+ getStatistics, err := h.energyService.GetStatistics()
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "缺少必要参数", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ tree, err := h.energyService.GetEnergyTree()
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "缺少必要参数", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ now := time.Now()
|
|
|
|
+ start_time := now.AddDate(0, 0, -7).Format("2006-01-02 00:00:00")
|
|
|
|
+ end_time := now.Format("2006-01-02 00:00:00")
|
|
|
|
+ var water, electricity model.T
|
|
|
|
+ var waterResp, electricityResp []model.EnergyTrend
|
|
|
|
+ var electricityNight, waterNight model.DayAndNightRespone
|
|
|
|
+ waterCount := make(map[string]any)
|
|
|
|
+ electricityCount := make(map[string]any)
|
|
|
|
+ for _, child := range tree.Data[0].Children {
|
|
|
|
+ if strings.Contains(child.Name, "电") {
|
|
|
|
+ electricity, err = h.energyService.GetEnergyTrend(model.Request{
|
|
|
|
+ TimeType: "day",
|
|
|
|
+ Nodes: []int{child.Id},
|
|
|
|
+ StartTime: start_time,
|
|
|
|
+ EndTime: end_time,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "获取电耗能源趋势失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ electricityNight, err = h.energyService.GetDayAndNight(model.DayAndNightRequst{
|
|
|
|
+ TimeType: "day",
|
|
|
|
+ Node: child.Id,
|
|
|
|
+ StartTime: start_time,
|
|
|
|
+ EndTime: end_time,
|
|
|
|
+ DayTime: "09:00",
|
|
|
|
+ NightTime: "18:00",
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "获取电耗夜耗失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if strings.Contains(child.Name, "水") {
|
|
|
|
+ water, err = h.energyService.GetEnergyTrend(model.Request{
|
|
|
|
+ TimeType: "day",
|
|
|
|
+ Nodes: []int{child.Id},
|
|
|
|
+ StartTime: start_time,
|
|
|
|
+ EndTime: end_time,
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "获取水耗能源趋势失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ waterNight, err = h.energyService.GetDayAndNight(model.DayAndNightRequst{
|
|
|
|
+ TimeType: "day",
|
|
|
|
+ Node: child.Id,
|
|
|
|
+ StartTime: start_time,
|
|
|
|
+ EndTime: end_time,
|
|
|
|
+ DayTime: "09:00",
|
|
|
|
+ NightTime: "18:00",
|
|
|
|
+ })
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "获取水耗夜耗失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ err = json.Unmarshal([]byte(water.RespJson), &waterResp)
|
|
|
|
+ err = json.Unmarshal([]byte(electricity.RespJson), &electricityResp)
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1202, "json反序列化失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ for _, trend := range waterResp[0].Json {
|
|
|
|
+ waterCount[trend.Time] = trend.TotalUsage
|
|
|
|
+ }
|
|
|
|
+ for _, trend := range electricityResp[0].Json {
|
|
|
|
+ electricityCount[trend.Time] = trend.TotalUsage
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ m["DeviceCount"] = getStatistics.DeviceCount //设备总数
|
|
|
|
+ m["GatewayCount"] = getStatistics.GatewayCount //网关总数
|
|
|
|
+ m["AlarmCount"] = getStatistics.AlarmCount //报警总数
|
|
|
|
+ m["MonthElectricityConsumption"] = getStatistics.RoomCount // 房间总数
|
|
|
|
+ //m["YesterdayWaterConsumption"] = rand.Intn(1000) //昨日水耗
|
|
|
|
+ //m["MonthWaterConsumption"] = rand.Intn(1000) //本月水耗
|
|
|
|
+ //m["OnedayPowerConsumption"] = OnedayPowerConsumption //本月水耗
|
|
|
|
+ m["OnedayWaterConsumption"] = OnedayWaterConsumption //本月水耗
|
|
|
|
+ //m["EnergyCount"] = rand.Intn(10000) //能源总量
|
|
|
|
+ //m["EnergyIntensity"] = rand.Intn(1000) //能耗强度
|
|
|
|
+ //m["CarbonEmissions"] = rand.Intn(1000) //碳排总量
|
|
|
|
+ //m["CarbonIsntensity"] = rand.Intn(1000) //碳排强度
|
|
|
|
+ //m["LastYearEnergyConsumption"] = LastYearEnergyConsumption //去年能耗
|
|
|
|
+ //m["YearEnergyConsumption"] = YearEnergyConsumption //今年能耗
|
|
|
|
+ m["electricityNight"] = electricityNight.Data //昼夜用电分析
|
|
|
|
+ m["waterNight"] = waterNight.Data //昼夜用水分析
|
|
|
|
+ m["electricityCount"] = electricityCount //用电统计
|
|
|
|
+ m["waterCount"] = waterCount //用水统计
|
|
resp.HandleSuccess(ctx, m)
|
|
resp.HandleSuccess(ctx, m)
|
|
}
|
|
}
|
|
|
|
|
|
-// 水表数据
|
|
|
|
-func (h *EnergyHandler) GetWaterMmeter(ctx *gin.Context) {
|
|
|
|
|
|
+// GetEnergyTrend 获取能耗趋势
|
|
|
|
+func (h *EnergyHandler) GetEnergyTrend(ctx *gin.Context) {
|
|
|
|
+ var rest model.Request
|
|
|
|
+ var tre []model.EnergyTrend
|
|
|
|
+ var waterResp []model.EnergyTrend
|
|
|
|
+
|
|
|
|
+ err := ctx.ShouldBindJSON(&rest)
|
|
|
|
+ if err != nil {
|
|
|
|
+ h.logger.Error("参数错误", zap.Any("err", err))
|
|
|
|
+ resp.HandleError(ctx, 1201, "参数错误", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ trend, err := h.energyService.GetEnergyTrend(rest)
|
|
|
|
+ if err != nil {
|
|
|
|
+ h.logger.Error("获取能耗趋势失败", zap.Any("err", err))
|
|
|
|
+ resp.HandleError(ctx, 1201, "获取能耗趋势失败", nil)
|
|
|
|
+ }
|
|
|
|
+ err = json.Unmarshal([]byte(trend.RespJson), &tre)
|
|
|
|
+ if err != nil {
|
|
|
|
+ h.logger.Error("json反序列化失败", zap.Any("err", err))
|
|
|
|
+ resp.HandleError(ctx, 1202, "json反序列化失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ err = json.Unmarshal([]byte(trend.RespJson), &waterResp)
|
|
|
|
+ if err != nil {
|
|
|
|
+ h.logger.Error("json反序列化失败", zap.Any("err", err))
|
|
|
|
+ resp.HandleError(ctx, 1202, "json反序列化失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ resp.HandleSuccess(ctx, waterResp)
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetBaseecic 获取能耗设备列表
|
|
|
|
+func (h *EnergyHandler) 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.energyService.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 *EnergyHandler) GetgatewayFind(ctx *gin.Context) {
|
|
|
|
+ gateway, err := h.energyService.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))
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetDayAndNight 获取日夜数据
|
|
|
|
+func (h *EnergyHandler) GetDayAndNight(ctx *gin.Context) {
|
|
|
|
+ var rest model.DayAndNightRequst
|
|
|
|
+ err := ctx.ShouldBindJSON(&rest)
|
|
|
|
+ if err != nil {
|
|
|
|
+ h.logger.Error("参数错误", zap.Any("err", err))
|
|
|
|
+ resp.HandleError(ctx, 1201, "参数错误", nil)
|
|
|
|
+ }
|
|
|
|
+ night, err := h.energyService.GetDayAndNight(rest)
|
|
|
|
+ if err != nil {
|
|
|
|
+ h.logger.Error("获取获取网关信息失败", zap.Any("err", err))
|
|
|
|
+ resp.HandleError(ctx, 1201, "获取网关信息失败", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ resp.HandleSuccess(ctx, night)
|
|
|
|
+ h.logger.Info("获取网关信息成功", zap.Any("night", night))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// GetEnergystatus 获取设备状态详情
|
|
|
|
+func (h *EnergyHandler) GetEnergystatus(ctx *gin.Context) {
|
|
// 设置响应头
|
|
// 设置响应头
|
|
ctx.Header("Content-Type", "text/event-stream")
|
|
ctx.Header("Content-Type", "text/event-stream")
|
|
ctx.Header("Cache-Control", "no-cache")
|
|
ctx.Header("Cache-Control", "no-cache")
|
|
@@ -80,18 +257,79 @@ func (h *EnergyHandler) GetWaterMmeter(ctx *gin.Context) {
|
|
// 监听客户端断开连接
|
|
// 监听客户端断开连接
|
|
conn := true
|
|
conn := true
|
|
notify := ctx.Writer.CloseNotify()
|
|
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"`
|
|
|
|
|
|
+ var response model.Response
|
|
|
|
+ m := make(map[string]any)
|
|
|
|
+ // 使用 map[string]interface{} 来接收未知结构的 JSON 对象
|
|
|
|
+ var data model.DevicesJsonObject
|
|
|
|
+ devices_udid := ctx.Query("devices_udid")
|
|
|
|
+ for conn {
|
|
|
|
+ select {
|
|
|
|
+ case <-notify:
|
|
|
|
+ conn = false
|
|
|
|
+ fmt.Println("断开连接")
|
|
|
|
+ return
|
|
|
|
+ default:
|
|
|
|
+ baseecic, err := h.energyService.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].OnLine
|
|
|
|
+ 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
|
|
|
|
+ }
|
|
|
|
+ m["总水量"] = data.EPE
|
|
|
|
+ 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)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- stationNo := ctx.Query("id")
|
|
|
|
- if len(stationNo) == 0 {
|
|
|
|
- resp.HandleError(ctx, 1201, "缺少必要参数", nil)
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// CategoryStatistics 获取设备类型统计
|
|
|
|
+func (h *EnergyHandler) CategoryStatistics(ctx *gin.Context) {
|
|
|
|
+ statistics, err := h.energyService.GetDeviceStatistics()
|
|
|
|
+ if err != nil {
|
|
|
|
+ h.logger.Error("获取设备类型统计失败", zap.Any("err", err))
|
|
|
|
+ resp.HandleError(ctx, 1201, "获取设备类型统计失败", nil)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
- var response Response
|
|
|
|
|
|
+ resp.HandleSuccess(ctx, statistics)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 水表数据
|
|
|
|
+func (h *EnergyHandler) GetWaterMmeter(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 model.TotalWater
|
|
|
|
+ devices_udid := ctx.Query("id")
|
|
for conn {
|
|
for conn {
|
|
select {
|
|
select {
|
|
case <-notify:
|
|
case <-notify:
|
|
@@ -99,15 +337,34 @@ func (h *EnergyHandler) GetWaterMmeter(ctx *gin.Context) {
|
|
fmt.Println("断开连接")
|
|
fmt.Println("断开连接")
|
|
return
|
|
return
|
|
default:
|
|
default:
|
|
- // 模拟数据
|
|
|
|
- data := make(map[string]any)
|
|
|
|
|
|
+ baseecic, err := h.energyService.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].OnLine
|
|
|
|
+ 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
|
|
|
|
+ }
|
|
|
|
+ m["总水量"] = data.TotalWater
|
|
response.Code = 200
|
|
response.Code = 200
|
|
- response.RequestId = strconv.Itoa(rand.Intn(1000))
|
|
|
|
- response.Msg = "success"
|
|
|
|
- data["今日用水"] = rand.Intn(1000)
|
|
|
|
- data["昨日用水"] = rand.Intn(1000)
|
|
|
|
- data["本月用水量"] = rand.Intn(1000)
|
|
|
|
- response.Data = data
|
|
|
|
|
|
+ response.Msg = "获取设备信息成功"
|
|
|
|
+ response.Data = m
|
|
res, _ := json.Marshal(&response)
|
|
res, _ := json.Marshal(&response)
|
|
fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
ctx.Writer.Flush()
|
|
ctx.Writer.Flush()
|
|
@@ -125,18 +382,11 @@ func (h *EnergyHandler) GetAmmeter(ctx *gin.Context) {
|
|
// 监听客户端断开连接
|
|
// 监听客户端断开连接
|
|
conn := true
|
|
conn := true
|
|
notify := ctx.Writer.CloseNotify()
|
|
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
|
|
|
|
|
|
+ var response model.Response
|
|
|
|
+ m := make(map[string]any)
|
|
|
|
+ // 使用 map[string]interface{} 来接收未知结构的 JSON 对象
|
|
|
|
+ var data model.DevicesJsonObject
|
|
|
|
+ devices_udid := ctx.Query("id")
|
|
for conn {
|
|
for conn {
|
|
select {
|
|
select {
|
|
case <-notify:
|
|
case <-notify:
|
|
@@ -144,15 +394,34 @@ func (h *EnergyHandler) GetAmmeter(ctx *gin.Context) {
|
|
fmt.Println("断开连接")
|
|
fmt.Println("断开连接")
|
|
return
|
|
return
|
|
default:
|
|
default:
|
|
- // 模拟数据
|
|
|
|
- data := make(map[string]any)
|
|
|
|
|
|
+ baseecic, err := h.energyService.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].OnLine
|
|
|
|
+ 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
|
|
|
|
+ }
|
|
|
|
+ m["总有攻电能(kW·h)"] = data.EPE
|
|
response.Code = 200
|
|
response.Code = 200
|
|
- response.RequestId = strconv.Itoa(rand.Intn(1000))
|
|
|
|
- response.Msg = "success"
|
|
|
|
- data["今日用电量"] = rand.Intn(1000)
|
|
|
|
- data["昨日用电量"] = rand.Intn(1000)
|
|
|
|
- data["本月用水量"] = rand.Intn(1000)
|
|
|
|
- response.Data = data
|
|
|
|
|
|
+ response.Msg = "获取设备信息成功"
|
|
|
|
+ response.Data = m
|
|
res, _ := json.Marshal(&response)
|
|
res, _ := json.Marshal(&response)
|
|
fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
fmt.Fprintf(ctx.Writer, "data: %s\n\n", string(res))
|
|
ctx.Writer.Flush()
|
|
ctx.Writer.Flush()
|
|
@@ -160,6 +429,42 @@ func (h *EnergyHandler) GetAmmeter(ctx *gin.Context) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// GetHisData 获取历史数据
|
|
|
|
+func (h *EnergyHandler) GetHisData(ctx *gin.Context) {
|
|
|
|
+ dev_udid := ctx.Query("dev_udid")
|
|
|
|
+ start_time := ctx.Query("start_time")
|
|
|
|
+ end_time := ctx.Query("end_time")
|
|
|
|
+ data_size := ctx.Query("data_size")
|
|
|
|
+ data_type := ctx.Query("data_type")
|
|
|
|
+ cuerrentPage := ctx.Query("cuerrentPage")
|
|
|
|
+ pageSize := ctx.Query("pageSize")
|
|
|
|
+ is_page := ctx.Query("is_page")
|
|
|
|
+ if len(data_size) == 0 {
|
|
|
|
+ data_size = "1"
|
|
|
|
+ }
|
|
|
|
+ if len(data_type) == 0 {
|
|
|
|
+ data_type = "1"
|
|
|
|
+ }
|
|
|
|
+ if len(is_page) == 0 {
|
|
|
|
+ is_page = "1"
|
|
|
|
+ }
|
|
|
|
+ start, err := time.Parse("2006-01-02 15:04:05", start_time)
|
|
|
|
+ end, err := time.Parse("2006-01-02 15:04:05", end_time)
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, "时间格式错误", nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ start_time = start.Format("2006-01-02 15:04:05")
|
|
|
|
+ end_time = end.Format("2006-01-02 15:04:05")
|
|
|
|
+ data, err := h.energyService.GetHisData(dev_udid, start_time, end_time, data_size, data_type, cuerrentPage, pageSize, is_page)
|
|
|
|
+ if err != nil {
|
|
|
|
+ resp.HandleError(ctx, 1201, err.Error(), nil)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ resp.HandleSuccess(ctx, data)
|
|
|
|
+}
|
|
|
|
+
|
|
func (h *EnergyHandler) GetTemperature(ctx *gin.Context) {
|
|
func (h *EnergyHandler) GetTemperature(ctx *gin.Context) {
|
|
// 设置响应头
|
|
// 设置响应头
|
|
ctx.Header("Content-Type", "text/event-stream")
|
|
ctx.Header("Content-Type", "text/event-stream")
|