87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
// Package realtimedata define real time data operation functions
|
|
package realtimedata
|
|
|
|
import (
|
|
"context"
|
|
|
|
"modelRT/config"
|
|
"modelRT/constant"
|
|
"modelRT/diagram"
|
|
"modelRT/logger"
|
|
"modelRT/network"
|
|
"modelRT/pool"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// 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) {
|
|
logger := logger.GetLoggerInstance()
|
|
|
|
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("query component info from diagram map by componet id failed", zap.Int64("component_id", componentID), zap.Error(err))
|
|
continue
|
|
}
|
|
|
|
componentType := component["component_type"].(int)
|
|
if componentType != constant.DemoType {
|
|
logger.Error("can not process real time data of component type not equal DemoType", zap.Int64("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:
|
|
}
|
|
}
|
|
}
|