def _image_to_bytes(self, image): if type(image) is not np.ndarray: raise InvalidArgumentError( f'invalid image object type: {type(image)}') ret, enc_img = cv2.imencode(self._format, image, self._encode_params) if not ret: raise InvalidArgumentError('image encoding failed') return enc_img.tobytes()
def __init__(self, params): self._queue = None self._topic = params.get('topic') if self._topic is None or not isinstance(self._topic, str): raise InvalidArgumentError() timeout_ms = params.get('receive_timeout_ms', inf) self._timeout = timeout_ms / 1000.0 if timeout_ms != inf else None
def _image_from_bytes(self, byte_data): nd_array = np.frombuffer(byte_data, dtype='uint8') ret = cv2.imdecode(nd_array, self._decode_flags) if ret is None: raise InvalidArgumentError( 'not a byte sequence representing an image') return ret
def publish(self, msg): if type(msg) != bytes: logger.error("MqttWriter: msg must be bytes") raise InvalidArgumentError("MqttWriter: msg must be bytes") return self._mqttc.publish(self.topic, payload=msg, qos=self.qos, retain=self.retain)
def _to_qos(params): qos = params.get('qos') if qos is not None: if qos not in QOS_MAP.values(): raise InvalidArgumentError('Invalid QoS level') return qos consistency = params.get('consistency') assert consistency in QOS_MAP return QOS_MAP[consistency]
def __init__(self, params): self._queue = None self._topic = params.get('topic') if self._topic is None or not isinstance(self._topic, str): raise InvalidArgumentError() self._on_message = None self._on_failure = None self._reader_executor = None self._future = None self._closed = True
def _get_broker(params): if 'brokers' not in params: logger.error("You must specify one broker.") raise InvalidArgumentError("You must specify one broker.") brokers = params["brokers"] if isinstance(brokers, list): if len(brokers) > 1: logger.error("only one broker can be specified") raise InvalidArgumentError("only one broker can be specified") elif len(brokers) == 0: logger.error("You must specify one broker.") raise InvalidArgumentError("You must specify one broker.") host_port = brokers[0].split(':', 1) else: host_port = brokers.split(':', 1) if len(host_port) == 2: return host_port[0], int(host_port[1]) else: return host_port[0], _get_default_port(params)
def publish(self, msg): logger.debug("MqttWriter:publish") if type(msg) != bytes: logger.error("MqttWriter: msg must be bytes") raise InvalidArgumentError("MqttWriter: msg must be bytes") msginfo = self._mqttc.publish(self.topic, payload=msg, qos=self.qos, retain=self.retain) logger.debug(f"publish => rc={msginfo.rc} mid={msginfo.mid}") if msginfo.rc != MQTT_ERR_QUEUE_SIZE: msginfo.wait_for_publish() elif msginfo.rc == MQTT_ERR_NO_CONN: raise ConnectionError('client is not currently connected') return msginfo
def __init__(self, params): self._params = _translate_tls_params(params) self._params.update(params) _replace_ssl_params(self._params) logger.debug(self._params) try: self._mqttc = _create_mqtt_client(self._params) except ValueError as ex: raise InvalidArgumentError(ex) self._mqttc.on_connect = self._on_connect self.qos = _to_qos(self._params) self.connection_timeout = self._params.get('connection_timeout', 10) self.keepalive = self._params.get('keepalive', 60) self.host, self.port = _get_broker(self._params) logger.debug(f'broker={self.host} port={self.port}') self.protocol = _to_protocol(self._params.get('protocol')), self._connection_result = None self._conn_cond = Condition()
def _translate_tls_params(params): tls = params.get('tls') if tls is None: return {} elif type(tls) is bool: return {'tls_set': {}} elif type(tls) is dict: tls_set = dict([ (key, tls[key]) for key in ['ca_certs', 'certfile', 'keyfile', 'ciphers'] if key in tls ]) mqtt_params = {'tls_set': tls_set} if 'check_hostname' in tls: mqtt_params['tls_insecure_set'] = { 'value': not (tls['check_hostname']), } return mqtt_params else: logger.error("tls: must be bool or associative array") raise InvalidArgumentError("tls: must be bool or associative array")
def _get_transport(params): transport = params.get('transport', 'tcp') if transport not in ['tcp', 'websockets']: raise InvalidArgumentError(f'transport: invalid value: {transport}') return transport
def _to_protocol(protocol): if protocol not in PROTOCOL_MAP: raise InvalidArgumentError(f'protocol: invalid value: {protocol}') return PROTOCOL_MAP[protocol]
def __init__(self, params): self._queue = None self._topic = params.get('topic') if self._topic is None or not isinstance(self._topic, str): raise InvalidArgumentError()