-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclientConfig.py
executable file
·231 lines (212 loc) · 12 KB
/
clientConfig.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/python3
import sys
import configparser
import json
def giveCount(file):
for i in (0, file.getint('maxNumberOfServers', 'serversNum')):
if file.has_section("Server" + str(i)):
pass
else:
return i
def addserver():
initial_configfile = configparser.ConfigParser()
initial_configfile.read("clientData.ini")
# Updating the number of the servers value in the clientConfig.ini file #
i = giveCount(initial_configfile)
max = initial_configfile.getint('maxNumberOfServers', 'serversNum')
# Updating the maximum number of the servers value in the clientConfig.ini file
if i == max:
initial_configfile.set('maxNumberOfServers', 'serversNum', (i + 1))
# Taking console inputs for the new server.
serverUrl = input("Type the Url of the new server: ")
serverName = input("Type the name of the new server: ")
serverType = input("Type the type of the server: ")
mqttUrl = input("Type the Url or username of the mqtt Broker: ")
mqttPort = input("Type the port of the mqtt Broker: ")
architectureTopic = input("Type the name of the architecture topic: ")
consoleTopic = input("Type the topic for console messages: ")
readTopic = input("Type the topic for the read values: ")
methRequestTopic = input("Type the topic for method requests: ")
readRequestTopic = input("Type the topic for reading values requests: ")
writeRequestTopic = input("Type the topic for writing values requests: ")
subRequestTopic = input("Type the topic for variable subscription requests: ")
unSubRequestTopic = input("unsubscribe request topic: ")
subscriptionTopic = input("Subscription Topic: ")
connectDisconnectTopic = input("Connect-Disconnect Topic: ")
# Setting the taken inputs in the clientConfig.ini file, in the new server's section.
initial_configfile.add_section("Server" + str(i))
initial_configfile.set('Server' + str(i), 'url', serverUrl)
initial_configfile.set('Server' + str(i), 'name', serverName)
initial_configfile.set('Server' + str(i), 'type', serverType)
initial_configfile.set('Server' + str(i), 'mqtturl', mqttUrl)
initial_configfile.set('Server' + str(i), 'mqttport', mqttPort)
initial_configfile.set('Server' + str(i), 'architecturetopic', architectureTopic)
initial_configfile.set('Server' + str(i), 'consoletopic', consoleTopic)
initial_configfile.set('Server' + str(i), 'readtopic', readTopic)
initial_configfile.set('Server' + str(i), 'methrequesttopic', methRequestTopic)
initial_configfile.set('Server' + str(i), 'readrequesttopic', readRequestTopic)
initial_configfile.set('Server' + str(i), 'writerequesttopic', writeRequestTopic)
initial_configfile.set('Server' + str(i), 'subrequesttopic', subRequestTopic)
initial_configfile.set('Server' + str(i), 'unsubrequesttopic', unSubRequestTopic)
initial_configfile.set('Server' + str(i), 'subscriptiontopic', subscriptionTopic)
initial_configfile.set('Server' + str(i), 'connectdisconnecttopic', connectDisconnectTopic)
initial_configfile.set('Server' + str(i), 'count', i)
with open(r"clientData.ini", 'w') as configfile:
initial_configfile.write(configfile)
return i
def edit_server(num):
initial_configfile = configparser.ConfigParser()
initial_configfile.read("clientData.ini")
# Taking console inputs for the new server.
serverUrl = input("Type the new Url of the server: ")
serverName = input("Type the new name of the new server: ")
serverType = input("Type the type of the server: ")
mqttUrl = input("Type the new Url or username of the mqtt Broker: ")
mqttPort = input("Type the new port of the mqtt Broker: ")
architectureTopic = input("Type the new name of the architecture topic: ")
consoleTopic = input("Type the new topic for console messages: ")
readTopic = input("Type the new topic for the read values: ")
methRequestTopic = input("Type the new topic for method requests: ")
readRequestTopic = input("Type the new topic for reading values requests: ")
writeRequestTopic = input("Type the new topic for writing values requests: ")
subRequestTopic = input("Type the new topic for variable subscription requests: ")
unSubRequestTopic = input("New Unsubscribe request topic: ")
subscriptionTopic = input("New Subscription Topic: ")
connectDisconnectTopic = input("New Connect-Disconnect Topic: ")
# Setting the taken inputs in the clientConfig.ini file, in the new server's section.
initial_configfile.set('Server' + str(num), 'url', serverUrl)
initial_configfile.set('Server' + str(num), 'name', serverName)
initial_configfile.set('Server' + str(num), 'type', serverType)
initial_configfile.set('Server' + str(num), 'mqtturl', mqttUrl)
initial_configfile.set('Server' + str(num), 'mqttport', mqttPort)
initial_configfile.set('Server' + str(num), 'architecturetopic', architectureTopic)
initial_configfile.set('Server' + str(num), 'consoletopic', consoleTopic)
initial_configfile.set('Server' + str(num), 'readtopic', readTopic)
initial_configfile.set('Server' + str(num), 'methrequesttopic', methRequestTopic)
initial_configfile.set('Server' + str(num), 'readrequesttopic', readRequestTopic)
initial_configfile.set('Server' + str(num), 'writerequesttopic', writeRequestTopic)
initial_configfile.set('Server' + str(num), 'subrequesttopic', subRequestTopic)
initial_configfile.set('Server' + str(num), 'unsubrequesttopic', unSubRequestTopic)
initial_configfile.set('Server' + str(num), 'subscriptiontopic', subscriptionTopic)
initial_configfile.set('Server' + str(num), 'connectdisconnecttopic', connectDisconnectTopic)
with open(r"clientData.ini", 'w') as configfile:
initial_configfile.write(configfile)
def addserver_from_UI(dataFromUI):
dataObject = json.loads(dataFromUI)
initial_configfile = configparser.ConfigParser()
initial_configfile.read("clientData.ini")
i = giveCount(initial_configfile)
max = initial_configfile.getint('maxNumberOfServers', 'serversNum')
# Updating the maximum number of the servers value in the clientConfig.ini file
if i == max:
max = max + 1
initial_configfile.set('maxNumberOfServers', 'serversNum', str(max))
# Taking console inputs for the new server.
serverUrl = dataObject["serverUrl"]
serverName = dataObject["serverName"]
serverType = dataObject["serverType"]
mqttUrl = dataObject["mqttUrl"]
mqttPort = dataObject["mqttPort"]
architectureTopic = dataObject["architectureTopic"]
consoleTopic = dataObject["consoleTopic"]
readTopic = dataObject["readTopic"]
methRequestTopic = dataObject["methRequestTopic"]
readRequestTopic = dataObject["readRequestTopic"]
writeRequestTopic = dataObject["writeRequestTopic"]
subRequestTopic = dataObject["subRequestTopic"]
unSubRequestTopic = dataObject["unSubRequestTopic"]
subscriptionTopic = dataObject["subscriptionTopic"]
connectDisconnectTopic = dataObject["connectDisconnectTopic"]
# Setting the taken inputs in the clientConfig.ini file, in the new server's section.
initial_configfile.add_section("Server" + str(i))
initial_configfile.set('Server' + str(i), 'url', serverUrl)
initial_configfile.set('Server' + str(i), 'name', serverName)
initial_configfile.set('Server' + str(i), 'type', serverType)
initial_configfile.set('Server' + str(i), 'mqtturl', mqttUrl)
initial_configfile.set('Server' + str(i), 'mqttport', str(mqttPort))
initial_configfile.set('Server' + str(i), 'architecturetopic', architectureTopic)
initial_configfile.set('Server' + str(i), 'consoletopic', consoleTopic)
initial_configfile.set('Server' + str(i), 'readtopic', readTopic)
initial_configfile.set('Server' + str(i), 'methrequesttopic', methRequestTopic)
initial_configfile.set('Server' + str(i), 'readrequesttopic', readRequestTopic)
initial_configfile.set('Server' + str(i), 'writerequesttopic', writeRequestTopic)
initial_configfile.set('Server' + str(i), 'subrequesttopic', subRequestTopic)
initial_configfile.set('Server' + str(i), 'unsubrequesttopic', unSubRequestTopic)
initial_configfile.set('Server' + str(i), 'subscriptiontopic', subscriptionTopic)
initial_configfile.set('Server' + str(i), 'connectdisconnecttopic', connectDisconnectTopic)
initial_configfile.set('Server' + str(i), 'count', str(i))
with open(r"clientData.ini", 'w') as configfile:
initial_configfile.write(configfile)
splt = architectureTopic.split('/')
uuid = splt[1]
fed = {"serveruuid": uuid, "count": i}
feedback = json.dumps(fed)
return feedback
def edit_server_from_UI(dataFromUI):
dataObject = json.loads(dataFromUI)
initial_configfile = configparser.ConfigParser()
initial_configfile.read("clientData.ini")
i = dataObject["numOfServer"]
serverUrl = dataObject["serverUrl"]
serverName = dataObject["serverName"]
serverType = dataObject["serverType"]
mqttUrl = dataObject["mqttUrl"]
mqttPort = dataObject["mqttPort"]
architectureTopic = dataObject["architectureTopic"]
consoleTopic = dataObject["consoleTopic"]
readTopic = dataObject["readTopic"]
methRequestTopic = dataObject["methRequestTopic"]
readRequestTopic = dataObject["readRequestTopic"]
writeRequestTopic = dataObject["writeRequestTopic"]
subRequestTopic = dataObject["subRequestTopic"]
unSubRequestTopic = dataObject["unSubRequestTopic"]
subscriptionTopic = dataObject["subscriptionTopic"]
connectDisconnectTopic = dataObject["connectDisconnectTopic"]
initial_configfile.set('Server' + str(i), 'url', serverUrl)
initial_configfile.set('Server' + str(i), 'name', serverName)
initial_configfile.set('Server' + str(i), 'type', serverType)
initial_configfile.set('Server' + str(i), 'mqtturl', mqttUrl)
initial_configfile.set('Server' + str(i), 'mqttport', mqttPort)
initial_configfile.set('Server' + str(i), 'architecturetopic', architectureTopic)
initial_configfile.set('Server' + str(i), 'consoletopic', consoleTopic)
initial_configfile.set('Server' + str(i), 'readtopic', readTopic)
initial_configfile.set('Server' + str(i), 'methrequesttopic', methRequestTopic)
initial_configfile.set('Server' + str(i), 'readrequesttopic', readRequestTopic)
initial_configfile.set('Server' + str(i), 'writerequesttopic', writeRequestTopic)
initial_configfile.set('Server' + str(i), 'subrequesttopic', subRequestTopic)
initial_configfile.set('Server' + str(i), 'unsubrequesttopic', unSubRequestTopic)
initial_configfile.set('Server' + str(i), 'subscriptiontopic', subscriptionTopic)
initial_configfile.set('Server' + str(i), 'connectdisconnecttopic', connectDisconnectTopic)
def deleteServer(i):
initial_configfile = configparser.ConfigParser()
initial_configfile.read("clientData.ini")
initial_configfile.remove_section('Server' + str(i))
maxNum = initial_configfile.getint('maxNumberOfServers', 'serversnum')
maxNum = maxNum - 1
initial_configfile.set('maxNumberOfServers', 'serversnum', str(maxNum))
with open(r"clientData.ini", 'w') as configfile:
initial_configfile.write(configfile)
def cleanConfig():
initial_configfile = configparser.ConfigParser()
initial_configfile.read("clientData.ini")
num = initial_configfile.getint('maxNumberOfServers', 'serversNum')
for i in range(0, num + 1):
initial_configfile.remove_section('Server' + str(i))
initial_configfile.set('maxNumberOfServers', 'serversNum', '0')
with open(r"clientData.ini", 'w') as configfile:
initial_configfile.write(configfile)
def deleteAll():
pass
if __name__ == '__main__':
args = sys.argv
# if len(args)
function_name = args[1]
if args:
if function_name == addserver_from_UI.__name__:
addserver_from_UI(args[2])
elif function_name == edit_server_from_UI.__name__:
edit_server_from_UI(args[2])
elif function_name == cleanConfig().__name__:
cleanConfig()
else:
print("Invalid function name.")