/
Could.py
215 lines (191 loc) · 5.21 KB
/
Could.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
import time
import requests
import numpy as np
from sklearn.linear_model import OrthogonalMatchingPursuit
import json
import gc
import os
import sys
Y = 52
M = 129
N = 256
L = Y/N*100
Q = [[0], [0]]
pId = []
Bstart = False
real = []
imag = []
realZ = []
imagZ = []
array = []
i = 0
urlSensor = ''
id_rompi = ""
id_sensor = ""
id_pasien = ""
data = ""
PPG = []
EKG = []
EMG = []
SUHU = []
AcceX = []
AcceY = []
AcceZ = []
ID = []
DataX = []
start = time.time()
for x in range(Y-2):
Q.append([0])
for x in range(Y):
for y in range(M-1):
Q[x].append(0)
Log = 0
Gaussian = np.loadtxt(fname="Gaussian52x129.txt")
for x in range(Y):
for y in range(M):
Q[x][y] = Gaussian[Log]
Log += 1
def restart_program():
"""Restarts the current program.
Note: this function does not return. Any cleanup action (like
saving data) must be done before calling this function."""
led.off()
print("Auto-reconnect")
python = sys.executable
os.execl(python, python, * sys.argv)
def kirim_():
data = {
"id_rompi": "002",
"id_sensor": "All01",
"id_pasien": ID,
"dataAccelerometer_X": AcceX,
"dataAccelerometer_Y": AcceY,
"dataAccelerometer_Z": AcceZ,
"dataSuhu": SUHU,
"dataEKG": EKG,
"dataPPG": PPG,
"dataEMG": EMG,
"dataSPO2": SpO,
"dataBPM": HR
}
url_POST = (
'https://bysonicsalpha0003.herokuapp.com/dataAllSensor/save')
response = requests.post(url_POST, None, data)
print(f"Request returned {response.status_code} : '{response.reason}'")
def CS_(real2, imag2):
gc.collect()
omp1 = OrthogonalMatchingPursuit()
omp1.fit(Q, real2)
coefreal = omp1.coef_
omp2 = OrthogonalMatchingPursuit()
omp2.fit(Q, imag2)
coefimag = omp2.coef_
realQ = coefreal[0]
realW = coefreal[M-1]
realX = coefreal[1:M-1]
realY = realX[::-1]
realZ = []
realZ = np.append(realZ, realQ)
realZ = np.append(realZ, realX)
realZ = np.append(realZ, realW)
realZ = np.append(realZ, realY)
imagQ = coefimag[0]
imagW = coefimag[M-1]
imagX = coefimag[1:M-1]
imagY = imagX[::-1]*-1
imagZ = []
imagZ = np.append(imagZ, imagQ)
imagZ = np.append(imagZ, imagX)
imagZ = np.append(imagZ, imagW)
imagZ = np.append(imagZ, imagY)
array = []
for x in range(N):
com = complex(realZ[x], imagZ[x])
array = np.append(array, com)
ftx = []
ftx = np.fft.ifft(array)
arr = []
arr = np.array(ftx.real)
arr2 = []
arr2 = arr.tolist()
myList = []
myList = [int(x) for x in arr2]
return myList
def Cek():
global DataX
x = requests.get(
'https://bysonicsalpha0003.herokuapp.com/rekonstruksiSensor/Lastest')
DataX = json.loads(x.text)[0]
# print(DataX)
try:
Cek()
Id = (DataX["_id"])
pId = (DataX["_id"])
print("connect to MongoDB")
except:
id = []
pId = []
print("Could not connect to MongoDB")
i = 0
while True:
try:
Cek()
Id = (DataX["_id"])
if pId != Id:
start = time.time()
print(f'Terdapat Data Baru, Id ={Id}')
Hreal = []
Himag = []
try:
HrealPPG = (DataX["dataPPGReal"])
HimagPPG = (DataX["dataPPGImag"])
HrealEKG = (DataX["dataEKGReal"])
HimagEKG = (DataX["dataEKGImag"])
HrealACCX = (DataX["dataAccelerometer_XReal"])
HimagACCX = (DataX["dataAccelerometer_XImag"])
HrealACCY = (DataX["dataAccelerometer_YReal"])
HimagACCY = (DataX["dataAccelerometer_YImag"])
HrealACCZ = (DataX["dataAccelerometer_ZReal"])
HimagACCZ = (DataX["dataAccelerometer_ZImag"])
HrealEMG = (DataX["dataEMGReal"])
HimagEMG = (DataX["dataEMGImag"])
HrealSUHU = (DataX["dataSuhuReal"])
HimagSUHU = (DataX["dataSuhuImag"])
SpO = (DataX["dataSPO2"])
HR = (DataX["dataBPM"])
ID = (DataX["id_pasien"])
print("Ada Data")
except:
print("Belum Ada Data")
try:
PPG = CS_(HrealPPG, HimagPPG)
EKG = CS_(HrealEKG, HimagEKG)
AcceX = CS_(HrealACCX, HimagACCX)
AcceY = CS_(HrealACCY, HimagACCY)
AcceZ = CS_(HrealACCZ, HimagACCZ)
SUHU = CS_(HrealSUHU, HimagSUHU)
EMG = [abs(number) if number >=
200 else 0 for number in CS_(HrealEMG, HimagEMG)]
print("CS OK")
except:
print("CS Error")
#print(SUHU)
pId = Id
try:
kirim_()
# pass
except:
print("Kirim Error")
end = time.time()
runTime = end - start
print(f"Runtime of the program is {runTime} Second")
print(f'Waiting for new data, Id = {Id}')
else:
pass
except:
print("Error")
time.sleep(10)
try:
pId = Id
except:
pId = []