Advertisement
Mochinov

Untitled

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