-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
91 lines (77 loc) · 2.82 KB
/
Copy pathmain.py
File metadata and controls
91 lines (77 loc) · 2.82 KB
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
from src.web_server import create_app
from src.buttons import ControlButton
from src.zabbix_exporter import ZabbixExporter
from src.logger import CSVLogger
from src.displays import AQM1602
from src.sensors import BME280
from src.utils import load_config
import time
import os
import logging
import threading
# ログ設定
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')
def main():
config = load_config('config.json')
# モジュールの初期化
sensor = BME280(config['sensor'])
logger = CSVLogger('logs/env_data.csv')
exporter = ZabbixExporter()
lcd = AQM1602(config['lcd']) if config['lcd'].get('enabled') else None
buttons = ControlButton(config['gpio']['start'], config['gpio']['stop'])
# Webサーバー起動処理
app = create_app(config['web'])
threading.Thread(
target=lambda: app.run(
host=config['web']['host'],
port=config['web']['port'],
threaded=True,
use_reloader=False
),
daemon=True
).start()
logging.info(f"Webサーバー起動: {config['web']['host']}:{config['web']['port']}")
is_running = False
if lcd:
lcd.display("WellComme !!", "Ready to start", force=True)
try:
while True:
# 1. ボタン操作の判定
if buttons.is_start_pressed():
is_running = True
if lcd:
lcd.display("Start monitor!!", "Logging...", force=True)
action = buttons.get_stop_action()
if action == 'LONG':
if lcd:
lcd.display("Force Shutdown", "Bye!!", force=True)
os.system("sudo shutdown -h now")
break
elif action == 'DOUBLE':
if lcd:
lcd.display("Shutdown !!", "Shutting down...", force=True)
os.system("sudo shutdown -h now")
break
elif action == 'SINGLE':
is_running = False
if lcd:
lcd.display("Maintenance !!", "Stopped", force=True)
# 2. 計測・出力処理
if is_running:
try:
data = sensor.read_sensor_data()
logger.log(data)
exporter.export(data)
if lcd:
lcd.display(f"t {data['temp']:.1f} C", f"h {data['hum']:.1f} %")
except Exception as e:
logging.error(f"計測エラー: {e}")
time.sleep(1) # CPU負荷軽減のためのインターバル
except KeyboardInterrupt:
logging.info("システム停止")
finally:
# クリーンアップ処理
if lcd:
lcd.display("System Stop", "Goodbye", force=True)
if __name__ == "__main__":
main()