/
main.py
180 lines (137 loc) · 5.02 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
# export PATH="/home/robert/.local/bin"
# C:\Users\robho\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts\uvicorn.exe main:app --reload
# python3.exe -m uvicorn main:app --reload
from queue import Queue
from threading import Thread
import time
from typing import List, Dict, Any
import json
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from controller.configurationController import ConfigurationController
from controller.protokolController import ProtokollController
from controller.serialController import SerialController
from models.command_tally import CommandTally, TallyFormatter
from models.command_values import CommandValues, ValuesFormatter
from models.command_value import CommandValue, ValueFormatter
import logging
app = FastAPI(title="CameraController Backend",
description="Rest Api to control Black Magic cameras connected to the sdi interface of a shield on a arduino board")
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
serial = SerialController()
protocol = ProtokollController('res/PROTOCOL.json')
configuration = ConfigurationController('res/config.yaml')
connection = None
connectionresult = serial.try_connect(0)
connection = connectionresult.getConnection()
command_queue = Queue()
command_thread_running = False
logger.info("Backend Application started")
def command_thread_func():
global command_thread_running
while command_thread_running:
if not command_queue.empty():
command = command_queue.get()
serial.send_command(command)
logger.info(f"Sent command: {command}")
time.sleep(0.1)
else:
time.sleep(0.1)
@app.on_event("startup")
async def startup_event():
global command_thread_running
command_thread_running = True
command_thread = Thread(target=command_thread_func)
command_thread.start()
@app.on_event("shutdown")
async def shutdown_event():
global command_thread_running
command_thread_running = False
@app.get("/groups")
async def get_protocol():
groups = protocol.get_groups()
return {"groups": groups}
@app.post("/groups")
async def save_groups(groups_data: dict):
protocol.save_groups(groups_data)
logger.info("Groups data saved successfully")
return {"message": "Groups data saved successfully"}
@app.post("/config")
async def set_config(config_data: dict):
data = configuration.save_config(config_data)
logger.info("Config data saved successfully")
return {"message": "Config data saved successfully"}
@app.get("/config")
async def get_config():
config = configuration.read_configfile()
return {"config": config}
@app.get("/ports")
async def get_ports():
ports = serial.get_ports()
return {"ports": ports}
@app.get("/connect/{port_id}")
async def connect(port_id: int):
global connection
if connection is not None:
return {"connection": "established"}
connectionresult = serial.try_connect(port_id)
if not connectionresult.connected:
raise HTTPException(
status_code=400, detail=f"Could not connect to port {port_id}: {connectionresult}")
connection = connectionresult.getConnection()
logger.info(f"Successfully connected to port {port_id}")
return {"connection": connectionresult}
@app.get("/disconnect")
async def disconnect():
global connection
if connection is None:
return {"connection": "disconnected"}
connection = None
response = serial.disconnect()
logger.info("Disconnected from serial port")
return response
@app.post("/command/value")
async def send_command_value(command: CommandValue):
formatter = ValueFormatter(command)
command_queue.put(formatter.format())
logger.info(f"Queued command: {formatter.format()}")
return
@app.post("/command/values")
async def send_command_values(command: CommandValues):
formatter = ValuesFormatter(command)
command_queue.put(formatter.format())
logger.info(f"Queued command: {formatter.format()}")
return {"result": "success"}
@app.post("/command/tally")
async def send_command_tally(command: CommandTally):
formatter = TallyFormatter(command)
command_queue.put(formatter.format())
logger.info(f"Queued command: {formatter.format()}")
return {"result": "success"}
@app.get("/")
async def status():
return {"status": status}
@app.get("/read")
async def read():
raw_data = serial.read()
try:
parsed_data = json.loads(raw_data)
except json.JSONDecodeError:
parsed_data = raw_data
response = {"data": parsed_data}
formatted_response = json.dumps(response, indent=4)
logger.info(f"Received data: {formatted_response}")
return response