Commit cd7e73d53bfbbabee977fff709acfe41cebbe9e4

Authored by Alexis Koralewski
1 parent d45d4a0b
Exists in dev

fixing plc_config.json issue if this file wasn't there by creating a file with N…

…one values, adding and uptading comments on this file
Showing 1 changed file with 84 additions and 26 deletions   Show diff stats
src/core/pyros_django/monitoring/plc_checker.py
... ... @@ -39,12 +39,16 @@ class PlcChecker(object):
39 39 self.sensors_table = []
40 40 self.load_config()
41 41  
42   - def monitor_name_switch(self, cmd_args):
  42 + def monitor_name_switch(self, cmd_args:str)->None:
43 43 """
44   - Replace sensor's monitring_name
45   - cmd : monitor_name_switch None Came:/S/N_A5EM:/Power_input SHUTTERS
46   - :param cmd_args: ex: None Came:/S/N_A5EM:/Power_input SHUTTERS
47   - :return:
  44 + Replace sensor's monitoring_name
  45 + example of full cmd : monitor_name_switch None Came:/S/N_A5EM:/Power_input SHUTTERS
  46 +
  47 + Args:
  48 + param cmd_args : arguments of command (example: None Came:/S/N_A5EM:/Power_input SHUTTERS)
  49 +
  50 + Returns :
  51 + None
48 52 """
49 53 #TODO test
50 54 old_monitoring_name, sensor_id, new_monitoring_name = cmd_args.split(" ")
... ... @@ -53,13 +57,17 @@ class PlcChecker(object):
53 57 self.sensors_table[5] = new_monitoring_name
54 58 self.save_config()
55 59  
56   - def sensor_switch(self, cmd_args):
  60 + def sensor_switch(self, cmd_args:str)->None:
57 61 """
58 62 Replace monitoring_mame's sensor
59 63 and switch selected in sensors_table
60   - cmd : sensor_switch Temperature_outside CV7:/S/N_09172419:/OutsideTemperature
61   - :param cmd_args: Temperature_outside CV7:/S/N_09172419:/OutsideTemperature
62   - :return:
  64 +
  65 + Args:
  66 + example of cmd : sensor_switch Temperature_outside CV7:/S/N_09172419:/OutsideTemperature
  67 + :param cmd_args: arguments of command (example : Temperature_outside CV7:/S/N_09172419:/OutsideTemperature)
  68 +
  69 + Returns :
  70 + None
63 71 """
64 72 #TODO test
65 73 # Replace monitoring_mame's sensor
... ... @@ -77,18 +85,28 @@ class PlcChecker(object):
77 85 self.sensors_table[7] = ""
78 86 self.save_config()
79 87  
80   - def reset_plc_checker(self, cmd_args):
  88 + def reset_plc_checker(self, cmd_args)->None:
81 89 """
82 90 Change plc_origin and next json check will make it reset
83   - :param cmd_args: Not used
84   - :return:
  91 +
  92 + Args:
  93 + cmd_args: Not used
  94 +
  95 + Returns:
  96 + None
85 97 """
86 98 #TODO test
87 99 no_arg = cmd_args
88 100 self.origin["plc_origin"] = "Tataouine"
89 101 self.origin["plc_origin"] = "Tataouine Observatory"
90 102  
91   - def load_config(self):
  103 + def load_config(self)->None:
  104 + """
  105 + Load config file (in json format) and update values of the attributes of this class
  106 +
  107 + If the config file isn't found, it will call save_config() to create file with null values.
  108 + Those values will be updated by chk_config() called by agentM.
  109 + """
92 110 try:
93 111 # ok with ./start_agent agentM
94 112 #plc_config_dir = Path("./monitoring/")
... ... @@ -107,10 +125,18 @@ class PlcChecker(object):
107 125 logger.info("Loaded : plc_config.json")
108 126 colibri_json.close
109 127 except:
110   - logger.error("No plc_config.json")
111   - raise Exception(f"Could not load plc_config.json ; current dir is {os.getcwd()} ; plc_config_dir is {plc_config_dir}")
  128 + # logger.error("No plc_config.json")
  129 + # raise Exception(f"Could not load plc_config.json ; current dir is {os.getcwd()} ; plc_config_dir is {plc_config_dir}")
  130 + # if the file doesn't exist we're creating a new one with initial values (null values)
  131 + self.save_config()
