modelRT/real-time-data/real_time_data_receive.go

83 lines
2.5 KiB
Go

// Package realtimedata define real time data operation functions
package realtimedata
import (
"context"
"modelRT/config"
constants "modelRT/constant"
"modelRT/diagram"
"modelRT/logger"
"modelRT/network"
"modelRT/pool"
)
// 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
func ReceiveChan(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case realTimeData := <-RealTimeDataChan:
// TODO 根据 componentID 缓存到来的实时数据
componentID := realTimeData.PayLoad.ComponentID
component, err := diagram.GetComponentMap(componentID)
if err != nil {
logger.Error(ctx, "query component info from diagram map by componet id failed", "component_id", componentID, "error", err)
continue
}
componentType := component["component_type"].(int)
if componentType != constants.DemoType {
logger.Error(ctx, "can not process real time data of component type not equal DemoType", "component_id", componentID)
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,
}
pool.GetComponentChan(ctx, componentID) <- anchorConfig
default:
}
}
}