109 lines
1.9 KiB
Go
109 lines
1.9 KiB
Go
package influx
|
|
|
|
import (
|
|
"context"
|
|
"datart/config"
|
|
"errors"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type influxClient struct {
|
|
*http.Client
|
|
url string
|
|
token string
|
|
org string
|
|
}
|
|
|
|
var client *influxClient
|
|
|
|
func init() {
|
|
client = new(influxClient)
|
|
|
|
influxConfig := config.Conf().InfluxConf("default")
|
|
client.Client = &http.Client{
|
|
Timeout: time.Duration(influxConfig.GetTimeout()) * time.Millisecond,
|
|
Transport: &http.Transport{
|
|
DialContext: (&net.Dialer{
|
|
Timeout: time.Second,
|
|
}).DialContext,
|
|
MaxIdleConns: 100,
|
|
MaxIdleConnsPerHost: 100,
|
|
IdleConnTimeout: 90 * time.Second,
|
|
},
|
|
}
|
|
|
|
client.url = influxConfig.GetURL()
|
|
client.token = influxConfig.GetToken()
|
|
client.org = influxConfig.GetOrg()
|
|
}
|
|
|
|
func CloseDefault() {
|
|
client.CloseIdleConnections()
|
|
}
|
|
|
|
func NewInfluxClient(cli *http.Client, url, org, token string) *influxClient {
|
|
return &influxClient{
|
|
Client: cli,
|
|
url: url,
|
|
org: org,
|
|
token: token,
|
|
}
|
|
}
|
|
|
|
func GetDB(tp string) (string, error) {
|
|
switch tp {
|
|
case "phasor":
|
|
return dbphasor, nil
|
|
case "sample":
|
|
return dbsample, nil
|
|
}
|
|
|
|
return "", errors.New("invalid type")
|
|
}
|
|
|
|
// serverConf
|
|
func GetTable(tp string, mainPos string) (string, error) {
|
|
switch tp {
|
|
case "phasor":
|
|
ssu2Type := config.Conf().ServerConf().GetSSUType()
|
|
switch ssu2Type[mainPos] {
|
|
case 1:
|
|
return "current", nil
|
|
case 2:
|
|
return "voltage", nil
|
|
default:
|
|
return "", errors.New("invalid main_pos")
|
|
}
|
|
case "sample":
|
|
return "sample", nil
|
|
}
|
|
|
|
return "", errors.New("invalid type")
|
|
}
|
|
|
|
func WriteLinesData(ctx context.Context, db string, data []byte) error {
|
|
return client.WriteLinesData(ctx, db, data)
|
|
}
|
|
|
|
type Request struct {
|
|
DB string
|
|
Table string
|
|
|
|
Type string
|
|
Station string
|
|
MainPos string
|
|
SubPos string // separate whith ','
|
|
Begin int64
|
|
End int64
|
|
Operate string
|
|
Step string
|
|
Default string
|
|
}
|
|
|
|
type TV struct {
|
|
Time int64 `json:"time"`
|
|
Value float64 `json:"value"`
|
|
}
|