-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMplatform.py
281 lines (236 loc) · 8.1 KB
/
SMplatform.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import requests
import json, csv, os, dotenv
def request(content, url, headers=None, verbose = False):
if(verbose == True): print(content)
r = requests.post(url, headers=headers, data={"query": content})
r.raise_for_status()
if(verbose):print(r.json())
return r.json()
def get_token (auth, password, name, url, role):
response = request(f'''
mutation authRequest {{
authenticationRequest(input: {{authenticator: "{auth}", role: "{role}", userName: "{name}"}}) {{
jwtRequest {{
challenge, message
}}
}}
}}
''', url)
jwt_request = response['data']['authenticationRequest']['jwtRequest']
if jwt_request['challenge'] is None:
raise Exception(jwt_request['message'])
else:
response=request(f'''
mutation authValidation {{
authenticationValidation( input: {{authenticator: "{auth}", signedChallenge: "{jwt_request["challenge"]}|{password}"}} ){{
jwtClaim
}}
}}
''', url)
jwt_claim = response['data']['authenticationValidation']['jwtClaim']
return f"Bearer {jwt_claim}"
def build_tsData_Query(tagID, start = "2020-12-1T00:00:00", end = "now", maxSamples=0):
query_string = f'''
query tsData_Query {{
getRawHistoryDataWithSampling(
startTime: "{start}"
endTime: "{end}"
ids: {tagID}
maxSamples: {maxSamples}
) {{
id
ts
boolvalue
floatvalue
intvalue
stringvalue
dataType
}}
}}
'''
return query_string
def build_RunIDts_Query(runID , startTime = "2020-12-1T00:00:00", endTime = "now"):
query_string = f'''
query RunIDts_Query {{
getRawHistoryDataWithSampling(
ids: "6530"
endTime: "{endTime}"
maxSamples: 0
filter: {{ stringvalue: {{ {runID} }} }}
startTime: "{startTime}"
) {{
ts
stringvalue
}}
}}
'''
return query_string
def getStartandEndTime(runID, endpoint_url, header, verbose=False):
StartandEnd=['','']
my_query = build_RunIDts_Query(f"equalTo: \"{runID}\" ")
if(verbose):print(my_query)
result = request(my_query, endpoint_url, header) # Execute the query
StartandEnd[0]= result['data']['getRawHistoryDataWithSampling'][0]['ts'] #save the start time
if(verbose):print(result)
my_query = build_RunIDts_Query("equalTo: \"0\"",StartandEnd[0]) #search for the finish
if(verbose):print(my_query)
result = request(my_query, endpoint_url, header) # Execute the query
StartandEnd[1]= result['data']['getRawHistoryDataWithSampling'][0]['ts']
if(verbose):print(result)
return StartandEnd
def build_TagList_Query(name):
query_string = f'''
query TagList_Query {{
tags(filter: {{ displayName: {{ startsWith: "{name}" }} }}) {{
id
displayName
description
dataType
}}
}}
'''
return query_string
def build_CreateTag_Mutation(name, dataType="STRING", desc = "", partOfId = "214"):
query_string = f'''
mutation CreateTag_Mutation {{
createTag(
input: {{
tag: {{
dataType: {dataType}
description: "{desc}"
displayName: "{name}"
relativeName: "{name}"
partOfId:"{partOfId}"
}}
}}
) {{
clientMutationId
}}
}}
'''
return query_string
def build_UpdateTagTS_Mutation(tagID, value, time="now", status="0"):
query_string = f'''
mutation UpdateTagTS_Mutation {{
replaceTimeSeriesRange(
input: {{
attributeOrTagId: "{tagID}"
entries: {{ value: {value}, timestamp: "{time}", status: "{status}" }}
}}
) {{
clientMutationId
json
}}
}}
'''
return query_string
def build_UpdateMultipleTagTS_Mutation(tagID, entries):
query_string = f'''
mutation UpdateTagTS_Mutation {{
replaceTimeSeriesRange(
input: {{
attributeOrTagId: "{tagID}"
entries: [{entries}]
}}
) {{
clientMutationId
json
}}
}}
'''
return query_string
def findTagID_Create (Tag_Identifier, tagName, dataType, endpoint_url, header, desc= "This was made using Python", create= True, verbose = False):
# Tag Identifier should proceed the tag
# This is usually in reference to the connector being used
# example: tagIdentifier.tagName, MQTT_Connector.ccam_sim_runID
if(verbose==True):print("find/create Tag ID start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
my_query = build_TagList_Query(Tag_Identifier)
if(verbose==True):print(my_query)
result = request(my_query, endpoint_url, header)
tagList = result.get('data').get('tags')
tagID = None
#search for tag in list
for i in tagList:
if i['displayName'] == tagName:
tagID= i['id']
#if tag not present create tag
if(create == True):
if (tagID == None):
if(verbose==True):print("\ntag not in list and will be added\n")
my_Mutation = build_CreateTag_Mutation(tagName, dataType, desc)
if(verbose==True):print(my_Mutation)
result = request(my_Mutation, endpoint_url, header)
if(verbose==True):print(result)
result = request(my_query, endpoint_url, header)
tagList = result.get('data').get('tags')
for i in tagList:
if i['displayName'] == tagName:
tagID= i['id']
else:
if(tagID == None):
if(verbose==True):print("No Tag found and will not create")
return
if(verbose==True):print(f"\n{tagName} tagID: {tagID}\n")
if(verbose==True):print("find/create Tag ID end ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
return [tagName, tagID]
def contains(small, big):
for i in range(len(big)-len(small)+1):
for j in range(len(small)):
if big[i+j] != small[j]:
break
else:
return True
return False
def remove_dup(a):
i = 0
while i < len(a):
j = i + 1
while j < len(a):
if a[i] == a[j]:
del a[j]
else:
j += 1
i += 1
def build_entries(config, file, verbose=False):
final_entries = [None] * len(config['Topic_toSMP'])
topic_count = 0
for t in config['Topic_toSMP']:
if(file != None):
entries = []
if(verbose==True):
print('\n************************************************************************')
print(f'''Uploading topic: {t} \nFrom: {file}''')
print('\n************************************************************************')
with open(file, newline='') as f:
reader = csv.reader(f)
data = list(reader)
#TODO confirm there isnt a bug in the MQTT Logger
#scan list for duplicates
remove_dup(data)
for x in data:
if (x != []):
y = x[0].split('_')
if (contains(t,y)):
dp = f'''{{value: "{x[1]}", timestamp: "{x[2]}", status: "0"}}'''
entries += [dp]
if(verbose==True): print(dp)
if(verbose==True): print(x)
entries = ','.join(entries)
final_entries[topic_count] = entries
topic_count += 1
if(verbose==True):print(final_entries)
return final_entries
def SMP_auth():
dotenv.load_dotenv()
endpoint_url = os.environ.get("endpoint_url")
authenticator = os.environ.get("authenticator")
pw = os.environ.get("pw")
user = os.environ.get("user")
role = os.environ.get("role")
token = get_token(authenticator, pw, user, endpoint_url, role)
header = {"Authorization": token}
return header
if __name__ == '__main__':
#SMP header setup
header = SMP_auth()
print(header)