-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
352 lines (276 loc) · 12.5 KB
/
main.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from typing import Optional, List
import os
import socket
import datetime as dt
from fastapi import Depends, FastAPI, HTTPException, Request, Header
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from sqlmodel import Session, select
import json
import urllib.request
# local imports
from src.db.db import get_session, sqlite_engine_gpio
from src.db.models_gpio import GPIO, GPIOSelect, GPIORead, GPIOUpdate
from templates.components.schemas import GPIOForm, idForm, initializationForm
app = FastAPI()
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
# Define Local Variables
hostname = socket.gethostname()
ipAddress = "10.10.0.32"
welcomeGPIO = "GPIO Control Interface"
welcomeHome = "GPIO Home Page"
welcomenodeRed = "Node-RED Server"
welcomeXTerm = "Embedded Terminal Page"
welcomeInitialize = "GPIO Initialization Page"
welcomeInitializeResponse = "GPIO Persistence Activated"
interactiveGPIOPinout = "https://pinout.xyz/"
table_pic = "/static/images/happy.png"
'''
Set the startup stuff into a login page endpoint so
when you login it will redirect you to the home page and load all the gpios
---> OR use the htmx code here which is also documented in /templates/gpio.html:
<div hx-get="/" hx-target="#gpios" hx-trigger="load">
<img alt="Result loading..." class="htmx-indicator" width="150" src="/img/bars.svg"/>
</div>
'''
# You can combine FastAPI features like automatic path parameter validation to get models by ID.
@app.on_event("startup")
async def set_gpio():
"""
Asynchronous function to set up GPIO on startup and start wssh on 10.10.0.32.
"""
'''
shellCommand = str(f"wssh --address""=""{ipAddress}")
os.system(shellCommand)
with Session(sqlite_engine_gpio) as session:
gpios = session.exec(select(GPIO)).all()
'''
@app.get("/home", response_class=HTMLResponse)
def loadHome(request: Request):
"""
Load the home page and return an HTML response.
Parameters:
request (Request): The incoming request object.
Returns:
TemplateResponse: The HTML response for the home page.
"""
context = {"request": request, "name": welcomeHome}
return templates.TemplateResponse("general_pages/home.html", context)
@app.get("/initialize", response_class=HTMLResponse)
async def get_initialize(request: Request, hx_request: Optional[str] = Header(None)):
"""
Get the initialize endpoint. Takes a request and an optional hx_request header.
Returns an HTMLResponse with the appropriate context.
"""
context = {"request": request, "name": welcomeInitialize}
hx_context = {"request": request, "name": welcomeInitialize}
if hx_request:
return templates.TemplateResponse("general_pages/initialize.html", hx_context)
return templates.TemplateResponse("general_pages/initialize.html", context)
@app.post("/initialize", response_class=HTMLResponse)
async def post_initialize(request: Request, hx_request: Optional[str] = Header(None), form_data: initializationForm = Depends(initializationForm.as_form)):
"""
A function to handle POST requests to '/initialize', with parameters request, hx_request, and form_data.
It sets up GPIO permissions and returns an HTMLResponse to confirm the status of the permissions of
/dev/gpiomem
Check that /dev/gpiomem has the correct permissions.
ls -l /dev/gpiomem
crw-rw---- 1 root gpio 244, 0 Dec 28 22:51 /dev/gpiomem
If it doesn't then set the correct permissions as follows
sudo chown root:gpio /dev/gpiomem
sudo chmod g+rw /dev/gpiomem
"""
form_data = dict(form_data)
# Extracting key-value of dictionary in variables
password = list(form_data.values())[1]
shellCommand = (f"echo {password} | sudo -S chown root:gpio /dev/gpiomem && sudo chmod g+rw /dev/gpiomem")
os.system(shellCommand)
status = os.system("ls -l /dev/gpiomem")
statusExpected = "crw-rw---- 1 root gpio 244, 0 /dev/gpiomem"
context = {"request": request, "name": welcomeInitializeResponse}
hx_context = {"request": request, "name": welcomeInitializeResponse, "statusExpected": statusExpected, "status": status}
if hx_request:
return templates.TemplateResponse("general_pages/initializeConfirmation.html", hx_context)
return templates.TemplateResponse("general_pages/initializeConfirmation.html", context)
@app.get("/", response_model=List[GPIO], response_class=HTMLResponse)
async def get_gpio(request: Request, hx_request: Optional[str] = Header(None), *, session: Session = Depends(get_session)):
"""
Get GPIO data and Nasa's Astronomy Picture of the Day for the HTML response.
Parameters:
- request: Request
- hx_request: Optional[str] = Header(None)
- session: Session = Depends(get_session)
Returns:
- TemplateResponse: "components/gpio.html" if hx_request is provided
- TemplateResponse: "general_pages/gpio.html" if hx_request is not provided
"""
# Nasa's Astronomy Picture of the Day
'''
apod_url = urllib.request.urlopen(
"https://api.nasa.gov/planetary/apod?api_key=6AFdWx2zcPvtlUT7T9EU4Gtcnfge1QIqDjObalYL")
apod_url = apod_url.read().decode("utf-8")
apod_url = json.loads(apod_url)
#print(apod_url)
apod_pic = (apod_url['url'])
#print(table_pic)
'''
# return All GPIOs
gpios = session.exec(select(GPIO)).all()
#context = {"request": request, "name": welcomeGPIO, "gpios": gpios, "apod_pic": apod_pic}
#hx_context = {"request": request, "gpios": gpios, "apod_pic": apod_pic}
# return offline_*_context when running app offline
offline_hx_context = {"request": request, "gpios": gpios}
offline_context = {"request": request, "name": welcomeGPIO, "gpios": gpios}
#print(f"from @app.get / endpoint >> {gpios}")
if hx_request:
return templates.TemplateResponse("components/gpio.html", offline_hx_context)
return templates.TemplateResponse("general_pages/gpio.html", offline_context)
@app.post("/", response_class=HTMLResponse)
async def update_GPIOs(request: Request, hx_request: Optional[str] = Header(None), form_data: GPIOForm = Depends(GPIOForm.as_form)):
"""
Submit form data to the server and update the database with the selected GPIO data.
Args:
request (Request): The incoming request object.
hx_request (str, optional): The hx_request header value. Defaults to None.
form_data (GPIOForm): The form data containing GPIO information.
Returns:
HTMLResponse: A response containing HTML content.
"""
#print(form_data)
form_data = dict(form_data)
changed_key = "changed"
changed_val = dt.datetime.now().strftime("%a %b %d %Y %I:%M:%S %p")
form_data[changed_key] = changed_val
#print(form_data)
# Extracting key-value of dictionary in variables
gpio_key = list(form_data.keys())[0]
gpio_val = list(form_data.values())[0]
type_key = list(form_data.keys())[1]
type_val = list(form_data.values())[1]
state_key = list(form_data.keys())[2]
state_val = list(form_data.values())[2]
usedfor_key = list(form_data.keys())[3]
usedfor_val = list(form_data.values())[3]
changed_key = list(form_data.keys())[4]
changed_val = list(form_data.values())[4]
# Returning a confirmation message
#print(gpio_val, usedfor_val, type_val, state_val, changed_val)
# Update the database with the selected GPIO data
id = gpio_val
with Session(sqlite_engine_gpio) as session:
db_GPIO = session.get(GPIO, id)
if not db_GPIO:
raise HTTPException(status_code=404, detail="GPIO not found")
for key, value in form_data.items():
setattr(db_GPIO, key, value)
session.add(db_GPIO)
session.commit()
session.refresh(db_GPIO)
with Session(sqlite_engine_gpio) as session:
gpios = session.exec(select(GPIO)).all()
os.system(f" python ./scripts/GPIO_set.py --gpio={gpio_val} --type={type_val} --state={state_val}")
hx_context = {"request": request, "gpios": gpios}
context = {'request': request, "name": welcomeGPIO, "gpios": gpios}
if hx_request:
return templates.TemplateResponse("components/gpio.html", hx_context)
return templates.TemplateResponse("general_pages/gpio.html", context)
@app.get("/gpio/", response_model=List[GPIO])
def read_GPIOs(*, session: Session = Depends(get_session)):
"""
Read all GPIO entries and return a list of GPIO objects.
Returns:
List[GPIO]: List of GPIO objects.
"""
gpios = session.exec(select(GPIO)).all()
context = {"name": welcomeGPIO, "gpios": gpios}
return templates.TemplateResponse("general_pages/gpio.html", context)
@app.patch("/gpio/{id}", response_model=GPIORead)
def update_GPIO(id: int, gpio: GPIOUpdate):
"""
Update GPIO by id with new data and return the updated GPIO object.
Params:
- id: int
- gpio: GPIOUpdate
Returns:
- GPIORead
Raises:
- HTTPException: If GPIO not found
"""
with Session(sqlite_engine_gpio) as session:
db_GPIO = session.get(GPIO, id)
if not db_GPIO:
raise HTTPException(status_code=404, detail="GPIO not found")
gpio_data = gpio.model_dump(exclude_unset=True)
for key, value in gpio_data.items():
setattr(db_GPIO, key, value)
session.add(db_GPIO)
session.commit()
session.refresh(db_GPIO)
gpios = session.exec(select(GPIO)).all()
return db_GPIO
@app.post("/idForm", response_model=List[GPIO], response_class=HTMLResponse)
def read_GPIO(request: Request, hx_request: Optional[str] = Header(None), form_data: idForm = Depends(idForm.as_form)):
"""
Read GPIO from idForm and return HTMLResponse
Args:
request: Request - The incoming request
hx_request: Optional[str] - Header value
form_data: idForm - Form data
Returns:
List[GPIO]: List of GPIO objects
"""
form_data = dict(form_data)
# Extracting key-value of dictionary in variables
gpio_key = list(form_data.keys())[0]
id = list(form_data.values())[0]
with Session(sqlite_engine_gpio) as session:
gpios = session.exec(select(GPIO).where(GPIO.gpio == id))
print(f"from sql response {gpios}")
context = {"request": request, "name": welcomeGPIO, "gpios": gpios}
hx_context = {"request": request, "name": welcomeGPIO, "gpios": gpios}
if hx_request:
return templates.TemplateResponse("components/gpio.html", hx_context)
return templates.TemplateResponse("general_pages/gpio.html", context)
@app.get("/nodeRed", response_class=HTMLResponse)
def nodeRed(request: Request):
"""
This function handles the GET request for '/nodeRed' and returns an HTML response.
Args:
request (Request): The request object
Returns:
TemplateResponse: The HTML response with the context
"""
#ipAddress = socket.gethostbyname(socket.gethostname())
print(ipAddress)
context = {"request": request, "name": welcomenodeRed, "ipAddress": ipAddress}
return templates.TemplateResponse("general_pages/nodeRed.html", context)
@app.get("/terminal", response_class=HTMLResponse)
def loadXTerm(request: Request):
"""
Function to handle GET requests to /terminal and load XTerm, taking a Request object as parameter and returning an HTMLResponse.
Inspired from: https://github.com/samuelcolvin/pydantic/blob/master/examples/fastapi_example.py
"""
#ipAddress = socket.gethostbyname(socket.gethostname())
context = {"request": request, "name": welcomeXTerm, "ipAddress": ipAddress}
return templates.TemplateResponse("general_pages/terminal.html", context)
@app.get("/interactiveGPIOPinout", response_class=HTMLResponse)
def interactiveGPIOP(request: Request):
"""
An API endpoint that renders an interactive GPIO pinout HTML page.
From the amazing website: https://pinout.xyz/
Parameters:
request (Request): The request object
Returns:
TemplateResponse: The HTML page with the GPIO pinout
"""
#ipAddress = socket.gethostbyname(socket.gethostname())
context = {"request": request, "name": interactiveGPIOPinout}
return templates.TemplateResponse("general_pages/interactiveGPIOPinout.html", context)
'''
@app.post("/GPIO_Type_Input", response_model=List[GPIO])
@app.post("/GPIO_Type_Output", response_model=List[GPIO])
@app.post("/GPIO_State_Low", response_model=List[GPIO])
@app.post("/GPIO_State_High", response_model=List[GPIO])
'''