redis_handler.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. import redis
  2. from datetime import datetime
  3. class RedisHandler:
  4. def __init__(self, host='localhost', port=6379, db=0, expire_time=60 * 60 * 24 * 30): # Keep data for 30 days
  5. self.client = redis.Redis(host=host, port=port, db=db)
  6. self.expire_time = expire_time
  7. def save_measurement(self, cell_id, voltage, current, temperature):
  8. """Save cell measurements with timestamp"""
  9. timestamp = datetime.now().isoformat()
  10. key = f"cell:{cell_id}:measurements:{timestamp}"
  11. self.client.hmset(key, {
  12. 'voltage': voltage,
  13. 'current': current,
  14. 'temperature': temperature
  15. })
  16. self.client.expire(key, self.expire_time)
  17. def get_capacity_estimate(self, cell_id):
  18. """Calculate estimated capacity based on historical data"""
  19. measurements = self.client.keys(f"cell:{cell_id}:measurements:*")
  20. if not measurements:
  21. return None
  22. # TODO: Put the capacity estimation algorithm
  23. # based on voltage curves, current integration, etc.
  24. return float(self.client.get(f"cell:{cell_id}:estimated_capacity") or 0)