112 132  
113   - def save_config(self):
  133 + def save_config(self)->None:
  134 + """
  135 + Save current config into json file.
  136 +
  137 + Raises:
  138 + Exception: failed to write into config file.
  139 + """
114 140 try:
115 141 _struct = {}
116 142 _struct["origin"] = self.origin
... ... @@ -129,16 +155,23 @@ class PlcChecker(object):
129 155 logger.error("Not saved plc_config.json")
130 156 raise Exception(f"Could not save plc_config.json ; current dir is {os.getcwd()} ; plc_config_dir is {plc_config_dir}")
131 157  
132   - def init_monitoring_names(self):
  158 + def init_monitoring_names(self)->None:
  159 + """
  160 + Initialize monitoring names with None values.
  161 + """
133 162 for sensor in self.monitoring_names:
134 163 self.monitoring_names[sensor] = None
135 164  
136   - def chk_config(self, status_plc):
  165 + def chk_config(self, status_plc:json)->bool:
137 166 """
138 167 Load struct new status_plc and check origin
139   - If origin changed. First time (ori or other PLC
140   - :param status_plc:
141   - :return: True
  168 + If origin changed. First time (original or other PLC)
  169 +
  170 + Args:
  171 + param status_plc: json given by plc (astroguita)
  172 +
  173 + Returns:
  174 + True
142 175 """
143 176 try:
144 177 struct = json.loads(status_plc)
... ... @@ -155,7 +188,13 @@ class PlcChecker(object):
155 188 self.save_config()
156 189 return True
157 190  
158   - def same_origin(self):
  191 + def same_origin(self)->bool:
  192 + """
  193 + Check if the origin from the json is the same as the plcChecker.
  194 + If they are different, it will update the origin and site values.
  195 + Returns:
  196 + Bool: True if they're the same, false otherwise
  197 + """
159 198 # check new origin
160 199 print('?')
161 200 if self.struct["origin"] == self.origin["plc_origin"] and self.struct["site"] == self.origin["plc_site"]:
... ... @@ -168,7 +207,7 @@ class PlcChecker(object):
168 207 self.init_monitoring_names()
169 208 return False
170 209  
171   - def known_sensor(self, name):
  210 + def known_sensor(self, name:str)->str:
172 211 known = None
173 212 if name in self.monitoring_names.keys():
174 213 known = name
... ... @@ -181,18 +220,34 @@ class PlcChecker(object):
181 220 selected = "checked"
182 221 return selected
183 222  
184   - def get_key(self, struct, key):
  223 + def get_key(self, struct:dict, key:str)->any:
  224 + """
  225 + Return value associate to key in struct
  226 +
  227 + Args:
  228 + struct (dict):
  229 + key (str):
  230 +
  231 + Returns:
  232 + any: the corresponding object of that key in struct
  233 + """
185 234 try:
186 235 ret = struct[key]
187 236 except:
188 237 ret = None
189 238 return ret
190 239  
191   - def get_sensor(self, monitored):
  240 + def get_sensor(self, monitored:str)->any:
192 241 """
193 242 Return value from struct with path = monitoring_names.key(monitored)
194 243 ex:'RG11:/S/N_207588:/RainSate'
195 244 ex:'DHT22:/MiFe_DHT1_1:/Humidity'
  245 +
  246 + Args:
  247 + monitored (str): sensor name
  248 +
  249 + Returns:
  250 + any: value associated to that sensor name.
196 251 """
197 252 _id = self.monitoring_names[monitored]
198 253 _value = None
... ... @@ -206,7 +261,10 @@ class PlcChecker(object):
206 261 return _value
207 262  
208 263  
209   - def scan_sensors(self):
  264 + def scan_sensors(self)->None:
  265 + """
  266 + log and update sensors table from struct
  267 + """
210 268 # return sensors table from struct
211 269 # TODO doc it
212 270 self.sensors_table = []
... ...