7static constexpr float DEG_TO_RAD_F =
static_cast<float>(DEG_TO_RAD);
12 const float cosPhi = std::cos(phi * DEG_TO_RAD_F);
22void Mycila::JSY::Metrics::clear() {
31 activeEnergyImported = 0;
32 activeEnergyReturned = 0;
34 reactiveEnergyImported = 0;
35 reactiveEnergyReturned = 0;
45 return (std::isnan(frequency) ? std::isnan(other.
frequency) : frequency == other.
frequency) &&
46 (std::isnan(voltage) ? std::isnan(other.
voltage) : voltage == other.voltage) &&
47 (std::isnan(current) ? std::isnan(other.current) : current == other.current) &&
48 (std::isnan(activePower) ? std::isnan(other.activePower) : activePower == other.activePower) &&
49 (std::isnan(reactivePower) ? std::isnan(other.reactivePower) : reactivePower == other.reactivePower) &&
50 (std::isnan(apparentPower) ? std::isnan(other.apparentPower) : apparentPower == other.apparentPower) &&
51 (std::isnan(powerFactor) ? std::isnan(other.powerFactor) : powerFactor == other.powerFactor) &&
52 (activeEnergy == other.activeEnergy) &&
53 (activeEnergyImported == other.activeEnergyImported) &&
54 (activeEnergyReturned == other.activeEnergyReturned) &&
55 (reactiveEnergy == other.reactiveEnergy) &&
56 (reactiveEnergyImported == other.reactiveEnergyImported) &&
57 (reactiveEnergyReturned == other.reactiveEnergyReturned) &&
58 (apparentEnergy == other.apparentEnergy) &&
59 (std::isnan(phaseAngleU) ? std::isnan(other.phaseAngleU) : phaseAngleU == other.phaseAngleU) &&
60 (std::isnan(phaseAngleI) ? std::isnan(other.phaseAngleI) : phaseAngleI == other.phaseAngleI) &&
61 (std::isnan(phaseAngleUI) ? std::isnan(other.phaseAngleUI) : phaseAngleUI == other.phaseAngleUI) &&
62 (std::isnan(thdU) ? std::isnan(other.thdU) : thdU == other.thdU) &&
63 (std::isnan(thdI) ? std::isnan(other.thdI) : thdI == other.thdI);
78 powerFactor = apparentPower == 0 ? NAN : abs(activePower / apparentPower);
79 reactivePower = std::sqrt(apparentPower * apparentPower - activePower * activePower);
83void Mycila::JSY::Metrics::operator=(
const Metrics& other) {
84 frequency = other.frequency;
85 voltage = other.voltage;
86 current = other.current;
87 activePower = other.activePower;
88 reactivePower = other.reactivePower;
89 apparentPower = other.apparentPower;
90 powerFactor = other.powerFactor;
91 activeEnergy = other.activeEnergy;
92 activeEnergyImported = other.activeEnergyImported;
93 activeEnergyReturned = other.activeEnergyReturned;
94 reactiveEnergy = other.reactiveEnergy;
95 reactiveEnergyImported = other.reactiveEnergyImported;
96 reactiveEnergyReturned = other.reactiveEnergyReturned;
97 apparentEnergy = other.apparentEnergy;
98 phaseAngleU = other.phaseAngleU;
99 phaseAngleI = other.phaseAngleI;
100 phaseAngleUI = other.phaseAngleUI;
105#ifdef MYCILA_JSON_SUPPORT
106void Mycila::JSY::Metrics::toJson(
const JsonObject& root)
const {
107 if (!std::isnan(frequency))
108 root[
"frequency"] = frequency;
109 if (!std::isnan(voltage))
110 root[
"voltage"] = voltage;
111 if (!std::isnan(current))
112 root[
"current"] = current;
113 if (!std::isnan(activePower))
114 root[
"active_power"] = activePower;
115 if (!std::isnan(reactivePower))
116 root[
"reactive_power"] = reactivePower;
117 if (!std::isnan(apparentPower))
118 root[
"apparent_power"] = apparentPower;
119 if (!std::isnan(powerFactor))
120 root[
"power_factor"] = powerFactor;
122 root[
"active_energy"] = activeEnergy;
124 root[
"apparent_energy"] = apparentEnergy;
125 if (activeEnergyImported)
126 root[
"active_energy_imported"] = activeEnergyImported;
127 if (activeEnergyReturned)
128 root[
"active_energy_returned"] = activeEnergyReturned;
130 root[
"reactive_energy"] = reactiveEnergy;
131 if (reactiveEnergyImported)
132 root[
"reactive_energy_imported"] = reactiveEnergyImported;
133 if (reactiveEnergyReturned)
134 root[
"reactive_energy_returned"] = reactiveEnergyReturned;
135 if (!std::isnan(phaseAngleU))
136 root[
"phase_angle_u"] = phaseAngleU;
137 if (!std::isnan(phaseAngleI))
138 root[
"phase_angle_i"] = phaseAngleI;
139 if (!std::isnan(phaseAngleUI))
140 root[
"phase_angle_ui"] = phaseAngleUI;
141 if (!std::isnan(thdU))
142 root[
"thd_u"] = thdU;
143 if (!std::isnan(thdI))
144 root[
"thd_i"] = thdI;
145 float r = resistance();
146 float d = dimmedVoltage();
147 float n = nominalPower();
150 root[
"resistance"] = r;
152 root[
"dimmed_voltage"] = d;
154 root[
"nominal_power"] = n;
float resistance() const
Compute the resistance of the load in ohms (R = P / I^2).
float apparentPower
Apparent power in volt-amperes (VA). Always positive.
float powerFactor
Power factor. Positive value between 0 and 1.
float thdi(float phi=0) const
Compute the total harmonic distortion percentage of current (THDi). This assumes THDu = 0 (perfect vo...
float nominalPower() const
Compute the nominal power of the load in watts (P = V^2 / R).
uint32_t reactiveEnergy
Reactive energy in volt-amperes reactive-hours (VArh).
float activePower
Active power in watts (W). Can be positive or negative.
uint32_t activeEnergyImported
Active energy imported in watt-hours (Wh), going to the load, when activePower > 0.
float current
Current in amperes (A).
uint32_t activeEnergy
Active energy in watt-hours (Wh).
uint32_t activeEnergyReturned
Active energy returned in watt-hours (Wh), coming from the load, when activePower < 0.
float voltage
Voltage in volts (V).
uint32_t reactiveEnergyReturned
Reactive energy returned in volt-amperes reactive-hours (VArh), coming from the load,...
uint32_t reactiveEnergyImported
Reactive energy imported in volt-amperes reactive-hours (VArh), going to the load,...
float dimmedVoltage() const
Compute the dimmed voltage (V = P / I).
float frequency
Frequency in hertz (Hz).
uint32_t apparentEnergy
Apparent energy in volt-amperes-hours (VAh).