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.

148 lines
3.3 KiB

  1. /*
  2. SENSOR MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. Copyright (C) 2020 by Maxim Prokhorov <prokhorov dot max at outlook dot com>
  5. */
  6. #pragma once
  7. #include "espurna.h"
  8. //--------------------------------------------------------------------------------
  9. namespace sensor {
  10. namespace type {
  11. enum Type : unsigned char {
  12. Base = 0,
  13. Emon = 1 << 0,
  14. Analog = 1 << 1
  15. };
  16. } // namespace type
  17. enum class Unit : int {
  18. Min_,
  19. None,
  20. Celcius,
  21. Farenheit,
  22. Kelvin,
  23. Percentage,
  24. Hectopascal,
  25. Ampere,
  26. Volt,
  27. Voltampere,
  28. Kilovoltampere,
  29. VoltampereReactive,
  30. KilovoltampereReactive,
  31. Watt,
  32. Kilowatt,
  33. WattSecond,
  34. Joule = WattSecond,
  35. KilowattHour,
  36. PartsPerMillion,
  37. Ohm,
  38. MicrogrammPerCubicMeter, // The concentration of an air pollutant
  39. MilligrammPerCubicMeter, //
  40. Lux,
  41. UltravioletIndex, // "measurement of the strength of sunburn-producing ultraviolet (UV) radiation at a particular place and time"
  42. // (XXX: Not a unit. Distinguish from None and specify decimals)
  43. CountsPerMinute, // Unit of local dose rate (Geiger counting)
  44. MicrosievertPerHour, // 2nd unit of local dose rate (Geiger counting)
  45. Meter,
  46. Max_
  47. };
  48. // Base units are 32 bit since we are the fastest with them.
  49. struct Ws {
  50. Ws();
  51. Ws(uint32_t);
  52. uint32_t value;
  53. };
  54. struct Wh {
  55. Wh();
  56. Wh(Ws);
  57. Wh(uint32_t);
  58. uint32_t value;
  59. };
  60. struct KWh {
  61. KWh();
  62. KWh(Ws);
  63. KWh(Wh);
  64. KWh(uint32_t);
  65. uint32_t value;
  66. };
  67. struct Energy {
  68. constexpr static uint32_t KwhMultiplier = 3600000ul;
  69. constexpr static uint32_t KwhLimit = ((1ul << 31ul) / KwhMultiplier);
  70. Energy() = default;
  71. // TODO: while we accept ws >= the kwh conversion limit,
  72. // should this be dealt with on the unit level?
  73. Energy(double);
  74. Energy(KWh, Ws);
  75. Energy(KWh);
  76. Energy(Wh);
  77. Energy(Ws);
  78. // Sets internal counters to zero
  79. void reset();
  80. // Check whether we have *any* energy recorded. Can be zero:
  81. // - on cold boot
  82. // - on overflow
  83. // - when we call `reset()`
  84. operator bool();
  85. // Generic conversion as-is
  86. double asDouble();
  87. // Convert back to input unit, with overflow mechanics when kwh values goes over 32 bit
  88. Ws asWs();
  89. // Generic sensors output energy in joules / watt-second
  90. Energy& operator +=(Ws);
  91. Energy operator +(Ws);
  92. // But sometimes we want to accept asDouble() value back
  93. Energy& operator =(double);
  94. // We are storing a kind-of integral and fractional parts
  95. // Using watt-second to avoid loosing precision, we don't expect these to be accessed directly
  96. KWh kwh;
  97. Ws ws;
  98. };
  99. }
  100. String magnitudeName(unsigned char index);
  101. String magnitudeUnits(unsigned char index);
  102. unsigned char magnitudeType(unsigned char index);
  103. // XXX: without param name it is kind of vague what exactly unsigned char is
  104. // consider using index instead of type or adding stronger param type
  105. String magnitudeTopic(unsigned char type);
  106. unsigned char sensorCount();
  107. unsigned char magnitudeCount();
  108. double magnitudeValue(unsigned char index);
  109. unsigned char magnitudeIndex(unsigned char index);
  110. String magnitudeTopicIndex(unsigned char index);
  111. void sensorWebSocketMagnitudes(JsonObject& root, const String& prefix);
  112. void sensorSetup();
  113. void sensorLoop();