Files
front_linux/LFtid1056/pqdif_semantic_ids.h

348 lines
12 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <cstdint>
#include <cstddef>
#include <string>
#include <unordered_map>
#include <vector>
#include <cstring>
#include "pqdif/include/pqdif_ph.h"
#include "pqdif/include/pqdif_id.h"
namespace pqdif_sem
{
// ============================================================================
// 说明
// ----------------------------------------------------------------------------
// 这份文件只做“语义解释”,不参与底层解析。
// 解析器里保留原始字段值UINT4 / GUID。
// 映射层通过这里的查表函数,把原始值解释成“标准语义名 + 中文说明”。
// ============================================================================
// ------------------------------
// GUID 基础工具
// ------------------------------
inline bool GuidEqual(const GUID& a, const GUID& b)
{
return std::memcmp(&a, &b, sizeof(GUID)) == 0;
}
struct GuidHash
{
std::size_t operator()(const GUID& g) const noexcept
{
const unsigned char* p = reinterpret_cast<const unsigned char*>(&g);
std::size_t h = 1469598103934665603ull; // FNV-1a 起点
for (std::size_t i = 0; i < sizeof(GUID); ++i)
{
h ^= static_cast<std::size_t>(p[i]);
h *= 1099511628211ull;
}
return h;
}
};
std::string GuidToString(const GUID& g);
// ============================================================================
// 一、整数型 ID 枚举来源PDF 表 B.2 / B.3 / B.4,且在 pqdif_id.h 中有宏定义)
// ============================================================================
// ------------------------------
// 1) 相别tagPhaseID物理类型 UINT4
// 来源PDF 表 B.2常量来源pqdif/include/pqdif_id.h
// ------------------------------
enum class PhaseId : uint32_t
{
None = ID_PHASE_NONE,
AN = ID_PHASE_AN,
BN = ID_PHASE_BN,
CN = ID_PHASE_CN,
NG = ID_PHASE_NG,
AB = ID_PHASE_AB,
BC = ID_PHASE_BC,
CA = ID_PHASE_CA,
RES = ID_PHASE_RES,
NET = ID_PHASE_NET,
Total = ID_PHASE_TOTAL,
LnAve = ID_PHASE_LN_AVE,
LlAve = ID_PHASE_LL_AVE,
Worst = ID_PHASE_WORST,
Plus = ID_PHASE_PLUS,
Minus = ID_PHASE_MINUS,
General1 = ID_PHASE_GENERAL_1,
General2 = ID_PHASE_GENERAL_2,
General3 = ID_PHASE_GENERAL_3,
General4 = ID_PHASE_GENERAL_4,
General5 = ID_PHASE_GENERAL_5,
General6 = ID_PHASE_GENERAL_6,
General7 = ID_PHASE_GENERAL_7,
General8 = ID_PHASE_GENERAL_8,
General9 = ID_PHASE_GENERAL_9,
General10 = ID_PHASE_GENERAL_10,
General11 = ID_PHASE_GENERAL_11,
General12 = ID_PHASE_GENERAL_12,
General13 = ID_PHASE_GENERAL_13,
General14 = ID_PHASE_GENERAL_14,
General15 = ID_PHASE_GENERAL_15,
General16 = ID_PHASE_GENERAL_16
};
// ------------------------------
// 2) 被测量tagQuantityMeasuredID物理类型 UINT4
// 来源PDF 表 B.2常量来源pqdif/include/pqdif_id.h
// 注意:这里没有 Frequency频率在 characteristic 表里。
// ------------------------------
enum class QuantityMeasuredId : uint32_t
{
None = ID_QM_NONE,
Voltage = ID_QM_VOLTAGE,
Current = ID_QM_CURRENT,
Power = ID_QM_POWER,
Energy = ID_QM_ENERGY,
Temperature = ID_QM_TEMPERATURE,
Pressure = ID_QM_PRESSURE,
Charge = ID_QM_CHARGE,
EField = ID_QM_EFIELD,
MField = ID_QM_MFIELD,
Velocity = ID_QM_VELOCITY,
Bearing = ID_QM_BEARING,
Force = ID_QM_FORCE,
Torque = ID_QM_TORQUE,
Position = ID_QM_POSITION,
FluxLinkage = ID_QM_FLUXLINKAGE,
FluxDensity = ID_QM_FLUXDENSITY,
Status = ID_QM_STATUS
};
// ------------------------------
// 3) 单位tagQuantityUnitsID物理类型 UINT4
// 来源PDF 表 B.2常量来源pqdif/include/pqdif_id.h
// ------------------------------
enum class QuantityUnitsId : uint32_t
{
None = ID_QU_NONE,
Timestamp = ID_QU_TIMESTAMP,
Seconds = ID_QU_SECONDS,
Cycles = ID_QU_CYCLES,
Volts = ID_QU_VOLTS,
Amps = ID_QU_AMPS,
VA = ID_QU_VA,
Watts = ID_QU_WATTS,
Vars = ID_QU_VARS,
Ohms = ID_QU_OHMS,
Siemens = ID_QU_SIEMENS,
VoltsPerAmp = ID_QU_VOLTSPERAMP,
Joules = ID_QU_JOULES,
Hertz = ID_QU_HERTZ,
Celcius = ID_QU_CELCIUS,
Degrees = ID_QU_DEGREES,
Db = ID_QU_DB,
Percent = ID_QU_PERCENT,
PerUnit = ID_QU_PERUNIT,
Samples = ID_QU_SAMPLES,
VarHours = ID_QU_VARHOURS,
WattHours = ID_QU_WATTHOURS,
VaHours = ID_QU_VAHOURS,
Mps = ID_QU_MPS,
Mph = ID_QU_MPH,
Bars = ID_QU_BARS,
Pascals = ID_QU_PASCALS,
Newtons = ID_QU_NEWTONS,
NewtonMeters = ID_QU_NEWTONMETERS,
Rpm = ID_QU_RPM,
RadPerSec = ID_QU_RADPERSEC,
Meters = ID_QU_METERS,
WeberTurns = ID_QU_WEBERTURNS,
Teslas = ID_QU_TESLAS,
Webers = ID_QU_WEBERS,
VoltsPerVolt = ID_QU_VOLTSPERVOLT,
AmpsPerAmp = ID_QU_AMPSPERAMP,
AmpsPerVolt = ID_QU_AMPSPERVOLT
};
// ------------------------------
// 4) 序列存储方式tagStorageMethodID物理类型 UINT4
// 来源PDF 表 B.2常量来源pqdif/include/pqdif_id.h
// ------------------------------
enum class StorageMethodId : uint32_t
{
Values = ID_SERIES_METHOD_VALUES,
Scaled = ID_SERIES_METHOD_SCALED,
Increment = ID_SERIES_METHOD_INCREMENT
};
// ------------------------------
// 5) 触发方式tagTriggerMethodID物理类型 UINT4
// 来源PDF 表 B.3 / B.4常量来源pqdif/include/pqdif_id.h
// ------------------------------
enum class TriggerMethodId : uint32_t
{
None = ID_TRIGGER_METH_NONE,
Channel = ID_TRIGGER_METH_CHANNEL,
Periodic = ID_TRIGGER_METH_PERIODIC,
External = ID_TRIGGER_METH_EXTERNAL,
PeriodicStats = ID_TRIGGER_METH_PERIODIC_STATS
};
// ============================================================================
// 二、整数型语义表条目
// ============================================================================
struct UIntSemanticEntry
{
uint32_t raw_value; // 原始 UINT4 值
const char* standard_name; // 标准名/头文件宏名,例如 ID_PHASE_AN
const char* display_name; // 中文展示名
const char* source_tag; // 该值属于哪个标准字段,例如 tagPhaseID
const char* comment; // 含义说明
};
// ============================================================================
// 三、GUID 型语义表
// ----------------------------------------------------------------------------
// 这里的 GUID 值不手抄,直接引用 pqdif_id.h 中已有常量。
// 这样每一项都能回溯到你的 pqdif 头文件定义。
// ============================================================================
enum class GuidSemanticField
{
QuantityType, // tagQuantityTypeID
ValueType, // tagValueTypeID
QuantityCharacteristic, // tagQuantityCharacteristicID
DisturbanceCategory // tagDisturbanceCategoryID
};
struct GuidSemanticEntry
{
GUID raw_guid; // 原始 GUID 值
GuidSemanticField field; // 所属字段类别
const char* standard_name; // 标准名/头文件常量名,例如 ID_QT_VALUELOG
const char* display_name; // 中文展示名
const char* source_tag; // 对应的标准字段名
const char* comment; // 含义说明
};
// ============================================================================
// 四、固定静态表内容固定来源固定registry 只是查询索引)
// ============================================================================
extern const UIntSemanticEntry kPhaseTable[];
extern const std::size_t kPhaseTableSize;
extern const UIntSemanticEntry kQuantityMeasuredTable[];
extern const std::size_t kQuantityMeasuredTableSize;
extern const UIntSemanticEntry kQuantityUnitsTable[];
extern const std::size_t kQuantityUnitsTableSize;
extern const UIntSemanticEntry kStorageMethodTable[];
extern const std::size_t kStorageMethodTableSize;
extern const UIntSemanticEntry kTriggerMethodTable[];
extern const std::size_t kTriggerMethodTableSize;
extern const GuidSemanticEntry kQuantityTypeTable[];
extern const std::size_t kQuantityTypeTableSize;
extern const GuidSemanticEntry kValueTypeTable[];
extern const std::size_t kValueTypeTableSize;
extern const GuidSemanticEntry kQuantityCharacteristicTable[];
extern const std::size_t kQuantityCharacteristicTableSize;
extern const GuidSemanticEntry kDisturbanceCategoryTable[];
extern const std::size_t kDisturbanceCategoryTableSize;
// ============================================================================
// 五、查询函数
// ============================================================================
const UIntSemanticEntry* FindUIntSemantic(
const UIntSemanticEntry* table,
std::size_t table_size,
uint32_t raw_value);
const UIntSemanticEntry* FindPhase(uint32_t raw_value);
const UIntSemanticEntry* FindQuantityMeasured(uint32_t raw_value);
const UIntSemanticEntry* FindQuantityUnits(uint32_t raw_value);
const UIntSemanticEntry* FindStorageMethod(uint32_t raw_value);
const UIntSemanticEntry* FindTriggerMethod(uint32_t raw_value);
std::string FindPhaseName(uint32_t raw_value);
std::string FindQuantityMeasuredName(uint32_t raw_value);
std::string FindQuantityUnitsName(uint32_t raw_value);
std::string FindStorageMethodName(uint32_t raw_value);
std::string FindStorageMethodFlagsName(uint32_t raw_value);
std::string FindTriggerMethodName(uint32_t raw_value);
// ============================================================================
// 六、GUID 注册表
// ----------------------------------------------------------------------------
// 表内容固定registry 只是把固定表建成快速查询索引。
// ============================================================================
class GuidSemanticRegistry
{
public:
GuidSemanticRegistry();
const GuidSemanticEntry* Find(GuidSemanticField field, const GUID& raw_guid) const;
std::string FindName(GuidSemanticField field, const GUID& raw_guid, const char* fallback = "UNKNOWN_GUID") const;
private:
struct Key
{
GuidSemanticField field;
GUID guid;
};
struct KeyHash
{
std::size_t operator()(const Key& k) const noexcept
{
std::size_t h1 = std::hash<int>{}(static_cast<int>(k.field));
std::size_t h2 = GuidHash{}(k.guid);
return h1 ^ (h2 << 1);
}
};
struct KeyEq
{
bool operator()(const Key& a, const Key& b) const noexcept
{
return a.field == b.field && GuidEqual(a.guid, b.guid);
}
};
std::unordered_map<Key, const GuidSemanticEntry*, KeyHash, KeyEq> map_;
};
const GuidSemanticRegistry& GetGuidSemanticRegistry();
// ============================================================================
// 七、统计映射常用 helper
// ----------------------------------------------------------------------------
// 这里只放“有明确来源且对统计映射常用”的判断。
// 不做猜测型 helper。
// ============================================================================
bool IsQuantityTypeValueLog(const GUID& raw_guid);
bool IsValueTypeTime(const GUID& raw_guid);
bool IsValueTypeVal(const GUID& raw_guid);
bool IsValueTypeMin(const GUID& raw_guid);
bool IsValueTypeMax(const GUID& raw_guid);
bool IsValueTypeAvg(const GUID& raw_guid);
bool IsValueTypeInterval(const GUID& raw_guid);
bool IsCharacteristicFrequency(const GUID& raw_guid);
bool IsCharacteristicRms(const GUID& raw_guid);
bool IsCharacteristicTotalThd(const GUID& raw_guid);
bool IsUnitHertz(uint32_t raw_value);
bool IsTriggerPeriodicStats(uint32_t raw_value);
} // namespace pqdif_sem#pragma once