bufferManager.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Import config
4 from config import PATH, CLIENT
5 
6 import rospy, os, bson
7 from pymongo import errors as pymongo_erros
8 from bson import errors as bson_errors
9 
10 
11 class bufferManager():
12  def __init__(self) -> None:
13  # Starts unique node in the ROS core with the name bufferManager
14  rospy.init_node('bufferManager', anonymous=False)
15  rospy.loginfo("Buffer manager started")
16  # Node rate
17  rate = rospy.Rate(1)
18  # While the ROS core is running
19  while not rospy.is_shutdown():
20  # If have files to send and the cloud is available
21  try:
22  if len(os.listdir(path=PATH)) > 0 and CLIENT.is_primary:
23  # Try to send the files
24  self.getFiles()
25  else:
26  # Wait 10 seconds for the next check
27  for i in range(0,10): rate.sleep()
28  except FileNotFoundError:
29  # Create directory if it don't exist
30  os.chmod
31  os.makedirs(name=PATH)
32  except (pymongo_erros.ConnectionFailure, pymongo_erros.ServerSelectionTimeoutError):
33  # Wait 10 seconds for the next check
34  for i in range(0,10): rate.sleep()
35  except Exception as e:
36  rospy.logerr("Error on file queue")
37  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
38 
39 
40 # Get the files in PATH for send to cloud
41  def getFiles(self):
42  try:
43  # Get all files in the directory
44  files = sorted(os.listdir(path=PATH), reverse=True)
45  files = list(filter(lambda file: file.endswith(".cjson"), files))
46  except Exception as e:
47  rospy.logerr("Error on get the file list")
48  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
49  for file in files:
50  try:
51  # Try to open the file
52  get = open(file=PATH+file, mode='rb')
53  except Exception as e:
54  rospy.logerr("Error on file open: " + file)
55  # Decode to BSON
56  try:
57  data = bson.BSON.decode(get.read())
58  except Exception as e:
59  rospy.logerr("Errosr on file decode: " + file)
60  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
61  get.close()
62  self.rm(file=file, msg="Error on BSON")
63  # Try send to the cloud
64  if self.send2cloud(dataPath=data['dataPath'], content=data['content']):
65  get.close()
66  self.rm(file=file)
67 
68 # Remove a file
69  def rm(self, file, msg = None):
70  try:
71  os.remove(PATH+file)
72  if msg != None:
73  rospy.loginfo("Delete file " + file + " | " + msg)
74  return True
75  except Exception as e:
76  rospy.logerr("Error on the file remove")
77  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
78  return False
79 
80 # Send the data to MongoDB cloud
81  def send2cloud(self, dataPath: bson, content: bson):
82  try:
83  # Check if dataPath is valid
84  test = dataPath['dataSource']
85  test = dataPath['dataBase']
86  test = dataPath['collection']
87  except Exception as e:
88  rospy.logerr("Error in storage data path")
89  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
90  return True
91  try:
92  # Try send to the cloud
93  if not isinstance(content, list):
94  content = [content]
95  return CLIENT[dataPath['dataBase']][dataPath['collection']].insert_many(content).acknowledged
96  except pymongo_erros.DuplicateKeyError:
97  # If the register duplicate
98  return True
99  except Exception as e:
100  rospy.logerr("Error when update to cloud")
101  rospy.logerr("An exception occurred:", type(e).__name__,e.args)
102  return False
103 
104 
105 if __name__ == '__main__':
106  try:
107  bufferManager()
108  except rospy.ROSInterruptException:
109  pass
bufferManager.bufferManager
Definition: bufferManager.py:11
bufferManager.bufferManager.__init__
None __init__(self)
Definition: bufferManager.py:12
bufferManager.bufferManager.getFiles
def getFiles(self)
Definition: bufferManager.py:41
bufferManager.bufferManager.rm
def rm(self, file, msg=None)
Definition: bufferManager.py:69
bufferManager.bufferManager.send2cloud
def send2cloud(self, bson dataPath, bson content)
Definition: bufferManager.py:81


cedri_node_monitoring
Author(s): Andre Luis Frana
autogenerated on Sat Jul 1 2023 02:34:25