@@ -57,7 +57,6 @@ class MQTT_API:
57
57
58
58
"""
59
59
60
- # pylint: disable=protected-access
61
60
def __init__ (self , mqtt_client ):
62
61
# Check that provided object is a MiniMQTT client object
63
62
mqtt_client_type = str (type (mqtt_client ))
@@ -69,19 +68,19 @@ def __init__(self, mqtt_client):
69
68
)
70
69
# Verify that the MiniMQTT client was setup correctly.
71
70
try :
72
- self ._user = self ._client ._user
71
+ self .user = self ._client .user
73
72
except :
74
73
raise TypeError ("Google Cloud Core IoT MQTT API requires a username." )
75
74
# Validate provided JWT before connecting
76
75
try :
77
- JWT .validate (self ._client ._pass )
76
+ JWT .validate (self ._client .password )
78
77
except :
79
78
raise TypeError ("Invalid JWT provided." )
80
79
# If client has KeepAlive =0 or if KeepAlive > 20min,
81
80
# set KeepAlive to 19 minutes to avoid disconnection
82
81
# due to Idle Time (https://cloud.google.com/iot/quotas).
83
- if self ._client ._keep_alive == 0 or self ._client ._keep_alive >= 1200 :
84
- self ._client ._keep_alive = 1140
82
+ if self ._client .keep_alive == 0 or self ._client .keep_alive >= 1200 :
83
+ self ._client .keep_alive = 1140
85
84
# User-defined MQTT callback methods must be init'd to None
86
85
self .on_connect = None
87
86
self .on_disconnect = None
@@ -94,14 +93,14 @@ def __init__(self, mqtt_client):
94
93
self ._client .on_message = self ._on_message_mqtt
95
94
self ._client .on_subscribe = self ._on_subscribe_mqtt
96
95
self ._client .on_unsubscribe = self ._on_unsubscribe_mqtt
97
- self ._logger = False
98
- if self ._client ._logger is not None :
96
+ self .logger = False
97
+ if self ._client .logger is not None :
99
98
# Allow MQTT_API to utilize MiniMQTT Client's logger
100
- self ._logger = True
99
+ self .logger = True
101
100
self ._client .set_logger_level ("DEBUG" )
102
101
self ._connected = False
103
102
# Set up a device identifier by splitting out the full CID
104
- self .device_id = self ._client ._client_id .split ("/" )[7 ]
103
+ self .device_id = self ._client .client_id .split ("/" )[7 ]
105
104
106
105
def __enter__ (self ):
107
106
return self
@@ -142,8 +141,8 @@ def is_connected(self):
142
141
def _on_connect_mqtt (self , client , userdata , flags , return_code ):
143
142
"""Runs when the mqtt client calls on_connect.
144
143
"""
145
- if self ._logger :
146
- self ._client ._logger .debug ("Client called on_connect." )
144
+ if self .logger :
145
+ self ._client .logger .debug ("Client called on_connect." )
147
146
if return_code == 0 :
148
147
self ._connected = True
149
148
else :
@@ -156,8 +155,8 @@ def _on_connect_mqtt(self, client, userdata, flags, return_code):
156
155
def _on_disconnect_mqtt (self , client , userdata , return_code ):
157
156
"""Runs when the client calls on_disconnect.
158
157
"""
159
- if self ._logger :
160
- self ._client ._logger .debug ("Client called on_disconnect" )
158
+ if self .logger :
159
+ self ._client .logger .debug ("Client called on_disconnect" )
161
160
self ._connected = False
162
161
# Call the user-defined on_disconnect callblack if defined
163
162
if self .on_disconnect is not None :
@@ -167,26 +166,26 @@ def _on_disconnect_mqtt(self, client, userdata, return_code):
167
166
def _on_message_mqtt (self , client , topic , payload ):
168
167
"""Runs when the client calls on_message.
169
168
"""
170
- if self ._logger :
171
- self ._client ._logger .debug ("Client called on_message" )
169
+ if self .logger :
170
+ self ._client .logger .debug ("Client called on_message" )
172
171
if self .on_message is not None :
173
172
self .on_message (self , topic , payload )
174
173
175
174
# pylint: disable=not-callable
176
175
def _on_subscribe_mqtt (self , client , user_data , topic , qos ):
177
176
"""Runs when the client calls on_subscribe.
178
177
"""
179
- if self ._logger :
180
- self ._client ._logger .debug ("Client called on_subscribe" )
178
+ if self .logger :
179
+ self ._client .logger .debug ("Client called on_subscribe" )
181
180
if self .on_subscribe is not None :
182
181
self .on_subscribe (self , user_data , topic , qos )
183
182
184
183
# pylint: disable=not-callable
185
184
def _on_unsubscribe_mqtt (self , client , user_data , topic , pid ):
186
185
"""Runs when the client calls on_unsubscribe.
187
186
"""
188
- if self ._logger :
189
- self ._client ._logger .debug ("Client called on_unsubscribe" )
187
+ if self .logger :
188
+ self ._client .logger .debug ("Client called on_unsubscribe" )
190
189
if self .on_unsubscribe is not None :
191
190
self .on_unsubscribe (self , user_data , topic , pid )
192
191
@@ -319,10 +318,10 @@ def __init__(self, esp, secrets, log=False):
319
318
raise AttributeError (
320
319
"Project settings are kept in secrets.py, please add them there!"
321
320
)
322
- self ._logger = None
321
+ self .logger = None
323
322
if log is True :
324
- self ._logger = logging .getLogger ("log" )
325
- self ._logger .setLevel (logging .DEBUG )
323
+ self .logger = logging .getLogger ("log" )
324
+ self .logger .setLevel (logging .DEBUG )
326
325
# Configuration, from secrets file
327
326
self ._proj_id = secrets ["project_id" ]
328
327
self ._region = secrets ["cloud_region" ]
@@ -340,8 +339,8 @@ def client_id(self):
340
339
client_id = "projects/{0}/locations/{1}/registries/{2}/devices/{3}" .format (
341
340
self ._proj_id , self ._region , self ._reg_id , self ._device_id
342
341
)
343
- if self ._logger :
344
- self ._logger .debug ("Client ID: {}" .format (client_id ))
342
+ if self .logger :
343
+ self .logger .debug ("Client ID: {}" .format (client_id ))
345
344
return client_id
346
345
347
346
@@ -357,8 +356,8 @@ def generate_jwt(self, ttl=43200, algo="RS256"):
357
356
print("Generated JWT: ", jwt)
358
357
359
358
"""
360
- if self ._logger :
361
- self ._logger .debug ("Generating JWT..." )
359
+ if self .logger :
360
+ self .logger .debug ("Generating JWT..." )
362
361
ntp = NTP .NTP (self ._esp )
363
362
ntp .set_time ()
364
363
claims = {
0 commit comments