-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (103 loc) · 3.16 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
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from pydantic_settings import BaseSettings
from typing import Any, Dict, List, Union
# from lib.waveshare_epd import epd7in5_V2
import platform
from importlib import import_module
import re
from DestBoardTable import DestBoardTable
class Settings(BaseSettings):
"""
Settings format.
Parameters
----------
BaseSettings : BaseSettings
pydantic model class
"""
table_row: int = 6
table_column: int = 2
margin_width: int = 5
margin_height: int = 5
padding_left: int = 5
padding_top: int = 5
cell_name_ratio: float = 0.3
font: str = "/usr/share/fonts/opentype/noto/NotoSansCJK-Bold.ttc"
font_size: int = 36
class Config:
env_file = '.env', 'prod.env', 'stg.env', 'dev.env'
env_file_encoding = 'utf-8'
class PostItem(BaseModel):
"""
HTTP PUT request body format.
Parameters
----------
BaseModel : BaseModel
pydantic model class.
"""
name: Union[str, None] = None
status: Union[str, None] = None
present: Union[bool, None] = None
settings = Settings(_env_file='dev.env', _env_file_encoding='utf-8')
app = FastAPI()
epd = None
# Windows又はmacの場合
if re.search("Windows|macOS", platform.platform()):
epdDummyModule = import_module("epd7in5_dummy")
epd = epdDummyModule.EPD()
# その他Linux(RaspberryPi)の場合
else:
epdModule = import_module(".epd7in5_V2", "lib.waveshare_epd")
epd = epdModule.EPD()
# configure CORS.
origins = [
"*",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["X-Requested-With", "X-HTTP-Method-Override", "Content-Type", "Accept"]
)
table = DestBoardTable(
epd.width, epd.height,
settings.margin_width, settings.margin_height,
settings.padding_left, settings.padding_top,
settings.table_row, settings.table_column, settings.cell_name_ratio,
settings.font, settings.font_size,
'./table.json')
@app.on_event("startup")
async def startup_event():
epd.init()
epd.Clear()
epd.display(epd.getbuffer(table.Himage))
@app.on_event("shutdown")
async def shutdown_event():
epd.init()
epd.Clear()
epd.sleep()
@app.post("/{row}/{column}")
async def set(row: int, column: int, item: PostItem):
if not item.name is None:
table.set_name(column, row, item.name)
if not item.status is None:
table.set_status(column, row, item.status)
if not item.present is None:
table.set_present(column, row, item.present)
epd.display(epd.getbuffer(table.Himage))
return {"status": "update success."}
@app.get("/{row}/{column}")
async def get(row: int, column: int) -> Dict[str, Any]:
return {
"name": table.get_name(column, row),
"status": table.get_status(column, row),
"present": table.get_present(column, row)}
@app.get("/")
async def root() -> List[List[Dict[str, Any]]]:
return table.get_all()
if __name__ == '__main__':
# start http server
uvicorn.run(app)