def initialize(self): # MQTT client self.client = Client(self.host, client_cert_filename=self.client_cert, private_key_filename=self.private_key, log_mqtt=self.debug) self.client.connect() self.log.info('connected to AWS IoT')
def test_publish(MockMqttClient): client = Client('host') def on_connect(*args, **kwargs): client._connected = True MockMqttClient.return_value.connect.side_effect = on_connect client.connect() client.publish('msg', 'topic') MockMqttClient.return_value.publish.assert_called_once_with('msg', 'topic')
def test_connect(MockMqttClient): client = Client('host') # TODO: test threading properly, or expose Client.on_connect def on_connect(*args, **kwargs): client._connected = True MockMqttClient.return_value.connect.side_effect = on_connect client.connect() MockMqttClient.return_value.connect.assert_called_once_with( 'host', port=8883) MockMqttClient.return_value.loop_start.assert_called_once_with()
def test_stop(MockMqttClient): client = Client('host') def on_connect(*args, **kwargs): client._connected = True MockMqttClient.return_value.connect.side_effect = on_connect client.connect() MockMqttClient.return_value.connect.assert_called_once_with( 'host', port=8883) MockMqttClient.return_value.loop_start.assert_called_once_with() client.disconnect() MockMqttClient.return_value.loop_stop.assert_called_once_with()
class AWSIoTNotifier(Notifier): def __init__(self, host=None, client_cert=None, private_key=None, aws_iot_message_unit_cost=5e-6, estimated_change_freq=0.0, debug=False): """ Create an AWS IoT MQTT notifier Args: host (str): host name of AWS IoT endpoint client_cert (str): name of client certificate file private_key (str): name of private key for client certificate aws_iot_message_unit_cost (float): cost of an AWS IoT message used to estimate monthly cost of operating this thingpin estimated_change_freq (float): estimate of how often each pin will change. Only used for guessing AWS costs at startup. debug (bool): if True log all MQTT traffic. """ self.log = logging.getLogger('thingpin') self.host = host self.client_cert = os.path.expanduser(client_cert) self.private_key = os.path.expanduser(private_key) self.debug = debug self.client = None self.log.info('AWS monthly cost guesstimate ${:,.8f}'.format( estimated_change_freq * 60 * 60 * 24 * 30 * aws_iot_message_unit_cost)) self.log.info('(don' 't take the guesstimate too seriously!)') def initialize(self): # MQTT client self.client = Client(self.host, client_cert_filename=self.client_cert, private_key_filename=self.private_key, log_mqtt=self.debug) self.client.connect() self.log.info('connected to AWS IoT') def cleanup(self): self.client.disconnect() def notify(self, name, value): self.log.info('AWS IoT: publish({}={})'.format(name, value)) Thing(name, self.client).publish_state(value)
class AWSIoTNotifier(Notifier): def __init__(self, host=None, client_cert=None, private_key=None, aws_iot_message_unit_cost=5e-6, estimated_change_freq=0.0, debug=False): """ Create an AWS IoT MQTT notifier Args: host (str): host name of AWS IoT endpoint client_cert (str): name of client certificate file private_key (str): name of private key for client certificate aws_iot_message_unit_cost (float): cost of an AWS IoT message used to estimate monthly cost of operating this thingpin estimated_change_freq (float): estimate of how often each pin will change. Only used for guessing AWS costs at startup. debug (bool): if True log all MQTT traffic. """ self.log = logging.getLogger('thingpin') self.host = host self.client_cert = os.path.expanduser(client_cert) self.private_key = os.path.expanduser(private_key) self.debug = debug self.client = None self.log.info('AWS monthly cost guesstimate ${:,.8f}'.format( estimated_change_freq * 60 * 60 * 24 * 30 * aws_iot_message_unit_cost )) self.log.info('(don''t take the guesstimate too seriously!)') def initialize(self): # MQTT client self.client = Client(self.host, client_cert_filename=self.client_cert, private_key_filename=self.private_key, log_mqtt=self.debug) self.client.connect() self.log.info('connected to AWS IoT') def cleanup(self): self.client.disconnect() def notify(self, name, value): self.log.info('AWS IoT: publish({}={})'.format(name, value)) Thing(name, self.client).publish_state(value)
def initialize(self): # MQTT client self.client = Client(self.host, client_cert_filename=self.client_cert, private_key_filename=self.private_key, log_mqtt=self.debug)