add i/u ratio
This commit is contained in:
parent
116b33b6fe
commit
ebd375d6c7
|
|
@ -1,10 +1,10 @@
|
||||||
{
|
{
|
||||||
"default":{
|
"default":{
|
||||||
"host":"127.0.0.1",
|
"host":"192.168.46.100",
|
||||||
"port":5432,
|
"port":9432,
|
||||||
"user":"postgres",
|
"user":"postgres",
|
||||||
"password":"password",
|
"password":"123RTYjkl",
|
||||||
"dbname":"postgres",
|
"dbname":"metamodule",
|
||||||
"timezone":"Asia/Shanghai"
|
"timezone":"Asia/Shanghai"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ package data
|
||||||
import (
|
import (
|
||||||
"datart/data/influx"
|
"datart/data/influx"
|
||||||
"datart/data/postgres"
|
"datart/data/postgres"
|
||||||
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
|
|
@ -45,6 +46,21 @@ func GenPhasorFields(channel string) []string {
|
||||||
return fields
|
return fields
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToPrimary(field string, value, iRatio, uRatio float64, iPolar int) float64 {
|
||||||
|
switch {
|
||||||
|
case field == "p", field == "q":
|
||||||
|
return value * iRatio * uRatio * float64(iPolar)
|
||||||
|
case strings.HasSuffix(field, "_pa"):
|
||||||
|
if iPolar < 0 {
|
||||||
|
return value + math.Pi
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return value * iRatio * float64(iPolar)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
type zUnit struct {
|
type zUnit struct {
|
||||||
Key string
|
Key string
|
||||||
Members []redis.Z
|
Members []redis.Z
|
||||||
|
|
|
||||||
|
|
@ -77,9 +77,6 @@ type fields struct {
|
||||||
Values [][]any `json:"values"`
|
Values [][]any `json:"values"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// respType json/csv
|
|
||||||
// json_time:"2024-12-18T08:12:21.4735154Z"
|
|
||||||
// csv_time:"1734572793695885000"
|
|
||||||
func (client *influxClient) getTVsResp(ctx context.Context, reqData url.Values,
|
func (client *influxClient) getTVsResp(ctx context.Context, reqData url.Values,
|
||||||
respType string) ([]TV, error) {
|
respType string) ([]TV, error) {
|
||||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet,
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet,
|
||||||
|
|
@ -112,9 +109,6 @@ func (client *influxClient) getTVsResp(ctx context.Context, reqData url.Values,
|
||||||
return respDataToTVs(respData, respType)
|
return respDataToTVs(respData, respType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// respType json/csv
|
|
||||||
// json_time:"2024-12-18T08:12:21.4735154Z"
|
|
||||||
// csv_time:"1734572793695885000"
|
|
||||||
func (client *influxClient) getF2TVsResp(ctx context.Context, reqData url.Values,
|
func (client *influxClient) getF2TVsResp(ctx context.Context, reqData url.Values,
|
||||||
respType string) (map[string][]TV, error) {
|
respType string) (map[string][]TV, error) {
|
||||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet,
|
request, err := http.NewRequestWithContext(ctx, http.MethodGet,
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,7 @@ func WriteLinesData(ctx context.Context, db string, data []byte) error {
|
||||||
type Request struct {
|
type Request struct {
|
||||||
DB string
|
DB string
|
||||||
Table string
|
Table string
|
||||||
|
Tags []string
|
||||||
|
|
||||||
Type string
|
Type string
|
||||||
Station string
|
Station string
|
||||||
|
|
|
||||||
|
|
@ -100,17 +100,7 @@ func (client *influxClient) GetSSUPointsLastLimit(ctx context.Context, req *Requ
|
||||||
"q": {sql},
|
"q": {sql},
|
||||||
}
|
}
|
||||||
|
|
||||||
f2tvs, err := client.getF2TVsResp(ctx, reqData, "csv")
|
return client.getF2TVsResp(ctx, reqData, "csv")
|
||||||
if err != nil {
|
|
||||||
return f2tvs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := make(map[string][]TV, len(f2tvs))
|
|
||||||
for f, tvs := range f2tvs {
|
|
||||||
ret[strings.Join([]string{req.Station, req.MainPos, f}, ".")] = tvs
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *influxClient) GetSSUPointDurationData(ctx context.Context, req *Request) ([]TV, error) {
|
func (client *influxClient) GetSSUPointDurationData(ctx context.Context, req *Request) ([]TV, error) {
|
||||||
|
|
@ -149,17 +139,7 @@ func (client *influxClient) GetSSUPointsDurationData(ctx context.Context, req *R
|
||||||
"q": {sql},
|
"q": {sql},
|
||||||
}
|
}
|
||||||
|
|
||||||
f2tvs, err := client.getF2TVsResp(ctx, reqData, "csv")
|
return client.getF2TVsResp(ctx, reqData, "csv")
|
||||||
if err != nil {
|
|
||||||
return f2tvs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := make(map[string][]TV, len(f2tvs))
|
|
||||||
for f, tvs := range f2tvs {
|
|
||||||
ret[strings.Join([]string{req.Station, req.MainPos, f}, ".")] = tvs
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *influxClient) GetSSUPointAfterLimit(ctx context.Context, req *Request, limit int) ([]TV, error) {
|
func (client *influxClient) GetSSUPointAfterLimit(ctx context.Context, req *Request, limit int) ([]TV, error) {
|
||||||
|
|
@ -183,17 +163,7 @@ func (client *influxClient) GetSSUPointsAfterLimit(ctx context.Context, req *Req
|
||||||
"q": {sql},
|
"q": {sql},
|
||||||
}
|
}
|
||||||
|
|
||||||
f2tvs, err := client.getF2TVsResp(ctx, reqData, "csv")
|
return client.getF2TVsResp(ctx, reqData, "csv")
|
||||||
if err != nil {
|
|
||||||
return f2tvs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := make(map[string][]TV, len(f2tvs))
|
|
||||||
for f, tvs := range f2tvs {
|
|
||||||
ret[strings.Join([]string{req.Station, req.MainPos, f}, ".")] = tvs
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *influxClient) GetSSUPointsBeforeLimit(ctx context.Context, req *Request, limit int) (map[string][]TV, error) {
|
func (client *influxClient) GetSSUPointsBeforeLimit(ctx context.Context, req *Request, limit int) (map[string][]TV, error) {
|
||||||
|
|
@ -203,17 +173,7 @@ func (client *influxClient) GetSSUPointsBeforeLimit(ctx context.Context, req *Re
|
||||||
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, req.End, limit)},
|
req.SubPos, req.Table, req.Station, req.MainPos, req.Begin, req.End, limit)},
|
||||||
}
|
}
|
||||||
|
|
||||||
f2tvs, err := client.getF2TVsResp(ctx, reqData, "csv")
|
return client.getF2TVsResp(ctx, reqData, "csv")
|
||||||
if err != nil {
|
|
||||||
return f2tvs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := make(map[string][]TV, len(f2tvs))
|
|
||||||
for f, tvs := range f2tvs {
|
|
||||||
ret[strings.Join([]string{req.Station, req.MainPos, f}, ".")] = tvs
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *influxClient) WriteLinesData(ctx context.Context, db string, data []byte) error {
|
func (client *influxClient) WriteLinesData(ctx context.Context, db string, data []byte) error {
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ func (a *Alarm) ConvertToEvent(ip string) (*Event, error) {
|
||||||
OP: ip,
|
OP: ip,
|
||||||
TS: a.AlarmTime,
|
TS: a.AlarmTime,
|
||||||
})
|
})
|
||||||
e.Alarm = a
|
e.Origin = a
|
||||||
}
|
}
|
||||||
|
|
||||||
return e, nil
|
return e, nil
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,7 @@ type Event struct {
|
||||||
From string `bson:"from" json:"from"`
|
From string `bson:"from" json:"from"`
|
||||||
Operations []*operation `bson:"operations" json:"operations"`
|
Operations []*operation `bson:"operations" json:"operations"`
|
||||||
// others
|
// others
|
||||||
Alarm *Alarm `bson:"alarm" json:"alarm"`
|
Origin *Alarm `bson:"origin" json:"origin"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) Marshall() ([]byte, error) {
|
func (e *Event) Marshall() ([]byte, error) {
|
||||||
|
|
|
||||||
|
|
@ -45,19 +45,51 @@ func (ds *dataSource) Value() (driver.Value, error) {
|
||||||
return json.Marshal(ds)
|
return json.Marshal(ds)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type bind struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
Ratio float64 `json:"ratio"`
|
||||||
|
Polarity int `json:"polarity"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type binding struct {
|
||||||
|
CT bind `json:"ct"`
|
||||||
|
PT bind `json:"pt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *binding) Scan(value any) error {
|
||||||
|
if value == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
bytes, ok := value.([]byte)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("type assertion to []byte failed")
|
||||||
|
}
|
||||||
|
return json.Unmarshal(bytes, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *binding) Value() (driver.Value, error) {
|
||||||
|
return json.Marshal(b)
|
||||||
|
}
|
||||||
|
|
||||||
type measurement struct {
|
type measurement struct {
|
||||||
ID int64 `gorm:"colunmn:id"`
|
ID int64 `gorm:"colunmn:id"`
|
||||||
Tag string `gorm:"column:tag"`
|
Tag string `gorm:"column:tag"`
|
||||||
Size int `gorm:"column:size"`
|
Size int `gorm:"column:size"`
|
||||||
DataSource *dataSource `gorm:"column:data_source;type:jsonb"`
|
DataSource *dataSource `gorm:"column:data_source;type:jsonb"`
|
||||||
|
Binding *binding `gorm:"column:binding;type:josnb"`
|
||||||
// others
|
// others
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChannelSize struct {
|
type ChannelSize struct {
|
||||||
|
Type int
|
||||||
Station string
|
Station string
|
||||||
Device string
|
Device string
|
||||||
Channel string
|
Channel string
|
||||||
Size int
|
Size int
|
||||||
|
IRatio float64
|
||||||
|
IPolar int
|
||||||
|
URatio float64
|
||||||
|
UPolar int
|
||||||
}
|
}
|
||||||
|
|
||||||
// channel is original
|
// channel is original
|
||||||
|
|
@ -200,10 +232,26 @@ func genMappingFromAddr(ssu2ChannelSizes map[string][]ChannelSize,
|
||||||
}
|
}
|
||||||
|
|
||||||
channelSize := ChannelSize{
|
channelSize := ChannelSize{
|
||||||
|
Type: 1,
|
||||||
Station: station,
|
Station: station,
|
||||||
Device: device,
|
Device: device,
|
||||||
Channel: channel,
|
Channel: channel,
|
||||||
Size: record.Size,
|
Size: record.Size,
|
||||||
|
IRatio: 1,
|
||||||
|
IPolar: 1,
|
||||||
|
URatio: 1,
|
||||||
|
UPolar: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
if record.Binding != nil {
|
||||||
|
if record.Binding.CT.Index > 0 {
|
||||||
|
channelSize.IRatio = record.Binding.CT.Ratio
|
||||||
|
channelSize.IPolar = record.Binding.CT.Polarity
|
||||||
|
}
|
||||||
|
if record.Binding.PT.Index > 0 {
|
||||||
|
channelSize.URatio = record.Binding.PT.Ratio
|
||||||
|
channelSize.UPolar = record.Binding.PT.Polarity
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ChannelSizes.Store(record.Tag, channelSize)
|
ChannelSizes.Store(record.Tag, channelSize)
|
||||||
|
|
|
||||||
|
|
@ -78,13 +78,13 @@ func sendSSUZUnit(ctx context.Context, channelSize postgres.ChannelSize, ssuChan
|
||||||
// }
|
// }
|
||||||
|
|
||||||
for f, tvs := range f2tvs {
|
for f, tvs := range f2tvs {
|
||||||
sdf := strings.Split(f, ".")
|
key := genRedisPhasorKey(channelSize.Station, channelSize.Device, f)
|
||||||
if len(sdf) != 3 {
|
|
||||||
log.Error("invalid influx field")
|
for i := range tvs {
|
||||||
return
|
tvs[i].Value = ToPrimary(f, tvs[i].Value,
|
||||||
|
channelSize.IRatio, channelSize.URatio, channelSize.IPolar)
|
||||||
}
|
}
|
||||||
|
|
||||||
key := genRedisPhasorKey(sdf[0], sdf[1], sdf[2])
|
|
||||||
members := convertTVsToMenmbers(tvs)
|
members := convertTVsToMenmbers(tvs)
|
||||||
ssuChan <- zUnit{
|
ssuChan <- zUnit{
|
||||||
Key: key,
|
Key: key,
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type command struct {
|
type command struct {
|
||||||
Command string `json:"command"`
|
Command int `json:"command"`
|
||||||
Timeout int64 `json:"timeout"`
|
Timeout int64 `json:"timeout"`
|
||||||
Args []any `json:"args"`
|
Args []any `json:"args"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Admin) PostExecuteCommand(ctx *gin.Context) {
|
func (a *Admin) PostExecuteCommand(ctx *gin.Context) {
|
||||||
|
|
@ -49,7 +49,7 @@ func (a *Admin) checkAndGenExecuteCommandRequest(ctx *gin.Context) (*command, er
|
||||||
return req, errors.New("invalid body param")
|
return req, errors.New("invalid body param")
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.Command != "GenSSU2ChannelSizes" {
|
if req.Command != 1 {
|
||||||
return nil, errors.New("invalid command")
|
return nil, errors.New("invalid command")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,19 +26,19 @@ func (a *Api) GetPoints(ctx *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var data map[string][]influx.TV
|
var ftvs map[string][]influx.TV
|
||||||
switch {
|
switch {
|
||||||
case request.Begin > 0 && request.End > 0:
|
case request.Begin > 0 && request.End > 0:
|
||||||
data, err = influx.GetSSUPointsDurationData(ctx.Request.Context(), request)
|
ftvs, err = influx.GetSSUPointsDurationData(ctx.Request.Context(), request)
|
||||||
|
|
||||||
case request.Begin > 0 && request.End < 0:
|
case request.Begin > 0 && request.End < 0:
|
||||||
data, err = influx.GetSSUPointsAfterLimit(ctx.Request.Context(), request, 1)
|
ftvs, err = influx.GetSSUPointsAfterLimit(ctx.Request.Context(), request, 1)
|
||||||
|
|
||||||
case request.Begin < 0 && request.End > 0:
|
case request.Begin < 0 && request.End > 0:
|
||||||
data, err = influx.GetSSUPointsBeforeLimit(ctx.Request.Context(), request, 1)
|
ftvs, err = influx.GetSSUPointsBeforeLimit(ctx.Request.Context(), request, 1)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
data, err = influx.GetSSUPointsLastLimit(ctx.Request.Context(), request, 1)
|
ftvs, err = influx.GetSSUPointsLastLimit(ctx.Request.Context(), request, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -52,10 +52,21 @@ func (a *Api) GetPoints(ctx *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle key value
|
||||||
|
v, _ := postgres.ChannelSizes.Load(request.Tags[len(request.Tags)-1])
|
||||||
|
if channelSize, ok := v.(postgres.ChannelSize); ok {
|
||||||
|
for f, tvs := range ftvs {
|
||||||
|
for i := range tvs {
|
||||||
|
tvs[i].Value = data.ToPrimary(f, tvs[i].Value,
|
||||||
|
channelSize.IRatio, channelSize.URatio, channelSize.IPolar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ctx.JSON(200, gin.H{
|
ctx.JSON(200, gin.H{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"msg": "success",
|
"msg": "success",
|
||||||
"data": data,
|
"data": ftvs,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,8 +79,8 @@ func (a *Api) checkAndGenGetPointsRequest(ctx *gin.Context) (*influx.Request, er
|
||||||
|
|
||||||
station, mainPos, subPos := "", "", ""
|
station, mainPos, subPos := "", "", ""
|
||||||
|
|
||||||
mtag := ctx.DefaultQuery("mtag", "")
|
tags := strings.Split(ctx.DefaultQuery("mtag", ""), ".")
|
||||||
v, ok := postgres.ChannelSizes.Load(mtag)
|
v, ok := postgres.ChannelSizes.Load(tags[len(tags)-1])
|
||||||
if ok {
|
if ok {
|
||||||
if channelSize, ok := v.(postgres.ChannelSize); ok {
|
if channelSize, ok := v.(postgres.ChannelSize); ok {
|
||||||
fields := data.GenPhasorFields(channelSize.Channel)
|
fields := data.GenPhasorFields(channelSize.Channel)
|
||||||
|
|
@ -79,20 +90,7 @@ func (a *Api) checkAndGenGetPointsRequest(ctx *gin.Context) (*influx.Request, er
|
||||||
subPos = strings.Join(fields, ",")
|
subPos = strings.Join(fields, ",")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
station = ctx.DefaultQuery("station", "")
|
return nil, errors.New("invalid mtag")
|
||||||
if len(station) <= 0 {
|
|
||||||
return nil, errors.New("invalid station")
|
|
||||||
}
|
|
||||||
|
|
||||||
mainPos = ctx.DefaultQuery("main_pos", "")
|
|
||||||
if len(mainPos) <= 0 {
|
|
||||||
return nil, errors.New("invalid main_pos")
|
|
||||||
}
|
|
||||||
|
|
||||||
subPos = ctx.DefaultQuery("sub_pos", "")
|
|
||||||
if len(subPos) <= 0 {
|
|
||||||
return nil, errors.New("invalid sub_pos")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
beginStr := ctx.DefaultQuery("begin", "")
|
beginStr := ctx.DefaultQuery("begin", "")
|
||||||
|
|
@ -120,8 +118,10 @@ func (a *Api) checkAndGenGetPointsRequest(ctx *gin.Context) (*influx.Request, er
|
||||||
}
|
}
|
||||||
|
|
||||||
return &influx.Request{
|
return &influx.Request{
|
||||||
DB: bucket,
|
DB: bucket,
|
||||||
Table: measure,
|
Table: measure,
|
||||||
|
Tags: tags,
|
||||||
|
|
||||||
Type: typeStr,
|
Type: typeStr,
|
||||||
Station: station,
|
Station: station,
|
||||||
MainPos: mainPos,
|
MainPos: mainPos,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue