def run(self) -> None: """Iterates trough the observation set and sends observations to an external callback function.""" # Return if observation is disabled. if not self._obs.get('enabled'): return # Disable the observation if it should run one time only. if self._obs.get('onetime'): self._obs.set('enabled', False) # Make a deep copy, since we don't want to do any changes to the # observation in our observation set. obs_copy = copy.deepcopy(self._obs) # Set IDs. obs_copy.set('id', Observation.get_new_id()) obs_copy.set('pid', self._project_id) obs_copy.set('nid', self._node_id) # Insert the name of the port module or the virtual sensor at the # beginning of the receivers list. receivers = obs_copy.get('receivers') receivers.insert(0, self._port_name) obs_copy.set('receivers', receivers) # Set the next receiver to the module following the port. obs_copy.set('nextReceiver', 1) self.logger.info(f'Starting job "{self._obs.get("name")}" for port ' f'"{self._port_name}" ...') # Get the sleep time of the whole observation. sleep_time = obs_copy.get('sleepTime', 0) # Create target, header, and payload in order to send the observation. target = self._port_name header = Observation.get_header() header['from'] = 'job' payload = obs_copy.data # Fire and forget the observation. self._uplink(target, header, payload) # Sleep until the next observation. self.logger.debug(f'Next observation starts in {sleep_time} s') time.sleep(sleep_time)
def process_observation(self, obs: Observation) -> Observation: if not self._is_enabled: return obs for receiver in self._receivers: obs_copy = copy.deepcopy(obs) target = f'{receiver}/{obs_copy.get("target")}' obs_copy.set('nextReceiver', 0) obs_copy.set('receivers', [target]) self.logger.debug(f'Publishing observation ' f'"{obs_copy.get("name")}" of target ' f'"{obs_copy.get("target")}" to "{target}"') header = Observation.get_header() payload = obs_copy.data self.publish(target, header, payload) return obs