From f1eedfa74f31b713cfb876e4748c540de8e33bd4 Mon Sep 17 00:00:00 2001 From: Nirav Shah Date: Thu, 13 May 2021 14:36:24 -0400 Subject: [PATCH] Code review comments resolved. --- src/controller/python/chip-device-ctrl.py | 25 ++++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index 1ec0b141dcd6e5..da29208feebd93 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -44,6 +44,7 @@ from chip.ChipBleUtility import FAKE_CONN_OBJ_VALUE from chip.setup_payload import SetupPayload from xmlrpc.server import SimpleXMLRPCServer + from enum import Enum from typing import Any, Dict,Optional # Extend sys.path with one or more directories, relative to the location of the @@ -73,11 +74,17 @@ elif sys.platform.startswith('linux'): from chip.ChipBluezMgr import BluezManager as BleManager -# The exceptions for CHIP Device Controller CLI class StatusCodeEnum(Enum): SUCCESS = 0 FAILED = 1 +class RPCResponseKeyEnum(Enum): + STATUS = "status" + RESULT = "result" + ERROR = "error" + +# The exceptions for CHIP Device Controller CLI + class ChipDevCtrlException(exceptions.ChipStackException): pass @@ -734,7 +741,7 @@ def echo_alive(message): print(message) return message -def resolve(fabric_id: int, node_id: int) -> str: +def resolve(fabric_id: int, node_id: int) -> Dict[str, Any]: try: __check_supported_os() err = device_manager.devCtrl.ResolveNode(fabric_id, node_id) @@ -771,19 +778,13 @@ def start_rpc_server(): print("\nKeyboard interrupt received, exiting.") sys.exit(0) -def __get_response_dict(status: StatusCodeEnum, result: Optional[Dict[Any, Any]] = None, error:Optional[str] = None) -> Dict [Any, Any]: - if error is not None: - return { "status" : status.value, "error" :f'Unable to connect due to exception {error}' } - else: - if result is not None: - return { "status" : status.value, "result": result} - else: - return { "status" : status.value} +def __get_response_dict(status: StatusCodeEnum, result: Optional[Dict[Any, Any]] = None, error:Optional[str] = None) -> Dict [str, Any]: + return { RPCResponseKeyEnum.STATUS.value : status.value, RPCResponseKeyEnum.RESULT.value : result, RPCResponseKeyEnum.ERROR.value : error } def __check_supported_os()-> bool: - if platform.system() == 'Darwin': + if platform.system().lower() == 'darwin': raise Exception(platform.system() + " not supported") - elif sys.platform.startswith('linux'): + elif sys.platform.lower().startswith('linux'): return True raise Exception("OS Not Supported")