134 lines
2.7 KiB
Go
134 lines
2.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
)
|
|
|
|
const (
|
|
tbmeasurement string = "public.measurement"
|
|
)
|
|
|
|
const (
|
|
ChannelCPrefix string = "Telemetry"
|
|
ChannelIPrefix string = "Telesignal"
|
|
ChannelP string = "P"
|
|
ChannelQ string = "Q"
|
|
ChannelS string = "S"
|
|
ChannelPF string = "PF"
|
|
ChannelF string = "F"
|
|
ChannelDF string = "deltaF"
|
|
ChannelUPrefix string = "U"
|
|
)
|
|
|
|
type addrSSU struct {
|
|
Station string `json:"station"`
|
|
Device string `json:"device"`
|
|
Channel string `json:"channel"`
|
|
}
|
|
|
|
type dataSource struct {
|
|
Type int `json:"type"`
|
|
Addr any `json:"io_address"`
|
|
}
|
|
|
|
type measurement struct {
|
|
ID int64 `gorm:"colunmn:id"`
|
|
Tag string `gorm:"column:tag"`
|
|
Size int `gorm:"column:size"`
|
|
DataSource *dataSource `gorm:"type:jsonb;column:data_source"`
|
|
// mapping TODO
|
|
}
|
|
|
|
type channelSize struct {
|
|
addrSSU
|
|
Size int
|
|
}
|
|
|
|
// channel is original
|
|
var SSU2ChannelSizes map[string][]channelSize
|
|
|
|
func GetMeasurements(ctx context.Context, batchSize int) ([]*measurement, error) {
|
|
var totalRecords []*measurement
|
|
|
|
id := int64(0)
|
|
for {
|
|
var records []*measurement
|
|
result := client.WithContext(ctx).Table(tbmeasurement).Where("id > ?", id).
|
|
Order("id ASC").Limit(batchSize).Find(&records)
|
|
if result.Error != nil {
|
|
return totalRecords, result.Error
|
|
}
|
|
|
|
length := len(records)
|
|
if length <= 0 {
|
|
break
|
|
}
|
|
id = records[length-1].ID
|
|
|
|
totalRecords = append(totalRecords, records...)
|
|
}
|
|
|
|
return totalRecords, nil
|
|
}
|
|
|
|
func GenSSU2ChannelSize(ctx context.Context, batchSize int) error {
|
|
id := int64(0)
|
|
for {
|
|
var records []*measurement
|
|
result := client.WithContext(ctx).Table(tbmeasurement).
|
|
Where("id > ?", id).Order("id ASC").Limit(batchSize).Find(&records)
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
length := len(records)
|
|
if length <= 0 {
|
|
break
|
|
}
|
|
|
|
for _, record := range records {
|
|
if record == nil || record.DataSource == nil {
|
|
continue
|
|
}
|
|
|
|
addrType := record.DataSource.Type
|
|
addr := record.DataSource.Addr
|
|
if err := genMappingFromAddr(addrType, addr, record.Size); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
id = records[length-1].ID
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func genMappingFromAddr(addrType int, addr any, size int) error {
|
|
switch addrType {
|
|
case 1:
|
|
if ins, ok := addr.(*addrSSU); ok {
|
|
channelSize := genChannelSize(ins, size)
|
|
SSU2ChannelSizes[ins.Device] = append(SSU2ChannelSizes[ins.Device], channelSize)
|
|
} else {
|
|
return errors.New("io_address not valid")
|
|
}
|
|
|
|
default:
|
|
return errors.New("data_source.type not valid")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func genChannelSize(addr *addrSSU, size int) channelSize {
|
|
return channelSize{
|
|
addrSSU{
|
|
Station: addr.Station,
|
|
Device: addr.Device,
|
|
Channel: addr.Channel,
|
|
},
|
|
size,
|
|
}
|
|
}
|