|
| 1 | +from .generic import GenericFileTransfer |
| 2 | +from .exceptions import AuthenticationFailure |
| 3 | +from .exceptions import ConnectionFailure |
| 4 | +#from .exceptions import TransferFailure |
| 5 | + |
| 6 | +from pathlib import Path |
| 7 | +import paho.mqtt.publish as publish |
| 8 | +from paho.mqtt import MQTTException |
| 9 | +import ssl |
| 10 | +import io |
| 11 | +import socket |
| 12 | +import time |
| 13 | +import logging |
| 14 | + |
| 15 | +logger = logging.getLogger('indi_allsky') |
| 16 | + |
| 17 | + |
| 18 | +class paho_mqtt(GenericFileTransfer): |
| 19 | + def __init__(self, *args, **kwargs): |
| 20 | + super(paho_mqtt, self).__init__(*args, **kwargs) |
| 21 | + |
| 22 | + self._port = 1883 |
| 23 | + |
| 24 | + self.mq_transport = None |
| 25 | + self.mq_hostname = None |
| 26 | + self.mq_auth = None |
| 27 | + self.mq_tls = None |
| 28 | + |
| 29 | + |
| 30 | + def connect(self, *args, **kwargs): |
| 31 | + super(paho_mqtt, self).connect(*args, **kwargs) |
| 32 | + |
| 33 | + transport = kwargs['transport'] |
| 34 | + hostname = kwargs['hostname'] |
| 35 | + username = kwargs['username'] |
| 36 | + password = kwargs.get('password') if kwargs.get('password') else None |
| 37 | + tls = kwargs.get('tls') |
| 38 | + cert_bypass = kwargs.get('cert_bypass') |
| 39 | + |
| 40 | + |
| 41 | + self.mq_transport = transport |
| 42 | + self.mq_hostname = hostname |
| 43 | + |
| 44 | + if tls: |
| 45 | + self.mq_tls = { |
| 46 | + 'ca_certs' : '/etc/ssl/certs/ca-certificates.crt', |
| 47 | + 'cert_reqs' : ssl.CERT_REQUIRED, |
| 48 | + 'insecure' : False, |
| 49 | + } |
| 50 | + |
| 51 | + if cert_bypass: |
| 52 | + self.mq_tls['cert_reqs'] = ssl.CERT_NONE |
| 53 | + self.mq_tls['insecure'] = True |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + if username: |
| 58 | + self.mq_auth = { |
| 59 | + 'username' : username, |
| 60 | + 'password' : password, |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + def close(self): |
| 65 | + super(paho_mqtt, self).close() |
| 66 | + |
| 67 | + |
| 68 | + def put(self, *args, **kwargs): |
| 69 | + super(paho_mqtt, self).put(*args, **kwargs) |
| 70 | + |
| 71 | + local_file = kwargs['local_file'] |
| 72 | + base_topic = kwargs['base_topic'] |
| 73 | + qos = kwargs['qos'] |
| 74 | + mq_data = kwargs['mq_data'] |
| 75 | + |
| 76 | + local_file_p = Path(local_file) |
| 77 | + |
| 78 | + |
| 79 | + message_list = list() |
| 80 | + |
| 81 | + # publish image |
| 82 | + with io.open(local_file_p, 'rb') as f_localfile: |
| 83 | + image_data = f_localfile.read() |
| 84 | + message_list.append({ |
| 85 | + 'topic' : '/'.join((base_topic, 'latest')), |
| 86 | + 'payload' : bytearray(image_data), |
| 87 | + 'qos' : qos, |
| 88 | + 'retain' : True, |
| 89 | + }) |
| 90 | + |
| 91 | + |
| 92 | + for k, v in mq_data.items(): |
| 93 | + message_list.append({ |
| 94 | + 'topic' : '/'.join((base_topic, k)), |
| 95 | + 'payload' : v, |
| 96 | + 'qos' : qos, |
| 97 | + 'retain' : True, |
| 98 | + }) |
| 99 | + |
| 100 | + |
| 101 | + start = time.time() |
| 102 | + |
| 103 | + try: |
| 104 | + publish.multiple( |
| 105 | + message_list, |
| 106 | + transport=self.mq_transport, |
| 107 | + hostname=self.mq_hostname, |
| 108 | + port=self._port, |
| 109 | + client_id='', |
| 110 | + keepalive=60, |
| 111 | + auth=self.mq_auth, |
| 112 | + tls=self.mq_tls, |
| 113 | + ) |
| 114 | + except socket.gaierror as e: |
| 115 | + raise ConnectionFailure(str(e)) from e |
| 116 | + except socket.timeout as e: |
| 117 | + raise ConnectionFailure(str(e)) from e |
| 118 | + except ssl.SSLCertVerificationError as e: |
| 119 | + raise ConnectionFailure(str(e)) from e |
| 120 | + except ConnectionRefusedError as e: |
| 121 | + raise ConnectionFailure(str(e)) from e |
| 122 | + except MQTTException as e: |
| 123 | + raise AuthenticationFailure(str(e)) from e |
| 124 | + |
| 125 | + upload_elapsed_s = time.time() - start |
| 126 | + local_file_size = local_file_p.stat().st_size |
| 127 | + logger.info('File transferred in %0.4f s (%0.2f kB/s)', upload_elapsed_s, local_file_size / upload_elapsed_s / 1024) |
| 128 | + |
| 129 | + |
0 commit comments