|
|
@@ -7,9 +7,7 @@ import (
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
- "math/rand"
|
|
|
"net/http"
|
|
|
- "os"
|
|
|
"time"
|
|
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
|
@@ -88,9 +86,9 @@ func updateElectricalData() {
|
|
|
ViewID int `json:"ViewID"`
|
|
|
}
|
|
|
reqData := RequestData{
|
|
|
- LineID: firstModel.LineID,
|
|
|
- StationID: firstModel.StationID,
|
|
|
- ViewID: firstModel.ViewID,
|
|
|
+ LineID: firstModel.LineID,
|
|
|
+ // StationID: firstModel.StationID,
|
|
|
+ // ViewID: firstModel.ViewID,
|
|
|
}
|
|
|
|
|
|
// 将请求数据转换为JSON
|
|
|
@@ -136,7 +134,7 @@ func updateElectricalData() {
|
|
|
if err := json.Unmarshal(equipBytes, &equipments); err == nil {
|
|
|
// 更新总包设备数据 (在更新时加锁)
|
|
|
manager.UpdateBatchData(equipments)
|
|
|
- fmt.Printf("成功更新线路%d车站%d视图%d的电调模型数据\n", firstModel.LineID, firstModel.StationID, firstModel.ViewID)
|
|
|
+ fmt.Printf("成功更新线路%d的电调模型数据\n", firstModel.LineID)
|
|
|
} else {
|
|
|
fmt.Printf("解析设备列表失败: %v\n", err)
|
|
|
}
|
|
|
@@ -150,8 +148,8 @@ type EDispatchController struct {
|
|
|
beego.Controller
|
|
|
}
|
|
|
|
|
|
-// /courserun/eqpmtstatus 更新电调设备状态
|
|
|
-func (c *EDispatchController) Eqpmtstatus() {
|
|
|
+// /courserun/eqpmtvalue 更新电调设备状态
|
|
|
+func (c *EDispatchController) Eqpmtvalue() {
|
|
|
var req models.DiandiaoModel
|
|
|
|
|
|
requestBody := c.Ctx.Input.RequestBody
|
|
|
@@ -299,7 +297,6 @@ func (c *EDispatchController) Addctrllog() {
|
|
|
c.ServeJSON()
|
|
|
}
|
|
|
|
|
|
-// /serverCtrl/subsysstatus 获取子系统状态
|
|
|
func (c *EDispatchController) SubSysStatus() {
|
|
|
// 从配置文件读取电调服务IP和端口
|
|
|
electricalIP, err := beego.AppConfig.String("ElectricalIP")
|
|
|
@@ -325,8 +322,26 @@ func (c *EDispatchController) SubSysStatus() {
|
|
|
// 构造电调服务地址
|
|
|
electricalAddr := fmt.Sprintf("http://%s:%d/ServerCtrl/SubSysStatus", electricalIP, electricalPort)
|
|
|
|
|
|
- // 调用电调服务获取子系统状态接口
|
|
|
- resp, err := http.Get(electricalAddr)
|
|
|
+ // 构造请求体,使用空的JSON对象
|
|
|
+ reqBody := "{}"
|
|
|
+
|
|
|
+ // 创建带适当头部的请求
|
|
|
+ req, err := http.NewRequest("POST", electricalAddr, bytes.NewBufferString(reqBody))
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "创建请求失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置Content-Type头部
|
|
|
+ req.Header.Set("Content-Type", "application/json")
|
|
|
+
|
|
|
+ // 创建HTTP客户端并发送请求
|
|
|
+ client := &http.Client{}
|
|
|
+ resp, err := client.Do(req)
|
|
|
if err != nil {
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
|
"code": 500,
|
|
|
@@ -359,171 +374,320 @@ func (c *EDispatchController) SubSysStatus() {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 尝试解析JSON响应
|
|
|
- var result interface{}
|
|
|
+ // 尝试解析电调服务返回的JSON
|
|
|
+ var result map[string]interface{}
|
|
|
if err := json.Unmarshal(bodyBytes, &result); err != nil {
|
|
|
- // 如果不是JSON格式,作为字符串返回
|
|
|
- c.Data["json"] = map[string]interface{}{
|
|
|
- "code": 200,
|
|
|
- "msg": "获取子系统状态成功",
|
|
|
- "data": string(bodyBytes),
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 如果是JSON格式,直接返回解析结果
|
|
|
- c.Data["json"] = map[string]interface{}{
|
|
|
- "code": 200,
|
|
|
- "msg": "获取子系统状态成功",
|
|
|
- "data": result,
|
|
|
- }
|
|
|
+ // 如果解析失败,返回原始内容
|
|
|
+ c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
|
|
|
+ c.Ctx.Output.Body(bodyBytes)
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
+ // 如果解析成功,直接返回JSON结果
|
|
|
+ c.Data["json"] = result
|
|
|
c.ServeJSON()
|
|
|
}
|
|
|
func (c *EDispatchController) UpdateTestStatus() {
|
|
|
|
|
|
}
|
|
|
|
|
|
+// // /serverCtrl/alleqpmtlist 获取所有设备列表
|
|
|
+// func (c *EDispatchController) AllEqpmtList() {
|
|
|
+// // 定义与testStatus.json文件匹配的结构体
|
|
|
+// type jsonData struct {
|
|
|
+// DataID string `json:"dataId"`
|
|
|
+// DataName string `json:"dataName"`
|
|
|
+// Value string `json:"value"`
|
|
|
+// ID string `json:"id"`
|
|
|
+// }
|
|
|
+
|
|
|
+// // 读取testStatus.json文件
|
|
|
+// filePath := "static/testStatus.json"
|
|
|
+// data, err := os.ReadFile(filePath)
|
|
|
+// if err != nil {
|
|
|
+// c.Ctx.Output.SetStatus(500)
|
|
|
+// c.Data["json"] = map[string]interface{}{
|
|
|
+// "code": 500,
|
|
|
+// "msg": "读取文件失败: " + err.Error(),
|
|
|
+// }
|
|
|
+// c.ServeJSON()
|
|
|
+// return
|
|
|
+// }
|
|
|
+
|
|
|
+// // 解析JSON数据
|
|
|
+// var jsonDataList []jsonData
|
|
|
+// err = json.Unmarshal(data, &jsonDataList)
|
|
|
+// if err != nil {
|
|
|
+// c.Ctx.Output.SetStatus(500)
|
|
|
+// c.Data["json"] = map[string]interface{}{
|
|
|
+// "code": 500,
|
|
|
+// "msg": "解析JSON失败: " + err.Error(),
|
|
|
+// }
|
|
|
+// c.ServeJSON()
|
|
|
+// return
|
|
|
+// }
|
|
|
+
|
|
|
+// // 转换为EquipmentData结构体
|
|
|
+// type EquipmentData struct {
|
|
|
+// DataID string `json:"dataId"`
|
|
|
+// DataName string `json:"dataName"`
|
|
|
+// Value string `json:"value"`
|
|
|
+// ID string `json:"id"`
|
|
|
+// }
|
|
|
+// //动态生成值 1-3改变
|
|
|
+
|
|
|
+// // 初始化随机数种子
|
|
|
+// rand.Seed(time.Now().UnixNano())
|
|
|
+
|
|
|
+// // 生成1-3之间的随机数
|
|
|
+// nValue := rand.Intn(3) + 1
|
|
|
+
|
|
|
+// var equipmentDataList []EquipmentData
|
|
|
+// for _, item := range jsonDataList {
|
|
|
+// equipment := EquipmentData{
|
|
|
+// DataID: item.DataID,
|
|
|
+// DataName: item.DataName,
|
|
|
+// Value: fmt.Sprintf("%d", nValue),
|
|
|
+// ID: item.ID,
|
|
|
+// }
|
|
|
+// equipmentDataList = append(equipmentDataList, equipment)
|
|
|
+// }
|
|
|
+
|
|
|
+// // 返回结果
|
|
|
+// c.Data["json"] = equipmentDataList
|
|
|
+// c.ServeJSON()
|
|
|
+
|
|
|
+// }
|
|
|
+
|
|
|
// /serverCtrl/alleqpmtlist 获取所有设备列表
|
|
|
func (c *EDispatchController) AllEqpmtList() {
|
|
|
- c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
|
|
|
- // 定义与testStatus.json文件匹配的结构体
|
|
|
- type jsonData struct {
|
|
|
+ // 转换缓存数据为指定格式
|
|
|
+ type EquipmentData struct {
|
|
|
DataID string `json:"dataId"`
|
|
|
DataName string `json:"dataName"`
|
|
|
Value string `json:"value"`
|
|
|
ID string `json:"id"`
|
|
|
}
|
|
|
|
|
|
- // 读取testStatus.json文件
|
|
|
- filePath := "static/testStatus.json"
|
|
|
- data, err := os.ReadFile(filePath)
|
|
|
+ // 从配置文件读取电调服务IP和端口
|
|
|
+ electricalIP, err := beego.AppConfig.String("ElectricalIP")
|
|
|
if err != nil {
|
|
|
- c.Ctx.Output.SetStatus(500)
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
|
"code": 500,
|
|
|
- "msg": "读取文件失败: " + err.Error(),
|
|
|
+ "msg": "读取电调服务IP配置失败: " + err.Error(),
|
|
|
}
|
|
|
c.ServeJSON()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 解析JSON数据
|
|
|
- var jsonDataList []jsonData
|
|
|
- err = json.Unmarshal(data, &jsonDataList)
|
|
|
+ electricalPort, err := beego.AppConfig.Int("ElectricalPort")
|
|
|
if err != nil {
|
|
|
- c.Ctx.Output.SetStatus(500)
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
|
"code": 500,
|
|
|
- "msg": "解析JSON失败: " + err.Error(),
|
|
|
+ "msg": "读取电调服务端口配置失败: " + err.Error(),
|
|
|
}
|
|
|
c.ServeJSON()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 转换为EquipmentData结构体
|
|
|
- type EquipmentData struct {
|
|
|
- DataID string `json:"dataId"`
|
|
|
- DataName string `json:"dataName"`
|
|
|
- Value string `json:"value"`
|
|
|
- ID string `json:"id"`
|
|
|
+ // 构造电调服务地址
|
|
|
+ electricalAddr := fmt.Sprintf("http://%s:%d/ServerCtrl/AllEqpmtList", electricalIP, electricalPort)
|
|
|
+
|
|
|
+ // 获取最新的课程运行记录以获取线路号
|
|
|
+ o := orm.NewOrm()
|
|
|
+ var latestCourse models.CourseRunLog
|
|
|
+ err = o.QueryTable(new(models.CourseRunLog)).
|
|
|
+ OrderBy("-RunLogID").
|
|
|
+ One(&latestCourse)
|
|
|
+
|
|
|
+ if err != nil && err != orm.ErrNoRows {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "查询最新课程运行记录失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
}
|
|
|
- //动态生成值 1-3改变
|
|
|
|
|
|
- // 初始化随机数种子
|
|
|
- rand.Seed(time.Now().UnixNano())
|
|
|
+ // 如果没有找到运行记录,默认线路号为0
|
|
|
+ lineID := 0
|
|
|
+ if err != orm.ErrNoRows {
|
|
|
+ // 根据运行课程ID查询课程信息以获取线路号
|
|
|
+ var courseInfo models.CourseInfo
|
|
|
+ err = o.QueryTable(new(models.CourseInfo)).
|
|
|
+ Filter("CourseID", latestCourse.RunCourseID).
|
|
|
+ One(&courseInfo)
|
|
|
|
|
|
- // 生成1-3之间的随机数
|
|
|
- nValue := rand.Intn(3) + 1
|
|
|
+ if err != nil && err != orm.ErrNoRows {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "查询课程信息失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- var equipmentDataList []EquipmentData
|
|
|
- for _, item := range jsonDataList {
|
|
|
- equipment := EquipmentData{
|
|
|
- DataID: item.DataID,
|
|
|
- DataName: item.DataName,
|
|
|
- Value: fmt.Sprintf("%d", nValue),
|
|
|
- ID: item.ID,
|
|
|
+ if err != orm.ErrNoRows {
|
|
|
+ lineID = courseInfo.LineNo
|
|
|
}
|
|
|
- equipmentDataList = append(equipmentDataList, equipment)
|
|
|
}
|
|
|
|
|
|
- // 返回结果
|
|
|
- c.Data["json"] = equipmentDataList
|
|
|
- c.ServeJSON()
|
|
|
+ // 生成请求key
|
|
|
+ model := models.DiandiaoModel{
|
|
|
+ LineID: lineID,
|
|
|
+ }
|
|
|
+ key := model.GenerateModelKey()
|
|
|
|
|
|
-}
|
|
|
+ // 获取 ElectricModelManager 单例实例
|
|
|
+ manager := models.GetInstance()
|
|
|
|
|
|
-// /serverCtrl/alleqpmtlist 获取所有设备列表
|
|
|
-func (c *EDispatchController) AllEqpmtList2() {
|
|
|
- c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
|
|
|
- // 定义与testStatus.json文件匹配的结构体
|
|
|
- type jsonData struct {
|
|
|
- DataID string `json:"dataId"`
|
|
|
- DataName string `json:"dataName"`
|
|
|
- Value string `json:"value"`
|
|
|
- ID string `json:"id"`
|
|
|
+ // 检查缓存中是否存在对应key的数据,且最后更新时间在5秒内
|
|
|
+ if viewMapModel, exists := manager.GetModelByKey(key); exists {
|
|
|
+ // 检查最后总包更新时间是否在5秒内
|
|
|
+ if time.Since(viewMapModel.LastBatchUpdate) <= 5*time.Second || time.Since(viewMapModel.LastDeviceUpdate) <= 5*time.Second {
|
|
|
+
|
|
|
+ var equipmentsData []EquipmentData
|
|
|
+ for _, model := range viewMapModel.Models {
|
|
|
+ equip := EquipmentData{
|
|
|
+ DataID: model.EqpmtStrID,
|
|
|
+ ID: model.EqpmtStrID,
|
|
|
+ DataName: model.EqpmtStdName,
|
|
|
+ }
|
|
|
+
|
|
|
+ switch model.CMDType {
|
|
|
+ case 0: //设备状态
|
|
|
+ equip.Value = fmt.Sprintf("%d", model.EqpmtStatus)
|
|
|
+ case 1: //设备数据控制
|
|
|
+ equip.Value = fmt.Sprintf("%d", model.FValue)
|
|
|
+ }
|
|
|
+ equipmentsData = append(equipmentsData, equip)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用缓存数据
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 200,
|
|
|
+ "msg": "获取所有设备列表成功(缓存)",
|
|
|
+ "data": equipmentsData,
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 准备请求数据
|
|
|
+ type RequestData struct {
|
|
|
+ LineID int `json:"lineID"`
|
|
|
+ StationID int `json:"StationID"`
|
|
|
+ ViewID int `json:"ViewID"`
|
|
|
+ }
|
|
|
+ reqData := RequestData{
|
|
|
+ LineID: lineID,
|
|
|
+ StationID: 0,
|
|
|
+ ViewID: 0,
|
|
|
}
|
|
|
|
|
|
- // 读取testStatus.json文件
|
|
|
- filePath := "static/testStatus.json"
|
|
|
- data, err := os.ReadFile(filePath)
|
|
|
+ // 将请求数据转换为JSON
|
|
|
+ reqBody, err := json.Marshal(reqData)
|
|
|
if err != nil {
|
|
|
- c.Ctx.Output.SetStatus(500)
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
|
"code": 500,
|
|
|
- "msg": "读取文件失败: " + err.Error(),
|
|
|
+ "msg": "构造请求数据失败: " + err.Error(),
|
|
|
}
|
|
|
c.ServeJSON()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 解析JSON数据
|
|
|
- var jsonDataList []jsonData
|
|
|
- err = json.Unmarshal(data, &jsonDataList)
|
|
|
+ // 调用电调服务获取所有设备列表接口,使用POST方法
|
|
|
+ resp, err := http.Post(electricalAddr, "application/json", bytes.NewBuffer(reqBody))
|
|
|
if err != nil {
|
|
|
- c.Ctx.Output.SetStatus(500)
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
|
"code": 500,
|
|
|
- "msg": "解析JSON失败: " + err.Error(),
|
|
|
+ "msg": "调用电调服务获取所有设备列表接口失败: " + err.Error(),
|
|
|
}
|
|
|
c.ServeJSON()
|
|
|
return
|
|
|
}
|
|
|
+ defer resp.Body.Close()
|
|
|
|
|
|
- // 转换为EquipmentData结构体
|
|
|
- type EquipmentData struct {
|
|
|
- DataID string `json:"dataId"`
|
|
|
- DataName string `json:"dataName"`
|
|
|
- Value string `json:"value"`
|
|
|
- ID string `json:"id"`
|
|
|
+ // 检查响应状态
|
|
|
+ if resp.StatusCode != http.StatusOK {
|
|
|
+ bodyBytes, _ := io.ReadAll(resp.Body)
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": fmt.Sprintf("电调服务返回异常状态码: %d,响应内容: %s", resp.StatusCode, string(bodyBytes)),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
}
|
|
|
- //动态生成值 1-3改变
|
|
|
|
|
|
- // 初始化随机数种子
|
|
|
- rand.Seed(time.Now().UnixNano())
|
|
|
+ // 读取并解析响应体
|
|
|
+ bodyBytes, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "读取电调服务响应失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- // 生成1-3之间的随机数
|
|
|
- nValue := rand.Intn(3) + 1
|
|
|
+ // 尝试解析JSON响应
|
|
|
+ var result map[string]interface{}
|
|
|
+ if err := json.Unmarshal(bodyBytes, &result); err != nil {
|
|
|
+ // 如果不是JSON格式,作为字符串返回
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 200,
|
|
|
+ "msg": "获取所有设备列表成功",
|
|
|
+ "data": string(bodyBytes),
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果是JSON格式,检查是否有data字段
|
|
|
+ if data, exists := result["data"]; exists {
|
|
|
+ // 解析data中的设备列表并更新到模型管理器
|
|
|
+ var equipments []models.DiandiaoModel
|
|
|
+ if equipBytes, err := json.Marshal(data); err == nil {
|
|
|
+ if err := json.Unmarshal(equipBytes, &equipments); err == nil {
|
|
|
+ // 更新总包设备数据
|
|
|
+ manager.UpdateBatchData(equipments)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var equipmentsData []EquipmentData
|
|
|
+ for _, model := range equipments {
|
|
|
+ equip := EquipmentData{
|
|
|
+ DataID: model.EqpmtStrID,
|
|
|
+ ID: model.EqpmtStrID,
|
|
|
+ DataName: model.EqpmtStdName,
|
|
|
+ }
|
|
|
+
|
|
|
+ if model.CMDType == 0 { //设备状态
|
|
|
+ equip.Value = fmt.Sprintf("%d", model.EqpmtStatus)
|
|
|
+ } else if model.CMDType == 1 { //设备数据控制
|
|
|
+ equip.Value = fmt.Sprintf("%d", model.FValue)
|
|
|
+ }
|
|
|
+ equipmentsData = append(equipmentsData, equip)
|
|
|
+ }
|
|
|
|
|
|
- var equipmentDataList []EquipmentData
|
|
|
- for _, item := range jsonDataList {
|
|
|
- equipment := EquipmentData{
|
|
|
- DataID: item.DataID,
|
|
|
- DataName: item.DataName,
|
|
|
- Value: fmt.Sprintf("%d", nValue),
|
|
|
- ID: item.ID,
|
|
|
+ // 返回响应
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 200,
|
|
|
+ "msg": "获取所有设备列表成功",
|
|
|
+ "data": equipmentsData,
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果没有data字段,返回整个响应
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "获取所有设备列表失败",
|
|
|
+ }
|
|
|
}
|
|
|
- equipmentDataList = append(equipmentDataList, equipment)
|
|
|
}
|
|
|
|
|
|
- // 返回结果
|
|
|
- c.Data["json"] = equipmentDataList
|
|
|
c.ServeJSON()
|
|
|
-
|
|
|
}
|
|
|
|
|
|
-// /serverCtrl/alleqpmtlist 获取所有设备列表
|
|
|
-func (c *EDispatchController) AllEqpmtList_bak() {
|
|
|
- c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
|
|
|
+// /serverCtrl/alleqpmtlist2 获取所有设备列表 测试使用
|
|
|
+func (c *EDispatchController) AllEqpmtList2() {
|
|
|
// 转换缓存数据为指定格式
|
|
|
type EquipmentData struct {
|
|
|
DataID string `json:"dataId"`
|
|
|
@@ -597,9 +761,7 @@ func (c *EDispatchController) AllEqpmtList_bak() {
|
|
|
|
|
|
// 生成请求key
|
|
|
model := models.DiandiaoModel{
|
|
|
- LineID: lineID,
|
|
|
- StationID: 0,
|
|
|
- ViewID: 0,
|
|
|
+ LineID: lineID,
|
|
|
}
|
|
|
key := model.GenerateModelKey()
|
|
|
|
|
|
@@ -614,10 +776,16 @@ func (c *EDispatchController) AllEqpmtList_bak() {
|
|
|
var equipmentsData []EquipmentData
|
|
|
for _, model := range viewMapModel.Models {
|
|
|
equip := EquipmentData{
|
|
|
- DataID: model.EqpmtID,
|
|
|
- ID: model.EqpmtID,
|
|
|
- Value: fmt.Sprintf("%d", model.Status),
|
|
|
- DataName: model.StatusName,
|
|
|
+ DataID: model.EqpmtStrID,
|
|
|
+ ID: model.EqpmtStrID,
|
|
|
+ DataName: model.EqpmtStdName,
|
|
|
+ }
|
|
|
+
|
|
|
+ switch model.CMDType {
|
|
|
+ case 0: //设备状态
|
|
|
+ equip.Value = fmt.Sprintf("%d", model.EqpmtStatus)
|
|
|
+ case 1: //设备数据控制
|
|
|
+ equip.Value = fmt.Sprintf("%d", model.FValue)
|
|
|
}
|
|
|
equipmentsData = append(equipmentsData, equip)
|
|
|
}
|
|
|
@@ -712,14 +880,19 @@ func (c *EDispatchController) AllEqpmtList_bak() {
|
|
|
}
|
|
|
|
|
|
var equipmentsData []EquipmentData
|
|
|
- for _, equip := range equipments {
|
|
|
- equipmentData := EquipmentData{
|
|
|
- DataID: equip.EqpmtID,
|
|
|
- DataName: equip.StatusName,
|
|
|
- Value: fmt.Sprintf("%d", equip.Status),
|
|
|
- ID: equip.EqpmtID,
|
|
|
+ for _, model := range equipments {
|
|
|
+ equip := EquipmentData{
|
|
|
+ DataID: model.EqpmtStrID,
|
|
|
+ ID: model.EqpmtStrID,
|
|
|
+ DataName: model.EqpmtStdName,
|
|
|
+ }
|
|
|
+
|
|
|
+ if model.CMDType == 0 { //设备状态
|
|
|
+ equip.Value = fmt.Sprintf("%d", model.EqpmtStatus)
|
|
|
+ } else if model.CMDType == 1 { //设备数据控制
|
|
|
+ equip.Value = fmt.Sprintf("%d", model.FValue)
|
|
|
}
|
|
|
- equipmentsData = append(equipmentsData, equipmentData)
|
|
|
+ equipmentsData = append(equipmentsData, equip)
|
|
|
}
|
|
|
|
|
|
// 返回响应
|
|
|
@@ -740,9 +913,235 @@ func (c *EDispatchController) AllEqpmtList_bak() {
|
|
|
c.ServeJSON()
|
|
|
}
|
|
|
|
|
|
-func (c *EDispatchController) Options() {
|
|
|
- c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
|
|
|
- c.Ctx.Output.Header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS")
|
|
|
- c.Ctx.Output.Header("Access-Control-Allow-Headers", "Content-Type,Authorization")
|
|
|
- c.Ctx.ResponseWriter.WriteHeader(200)
|
|
|
+// /serverctrl/seteqpmtoper 设置电调设备操作
|
|
|
+func (c *EDispatchController) Seteqpmtoper() {
|
|
|
+ // 定义接收的请求数据结构
|
|
|
+ var req struct {
|
|
|
+ EqpmtStrID string `json:"EqpmtStrID"`
|
|
|
+ LineID int `json:"LineID"`
|
|
|
+ CMDType int `json:"CMDType"`
|
|
|
+ EqpmtStatus int `json:"EqpmtStatus"`
|
|
|
+ FValue int `json:"fValue"`
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析请求体中的JSON数据
|
|
|
+ if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
|
|
|
+ c.Ctx.Output.SetStatus(400)
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 400,
|
|
|
+ "msg": "请求参数格式错误: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构造转发给电调服务器的请求数据
|
|
|
+ forwardReq := struct {
|
|
|
+ EqpmtStrID string `json:"EqpmtStrID"`
|
|
|
+ LineID int `json:"LineID"`
|
|
|
+ EqpmtStdName string `json:"EqpmtStdName"`
|
|
|
+ CMDType int `json:"CMDType"`
|
|
|
+ EqpmtStatus int `json:"EqpmtStatus"`
|
|
|
+ CtrlType int `json:"CtrlType"`
|
|
|
+ OpeMark int `json:"OpeMark"`
|
|
|
+ FValue int `json:"fValue"`
|
|
|
+ NValueType int `json:"nValueType"`
|
|
|
+ IsReset int `json:"IsReset"`
|
|
|
+ }{
|
|
|
+ EqpmtStrID: req.EqpmtStrID,
|
|
|
+ LineID: req.LineID,
|
|
|
+ EqpmtStdName: "", // 默认空值
|
|
|
+ CMDType: req.CMDType,
|
|
|
+ EqpmtStatus: req.EqpmtStatus,
|
|
|
+ CtrlType: 0, // 默认值
|
|
|
+ OpeMark: 0, // 默认值
|
|
|
+ FValue: req.FValue,
|
|
|
+ NValueType: 0, // 默认值
|
|
|
+ IsReset: 0, // 默认值
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将构造好的请求数据序列化为JSON
|
|
|
+ forwardReqBody, err := json.Marshal(forwardReq)
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "构造转发请求数据失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从配置文件读取电调服务IP和端口
|
|
|
+ electricalIP, err := beego.AppConfig.String("ElectricalIP")
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "读取电调服务IP配置失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ electricalPort, err := beego.AppConfig.Int("ElectricalPort")
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "读取电调服务端口配置失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构造电调服务地址
|
|
|
+ electricalAddr := fmt.Sprintf("http://%s:%d/ServerCtrl/SetEqpmtOper", electricalIP, electricalPort)
|
|
|
+
|
|
|
+ // 将请求数据转发到电调服务器
|
|
|
+ resp, err := http.Post(electricalAddr, "application/json", bytes.NewBuffer(forwardReqBody))
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "转发请求到电调服务器失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ fmt.Println("Seteqpmtoper 发送 JSON:", string(forwardReqBody))
|
|
|
+
|
|
|
+ // 读取响应体以便打印,然后将其重新放回供后续处理
|
|
|
+ respBodyBytes, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("Seteqpmtoper: 读取响应用于打印失败: %v\n", err)
|
|
|
+ } else {
|
|
|
+ fmt.Println("Seteqpmtoper 接收 JSON:", string(respBodyBytes))
|
|
|
+ // 由于后续代码还会读取 resp.Body,这里把数据重新放回
|
|
|
+ resp.Body = io.NopCloser(bytes.NewBuffer(respBodyBytes))
|
|
|
+ }
|
|
|
+
|
|
|
+ // 读取响应体
|
|
|
+ bodyBytes, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "读取电调服务响应失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查响应状态
|
|
|
+ if resp.StatusCode != http.StatusOK {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": resp.StatusCode,
|
|
|
+ "msg": fmt.Sprintf("电调服务返回异常状态码: %d,响应内容: %s", resp.StatusCode, string(bodyBytes)),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试解析响应为JSON
|
|
|
+ var result map[string]interface{}
|
|
|
+ if err := json.Unmarshal(bodyBytes, &result); err != nil {
|
|
|
+ // 如果不是JSON格式,原样返回
|
|
|
+ c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
|
|
|
+ c.Ctx.Output.Body(bodyBytes)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回电调服务器的响应
|
|
|
+ c.Data["json"] = result
|
|
|
+ c.ServeJSON()
|
|
|
+}
|
|
|
+
|
|
|
+// /serverctrl/seteqpmtfault 设置设备故障
|
|
|
+func (c *EDispatchController) Seteqpmtfault() {
|
|
|
+ // 定义接收的请求数据结构
|
|
|
+ var req struct {
|
|
|
+ EqpmtStrID string `json:"EqpmtStrID"`
|
|
|
+ LineID int `json:"LineID"`
|
|
|
+ EventType int `json:"EventType"`
|
|
|
+ EventId int `json:"EventId"`
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析请求体中的JSON数据
|
|
|
+ if err := json.Unmarshal(c.Ctx.Input.RequestBody, &req); err != nil {
|
|
|
+ c.Ctx.Output.SetStatus(400)
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 400,
|
|
|
+ "msg": "请求参数格式错误: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从配置文件读取电调服务IP和端口
|
|
|
+ electricalIP, err := beego.AppConfig.String("ElectricalIP")
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "读取电调服务IP配置失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ electricalPort, err := beego.AppConfig.Int("ElectricalPort")
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "读取电调服务端口配置失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构造电调服务地址
|
|
|
+ electricalAddr := fmt.Sprintf("http://%s:%d/ServerCtrl/SetEqpmtFault", electricalIP, electricalPort)
|
|
|
+
|
|
|
+ // 将请求数据转发到电调服务器
|
|
|
+ resp, err := http.Post(electricalAddr, "application/json", bytes.NewBuffer(c.Ctx.Input.RequestBody))
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "转发请求到电调服务器失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // 读取响应体
|
|
|
+ bodyBytes, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": 500,
|
|
|
+ "msg": "读取电调服务响应失败: " + err.Error(),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查响应状态
|
|
|
+ if resp.StatusCode != http.StatusOK {
|
|
|
+ c.Data["json"] = map[string]interface{}{
|
|
|
+ "code": resp.StatusCode,
|
|
|
+ "msg": fmt.Sprintf("电调服务返回异常状态码: %d,响应内容: %s", resp.StatusCode, string(bodyBytes)),
|
|
|
+ }
|
|
|
+ c.ServeJSON()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 尝试解析响应为JSON
|
|
|
+ var result map[string]interface{}
|
|
|
+ if err := json.Unmarshal(bodyBytes, &result); err != nil {
|
|
|
+ // 如果不是JSON格式,原样返回
|
|
|
+ c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
|
|
|
+ c.Ctx.Output.Body(bodyBytes)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回电调服务器的响应
|
|
|
+ c.Data["json"] = result
|
|
|
+ c.ServeJSON()
|
|
|
}
|