2025-01-20 16:20:21 +08:00
|
|
|
// Package realtimedata define real time data operation functions
|
|
|
|
|
package realtimedata
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2025-01-21 16:35:44 +08:00
|
|
|
"modelRT/config"
|
2025-08-05 15:20:07 +08:00
|
|
|
"modelRT/constants"
|
2025-01-21 16:35:44 +08:00
|
|
|
"modelRT/diagram"
|
|
|
|
|
"modelRT/logger"
|
2025-01-20 16:20:21 +08:00
|
|
|
"modelRT/network"
|
2025-01-22 16:38:46 +08:00
|
|
|
"modelRT/pool"
|
2025-01-20 16:20:21 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// RealTimeDataChan define channel of real time data receive
|
|
|
|
|
var RealTimeDataChan chan network.RealTimeDataReceiveRequest
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
RealTimeDataChan = make(chan network.RealTimeDataReceiveRequest, 100)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReceiveChan define func of real time data receive and process
|
2025-01-22 16:38:46 +08:00
|
|
|
func ReceiveChan(ctx context.Context) {
|
2025-01-20 16:20:21 +08:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return
|
|
|
|
|
case realTimeData := <-RealTimeDataChan:
|
2025-01-21 16:35:44 +08:00
|
|
|
// TODO 根据 componentID 缓存到来的实时数据
|
|
|
|
|
componentID := realTimeData.PayLoad.ComponentID
|
|
|
|
|
component, err := diagram.GetComponentMap(componentID)
|
|
|
|
|
if err != nil {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(ctx, "query component info from diagram map by componet id failed", "component_id", componentID, "error", err)
|
2025-01-21 16:35:44 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentType := component["component_type"].(int)
|
2025-06-13 15:34:49 +08:00
|
|
|
if componentType != constants.DemoType {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(ctx, "can not process real time data of component type not equal DemoType", "component_id", componentID)
|
2025-01-21 16:35:44 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var anchorName string
|
|
|
|
|
var compareValUpperLimit, compareValLowerLimit float64
|
|
|
|
|
var anchorRealTimeData []float64
|
|
|
|
|
var calculateFunc func(archorValue float64, args ...float64) float64
|
|
|
|
|
if anchoringV := component["anchor_v"].(bool); anchoringV {
|
|
|
|
|
anchorName = "voltage"
|
|
|
|
|
compareValUpperLimit = component["uv_alarm"].(float64)
|
|
|
|
|
compareValLowerLimit = component["ov_alarm"].(float64)
|
|
|
|
|
} else {
|
|
|
|
|
anchorName = "current"
|
|
|
|
|
compareValUpperLimit = component["ui_alarm"].(float64)
|
|
|
|
|
compareValLowerLimit = component["oi_alarm"].(float64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentData := map[string]interface{}{
|
|
|
|
|
"resistance": component["resistance"].(float64),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
calculateFunc, params := config.SelectAnchorCalculateFuncAndParams(componentType, anchorName, componentData)
|
|
|
|
|
|
|
|
|
|
for _, param := range realTimeData.PayLoad.Values {
|
|
|
|
|
anchorRealTimeData = append(anchorRealTimeData, param.Value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
anchorConfig := config.AnchorParamConfig{
|
|
|
|
|
AnchorParamBaseConfig: config.AnchorParamBaseConfig{
|
|
|
|
|
ComponentID: componentID,
|
|
|
|
|
AnchorName: anchorName,
|
|
|
|
|
CompareValUpperLimit: compareValUpperLimit,
|
|
|
|
|
CompareValLowerLimit: compareValLowerLimit,
|
|
|
|
|
AnchorRealTimeData: anchorRealTimeData,
|
|
|
|
|
},
|
|
|
|
|
CalculateFunc: calculateFunc,
|
|
|
|
|
CalculateParams: params,
|
|
|
|
|
}
|
2025-01-22 16:38:46 +08:00
|
|
|
pool.GetComponentChan(ctx, componentID) <- anchorConfig
|
2025-01-20 16:20:21 +08:00
|
|
|
default:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|