battery.cc 864 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "iot/thing.h"
  2. #include "board.h"
  3. #include <esp_log.h>
  4. #define TAG "Battery"
  5. namespace iot {
  6. // 这里仅定义 Battery 的属性和方法,不包含具体的实现
  7. class Battery : public Thing {
  8. private:
  9. int level_ = 0;
  10. bool charging_ = false;
  11. bool discharging_ = false;
  12. public:
  13. Battery() : Thing("Battery", "电池管理") {
  14. // 定义设备的属性
  15. properties_.AddNumberProperty("level", "当前电量百分比", [this]() -> int {
  16. auto& board = Board::GetInstance();
  17. if (board.GetBatteryLevel(level_, charging_, discharging_)) {
  18. return level_;
  19. }
  20. return 0;
  21. });
  22. properties_.AddBooleanProperty("charging", "是否充电中", [this]() -> int {
  23. return charging_;
  24. });
  25. }
  26. };
  27. } // namespace iot
  28. DECLARE_THING(Battery);