def on_message(self, message):
try:<EOL><INDENT>message = json.loads(message)<EOL><DEDENT>except ValueError:<EOL><INDENT>try:<EOL><INDENT>self.write_message(json.dumps({<EOL>'<STR_LIT>': '<STR_LIT:error>',<EOL>'<STR_LIT:data>': {<EOL>'<STR_LIT:status>': '<STR_LIT>',<EOL>'<STR_LIT:message>': '<STR_LIT>',<EOL>},<EOL>}))<EOL><DEDENT>except tornado.websocket.WebSocketClosedError:<EOL><INDENT>pass<EOL><DEDENT>return<EOL><DEDENT>if '<STR_LIT>' not in message or '<STR_LIT:data>' not in message:<EOL><INDENT>try:<EOL><INDENT>self.write_message(json.dumps({<EOL>'<STR_LIT>': '<STR_LIT:error>',<EOL>'<STR_LIT:data>': {<EOL>'<STR_LIT:status>': '<STR_LIT>',<EOL>'<STR_LIT:message>': '<STR_LIT>',<EOL>},<EOL>}))<EOL><DEDENT>except tornado.websocket.WebSocketClosedError:<EOL><INDENT>pass<EOL><DEDENT>return<EOL><DEDENT>msg_type = message['<STR_LIT>']<EOL>if msg_type == '<STR_LIT>':<EOL><INDENT>for property_name, property_value in message['<STR_LIT:data>'].items():<EOL><INDENT>try:<EOL><INDENT>self.thing.set_property(property_name, property_value)<EOL><DEDENT>except PropertyError as e:<EOL><INDENT>self.write_message(json.dumps({<EOL>'<STR_LIT>': '<STR_LIT:error>',<EOL>'<STR_LIT:data>': {<EOL>'<STR_LIT:status>': '<STR_LIT>',<EOL>'<STR_LIT:message>': str(e),<EOL>},<EOL>}))<EOL><DEDENT><DEDENT><DEDENT>elif msg_type == '<STR_LIT>':<EOL><INDENT>for action_name, action_params in message['<STR_LIT:data>'].items():<EOL><INDENT>input_ = None<EOL>if '<STR_LIT:input>' in action_params:<EOL><INDENT>input_ = action_params['<STR_LIT:input>']<EOL><DEDENT>action = self.thing.perform_action(action_name, input_)<EOL>if action:<EOL><INDENT>tornado.ioloop.IOLoop.current().spawn_callback(<EOL>perform_action,<EOL>action,<EOL>)<EOL><DEDENT>else:<EOL><INDENT>self.write_message(json.dumps({<EOL>'<STR_LIT>': '<STR_LIT:error>',<EOL>'<STR_LIT:data>': {<EOL>'<STR_LIT:status>': '<STR_LIT>',<EOL>'<STR_LIT:message>': '<STR_LIT>',<EOL>'<STR_LIT>': message,<EOL>},<EOL>}))<EOL><DEDENT><DEDENT><DEDENT>elif msg_type == '<STR_LIT>':<EOL><INDENT>for event_name in message['<STR_LIT:data>'].keys():<EOL><INDENT>self.thing.add_event_subscriber(event_name, self)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>self.write_message(json.dumps({<EOL>'<STR_LIT>': '<STR_LIT:error>',<EOL>'<STR_LIT:data>': {<EOL>'<STR_LIT:status>': '<STR_LIT>',<EOL>'<STR_LIT:message>': '<STR_LIT>' + msg_type,<EOL>'<STR_LIT>': message,<EOL>},<EOL>}))<EOL><DEDENT>except tornado.websocket.WebSocketClosedError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>
Handle an incoming message.

message -- message to handle