Skip to content

Commit

Permalink
Properly handle missing iSpindel data in Redis (Fixes #621)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorrak committed Apr 5, 2021
1 parent 6cec82f commit 518a358
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/source/develop/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Fixed
- Resolved issue causing false failures of the connectivity test (Thanks postalbunny!)
- Fixed issue preventing renaming of BrewPi controllers
- Dashes now allowed in TiltBridge mDNS IDs
- Corrected issue where iSpindel data couldn't be loaded if a data point wasn't availble in Redis



Expand Down
5 changes: 4 additions & 1 deletion gravity/api/sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def get_ispindel_extras(req, device_id):
extras = device.ispindel_configuration.load_extras_from_redis()
extras['device_name'] = device.name
extras['device_id'] = device.id
extras['log_time'] = device.ispindel_configuration.load_last_log_time_from_redis()

last_log_time = device.ispindel_configuration.load_last_log_time_from_redis()
if last_log_time:
extras['log_time'] = last_log_time
else:
extras = {}

Expand Down
10 changes: 6 additions & 4 deletions gravity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,13 +881,15 @@ def load_extras_from_redis(self) -> dict:

return extras

def load_last_log_time_from_redis(self) -> str:
def load_last_log_time_from_redis(self) -> str or None:
r = redis.Redis.from_url(url=settings.REDIS_URL)
redis_response = r.get('grav_{}_full'.format(self.sensor_id)).decode(encoding="utf-8")
redis_response = r.get('grav_{}_full'.format(self.sensor_id))

if redis_response is None:
# If we didn't get anything back (i.e. no data has been saved to redis yet) then return None
return {}
return None
else:
redis_response = redis_response.decode(encoding="utf-8")

t = json.loads(redis_response)
if 'fields' in t[0]:
Expand All @@ -896,4 +898,4 @@ def load_last_log_time_from_redis(self) -> str:
dt = datetime.datetime.fromisoformat( t[0]['fields']['log_time'].replace("Z","") )
return datetime.datetime.strftime( dt, "%c" )

return {}
return None

0 comments on commit 518a358

Please sign in to comment.