Fork of the espurna firmware for `mhsw` switches
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.

172 lines
4.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // Abstract emon sensor class (other sensor classes extend this class)
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include "../sensor.h"
  7. #include "BaseSensor.h"
  8. class BaseEmonSensor : public BaseSensor {
  9. public:
  10. BaseEmonSensor(size_t devices) :
  11. _energy(devices),
  12. _devices(devices)
  13. {}
  14. BaseEmonSensor() :
  15. BaseEmonSensor(1)
  16. {}
  17. unsigned char type() {
  18. return sensor::type::Emon;
  19. }
  20. // --- energy monitoring --
  21. virtual void resizeDevices(size_t devices) {
  22. _energy.resize(devices);
  23. _devices = devices;
  24. }
  25. virtual size_t countDevices() {
  26. return _devices;
  27. }
  28. virtual void resetEnergy(unsigned char index, sensor::Energy energy) {
  29. _energy[index] = energy;
  30. }
  31. virtual void resetEnergy(unsigned char index) {
  32. _energy[index].reset();
  33. };
  34. virtual void resetEnergy() {
  35. for (auto& energy : _energy) {
  36. energy.reset();
  37. }
  38. }
  39. virtual sensor::Energy totalEnergy(unsigned char index) {
  40. return _energy[index];
  41. }
  42. virtual sensor::Energy totalEnergy() {
  43. return totalEnergy(0);
  44. }
  45. virtual double getEnergy(unsigned char index) {
  46. return _energy[index].asDouble();
  47. }
  48. virtual double getEnergy() {
  49. return getEnergy(0);
  50. }
  51. // --- configuration ---
  52. virtual double defaultVoltage() {
  53. return 0.0;
  54. }
  55. // when sensor needs explicit mains voltage value
  56. virtual void setVoltage(double) {}
  57. virtual void setVoltage(unsigned char index, double value) {
  58. setVoltage(value);
  59. }
  60. virtual double getVoltage() {
  61. return defaultVoltage();
  62. }
  63. virtual double getVoltage(unsigned char index) {
  64. return getVoltage();
  65. }
  66. // when sensor implements ratios / multipliers
  67. virtual void setCurrentRatio(double) {}
  68. virtual void setVoltageRatio(double) {}
  69. virtual void setPowerRatio(double) {}
  70. virtual void setEnergyRatio(double) {}
  71. // when sensor implements ratios / multipliers
  72. virtual void setCurrentRatio(unsigned char index, double value) {
  73. setCurrentRatio(value);
  74. }
  75. virtual void setVoltageRatio(unsigned char index, double value) {
  76. setVoltageRatio(value);
  77. }
  78. virtual void setPowerRatio(unsigned char index, double value) {
  79. setPowerRatio(value);
  80. }
  81. virtual void setEnergyRatio(unsigned char index, double value) {
  82. setEnergyRatio(value);
  83. }
  84. // Generic ratio configuration, default is a
  85. // no-op and must be implemented by the sensor class
  86. virtual double defaultCurrentRatio() const {
  87. return 0.0;
  88. }
  89. virtual double defaultVoltageRatio() const {
  90. return 0.0;
  91. }
  92. virtual double defaultPowerRatio() const {
  93. return 0.0;
  94. }
  95. virtual double defaultEnergyRatio() const {
  96. return 0.0;
  97. }
  98. // when sensor implements a single device
  99. virtual double getCurrentRatio() {
  100. return defaultCurrentRatio();
  101. }
  102. virtual double getVoltageRatio() {
  103. return defaultVoltageRatio();
  104. }
  105. virtual double getPowerRatio() {
  106. return defaultPowerRatio();
  107. }
  108. virtual double getEnergyRatio() {
  109. return defaultEnergyRatio();
  110. }
  111. // when sensor implements more than one device
  112. virtual double getCurrentRatio(unsigned char index) {
  113. return getCurrentRatio();
  114. }
  115. virtual double getVoltageRatio(unsigned char index) {
  116. return getVoltageRatio();
  117. }
  118. virtual double getPowerRatio(unsigned char index) {
  119. return getPowerRatio();
  120. }
  121. virtual double getEnergyRatio(unsigned char index) {
  122. return getEnergyRatio();
  123. }
  124. virtual void expectedCurrent(double value) {}
  125. virtual void expectedVoltage(unsigned int value) {}
  126. virtual void expectedPower(unsigned int value) {}
  127. virtual void expectedCurrent(unsigned char index, double value) {
  128. expectedCurrent(value);
  129. }
  130. virtual void expectedVoltage(unsigned char index, unsigned int value) {
  131. expectedVoltage(value);
  132. }
  133. virtual void expectedPower(unsigned char index, unsigned int value) {
  134. expectedPower(value);
  135. }
  136. virtual void resetRatios() {}
  137. protected:
  138. std::vector<sensor::Energy> _energy;
  139. size_t _devices;
  140. };