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.

144 lines
4.0 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. // when sensor needs explicit mains voltage value
  53. virtual void setVoltage(double) {}
  54. virtual void setVoltage(unsigned char index, double value) {
  55. setVoltage(value);
  56. }
  57. virtual double getVoltage() {
  58. return 0.0;
  59. }
  60. virtual double getVoltage(unsigned char index) {
  61. return getVoltage();
  62. }
  63. // when sensor implements ratios / multipliers
  64. virtual void setCurrentRatio(double) {}
  65. virtual void setVoltageRatio(double) {}
  66. virtual void setPowerRatio(double) {}
  67. virtual void setEnergyRatio(double) {}
  68. // when sensor implements ratios / multipliers
  69. virtual void setCurrentRatio(unsigned char index, double value) {
  70. setCurrentRatio(value);
  71. }
  72. virtual void setVoltageRatio(unsigned char index, double value) {
  73. setVoltageRatio(value);
  74. }
  75. virtual void setPowerRatio(unsigned char index, double value) {
  76. setPowerRatio(value);
  77. }
  78. virtual void setEnergyRatio(unsigned char index, double value) {
  79. setEnergyRatio(value);
  80. }
  81. // when sensor implements a single device
  82. virtual double getCurrentRatio() {
  83. return 0.0;
  84. }
  85. virtual double getVoltageRatio() {
  86. return 0.0;
  87. }
  88. virtual double getPowerRatio() {
  89. return 0.0;
  90. }
  91. virtual double getEnergyRatio() {
  92. return 0.0;
  93. }
  94. // when sensor implements more than one device
  95. virtual double getCurrentRatio(unsigned char index) {
  96. return getCurrentRatio();
  97. }
  98. virtual double getVoltageRatio(unsigned char index) {
  99. return getVoltageRatio();
  100. }
  101. virtual double getPowerRatio(unsigned char index) {
  102. return getPowerRatio();
  103. }
  104. virtual double getEnergyRatio(unsigned char index) {
  105. return getEnergyRatio();
  106. }
  107. virtual void expectedCurrent(double value) {}
  108. virtual void expectedVoltage(unsigned int value) {}
  109. virtual void expectedPower(unsigned int value) {}
  110. virtual void resetCalibration(double value) {}
  111. virtual void resetRatios() {}
  112. protected:
  113. std::vector<sensor::Energy> _energy;
  114. size_t _devices;
  115. };