-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_simulator.py
More file actions
82 lines (59 loc) · 2.66 KB
/
Copy pathpython_simulator.py
File metadata and controls
82 lines (59 loc) · 2.66 KB
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
from flask import Flask
import os
import requests
import json
import base64
import websocket
import datetime
import time
import pandas as pd
uaaUrl = "https://yourUAAID.predix-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token"
tsUrl = "https://time-series-store-predix.run.aws-usw02-pr.ice.predix.io/v1/datapoints"
zoneId = "your timeseries zone id"
token = str(base64.b64encode(b'client:secret'))[2:-1]
tstags = "https://time-series-store-predix.run.aws-usw02-pr.ice.predix.io/v1/tags"
ingesturl="wss://gateway-predix-data-services.run.aws-usw02-pr.ice.predix.io/v1/stream/messages"
app = Flask(__name__)
port = int(os.getenv("PORT", 64781))
def doIngest(ingesturl,payload,tsUrl, uaaUrl, token, zoneId,k):
headers = {
'authorization': "Basic " + token,
'cache-control': "no-cache",
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request('POST', uaaUrl, data="grant_type=client_credentials", headers=headers)
token = json.loads(response.text)['access_token']
headers = {
'authorization': "Bearer " + token,
'predix-zone-id': zoneId,
'content-type': "application/json",
'Origin': "https://time-series-store-predix.run.aws-usw02-pr.ice.predix.io",
'cache-control': "no-cache"
}
try:
ws = websocket.create_connection(ingesturl, header=headers)
ws.send(payload)
result = ws.recv()
ws.close()
return result
except Exception:
return "failed"
@app.route('/')
def simulator():
while True:
df=pd.read_csv("zeus.csv")
#j=0
for index, row in df.iterrows():
msg = row
current_time = datetime.datetime.now(datetime.timezone.utc)
unix_timestamp = int(round(current_time.timestamp(),0)) # works if Python >= 3.3
for k in range ((len(msg)-1)):
payload_tm="{\n \"messageId\": \"1453338376231\",\n \"body\": [\n {\n \"name\": \"zeus:"+ str(df.columns[k+1]) +"\",\n \"datapoints\": [\n [\n " +str(unix_timestamp)+"000,\n "+str(round (msg[k+1],2))+",\n 3\n ]\n ],\n \"attributes\": {\n \"host\": \"server1\",\n \"customer\": \"Zeus\"\n }\n }\n ]\n}"
try:
rrr=doIngest(ingesturl,payload_tm,tsUrl, uaaUrl, token, zoneId,k)
except Exception:
rrr="failed"
pass
time.sleep(5)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)