diff --git a/solar/solar/interfaces/db/redis_db.py b/solar/solar/interfaces/db/redis_db.py index d6a166ce..3c43ccb7 100644 --- a/solar/solar/interfaces/db/redis_db.py +++ b/solar/solar/interfaces/db/redis_db.py @@ -9,7 +9,7 @@ from solar import errors class RedisDB(object): COLLECTIONS = Enum( 'Collections', - 'connection resource' + 'connection resource state_data state_log' ) DB = { 'host': 'localhost', @@ -21,9 +21,12 @@ class RedisDB(object): self.entities = {} def read(self, uid, collection=COLLECTIONS.resource): - return json.loads( - self._r.get(self._make_key(collection, uid)) - ) + try: + return json.loads( + self._r.get(self._make_key(collection, uid)) + ) + except TypeError: + return None def save(self, uid, data, collection=COLLECTIONS.resource): return self._r.set( diff --git a/solar/solar/state.py b/solar/solar/state.py index 3d1070e5..b015a64c 100644 --- a/solar/solar/state.py +++ b/solar/solar/state.py @@ -76,8 +76,9 @@ class Log(object): def __init__(self, path): self.path = path items = [] - if path in db: - items = db[path] or items + r = db.read(path, collection=db.COLLECTIONS.state_log) + if r: + items = r or items self.items = deque([LogItem( l['uid'], l['res'], @@ -85,7 +86,12 @@ class Log(object): getattr(STATES, l['state'])) for l in items]) def sync(self): - db[self.path] = [i.to_dict() for i in self.items] + #db[self.path] = [i.to_dict() for i in self.items] + db.save( + self.path, + [i.to_dict() for i in self.items], + collection=db.COLLECTIONS.state_log + ) def add(self, logitem): @@ -121,19 +127,22 @@ class Data(collections.MutableMapping): def __init__(self, path): self.path = path self.store = {} - if path in db: - self.store = db[path] or self.store + r = db.read(path, collection=db.COLLECTIONS.state_data) + if r: + self.store = r or self.store def __getitem__(self, key): return self.store[key] def __setitem__(self, key, value): self.store[key] = value - db[self.path] = self.store + db.save(self.path, self.store, collection=db.COLLECTIONS.state_data) + #db[self.path] = self.store def __delitem__(self, key): self.store.pop(key) - db[self.path] = self.store + db.save(self.path, self.store, collection=db.COLLECTIONS.state_data) + #db[self.path] = self.store def __iter__(self): return iter(self.store)