Shutdown on signal.

This commit is contained in:
yohan 2024-06-06 23:12:34 +02:00
parent 2cbfe5781d
commit 5a9a53ec2f

View File

@ -14,6 +14,8 @@ import requests
import subprocess import subprocess
from threading import Thread from threading import Thread
from threading import Lock from threading import Lock
from threading import Event
import signal
import sqlite3 import sqlite3
# This code has been written for # This code has been written for
@ -156,6 +158,16 @@ def get_forced_mode(cursor):
return data['value'] return data['value']
## ##
stop = Event()
def handler(signum, frame):
global stop
logging.info("Got interrupt: "+str(signum))
stop.set()
logging.info("Shutdown")
signal.signal(signal.SIGTERM,handler)
signal.signal(signal.SIGINT,handler)
app = Flask(__name__) app = Flask(__name__)
app.config.from_mapping(flask_settings) app.config.from_mapping(flask_settings)
@ -183,6 +195,13 @@ Set_mode_resource_fields = {
} }
Set_mode_model = api.model('Set_mode_Model', Set_mode_resource_fields) Set_mode_model = api.model('Set_mode_Model', Set_mode_resource_fields)
#TODO: Add this to swagger documentation
#'Temporary forced modes (will stay in effect for '+str(int(forced_mode_duration))+' seconds):\n'\
#+'\n'.join(['# /'+target+'\n' for target in targets])\
#+'\n'\
#+'Permanent forced modes:\n'\
#+'\n'.join(['# /'+mode+'\n' for mode in modes])
@ns_thermostat.route('/set_mode') @ns_thermostat.route('/set_mode')
class Set_mode_thermostat(Resource): class Set_mode_thermostat(Resource):
@auth_required @auth_required
@ -260,13 +279,12 @@ def thermostat_loop():
dbconn = sqlite3.connect("./instance/thermostat.db") dbconn = sqlite3.connect("./instance/thermostat.db")
cursor = dbconn.cursor() cursor = dbconn.cursor()
logging.info("====== Starting ======")
while True: while True:
# if stop.is_set(): if stop.is_set():
# httpd.shutdown() dbconn.close()
# httpd.server_close() break
# dbconn.close()
# break
if new_forced_mode is not None: if new_forced_mode is not None:
cursor.execute("INSERT OR REPLACE INTO set_mode (value) VALUES ('"+new_forced_mode+"')") cursor.execute("INSERT OR REPLACE INTO set_mode (value) VALUES ('"+new_forced_mode+"')")
@ -368,82 +386,3 @@ t1 = Thread(target=thermostat_loop)
t1.daemon = True t1.daemon = True
t1.start() t1.start()
logging.info("====== Ended successfully ======") logging.info("====== Ended successfully ======")
#import signal
#from threading import Event
#from http.server import BaseHTTPRequestHandler
#import threading
#import socketserver
#
#def api_help():
# return """
#### HTTP API description:
## /status
#
## /help : This message
#
#"""\
#+'Temporary forced modes (will stay in effect for '+str(int(forced_mode_duration))+' seconds):\n'\
#+'\n'.join(['# /'+target+'\n' for target in targets])\
#+'\n'\
#+'Permanent forced modes:\n'\
#+'\n'.join(['# /'+mode+'\n' for mode in modes])
#
#logging.info("====== Starting ======")
#stop = Event()
#last_data = {}
#
#def handler(signum, frame):
# global stop
# logging.info("Got interrupt: "+str(signum))
# stop.set()
# logging.info("Shutdown")
#
#signal.signal(signal.SIGTERM,handler)
#signal.signal(signal.SIGINT,handler)
#
#class MyHandler(BaseHTTPRequestHandler):
# def do_GET(self):
# global new_forced_mode
# request = self.path[1:]
# if request in targets or request in modes:
# self.send_response(200)
# new_forced_mode = request
# elif self.path == '/status':
# self.send_response(200)
# self.send_header('Content-type', 'text/plain')
# self.end_headers()
# self.wfile.write(status_as_text().encode("utf-8"))
# elif self.path == '/help':
# self.send_response(200)
# self.send_header('Content-type', 'text/plain')
# self.end_headers()
# self.wfile.write(api_help().encode("utf-8"))
# else:
# self.send_response(404)
# # This rewrites the BaseHTTP logging function
# def log_message(self, format, *args):
# if verbosity == 'INFO':
# xprint("%s - - [%s] %s" %
# (self.address_string(),
# self.log_date_time_string(),
# format%args))
#
#class WebThread(threading.Thread):
# def run(self):
# httpd.serve_forever()
#
#httpd = socketserver.TCPServer(("", http_port), MyHandler, bind_and_activate=False)
#httpd.allow_reuse_address = True
#httpd.server_bind()
#httpd.server_activate()
#webserver_thread = WebThread()
#webserver_thread.start()
#