Advertisement
aranhid

Untitled

May 27th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from pymongo import MongoClient
  3. import numpy as np
  4. from datetime import datetime
  5. from _tkinter import TclError
  6.  
  7.  
  8. def get_pushmina():
  9. values = list(client.pushmina.photoresistor.find({}))
  10. values = sorted(values, key=lambda x: x['datetime'])
  11. timestamps = [value['datetime'] for value in values]
  12. values = [value['value'] for value in values]
  13.  
  14. new_values = []
  15. previous = 0
  16. for value in values:
  17. if value != "":
  18. new_values.append(int(value))
  19. previous = int(value)
  20. else:
  21. new_values.append(previous)
  22. return timestamps, new_values
  23.  
  24.  
  25. def correct_values(timestamps, values):
  26. new_values = []
  27. previous = 0
  28. for value in values:
  29. if value != "":
  30. new_values.append(int(value))
  31. previous = int(value)
  32. else:
  33. new_values.append(previous)
  34. return timestamps, new_values
  35.  
  36.  
  37. def get_others(label):
  38. values = []
  39. if label == "serebrennikov":
  40. values = list(client.serebrennikov.photoresistor.find({}))
  41. if label == "tsybulya":
  42. values = list(client.tsybulya.photoresistor.find({}))
  43. if label == "chernogor":
  44. values = list(client.chernogor.photoresistor.find({}))
  45. if label == "bykov":
  46. values = list(client.bykov.values.find({}))
  47. if label == "pushmina":
  48. values = list(client.pushmina.photoresistor.find({}))
  49.  
  50. values = sorted(values, key=lambda x: x['datetime'])
  51. timestamps = [value['datetime'] for value in values]
  52. values = [value['value'] for value in values]
  53. return timestamps, values
  54.  
  55.  
  56. def sortByStartDate(dt, timestamps, values):
  57. sorted = np.c_[timestamps, values]
  58. result = np.array([])
  59. result.shape = (0, 2)
  60. for value in sorted:
  61. if value[0] >= dt:
  62. result = np.vstack((result, value))
  63. return result[:, 0], result[:, 1]
  64.  
  65.  
  66. def sortByStopDate(dt, timestamps, values):
  67. sorted = np.c_[timestamps, values]
  68. result = np.array([])
  69. result.shape = (0, 2)
  70. for value in sorted:
  71. if value[0] <= dt:
  72. result = np.vstack((result, value))
  73. return result[:, 0], result[:, 1]
  74.  
  75.  
  76. client = MongoClient("roboforge.ru", username="admin",
  77. password="pinboard123", authsource="admin",
  78. serverSelectionTimeoutMS=5000, socketTimeoutMS=2000)
  79.  
  80.  
  81. startDatetime = datetime(2021, 5, 27, 10, 30)
  82. stopDatetime = datetime(2021, 5, 27, 16, 30)
  83.  
  84. # startDatetime = None
  85. # stopDatetime = None
  86.  
  87. fig = plt.figure()
  88.  
  89. databases = ["bykov", "chernogor", "pushmina", "serebrennikov", "tsybulya"]
  90. while True:
  91. try:
  92. for database in databases:
  93. timestamps, values = get_others(database)
  94. if database == "pushmina":
  95. timestamps, values = correct_values(timestamps, values)
  96.  
  97. if startDatetime:
  98. timestamps, values = sortByStartDate(
  99. startDatetime, timestamps, values)
  100.  
  101. if stopDatetime:
  102. timestamps, values = sortByStopDate(
  103. stopDatetime, timestamps, values)
  104.  
  105. plt.plot(timestamps, values)
  106.  
  107. fig.legend(databases)
  108.  
  109. print(datetime.now())
  110.  
  111. plt.draw()
  112. plt.pause(60)
  113. fig.clear()
  114. except TclError:
  115. break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement