Advertisement
Mochinov

Untitled

May 11th, 2021
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.94 KB | None | 0 0
  1. import json
  2. import redis
  3.  
  4. class StateDataBase:
  5.     LIST_BIO_PARAMETRS = (
  6.         'bloodoxygen',
  7.         'heartrate',
  8.         'thermometer',
  9.         'tonometer'
  10.     )
  11.     def __init__(self, db_name):
  12.         """Конструктор класса промежуточного состаяния"""
  13.         self.connection = redis.Redis(host='192.168.0.18', port=6379,db=10)
  14.         self.db_name = db_name
  15.  
  16.         all_connection = self.connection.hgetall(self.db_name)
  17.         if bytes('bloodoxygen',encoding='utf-8') in all_connection:
  18.             pass
  19.         else:
  20.             objects = {
  21.                 f'bloodoxygen' : ''
  22.             }
  23.             print(objects)
  24.             self.connection.hmset(self.db_name, objects)
  25.  
  26.  
  27.     def GetAllParams(self) -> json:
  28.         """Возвращает все данные из бд"""
  29.         return self.connection.hgetall(self.db_name)
  30.  
  31.     def ClearAllState(self):
  32.         """Очищает всю используемую бд"""
  33.         # self.connection.unlink(self.db_name)
  34.         self.connection.flushdb()
  35.  
  36.     def GetParametrs(self, parametr) -> json:
  37.         """Геттер класса DoctorState (Возвращает по ключу значение из бд Redis)"""
  38.         try:
  39.             key = bytes(
  40.                 parametr,
  41.                 encoding='utf-8'
  42.             )
  43.  
  44.             compound = json.loads(
  45.                 self.connection.hgetall(self.db_name)[key].decode('utf-8')
  46.             )
  47.             return compound
  48.         except Exception as e:
  49.             print('==== get_user_parametrs ==== %s ' % (e))
  50.  
  51.     def SetParametrs(self, key_parametr, value):
  52.         """Устанавливает по ключу значение переданных через параметры метада , бд Redis"""
  53.  
  54.         key = bytes(
  55.             key_parametr,
  56.             encoding='utf-8'
  57.         )
  58.         compound = {}
  59.         try:
  60.             compound = json.loads(
  61.                 self.connection.hgetall(self.db_name)[key].decode('utf-8')
  62.             )
  63.         except:
  64.             print("No date !")
  65.  
  66.         value = json.loads(value)
  67.         if compound:
  68.             mapp = compound
  69.             mapp.append(value)
  70.         else:
  71.             mapp = []
  72.             mapp.append(value)
  73.  
  74.         self.connection.hset(self.db_name, key, json.dumps(mapp))
  75.  
  76.  
  77.  
  78. params = {
  79.     "android_client":{
  80.         "battery_charge":68,
  81.         "mac":"02:00:00:00:00:00"
  82.     },
  83.     "location":{
  84.         "lat":53.2342293,
  85.         "lon":50.2242647
  86.     },
  87.     "health_device":{
  88.         "battery_charge":68,
  89.         "mac":"E1:0D:FF:E3:F2:BD",
  90.         "type":"wristband"
  91.     },
  92.     "measurements":{
  93.         "kilocalories":7,
  94.         "datetime":"2021-05-07-14-04-23"
  95.     },
  96.     "auth_token":"76e1140647698135cc48e6cf57d7b4443cd458b2"
  97. }
  98. st_db = StateDataBase('Test')
  99. print(st_db.GetParametrs('bloodoxygen'))
  100. st_db.SetParametrsTest('bloodoxygen', json.dumps(params))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement