cell.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from redis_handler import RedisHandler
  2. from db_handler import DatabaseHandler
  3. class Cell:
  4. def __init__(self, cell_id):
  5. self.cell_id = cell_id
  6. self.voltage = 0
  7. self.current = 0
  8. self.temperature = 0
  9. self._redis = RedisHandler()
  10. self._db = DatabaseHandler()
  11. # Get estimated capacity from database or use nominal as default
  12. self.nominal_capacity_mah = self._db.get_cell_capacity_nom(self.cell_id)
  13. self.estimated_capacity_mah = self._db.get_cell_capacity(self.cell_id) or self.nominal_capacity_mah
  14. self.cell_limits = self._db.get_cell_limits(self.cell_id)
  15. def measure_voltage(self):
  16. # Logic to measure voltage
  17. return self.voltage
  18. def measure_current(self):
  19. # Logic to measure current
  20. return self.current
  21. def measure_temperature(self):
  22. # Logic to measure temperature
  23. return self.temperature
  24. def disconnect(self):
  25. # Logic to disconnect the cell
  26. pass
  27. def connect(self):
  28. # Logic to connect the cell
  29. pass
  30. def update_estimated_capacity(self, new_estimate_mah):
  31. """Update the estimated capacity based on usage patterns and measurements"""
  32. self.estimated_capacity_mah = new_estimate_mah
  33. self._db.update_cell_capacity(self.cell_id, new_estimate_mah)
  34. def get_capacity_health(self):
  35. """Return cell health as a percentage of nominal capacity"""
  36. return (self.estimated_capacity_mah / self.nominal_capacity_mah) * 100
  37. def update_measurements(self, voltage, current, temperature):
  38. """Update measurements and save to Redis"""
  39. self.voltage = voltage
  40. self.current = current
  41. self.temperature = temperature
  42. self._redis.save_measurement(self.cell_id, voltage, current, temperature)
  43. self._update_capacity_estimate()
  44. def _update_capacity_estimate(self):
  45. """Update capacity estimate based on Redis data"""
  46. new_estimate = self._redis.get_capacity_estimate(self.cell_id)
  47. if new_estimate:
  48. self.estimated_capacity_mah = new_estimate