Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import redis
- class StateDataBase:
- def __init__(self, db_name):
- LIST_BIO_PARAMETRS = (
- 'bloodoxygen',
- 'heartrate',
- 'thermometer',
- 'tonometer',
- 'distance',
- 'calories',
- )
- """Конструктор класса промежуточного состаяния"""
- self.connection = redis.Redis(host='176.124.136.242', port=6379,db=10)
- self.db_name = db_name
- all_connection = self.connection.hgetall(self.db_name)
- for val in LIST_BIO_PARAMETRS:
- if not bytes(val,encoding='utf-8') in all_connection:
- objects = {
- f'{val}' : '',
- }
- print(objects)
- self.connection.hmset(self.db_name, objects)
- def GetAllParams(self) -> json:
- """Возвращает все данные из бд"""
- return self.connection.hgetall(self.db_name)
- def ClearAllState(self):
- """Очищает всю используемую бд"""
- # self.connection.unlink(self.db_name)
- self.connection.flushdb()
- def GetParametrs(self, parametr) -> json:
- """Геттер класса StateDataBase (Возвращает по ключу значение из бд Redis)"""
- try:
- key = bytes(
- parametr,
- encoding='utf-8'
- )
- compound = json.loads(
- self.connection.hgetall(self.db_name)[key].decode('utf-8')
- )
- return compound
- except Exception as e:
- print('==== get_user_parametrs ==== %s ' % (e))
- def SetParametrs(self, key_parametr, value):
- """Устанавливает по ключу значение переданных через параметры метада , бд Redis"""
- key = bytes(
- key_parametr,
- encoding='utf-8'
- )
- compound = {}
- try:
- compound = json.loads(
- self.connection.hgetall(self.db_name)[key].decode('utf-8')
- )
- except:
- print("No date !")
- value = json.loads(value)
- if compound:
- mapp = compound
- mapp.append(value)
- else:
- mapp = []
- mapp.append(value)
- self.connection.hset(self.db_name, key, json.dumps(mapp))
- params = {
- "android_client":{
- "battery_charge":68,
- "mac":"02:00:00:00:00:00"
- },
- "location":{
- "lat":53.2342293,
- "lon":50.2242647
- },
- "health_device":{
- "battery_charge":68,
- "mac":"E1:0D:FF:E3:F2:BD",
- "type":"wristband"
- },
- "measurements":{
- "kilocalories":7,
- "datetime":"2021-05-07-14-04-23"
- },
- "auth_token":"76e1140647698135cc48e6cf57d7b4443cd458b2"
- }
- st_db = StateDataBase('Test')
- st_db.SetParametrs('bloodoxygen', json.dumps(params))
- st_db.SetParametrs('heartrate', json.dumps(params))
- st_db.SetParametrs('thermometer', json.dumps(params))
- st_db.SetParametrs('tonometer', json.dumps(params))
- st_db.SetParametrs('distance', json.dumps(params))
- st_db.SetParametrs('calories', json.dumps(params))
- print(st_db.GetParametrs('bloodoxygen'))
- print(st_db.GetParametrs('heartrate'))
- print(st_db.GetParametrs('thermometer'))
- print(st_db.GetParametrs('tonometer'))
- print(st_db.GetParametrs('distance'))
- print(st_db.GetParametrs('calories'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement