-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_api_server.py
94 lines (85 loc) · 3.12 KB
/
rest_api_server.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
import numpy as np
from fastapi import FastAPI, Header, HTTPException
from fastapi.responses import JSONResponse, FileResponse
from pydantic import BaseModel
from stepanalyzer.errors import SysError, StabilityError, PropertyError, InputError
from stepanalyzer.system import sys
from stepanalyzer.step import StepAnalyzer
class submit_body(BaseModel):
num: list
den: list
time_points: list
app = FastAPI()
@app.get('/sysinfo')
async def sysInfo(numerator: str = Header(None), denominator: str = Header(None)):
try:
num = list(map(int, numerator.split(' ')))
den = list(map(int, denominator.split(' ')))
syst = sys(num, den)
_ = syst.transfer_function()
try:
props = syst.properties()
type = syst.getType()
except PropertyError as e:
props = ['undefined', 'undefined']
type = 'undefined'
st_state = syst.steadystate()
if np.isinf(st_state):
st_state = 'infinity'
content = {
'tf': syst.getTfprint(),
'order': syst.getOrder(),
'type': type,
'stability': syst.getStability(),
'wn': props[0],
'zeta': props[1],
'steadystate': st_state
}
resp = JSONResponse(content=content)
return resp
except SysError as e:
raise HTTPException(status_code=400, detail=str(e))
except StabilityError as e:
raise HTTPException(status_code=400, detail=str(e))
except PropertyError as e:
raise HTTPException(status_code=400, detail=str(e))
except InputError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get('/stepanalysis')
async def sysAnalysis(numerator: str = Header(None), denominator: str = Header(None),
time_points: str = Header(None)):
try:
num = list(map(int, numerator.split(' ')))
den = list(map(int, denominator.split(' ')))
t_points = list(map(int, time_points.split(' ')))
t = np.linspace(t_points[0],
t_points[1],
t_points[2])
stepAnal = StepAnalyzer(num, den, t)
peak = tuple(stepAnal.peak())
settling = stepAnal.settle()
try:
rise = stepAnal.rise()
except:
rise = 'Undefined'
overshoot = stepAnal.overshoot()
content = {
'peak': dict(peakTime=peak[2], peakvalue=peak[0]),
'rise': rise,
'settling': settling,
'overshoot': overshoot
}
resp = JSONResponse(content=content)
return resp
except SysError as e:
raise HTTPException(status_code=400, detail=str(e))
except StabilityError as e:
raise HTTPException(status_code=400, detail=str(e))
except PropertyError as e:
raise HTTPException(status_code=400, detail=str(e))
except InputError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))