-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_cli2.py
executable file
·80 lines (64 loc) · 2.33 KB
/
mqtt_cli2.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
# -*- coding: utf-8 -*-
import noise_prot_interface
import socket
import sys, getopt
import time
def read_file(f_name):
with open(f_name, 'r') as f:
return f.read()
def cpu_temp():
return read_file('/sys/class/thermal/thermal_zone0/temp')
class MQTTClient:
def __init__(self, username, password):
#self.salt = read_file('salt')
self.password = password
self.client = noise_prot_interface.NoiseProtocolClient()
self.username = username
def cli(self, cli, topic, port):
self.client.connect(destination='localhost', port= port)
print(str(self.password))
self.client.sendMessage('connect ' + str(self.username) + ':' + str(self.password))
msg = self.client.getMessage()
print(msg)
if msg == 'Connected':
if cli == 'pub':
self.client.sendMessage('publish')
#print(self.client.getMessage())
while True:
print(cpu_temp())
self.client.sendMessage("{}:{}".format(topic, cpu_temp()))
time.sleep(15)
if cli == 'sub':
self.client.sendMessage('{}:subscribe'.format(topic))
while True:
print(self.client.getMessage())
time.sleep(5)
else:
return self.client.getMessage()
def main():
print("Args: %s" % (sys.argv))
try:
opts, args = getopt.getopt(sys.argv[1:],"hi:u:p:t:c:n:",["username=","password=","topic=", "client=","n="])
except getopt.GetoptError:
print ("%s -u <username> -p <password> -t <topic> -c <pub/sub> -n <portnu>" % (sys.argv[0]))
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ("%s -u <username> -p <password> -t <topic> -c <pub/sub> -n <portnu>" % (sys.argv[0]))
sys.exit()
elif opt in ("-u", "--username"):
username = arg
elif opt in ("-p", "--password"):
password = arg
elif opt in ("-t", "--topic"):
topic = arg
#TODO pub sub arg
elif opt in ("-c", "--client"):
cli = arg
elif opt in ("-n", "--port"):
port = arg
s_ip = 'localhost'
while True:
msg = MQTTClient(username, password).cli(cli, topic, int(port))
print(msg)
main()