def __fill_request(self, request): if StringUtil.is_empty(request.get_id()): request.set_id(str(MqttConnection.inc_and_get_message_id())) # If publish reply, version is not request if isinstance(request, BaseRequest) and StringUtil.is_empty(request.get_version()): request.set_version(Profile.VERSION) if StringUtil.is_empty(request.get_product_key()) and StringUtil.is_empty(request.get_device_key()): request.set_product_key(self.__profile.get_product_key()) request.set_device_key(self.__profile.get_device_key())
def build(self): request = self.create_request_instance() if StringUtil.is_not_empty(self._product_key): request.set_product_key(self._product_key) if StringUtil.is_not_empty(self._device_key): request.set_device_key(self._device_key) request.set_method(self.create_method()) request.set_payload(self.__payload) return request
def __init__(self, product_key, product_secret, device_key, device_secret): self.__product_key = None self.__product_secret = None self.__device_key = None self.__device_secret = None if StringUtil.is_not_empty(product_key): self.__product_key = product_key.strip() if StringUtil.is_not_empty(product_secret): self.__product_secret = product_secret.strip() if StringUtil.is_not_empty(device_key): self.__device_key = device_key.strip() if StringUtil.is_not_empty(device_secret): self.__device_secret = device_secret.strip()
def get_secure_mode(product_key, product_secret, device_key, device_secret): if StringUtil.is_not_empty(device_secret): return SecureMode(VIA_DEVICE_SECRET, device_key, device_secret) if StringUtil.is_not_empty(product_secret): if INTEGRATION_DK == device_key: # integration use product key as client id return SecureMode(VIA_PRODUCT_SECRET_FOR_INTEGRATION, product_key, product_secret) return SecureMode(VIA_PRODUCT_SECRET, device_key, product_secret) raise EnvisionException( "<device_secret> or <product_secret> should be provided")
def create_reg_info_dict(product_key, reg_option): param = dict() param['productKey'] = product_key if reg_option.device_attributes: param['deviceAttributes'] = reg_option.device_attributes if StringUtil.is_not_empty(reg_option.device_key): param['deviceKey'] = reg_option.device_key if reg_option.device_name is not None: param['deviceName'] = reg_option.device_name.encode() if StringUtil.is_not_empty(reg_option.device_desc): param['deviceDesc'] = reg_option.device_desc if StringUtil.is_not_empty(reg_option.timezone): param['timezone'] = reg_option.timezone return param
def build(self): reply = self.create_reply_instance() if self.__code is not None: reply.set_code(self.__code) else: reply.set_code(200) if StringUtil.is_not_empty(self.__message): reply.set_message(self.__message) if StringUtil.is_not_empty(self.__product_key): reply.set_product_key(self.__product_key.strip()) if StringUtil.is_not_empty(self.__device_key): reply.set_device_key(self.__device_key.strip()) reply.set_data(self.create_data()) reply.set_qos(self.__qos) return reply
def sign(cls, app_secret, params, sign_method): if StringUtil.is_empty(sign_method): raise EnvisionException('sign method is empty') content = SignUtil.__make_string(app_secret, params) if sign_method == cls.SHA256: return cls.sha256_sign(content) if sign_method == cls.MD5: return cls.md5_sign(content) if sign_method == cls.SHA1: return cls.sha1_sign(content) else: raise EnvisionException('invalid sign method')
def check_not_empty(value, field_name): if value is None: raise EnvisionException( "parameter check error: " + field_name + " is mandatory", CODE_ERROR_MISSING_ARGS.get_error_code()) if isinstance(value, str) and StringUtil.is_empty(value): raise EnvisionException( "parameter check error: " + field_name + " is mandatory", CODE_ERROR_MISSING_ARGS.get_error_code()) if isinstance(value, list) and not value: raise EnvisionException( "parameter check error: " + field_name + " is mandatory", CODE_ERROR_MISSING_ARGS.get_error_code()) if isinstance(value, dict) and not value: raise EnvisionException( "parameter check error: " + field_name + " is mandatory", CODE_ERROR_MISSING_ARGS.get_error_code())