重建 common_utils
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
7.5 KiB

package common_utils
import (
"fmt"
"gitea.anxinyun.cn/container/common_calc"
"gitea.anxinyun.cn/container/common_models"
"github.com/stretchr/testify/assert"
"log"
"testing"
"time"
)
func Test_maxMin(t *testing.T) {
d := []float64{2.1, 3.1, 1.1, 5.1, 8.1, 7.1, 6.1}
_min, _max := common_calc.MinMax(d)
_basMax := common_calc.AbsMax(d)
println(_min, _max, _basMax)
}
func Test_uuid(t *testing.T) {
//createNativeRaw := common_models.EsRaw{
// StructId: 1,
// IotaDeviceName: "设备2",
// ThemeData: map[string]any{"temperature": 2.1, "humidity": 2.222},
// CollectTime: time.Now(),
// Meta: map[string]string{"temperature": "温度", "humidity": "湿度"},
// IotaDevice: "id_2",
// CreateTime: time.Now(),
//}
//rawStr := "94509763-bd66-4c53-9f0c-1bf9402ada9e-1710131814289"
//uid := uuidFromString(rawStr)
//println(uid)
//89d375ed-31ec-4759-a573-250f69fd204f-1666141882011 => uuid -> 81d8bcb0-c22a-366f-af0b-dcc1d7f5f8e4
//94509763-bd66-4c53-9f0c-1bf9402ada9e-1666141882011 -> uuid -> ce6ce4c1-8443-3f21-9fa1-6beb625e1504
rawStr := "94509763-bd66-4c53-9f0c-1bf9402ada9e-1666141882011"
println(common_calc.UUIDFromString(rawStr))
println("==============")
//uu, er := uuid.FromString("81d8bcb0-c22a-366f-af0b-dcc1d7f5f8e4")
//println(uu.String(), er)
//byteName := []byte("81d8bcb0-c22a-366f-af0b-dcc1d7f5f8e4")
//uu1, er1 := uuid.FromBytes(byteName)
//println(uu1.String(), er1)
}
func Test_uuid2(t *testing.T) {
//9c94a305-dd18-4809-8fc6-4191c699e726-1711587207244 -> uuid -> a167ece7-ecd6-3dd1-9d44-691f42f27798
rawStr := "9c94a305-dd18-4809-8fc6-4191c699e726-1711587207244"
println(common_calc.NameUUIDFromString(rawStr))
println("==============")
}
func TestRedisGet(t *testing.T) {
r := NewRedisHelper("", "10.8.30.160:30379")
key := "iota_meta:003540d0-616c-4611-92c1-1cd31005eabf"
v := r.Get(key)
println(v)
}
func TestRedisHMGet(t *testing.T) {
r := NewRedisHelper("", "10.8.30.160:30379")
key := "cache_filter:198"
field := "temperature"
//cc := common_models.FilterTp{}
var cc []common_models.AnalyzeData
err := r.HMGetObj(&cc, key, field)
if err != nil {
println(err.Error())
}
println("=====")
}
func TestConfigHelper_GetIotaDevice(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
key := "1f2ebeac-c8b2-4abd-8706-470e5dfc8542"
v, r := cf.GetIotaDevice(key)
if r != nil {
fmt.Printf("%+v\n", r)
} else {
fmt.Printf("%+v\n", v)
}
}
func TestConfigHelper_GetStructure(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
key := "5da9aa1b-05b7-4943-be57-dedb34f7a1bd"
v, r := cf.GetThingStruct(key)
if r != nil {
fmt.Printf("%+v\n", r)
} else {
fmt.Printf("%+v\n", v)
}
}
// GetStationThreshold
func TestConfigHelper_GetStationThreshold(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
threshold, err := cf.GetStationThreshold(198)
if err == nil {
fmt.Printf("%+v\n", threshold)
} else {
fmt.Printf("%+v\n", err)
}
}
// GetStationGroup
func TestConfigHelper_GetStationGroup(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
model, err := cf.GetStationGroup(36)
if err == nil {
fmt.Printf("%+v\n", model)
} else {
fmt.Printf("%+v\n", err)
}
}
func TestConfigHelper_GetStationGroupInfo(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
model, err := cf.GetStationGroupInfo(193)
if err == nil {
fmt.Printf("%+v\n", model)
} else {
fmt.Printf("%+v\n", err)
}
}
//func TestConfigHelper_GetStationCorrGroup(t *testing.T) {
// var cf = NewConfigHelper("10.8.30.160:30379")
// model, err := cf.GetStationCorrGroup(193)
// if err == nil {
// fmt.Printf("%+v\n", model)
// } else {
// fmt.Printf("%+v\n", err)
// }
//}
func TestConfigHelper_GetIotaScheme(t *testing.T) {
// scheme:01dc6242-84ab-4690-b640-8c21cdffcf39
var cf = NewConfigHelper("10.8.30.160:30379")
model, err := cf.GetIotaScheme("01dc6242-84ab-4690-b640-8c21cdffcf39")
if err == nil {
fmt.Printf("%+v\n", model)
} else {
fmt.Printf("%+v\n", err)
}
}
func TestConfigHelper_GetStationFilter(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
Filter, err := cf.GetFilter(198)
if err == nil {
fmt.Printf("%+v\n", Filter)
} else {
fmt.Printf("%+v\n", err)
}
}
func TestConfigHelper_GetDeviceInfo(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
key := "1f2ebeac-c8b2-4abd-8706-470e5dfc8542"
device, err := cf.GetIotaDevice(key)
if err == nil {
return
}
thingStruct, err := cf.GetThingStruct(device.ThingId)
if err != nil {
return
}
iotaMeta, err := cf.GetIotaMeta(key)
if err != nil {
return
}
s := common_models.Structure{
ThingId: thingStruct.ThingId,
Id: thingStruct.Id,
Name: thingStruct.Name,
SType: thingStruct.Type,
OrgId: thingStruct.OrgId,
Latitude: 0,
Longitude: 0,
}
deviceInfo := common_models.DeviceInfo{
Id: device.Id,
Name: device.Name,
Structure: s,
DeviceMeta: iotaMeta,
}
log.Printf("%v", deviceInfo)
//return deviceInfo
}
func TestConfigHelper_GetSubDeviceNext(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
//dtuId
dtuId := "55f1a93e-f5f5-4d62-baa9-3014d7cfcfd8"
thingId := "5da9aa1b-05b7-4943-be57-dedb34f7a1bd"
dtuSubs := cf.GetSubDeviceNext(dtuId, thingId)
println(dtuSubs)
iotaId := "c919fa99-1b95-4bc1-a427-2f26dfacc2da"
subs := cf.GetSubDeviceNext(iotaId, thingId)
println(subs)
}
func TestConfigHelper_GetSubDeviceAll(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
//dtuId
dtuId := "55f1a93e-f5f5-4d62-baa9-3014d7cfcfd8"
thingId := "5da9aa1b-05b7-4943-be57-dedb34f7a1bd"
dtuSubs := cf.GetSubDeviceAll(dtuId, thingId)
println(dtuSubs)
iotaId := "c919fa99-1b95-4bc1-a427-2f26dfacc2da"
subs := cf.GetSubDeviceAll(iotaId, thingId)
println(subs)
}
func TestConfigHelper_GetDeviceFactorProto(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
//device-proto:4004:7efbc3e8-ff6b-433b-9041-c76b9df89644
factorProtoId := "4004"
deviceMetaId := "7efbc3e8-ff6b-433b-9041-c76b9df89644"
dp, err := cf.GetDeviceFactorProto(factorProtoId, deviceMetaId)
log.Println(dp, err)
//GetDeviceFactorProto
}
func TestConfigHelper_GetTransform_units(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
dp, err := cf.GetDataUnit()
units := map[string][]common_models.DataUnit{}
for _, unit := range dp {
groupK := unit.Dimension
if _, ok := units[groupK]; ok {
units[groupK] = append(units[groupK], unit)
} else {
units[groupK] = []common_models.DataUnit{unit}
}
}
log.Println(dp, err)
//GetDeviceFactorProto
}
func TestConfigHelper_GetFactorInfo(t *testing.T) {
cf := NewConfigHelper("10.8.30.160:30379")
// 测试存在的 factor
factor, err := cf.GetFactorInfo(11)
log.Println(factor, err)
assert.Equal(t, 11, factor.Id)
// 测试获取不存在的 factor
factor_nil, err := cf.GetFactorInfo(1111)
assert.Nil(t, factor_nil.Items)
// 测试获取存在的 station
station, err := cf.getStation(1)
log.Println(station, err)
assert.NotEqual(t, 0, station.Info.Id)
// 测试获取多个测点
stations, err := cf.GetStations(106, 1111)
log.Println(stations, err)
assert.Equal(t, 2, len(stations))
// 测试获取不存在的变化速率阈值
aggThreshold_notExist, err := cf.GetAggThreshold(1, 12)
assert.Nil(t, aggThreshold_notExist.Items)
}
func TestConfigHelper_SetMap(t *testing.T) {
redisAddr := "10.8.30.160:30379"
cf := NewConfigHelper(redisAddr)
key := "cache_filter:500"
m := map[string]float64{"v1": 20, "v2": 30}
err := cf.SetChainedCacheObj(key, m)
log.Println(err)
time.Sleep(time.Second * 2)
println("=========")
}