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.

113 lines
3.5 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 double getVoltage() { return 0.0; }
  55. virtual double getVoltage(unsigned char index) { return getVoltage(); }
  56. // when sensor implements ratios / multipliers
  57. virtual void setCurrentRatio(double) {}
  58. virtual void setVoltageRatio(double) {}
  59. virtual void setPowerRatio(double) {}
  60. virtual void setEnergyRatio(double) {}
  61. // when sensor implements ratios / multipliers
  62. virtual void setCurrentRatio(unsigned char index, double) {}
  63. virtual void setVoltageRatio(unsigned char index, double) {}
  64. virtual void setPowerRatio(unsigned char index, double) {}
  65. virtual void setEnergyRatio(unsigned char index, double) {}
  66. // when sensor implements a single device
  67. virtual double getCurrentRatio() { return 0.0; }
  68. virtual double getVoltageRatio() { return 0.0; }
  69. virtual double getPowerRatio() { return 0.0; }
  70. virtual double getEnergyRatio() { return 0.0; }
  71. // when sensor implements more than one device
  72. virtual double getCurrentRatio(unsigned char index) { return getCurrentRatio(); }
  73. virtual double getVoltageRatio(unsigned char index) { return getCurrentRatio(); }
  74. virtual double getPowerRatio(unsigned char index) { return getCurrentRatio(); }
  75. virtual double getEnergyRatio(unsigned char index) { return getCurrentRatio(); }
  76. virtual void expectedCurrent(double value) {}
  77. virtual void expectedVoltage(unsigned int value) {}
  78. virtual void expectedPower(unsigned int value) {}
  79. virtual void resetCalibration(double value) {}
  80. virtual void resetRatios() {}
  81. protected:
  82. std::vector<sensor::Energy> _energy;
  83. size_t _devices;
  84. };