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.

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