modelRT/pool/concurrency_anchor_parse.go

116 lines
3.6 KiB
Go
Raw Normal View History

2024-12-18 16:25:49 +08:00
// Package pool define concurrency call function in ants
package pool
import (
"fmt"
"modelRT/config"
"modelRT/diagram"
2024-12-18 16:25:49 +08:00
"go.uber.org/zap"
)
var AnchorFunc = func(anchorConfig interface{}) {
logger := zap.L()
paramConfig, ok := anchorConfig.(config.AnchorParamConfig)
if !ok {
logger.Error("conversion model anchor param config type failed")
return
}
calculateFunc := paramConfig.CalculateFunc
for {
// TODO 通过 api 循环获取 vlaue 实时值
var value float32
anchorName, err := diagram.GetAnchorValue(paramConfig.UUID)
if err != nil {
logger.Error("can not get anchor value from map by uuid", zap.String("uuid", paramConfig.UUID), zap.Error(err))
continue
}
2024-12-18 16:25:49 +08:00
if anchorName != paramConfig.AnchorName {
logger.Error("anchor name not equal param config anchor value", zap.String("map_anchor_name", anchorName), zap.String("param_anchor_name", paramConfig.AnchorName))
continue
}
upperLimitVal := paramConfig.CompareValUpperLimit
lowerlimitVal := paramConfig.CompareValLowerLimit
compareValue := calculateFunc(value, paramConfig.CalculateParams...)
if compareValue > upperLimitVal || compareValue < lowerlimitVal {
fmt.Println("log warning")
}
}
2024-12-18 16:25:49 +08:00
}
// type APIEndpoint struct {
// URL string `json:"url"`
// Method string `json:"method"` // HTTP 方法,如 "GET", "POST"
// Headers map[string]string `json:"headers"`
// QueryParams map[string]string `json:"query_params"`
// Body string `json:"body"` // 对于 POST 请求等,可能需要一个请求体
// Interval int `json:"interval"` // 轮询间隔时间(秒)
// }
2024-12-18 16:25:49 +08:00
// // fetchAPI 执行 HTTP 请求并返回响应体(作为字符串)或错误
// func fetchAPI(endpoint APIEndpoint) (string, error) {
// client := &http.Client{}
2024-12-18 16:25:49 +08:00
// // 构建请求
// req, err := http.NewRequest(endpoint.Method, endpoint.URL, nil)
// if err != nil {
// return "", err
// }
2024-12-18 16:25:49 +08:00
// // 设置请求头
// for key, value := range endpoint.Headers {
// req.Header.Set(key, value)
// }
2024-12-18 16:25:49 +08:00
// // 设置查询参数(如果需要)
// q := req.URL.Query()
// for key, value := range endpoint.QueryParams {
// q.Set(key, value)
// }
// req.URL.RawQuery = q.Encode()
2024-12-18 16:25:49 +08:00
// // 设置请求体(如果需要,例如 POST 请求)
// if endpoint.Method == "POST" || endpoint.Method == "PUT" {
// req.Body = ioutil.NopCloser(strings.NewReader(endpoint.Body))
// req.Header.Set("Content-Type", "application/json") // 假设是 JSON 请求体
// }
2024-12-18 16:25:49 +08:00
// // 执行请求
// resp, err := client.Do(req)
// if err != nil {
// return "", err
// }
// defer resp.Body.Close()
2024-12-18 16:25:49 +08:00
// // 读取响应体
// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// return "", err
// }
2024-12-18 16:25:49 +08:00
// return string(body), nil
// }
// // pollAPIEndpoints 轮询 API 端点列表,并根据指定的间隔时间执行请求
// func pollAPIEndpoints(endpoints []APIEndpoint, interval int, wg *sync.WaitGroup, results chan<- string) {
// defer wg.Done()
// for _, endpoint := range endpoints {
// for {
// body, err := fetchAPI(endpoint)
// if err != nil {
// log.Printf("Error fetching from %s: %v", endpoint.URL, err)
// } else {
// results <- fmt.Sprintf("Response from %s: %s", endpoint.URL, body)
// }
// time.Sleep(time.Duration(interval) * time.Second)
// // 注意:这里使用了 endpoint.Interval 而不是传入的 interval
// // 但为了示例简单,我们统一使用传入的 interval。
// // 如果要根据每个端点的不同间隔来轮询,应该使用 endpoint.Interval。
// }
// }
// }