def parse(input_request): """ Getting Logger Object for Executor Module """ logg = PyLogs(__name__).get_pylogger() logg.info('Inside parsing Method') # print('Inside parsing Method') user_info = input_request.input_message.user_info input_message_string = input_request.input_message.input_message_bytes.decode( 'UTF-8') output_message = input_request.input_message.output_message socket_context = input_request.input_message.socket_context logger = input_request.input_message.logger ''' Decoding input message string in to Python object ''' logg.info('Going to decode input request') decoded_input_message = Decoder().decode_json(input_message_string, socket_context, logg) ''' Creation Session Object for next module(Authenticator) ''' logg.info('Going to create session for Authenticator') req_parser_session = Session(user_info, decoded_input_message, output_message, socket_context, logger) ''' Prepare Authenticator Request Message and Call Authenticator ''' logg.info('Calling Authenticator') auth_message = InputMessage(req_parser_session) auth_request_obj = Request(auth_message) auth_router_obj = Router() auth_router_obj.execute(auth_request_obj)
def parse(self, req_res_parser_obj): # print('In Parse method of BasicResponseParser Class') logg = PyLogs(__name__).get_pylogger() logg.info('In Parse method of BasicResponseParser Class') user_info = None input_request = None # response_message = None # socket_context = None logger = None if req_res_parser_obj.status == "ERROR": # print('Response is having error, processing error response') logg.info('Response is having error, processing error response') response_message = self.process_error(req_res_parser_obj, logg) socket_context = req_res_parser_obj.socket_context else: # print('Successful request processing, in response parser for response parsing') logg.info( 'Successful request processing, in response parser for response parsing' ) user_info = req_res_parser_obj.input_message.user_info input_request = req_res_parser_obj.input_message.input_message response_message = req_res_parser_obj.input_message.output_message socket_context = req_res_parser_obj.input_message.socket_context logger = req_res_parser_obj.input_message.logger # print('Connection details are as: ', socket_context) # ''' Fetching Socket Context From Globally Maintained List ''' # # socketcontext = loadparamfile.GlobalData.store_socket[1] # socketcontext = loadparamfile.GlobalData.store_socket[message_id] ''' Prepare your Response Object and Pass it to the given below mention Object for time being it is Decoded Request Object ''' # print('Encoding JSON String into Request Parser') logg.info('Encoding JSON String into Request Parser') encoded_output_message = Encoder().encode_pyobject(response_message) print(encoded_output_message) ''' Encoding Back the Message in Bytes ''' message_bytes = bytes(encoded_output_message, 'UTF-8') ''' Preparing Session object for response dispatcher ''' res_dispatcher_session = Session(user_info, input_request, message_bytes, socket_context, logger) # output_message = OutputMessage(socket_context, message_bytes) output_message = OutputMessage(res_dispatcher_session) req_tcp_res_disp_obj = Request(output_message) router_tcp_res_disp_obj = Router() router_tcp_res_disp_obj.execute(req_tcp_res_disp_obj)
def send_reply(req_tcp_res_disp_obj): # print('in Send_Reply in BasicTcpResponseDispatcher ') logg = PyLogs(__name__).get_pylogger() logg.info('in Send_Reply in BasicTcpResponseDispatcher ') try: socket_context = req_tcp_res_disp_obj.output_response.socket_context except (AttributeError, TypeError) as err_message: logg.error('Error while fetching socket context') print( 'Res Dispatcher: Executor: BasicTcpResponseDispatcher: send_reply: check 1' ) print( 'Connection Error, Error Code {} and Error Message: {}'.format( err_message[0], err_message[1])) sys.exit() try: socket_context.sendall( req_tcp_res_disp_obj.output_response.output_message) except socket.error as err_message: logg.error('Error while sending data to client') print( 'Res Dispatcher: Executor: BasicTcpResponseDispatcher: send_reply: check 2' ) print('Data to client failed, Error Code {} and Error Message: {}'. format(err_message[0], err_message[1])) sys.exit() print('Response Sent to Client is Successful') logg.info('Response Sent to Client is Successful') try: socket_context.close() except socket.error as err_message: logg.error('Socket close error') print( 'Res Dispatcher: Executor: BasicTcpResponseDispatcher: send_reply: check 3' ) print('Socket Closing Error, Error Code {} and Error Message: {}'. format(err_message[0], err_message[1])) sys.exit()
def prepare_msg(auth_req_obj): logg = PyLogs(__name__).get_pylogger() logg.info('Inside prepare message method') user_info = auth_req_obj.input_message.user_info input_message_data = auth_req_obj.input_message.input_message_data output_message = auth_req_obj.input_message.output_message socket_context = auth_req_obj.input_message.socket_context logger = auth_req_obj.input_message.logger ''' Creation Session Object for next module(Request Router) ''' logg.info('Creating Session object for Router Module') req_router_session = Session(user_info, input_message_data, output_message, socket_context, logger) ''' Prepare Router request and Call Service Router Module ''' logg.info('Calling Router Module') auth_message = InputMessage(req_router_session) req_router_request_obj = Request(auth_message) req_router_router_obj = Router() req_router_router_obj.execute(req_router_request_obj)
def process_exception(self): # print('Inside process_exception method of SerGadException') logg = PyLogs(__name__).get_pylogger() logg.info('Inside process_exception method of SerGadException') self.trigger_response(logg)
def process(self, req_change_device_state_obj): print('Inside Change Device State Process') logg = PyLogs(__name__).get_pylogger() logg.info('Inside Change Device State Process') # input_request = None # input_request_id = None # user_info = None # input_message_data = None # output_message = None # socket_context = None # logger = None # noinspection PyBroadException user_info = req_change_device_state_obj.input_message.user_info input_message_data = req_change_device_state_obj.input_message.input_message_data output_message = req_change_device_state_obj.input_message.output_message socket_context = req_change_device_state_obj.input_message.socket_context logger = req_change_device_state_obj.input_message.logger # try: # pass # except AttributeError as ser_cds_error_msg: # error_source = "process method in Executor of CDS Service" # error_type = ser_cds_error_msg.__class__.__name__ # error_msg = "AttributeError while Fetching Input Data or MetaData" # process_failure(error_source, error_type, error_msg) # except: # error_source = "process method in Executor of CDS Service" # error_type = sys.exc_info()[0].__name__ # error_msg = "Unexpected Error while Fetching Input Data or MetaData" # process_failure(error_source, error_type, error_msg) # print('message from Client in CDS Service:', input_message_data) """ Fetch Device Id & Desired State change request from Input Request and Trigger MicroController Module for further Processing """ device_name = None device_id = None desired_device_state = None # noinspection PyBroadException try: logg.info( 'fetching device name, device id and requested device state from input message' ) device_name = self.get_device_name(input_message_data) device_id = self.get_device_id(input_message_data) desired_device_state = self.get_desired_device_state( input_message_data) logg.info(device_name + ':' + device_id + ':' + desired_device_state) except AttributeError as ser_CDS_error_msg: error_source = "process method in Executor of CDS Service" error_type = ser_CDS_error_msg.__class__.__name__ error_msg = "AttributeError while Fetching Device Info" logg.error('Error in fetching device details, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except: error_source = "process method in Executor of CDS Service" error_type = sys.exc_info()[0].__name__ error_msg = "Unexpected Error while Fetching Device Info" logg.error('Error in fetching device details, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) # print('desired_device_state is : {}'.format(desired_device_state)) # print('device_name is : {}'.format(device_name)) # print('device_id is : {}'.format(device_id)) ''' Calling MicroController Module for Actual Processing ''' mc_response = None # noinspection PyBroadException try: logg.info('Calling Micro-controller Module') mc_request = mc_comm.Request(device_name, device_id, desired_device_state, socket_context) mc_router = mc_comm.Router() mc_response = mc_router.execute(mc_request) except: error_source = "process method in Executor of CDS Service" error_type = sys.exc_info()[0].__name__ error_msg = "Error while MicroController Processing" logg.error('Error while MicroController Processing, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) # print(mc_response) logg.info(mc_response) """ This mc_response is the returned response object of MicroController Module. it will have Resonse message Positive or Negative. Further Processing will be depends on Response Message Preparing Positive Response from Change Device State Module Assuming MicroController Processing is successful """ # new_session = None # noinspection PyBroadException try: logg.info( 'Preparing positive response after Micro-controller module processing' ) output_response = SerOneOutMsg() output_response.outputDictString["comm_msg"][ "response_stat"] = "OK" print(output_response.outputDictString) output_message = output_response.outputDictString # new_session = req_change_device_state_obj # new_session.session.message.message_data = output_response.outputDictString # new_session.session.message.message_id = input_request_id except AttributeError as ser_CDS_error_msg: error_source = "process method in Executor of CDS Service" error_type = ser_CDS_error_msg.__class__.__name__ error_msg = "AttributeError while Generating Service Response" logg.error('Error while Generating Service Response, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except: error_source = "process method in Executor of CDS Service" error_type = sys.exc_info()[0].__name__ error_msg = "Unexpected Error while Generating Service Response" logg.error('Error while Generating Service Response, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) # print(new_session.session.message.message_data) ''' Generation Session for Response Parser Module ''' logg.info('Generating session for request parser module') res_parser_session = Session(user_info, input_message_data, output_message, socket_context, logger) ''' Calling Response Parser with Service Positive Response ''' # noinspection PyBroadException try: logg.info('Calling request parser module with positive result') req_res_parser_obj = resp_comm.Request(res_parser_session, "OK") router_res_parser_obj = resp_comm.Router() router_res_parser_obj.execute(req_res_parser_obj) except: error_source = "process method in Executor of CDS Service" error_type = sys.exc_info()[0].__name__ error_msg = "Error while Calling Response Parser" logg.error('Error whie calling resonse parser, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg)
def process(self, req_getalldevice_msg_obj): # print('in GetAllDevices Process') # logg = req_getalldevice_msg_obj.input_message.logger logg = PyLogs(__name__).get_pylogger() logg.info('Inside GetAllDevices Process method') # user_info = None # input_message_data = None # output_message = None # socket_context = None # logger = None # noinspection PyBroadException # try: user_info = req_getalldevice_msg_obj.input_message.user_info input_message_data = req_getalldevice_msg_obj.input_message.input_message_data output_message = req_getalldevice_msg_obj.input_message.output_message socket_context = req_getalldevice_msg_obj.input_message.socket_context logger = req_getalldevice_msg_obj.input_message.logger # except AttributeError as ser_gad_error_msg: # error_source = "process method in Executor of GAD Service" # error_type = ser_gad_error_msg.__class__.__name__ # error_msg = "AttributeError while Fetching Input Data or MetaData" # process_failure(error_source, error_type, error_msg) # except: # error_source = "process method in Executor of GAD Service" # error_type = sys.exc_info()[0].__name__ # error_msg = "Unexpected Error while Fetching Input Data or MetaData" # process_failure(error_source, error_type, error_msg) # print('message from Client in Service:', input_message_data) logg.info('message from Client in Service:' + input_message_data) ''' Fetching All Devices from All Device Details Params file ''' output_response = SerOneOutMsg() devices_details = None # noinspection PyBroadException try: logg.info('Calling get all device method') devices_details = self.get_all_devices() except ValueError as ser_gad_error_msg: error_source = "process method in Executor of GAD Service" error_type = ser_gad_error_msg.__class__.__name__ error_msg = "ValueError, params file load error" logg.error('Param file load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except TypeError as ser_gad_error_msg: error_source = "process method in Executor of GAD Service" error_type = ser_gad_error_msg.__class__.__name__ error_msg = "TypeError, param file load error" logg.error('Param file load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except EOFError as ser_gad_error_msg: error_source = "process method in Executor of GAD Service" error_type = ser_gad_error_msg.__class__.__name__ error_msg = "EOFError, params file is empty" logg.error('Param file load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except IOError as ser_gad_error_msg: error_source = "process method in Executor of GAD Service" error_type = ser_gad_error_msg.__class__.__name__ error_msg = "IOError, loding param file error" logg.error('Param file load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except AttributeError as ser_gad_error_msg: error_source = "process method in Executor of GAD Service" error_type = ser_gad_error_msg.__class__.__name__ error_msg = "AttributeError while Fetching Input Data or MetaData" logg.error('Param file load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except: error_source = "process method in Executor of GAD Service" error_type = sys.exc_info()[0].__name__ error_msg = "Unexpected Error while Fetching Input Data or MetaData" logg.error('Param file load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) logg.info(devices_details["home_info"]) # print(devices_details["home_info"]) ''' Generating Response for Service Get All Devices ''' # new_session = None # noinspection PyBroadException try: output_response.outputDictString["comm_msg"][ "response_stat"] = "OK" output_response.outputDictString["comm_msg"][ "home_info"] = devices_details["home_info"] # print(output_response.outputDictString) logg.info(output_response.outputDictString) output_message = output_response.outputDictString except AttributeError as ser_gad_error_msg: error_source = "process method in Executor of GAD Service" error_type = ser_gad_error_msg.__class__.__name__ error_msg = "AttributeError while Generating Service Response" logg.error('Error while generating service response, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except: error_source = "process method in Executor of GAD Service" error_type = sys.exc_info()[0].__name__ error_msg = "Unexpected Error while Generating Service Response" logg.error('Error while generating service response, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) print(output_message) logg.info(output_message) ''' Generation Session for Response Parser Module ''' res_parser_session = Session(user_info, input_message_data, output_message, socket_context, logger) ''' Calling Response Parser with Positive Service response ''' # noinspection PyBroadException try: # req_res_parser_obj = Request(new_session, "OK") logg.info('Calling response parser for sending outputs') req_res_parser_obj = Request(res_parser_session, "OK") router_res_parser_obj = Router() router_res_parser_obj.execute(req_res_parser_obj) except: error_source = "process method in Executor of GAD Service" error_type = sys.exc_info()[0].__name__ error_msg = "Error while Calling Response Parser" logg.error('Error while Calling Response Parser, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg)
def process(mc_request, mc_response): # print('Inside process Method of Micro-controller Interface Class') logg = PyLogs(__name__).get_pylogger() logg.info('Inside process Method of Micro-controller Interface Class') socket_context = mc_request.socket_context mc_param_obj = MicroParameters() ref_device_param = None ref_device_state = None ref_device_type_id2states = None ref_mc_param = None ref_mc_signal = None device_signal = None device_state_signal = None module_signal = None # noinspection PyBroadException try: """ Loading Device Parameters """ logg.info('Loading Device Params') device_param_file = mc_param_obj.get_device_params_file_name() ref_device_param = loadparamfile.LoadParamsFile(device_param_file) print(ref_device_param) """ Loading Device Type States Parameters """ logg.info('Loading Device Type States Parameters') device_state_file = mc_param_obj.get_device_states_file_name() ref_device_state = loadparamfile.LoadParamsFile(device_state_file) print(ref_device_state) """ Loading Device Type Parameters """ logg.info('Loading Device Type Parameters') device_type_id2states_file = mc_param_obj.get_device_type_id2states_file_name() ref_device_type_id2states = loadparamfile.LoadParamsFile(device_type_id2states_file) print(ref_device_type_id2states) """ Loading MicroController Parameters """ logg.info('Loading MicroController Parameters') mc_param_file = mc_param_obj.get_microcontroller_params_file_name() ref_mc_param = loadparamfile.LoadParamsFile(mc_param_file) print(ref_mc_param) """ Loading MicroController Hardware Signals Parameters """ logg.info('Loading MicroController Hardware Signals Parameters') mc_signal_file = mc_param_obj.get_microcontroller_signals_file_name() ref_mc_signal = loadparamfile.LoadParamsFile(mc_signal_file) print(ref_mc_signal) except ValueError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "ValueError, params file load error" logg.error('Param files load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except TypeError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "TypeError, param file load error" logg.error('Param files load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except EOFError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "EOFError, params file is empty" logg.error('Param files load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except IOError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "IOError, loding param file error" logg.error('Param files load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except AttributeError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "AttributeError while Fetching param files" logg.error('Param files load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except: error_source = "process method in Executor of MicroController" error_type = sys.exc_info()[0].__name__ error_msg = "Unexpected Error while Fetching params files" logg.error('Param files load error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) # noinspection PyBroadException try: """ Fetching Device Type Id, Module Id(MicroController Id) and Device Signal Id for Further Processing """ device_type_id = mc_param_obj.get_device_type_id(mc_request.device_id, ref_device_param) module_id = mc_param_obj.get_micontroller_id(mc_request.device_id, ref_device_param) print('data is: {} : {} '.format(device_type_id, module_id)) """ Now Fetching Device Type States IDs and Signal IDs for Device, Device States and MicrController """ device_signal_id = mc_param_obj.get_device_signal_id(mc_request.device_id, ref_device_param) device_type_state_id = mc_param_obj.get_device_type_state_id(device_type_id, ref_device_type_id2states) device_state_signal_id = mc_param_obj.get_devcie_state_signal_id(device_type_state_id, mc_request.desired_device_state, ref_device_state) module_signal_id = mc_param_obj.get_microcontroller_signal_id(module_id, ref_mc_param) """ Finally Fetching Hardware Triggering MicrController Signal for device, Device state and MicroController """ device_signal = mc_param_obj.get_hex_device_id_signal(device_signal_id, ref_mc_signal) device_state_signal = mc_param_obj.get_hex_device_state_signal(device_state_signal_id, ref_mc_signal) module_signal = mc_param_obj.get_hex_microcontroller_signal(module_signal_id, ref_mc_signal) except ValueError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "ValueError, params file load error" logg.error('Param files fetch value error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except TypeError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ logg.error('Param files fetch value error, Reason: ' + error_type) error_msg = "TypeError, param file load error" process_failure(error_source, error_type, error_msg, socket_context, logg) except KeyError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "KeyError, param file load error" logg.error('Param files fetch value error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except IndexError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "IndexError, param file load error" logg.error('Param files fetch value error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except EOFError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "EOFError, params file is empty" logg.error('Param files fetch value error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except IOError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "IOError, loding param file error" logg.error('Param files fetch value error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except AttributeError as mc_error_msg: error_source = "process method in Executor of MicroController" error_type = mc_error_msg.__class__.__name__ error_msg = "AttributeError while Fetching param files" logg.error('Param files fetch value error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) except: error_source = "process method in Executor of MicroController" error_type = sys.exc_info()[0].__name__ error_msg = "Unexpected Error while Fetching params files" logg.error('Param files fetch value error, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) """ Passing These Control Signals To MicroController Module for Hardware Processing(Calling Send data Function to Send signal to MicroController) Write Response of MicroController in Response Class for Further Decision Making """ # noinspection PyBroadException try: logg.info('Passing Control Signals to Micro-controller') mc_obj = Microcontroller(device_signal, device_state_signal, module_signal) mc_obj.send_data() except: error_source = "process method in Executor of MicroController" error_type = sys.exc_info()[0].__name__ error_msg = "Unexpected Error while Hardware Processing" logg.error('Error while Hradware Processing, Reason: ' + error_type) process_failure(error_source, error_type, error_msg, socket_context, logg) """ For Time being HardCoded Positive Acknowledgement have been sent back. At time of actual Processing, please change Accordingly """ logg.info('Returning Positivve Response from Mixro-Controller Module to Service') mc_response.response_message = "OK" return mc_response
def route(self, req_router_request_obj): # print('Inside Request Router route method') logg = PyLogs(__name__).get_pylogger() logg.info('Inside route method') self.identify_and_call_service(req_router_request_obj, logg)