-
Notifications
You must be signed in to change notification settings - Fork 0
/
client2.py
214 lines (177 loc) · 5.99 KB
/
client2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# =============================================================================
# imported libraries
from paho.mqtt import client as mqtt
import time
import colorama
from colorama import Fore
import random
import os
# =============================================================================
# Global constants
# => To referesh the cmd.
colorama.init(autoreset=True)
tcp_port = 1883
tcp_ssl_port = 8883
# =============================================================================
# Basic configurations
client_id = f'publisher-client-id-{random.randint(0, 127)}'
broker_address = 'mqtt.eclipseprojects.io'
port = tcp_port
Qos = 2
username = f'mohamed-ashraf-wx'
password = f'mohamed-ashraf'
# Subscriber / Publisher Configurations
topic = 'mqtt_testing_python/chat_app/test1'
# =============================================================================
# Functions implementation
firmware = []
def load_firmware(dir_name=None, file_name=None, file_extension=None):
file_path_name = f'{dir_name}/{file_name}.{file_extension}'
with open(f'CAN_PROTOCOL_TEST.hex', 'r') as file:
for line in file.readlines():
firmware.append(line)
print(f'{Fore.GREEN}Loading the firmware done.')
'''
@brief Function to handle the files
'''
def file_dir_handle(dir_name=f'firmware_dir', file_name=f'firmware', file_extension=f'txt'):
# Check if the file is already exsits
if os.path.exists(f'./{dir_name}'):
print(f'{Fore.RED}Directory {Fore.WHITE}`{dir_name}` {Fore.RED}already exisits!.')
else:
# Create the directory
os.mkdir(dir_name)
print(f'{Fore.GREEN}Created directory {Fore.WHITE}`{dir_name}` {Fore.GREEN}sucessfully.')
# Create new file
global file_path_name
file_path_name = f'{dir_name}/{file_name}.{file_extension}'
# Check if the file already exists.
if os.path.exists(f'{file_path_name}'):
print(f'{Fore.RED}File {Fore.WHITE}`{file_path_name}` {Fore.RED}already exisits!.')
else:
with open(f'{file_path_name}', 'w') as file:
# write nothing to force the os creating the file.
file.write(f'')
if os.path.exists(f'{file_path_name}'):
print(f'{Fore.GREEN}Created {Fore.WHITE}{file_name}.{file_extension} {Fore.GREEN}succesfully.')
else:
print(f'{Fore.RED}Could not create {Fore.WHITE}{file_name}.{file_extension}')
'''
@brief Callback Function to print the status on connection.
'''
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(f"{Fore.GREEN}Connection to the broker {Fore.WHITE}`{broker_address}` successful.")
else:
print(f"{Fore.RED}Connection to the broker {Fore.WHITE}`{broker_address}` failed.")
'''
@brief Function to print the incoming message from the broker.
'''
def on_message(client, userdata, msg):
global message
message = msg.payload.decode('ascii')
topic = msg.topic
print(f'{Fore.GREEN}Received {Fore.WHITE}`{message}` {Fore.GREEN}on topic {Fore.WHITE}`{topic}`')
'''
@brief Callback Function
'''
def on_publish(client, userdata, mid):
pass
'''
@brief Callback Function
'''
def on_subscribe(client, userdata, mid, granted_qos):
pass
'''
@brief Callback Function
'''
def on_unsubscribe(client, userdata, mid):
pass
'''
@brief Callback Function
'''
def on_disconnect(client, userdata, mid):
pass
'''
@brief Function to setup the basic configuration
'''
def mqtt_set_cfgs(
client_id='0',
broker='mqtt.eclipseprojects.io',
port=1883,
Qos=2,
username=None,
password=None):
# Constructing setter
# Set up the basic configurations
client = mqtt.Client(client_id, clean_session=True)
client.username_pw_set(username, password)
# Callbacks
client.on_connect = on_connect
client.on_publish = on_publish
client.on_disconnect = on_disconnect
client.on_subscribe = on_subscribe
client.on_unsubscribe = on_unsubscribe
return client
'''
@brief Function to setup the connection between the client & broker.
'''
def connect_mqtt():
# Setting the configuration.
client = mqtt_set_cfgs(
client_id,
broker_address,
port,
Qos,
username,
password)
# Connect to the broker
client.connect(broker_address, port, Qos)
return client
'''
@brief Function to publish (send) a message to the broker
with the specified topic.
'''
def publish(client=None, topic=None, message=None):
# Publish the message
result = client.publish(topic, message)
# Get the status
status = result[0]
if status == 0:
print(f"{Fore.GREEN} Published {Fore.WHITE}`{message}` {Fore.GREEN}on Topic {Fore.WHITE}`{topic}`")
else:
print(f"{Fore.RED} Failed to Publish {Fore.WHITE}`{message}` {Fore.RED}on Topic {Fore.WHITE}`{topic}`")
'''
@brief Function to subscribe to a topic to receive its messages.
'''
def subscribe(client=None, topic=None):
# Subscribe to the topic
client.subscribe(topic)
client.on_message = on_message
'''
@brief Function to set and run the application.
'''
def run_app():
pub_topic = f'{topic}/client2-pub'
sub_topic = f'{topic}/client1-pub'
try:
client = connect_mqtt()
subscribe(client, sub_topic)
client.loop_start()
time.sleep(1)
while True:
#msg = str(input("Enter: "))
msg = "Hello!"
publish(client, pub_topic, msg)
time.sleep(1)
except:
client.disconnect()
client.loop_stop()
print(f'{Fore.GREEN}Disconnected from broker {Fore.WHITE}`{broker_address}` - {Fore.GREEN}Client {Fore.WHITE}`{client_id}`')
# =============================================================================
# Program entry point
# Run the application from the entry point.
if __name__ == "__main__":
file_dir_handle()
# Run the MQTT application.
run_app()