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):
  5. self.client = redis.Redis(host=host, port=port, db=db)
  6. def save_measurement(self, cell_id, voltage, current, temperature):
  7. """Save cell measurements with timestamp"""
  8. timestamp = datetime.now().isoformat()
  9. key = f"cell:{cell_id}:measurements:{timestamp}"
  10. self.client.hmset(key, {
  11. 'voltage': voltage,
  12. 'current': current,
  13. 'temperature': temperature
  14. })
  15. self.client.expire(key, 60 * 60 * 24 * 30) # Keep data for 30 days
  16. def get_capacity_estimate(self, cell_id):
  17. """Calculate estimated capacity based on historical data"""
  18. measurements = self.client.keys(f"cell:{cell_id}:measurements:*")
  19. if not measurements:
  20. return None
  21. # Here you would implement your capacity estimation algorithm
  22. # based on voltage curves, current integration, etc.
  23. # This is a simplified example
  24. return float(self.client.get(f"cell:{cell_id}:estimated_capacity") or 0)