2018-11-18 23:09:17 +01:00
|
|
|
import datetime
|
2019-01-28 22:21:31 +01:00
|
|
|
import re
|
|
|
|
|
2019-07-31 07:58:54 +02:00
|
|
|
import aiohttp
|
2018-11-01 19:50:04 +01:00
|
|
|
from aiohttp import web
|
2019-01-28 22:21:31 +01:00
|
|
|
import os
|
2018-11-01 19:50:04 +01:00
|
|
|
from aiojobs.aiohttp import get_scheduler_from_app
|
|
|
|
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.api import *
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2019-01-05 20:43:48 +01:00
|
|
|
from cbpi.utils import json_dumps
|
2019-01-04 09:29:09 +01:00
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
|
|
|
|
class SystemController():
|
|
|
|
|
2018-11-01 21:25:42 +01:00
|
|
|
def __init__(self, cbpi):
|
|
|
|
self.cbpi = cbpi
|
|
|
|
self.service = cbpi.actor
|
2019-07-31 07:58:54 +02:00
|
|
|
|
2018-11-01 21:25:42 +01:00
|
|
|
self.cbpi.register(self, "/system")
|
2019-07-31 07:58:54 +02:00
|
|
|
self.cbpi.app.on_startup.append(self.check_for_update)
|
|
|
|
|
|
|
|
|
|
|
|
async def check_for_update(self, app):
|
|
|
|
timeout = aiohttp.ClientTimeout(total=1)
|
|
|
|
async with aiohttp.ClientSession(timeout=timeout) as session:
|
|
|
|
async with session.post('http://localhost:2202/check', json=dict(version=app["cbpi"].version)) as resp:
|
|
|
|
if (resp.status == 200):
|
|
|
|
data = await resp.json()
|
|
|
|
print(data)
|
|
|
|
|
|
|
|
|
2018-11-01 19:50:04 +01:00
|
|
|
|
2019-01-07 22:05:52 +01:00
|
|
|
@request_mapping("/", method="GET", auth_required=False)
|
2019-01-17 22:11:55 +01:00
|
|
|
async def state(self, request):
|
2019-01-21 22:37:00 +01:00
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Get complete system state
|
|
|
|
tags:
|
|
|
|
- System
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
2019-01-04 09:29:09 +01:00
|
|
|
return web.json_response(data=dict(
|
|
|
|
actor=self.cbpi.actor.get_state(),
|
|
|
|
sensor=self.cbpi.sensor.get_state(),
|
|
|
|
kettle=self.cbpi.kettle.get_state(),
|
2019-01-17 22:11:55 +01:00
|
|
|
step=await self.cbpi.step.get_state(),
|
2019-01-07 22:05:52 +01:00
|
|
|
dashboard=self.cbpi.dashboard.get_state(),
|
|
|
|
translations=self.cbpi.translation.get_all(),
|
|
|
|
config=self.cbpi.config.get_state())
|
2019-01-04 09:29:09 +01:00
|
|
|
, dumps=json_dumps)
|
|
|
|
|
2018-11-04 01:55:54 +01:00
|
|
|
@request_mapping("/restart", method="POST", name="RestartServer", auth_required=False)
|
|
|
|
def restart(self, request):
|
2019-01-21 22:37:00 +01:00
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Restart System - Not implemented
|
|
|
|
tags:
|
|
|
|
- System
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
2018-11-04 01:55:54 +01:00
|
|
|
return web.Response(text="NOT IMPLEMENTED")
|
|
|
|
|
|
|
|
@request_mapping("/shutdown", method="POST", name="ShutdownSerer", auth_required=False)
|
2019-01-28 22:21:31 +01:00
|
|
|
def shutdown(self, request):
|
2019-01-21 22:37:00 +01:00
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Shutdown System - Not implemented
|
|
|
|
tags:
|
|
|
|
- System
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
2018-11-04 01:55:54 +01:00
|
|
|
return web.Response(text="NOT IMPLEMENTED")
|
|
|
|
|
|
|
|
@request_mapping("/jobs", method="GET", name="get_jobs", auth_required=False)
|
2018-11-01 19:50:04 +01:00
|
|
|
def get_all_jobs(self, request):
|
2019-01-21 22:37:00 +01:00
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Get all running Jobs
|
|
|
|
tags:
|
|
|
|
- System
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
2018-11-01 21:25:42 +01:00
|
|
|
scheduler = get_scheduler_from_app(self.cbpi.app)
|
2018-11-18 15:40:10 +01:00
|
|
|
result = []
|
2018-11-01 19:50:04 +01:00
|
|
|
for j in scheduler:
|
2018-11-18 15:40:10 +01:00
|
|
|
try:
|
|
|
|
result.append(dict(name=j.name, type=j.type, time=j.start_time))
|
|
|
|
except:
|
|
|
|
pass
|
2018-11-30 23:27:11 +01:00
|
|
|
return web.json_response(data=result)
|
|
|
|
|
|
|
|
@request_mapping("/events", method="GET", name="get_all_events", auth_required=False)
|
|
|
|
def get_all_events(self, request):
|
2019-01-21 22:37:00 +01:00
|
|
|
"""
|
|
|
|
---
|
|
|
|
description: Get list of all registered events
|
|
|
|
tags:
|
|
|
|
- System
|
|
|
|
responses:
|
|
|
|
"200":
|
|
|
|
description: successful operation
|
|
|
|
"""
|
2019-01-07 22:05:52 +01:00
|
|
|
return web.json_response(data=self.cbpi.bus.dump())
|
|
|
|
|
2019-01-28 22:21:31 +01:00
|
|
|
@request_mapping(path="/logs", auth_required=False)
|
|
|
|
async def http_get_log(self, request):
|
|
|
|
result = []
|
|
|
|
file_pattern = re.compile("^(\w+.).log(.?\d*)")
|
|
|
|
for filename in sorted(os.listdir("./logs"), reverse=True):#
|
|
|
|
if file_pattern.match(filename):
|
|
|
|
result.append(filename)
|
|
|
|
|
|
|
|
return web.json_response(result)
|
|
|
|
|
|
|
|
@request_mapping(path="/logs/{name}", method="DELETE", auth_required=False)
|
|
|
|
async def http_delete_log(self, request):
|
|
|
|
log_name = request.match_info['name']
|
|
|
|
file_patter = re.compile("^(\w+.).log(.?\d*)")
|
|
|
|
file_sensor_log = re.compile("^sensor_(\d).log(.?\d*)")
|
|
|
|
|
|
|
|
if file_patter.match(log_name):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@request_mapping(path="/logs", method="DELETE", auth_required=False)
|
|
|
|
async def http_delete_logs(self, request):
|
|
|
|
|
|
|
|
sensor_log_pattern = re.compile("sensor_([\d]).log$")
|
|
|
|
sensor_log_pattern2 = re.compile("sensor_([\d]).log.[\d]*$")
|
|
|
|
|
|
|
|
app_log_pattern = re.compile("app.log$")
|
|
|
|
|
|
|
|
for filename in sorted(os.listdir("./logs"), reverse=True):#
|
|
|
|
if app_log_pattern.match(filename):
|
|
|
|
with open(os.path.join("./logs/%s" % filename), 'w'):
|
|
|
|
pass
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
for filename in sorted(os.listdir("./logs/sensors"), reverse=True):
|
|
|
|
|
|
|
|
if sensor_log_pattern.match(filename):
|
|
|
|
with open(os.path.join("./logs/sensors/%s" % filename), 'w'):
|
|
|
|
pass
|
|
|
|
continue
|
|
|
|
elif sensor_log_pattern2.match(filename):
|
|
|
|
os.remove(os.path.join("./logs/sensors/%s" % filename))
|
|
|
|
|
|
|
|
return web.Response(status=204)
|