def action_progress_update(self, request_id, message): """ Update message for action (method) request in Cloud """ cmd = tr50.create_mailbox_update(request_id, message) message = defs.OutMessage(cmd, "Update Action Progress " "{} \"{}\"".format(request_id, message)) return self.send(message)
def handle_action(self, action_request): """ Handle action execution requests from Cloud """ result_code = -1 result_args = {"mail_id":action_request.request_id} action_result = None action_failed = False try: # Execute callback action_result = self.callbacks.execute_action(action_request) except Exception as error: # Error with action execution. Might not have been registered. action_failed = True self.logger.error("Action %s execution failed", action_request.name) self.logger.error(".... %s", str(error)) result_code = constants.STATUS_FAILURE result_args["error_message"] = "ERROR: {}".format(str(error)) if action_request.name not in self.callbacks: result_code = constants.STATUS_NOT_FOUND else: self.logger.exception("Exception:") # Action execution did not raise an error if not action_failed: # Handle returning a tuple or just a status code if action_result.__class__.__name__ == "tuple": result_code = action_result[0] if len(action_result) >= 2: result_args["error_message"] = str(action_result[1]) if len(action_result) >= 3: result_args["params"] = action_result[2] else: result_code = action_result if not is_valid_status(result_code): # Returned 'status' is not a valid status error_string = ("Invalid return status: " + str(result_code)) self.logger.error(error_string) result_code = constants.STATUS_BAD_PARAMETER result_args["error_message"] = "ERROR: " + error_string # Return status to Cloud # Check for invoked status. If so, return mail box update not # ack. Ack is the final notification. This breaks triggers # etc because it doesn't update the status. result_args["error_code"] = tr50.translate_error_code(result_code) if result_code == constants.STATUS_INVOKED: update_args = {"mail_id":action_request.request_id} update_args["message"] = "Invoked" mailbox_ack = tr50.create_mailbox_update(**update_args) else: mailbox_ack = tr50.create_mailbox_ack(**result_args) message_desc = "Action Complete \"{}\"".format(action_request.name) message_desc += " result : {}({})".format(result_code, status_string(result_code)) if result_args.get("error_message"): message_desc += " \"{}\"".format(result_args["error_message"]) if result_args.get("params"): message_desc += " \"{}\"".format(str(result_args["params"])) message = defs.OutMessage(mailbox_ack, message_desc) status = self.send(message) return status