mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-25 16:38:36 +01:00
Merge pull request #25 from avollkopf/development
Merge from Development Branch
This commit is contained in:
commit
1efe31cb25
6 changed files with 129 additions and 6 deletions
|
@ -1 +1 @@
|
||||||
__version__ = "4.0.0.57"
|
__version__ = "4.0.0.59"
|
||||||
|
|
37
cbpi/cli.py
37
cbpi/cli.py
|
@ -61,6 +61,12 @@ def create_config_file():
|
||||||
srcfile = os.path.join(os.path.dirname(__file__), "config", "craftbeerpi.service")
|
srcfile = os.path.join(os.path.dirname(__file__), "config", "craftbeerpi.service")
|
||||||
destfile = os.path.join(".", 'config')
|
destfile = os.path.join(".", 'config')
|
||||||
shutil.copy(srcfile, destfile)
|
shutil.copy(srcfile, destfile)
|
||||||
|
|
||||||
|
if os.path.exists(os.path.join(".", 'config', "chromium.desktop")) is False:
|
||||||
|
srcfile = os.path.join(os.path.dirname(__file__), "config", "chromium.desktop")
|
||||||
|
destfile = os.path.join(".", 'config')
|
||||||
|
shutil.copy(srcfile, destfile)
|
||||||
|
|
||||||
print("Config Folder created")
|
print("Config Folder created")
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,6 +190,21 @@ def plugins_add(package_name):
|
||||||
return
|
return
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if package_name == 'chromium':
|
||||||
|
print("Add chromium.desktop to /etc/xdg/autostart/")
|
||||||
|
try:
|
||||||
|
if os.path.exists(os.path.join("/etc/xdg/autostart/","chromium.desktop")) is False:
|
||||||
|
srcfile = os.path.join(".", "config", "chromium.desktop")
|
||||||
|
destfile = os.path.join("/etc/xdg/autostart/")
|
||||||
|
shutil.copy(srcfile, destfile)
|
||||||
|
print("Copied chromium.desktop to /etc/xdg/autostart/")
|
||||||
|
else:
|
||||||
|
print("chromium.desktop is already located in /etc/xdg/autostart/")
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(".", 'config', "config.yaml"), 'rt') as f:
|
with open(os.path.join(".", 'config', "config.yaml"), 'rt') as f:
|
||||||
data = yaml.load(f, Loader=yaml.FullLoader)
|
data = yaml.load(f, Loader=yaml.FullLoader)
|
||||||
|
@ -230,6 +251,18 @@ def plugin_remove(package_name):
|
||||||
return
|
return
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if package_name == 'chromium':
|
||||||
|
print("Remove chromium.desktop from /etc/xdg/autostart/")
|
||||||
|
try:
|
||||||
|
if os.path.exists(os.path.join("/etc/xdg/autostart/","chromium.desktop")) is True:
|
||||||
|
os.remove(os.path.join("/etc/xdg/autostart/","chromium.desktop"))
|
||||||
|
print("Deleted chromium.desktop from /etc/xdg/autostart/")
|
||||||
|
else:
|
||||||
|
print("chromium.desktop is not located in /etc/xdg/autostart/")
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -366,14 +399,14 @@ def plugins():
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.argument('name')
|
@click.argument('name')
|
||||||
def add(name):
|
def add(name):
|
||||||
'''Activate Plugin'''
|
'''Activate Plugin, autostart or chromium '''
|
||||||
plugins_add(name)
|
plugins_add(name)
|
||||||
|
|
||||||
|
|
||||||
@click.command()
|
@click.command()
|
||||||
@click.argument('name')
|
@click.argument('name')
|
||||||
def remove(name):
|
def remove(name):
|
||||||
'''Deactivate Plugin'''
|
'''Deactivate Plugin, autostart or chromium'''
|
||||||
plugin_remove(name)
|
plugin_remove(name)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -109,7 +109,7 @@ class SatelliteController:
|
||||||
async with AsyncExitStack() as stack:
|
async with AsyncExitStack() as stack:
|
||||||
self.tasks = set()
|
self.tasks = set()
|
||||||
stack.push_async_callback(cancel_tasks, self.tasks)
|
stack.push_async_callback(cancel_tasks, self.tasks)
|
||||||
self.client = Client(self.host, port=self.port, username=self.username, password=self.password, will=Will(topic="cbpi/diconnect", payload="CBPi Server Disconnected"))
|
self.client = Client(self.host, port=self.port, username=self.username, password=self.password, will=Will(topic="cbpi/disconnect", payload="CBPi Server Disconnected"))
|
||||||
|
|
||||||
await stack.enter_async_context(self.client)
|
await stack.enter_async_context(self.client)
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
import psutil
|
import psutil
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import json
|
||||||
import aiohttp
|
import aiohttp
|
||||||
from voluptuous.schema_builder import message
|
from voluptuous.schema_builder import message
|
||||||
from cbpi.api.dataclasses import NotificationAction, NotificationType
|
from cbpi.api.dataclasses import NotificationAction, NotificationType
|
||||||
|
@ -40,6 +41,61 @@ class SystemController:
|
||||||
dir_name = pathlib.Path(os.path.join(".", 'config'))
|
dir_name = pathlib.Path(os.path.join(".", 'config'))
|
||||||
shutil.make_archive(output_filename, 'zip', dir_name)
|
shutil.make_archive(output_filename, 'zip', dir_name)
|
||||||
|
|
||||||
|
async def downloadlog(self, logtime):
|
||||||
|
filename = "cbpi4.log"
|
||||||
|
fullname = pathlib.Path(os.path.join(".",filename))
|
||||||
|
pluginname = "cbpi4_plugins.txt"
|
||||||
|
fullpluginname = pathlib.Path(os.path.join(".",pluginname))
|
||||||
|
actorname = "cbpi4_actors.txt"
|
||||||
|
fullactorname = pathlib.Path(os.path.join(".",actorname))
|
||||||
|
sensorname = "cbpi4_sensors.txt"
|
||||||
|
fullsensorname = pathlib.Path(os.path.join(".",sensorname))
|
||||||
|
kettlename = "cbpi4_kettles.txt"
|
||||||
|
fullkettlename = pathlib.Path(os.path.join(".",kettlename))
|
||||||
|
|
||||||
|
output_filename="cbpi4_log.zip"
|
||||||
|
|
||||||
|
if logtime == "b":
|
||||||
|
os.system('journalctl -b -u craftbeerpi.service > {}'.format(fullname))
|
||||||
|
else:
|
||||||
|
os.system('journalctl --since \"{} hours ago\" -u craftbeerpi.service > {}'.format(logtime, fullname))
|
||||||
|
|
||||||
|
os.system('cbpi plugins > {}'.format(fullpluginname))
|
||||||
|
|
||||||
|
try:
|
||||||
|
actors = self.cbpi.actor.get_state()
|
||||||
|
json.dump(actors['data'],open(fullactorname,'w'),indent=4, sort_keys=True)
|
||||||
|
sensors = self.cbpi.sensor.get_state()
|
||||||
|
json.dump(sensors['data'],open(fullsensorname,'w'),indent=4, sort_keys=True)
|
||||||
|
kettles = self.cbpi.kettle.get_state()
|
||||||
|
json.dump(kettles['data'],open(fullkettlename,'w'),indent=4, sort_keys=True)
|
||||||
|
except Exception as e:
|
||||||
|
logging.info(e)
|
||||||
|
self.cbpi.notify("Error", "Creation of files failed: {}".format(e), NotificationType.ERROR)
|
||||||
|
|
||||||
|
try:
|
||||||
|
zipObj=zipfile.ZipFile(output_filename , 'w', zipfile.ZIP_DEFLATED)
|
||||||
|
zipObj.write(fullname)
|
||||||
|
zipObj.write(fullpluginname)
|
||||||
|
zipObj.write(fullactorname)
|
||||||
|
zipObj.write(fullsensorname)
|
||||||
|
zipObj.write(fullkettlename)
|
||||||
|
zipObj.close()
|
||||||
|
except Exception as e:
|
||||||
|
logging.info(e)
|
||||||
|
self.cbpi.notify("Error", "Zip creation failed: {}".format(e), NotificationType.ERROR)
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.remove(fullname)
|
||||||
|
os.remove(fullpluginname)
|
||||||
|
os.remove(fullactorname)
|
||||||
|
os.remove(fullsensorname)
|
||||||
|
os.remove(fullkettlename)
|
||||||
|
except Exception as e:
|
||||||
|
logging.info(e)
|
||||||
|
self.cbpi.notify("Error", "Removal of original files failed: {}".format(e), NotificationType.ERROR)
|
||||||
|
|
||||||
|
|
||||||
def allowed_file(self, filename, extension):
|
def allowed_file(self, filename, extension):
|
||||||
return '.' in filename and filename.rsplit('.', 1)[1] in set([extension])
|
return '.' in filename and filename.rsplit('.', 1)[1] in set([extension])
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ from cbpi.api import *
|
||||||
import xml.etree.ElementTree
|
import xml.etree.ElementTree
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from voluptuous.schema_builder import message
|
from voluptuous.schema_builder import message
|
||||||
from cbpi.api.dataclasses import NotificationAction, NotificationType
|
from cbpi.api.dataclasses import NotificationAction, NotificationType, Actor, Sensor, Kettle
|
||||||
from cbpi.controller.kettle_controller import KettleController
|
from cbpi.controller.kettle_controller import KettleController
|
||||||
from cbpi.api.base import CBPiBase
|
from cbpi.api.base import CBPiBase
|
||||||
from cbpi.api.config import ConfigType
|
from cbpi.api.config import ConfigType
|
||||||
|
|
|
@ -146,6 +146,40 @@ class SystemHttpEndpoints:
|
||||||
await response.write_eof()
|
await response.write_eof()
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
|
@request_mapping("/log/{logtime}/", method="GET", name="BackupConfig", auth_required=False)
|
||||||
|
async def downloadlog(self, request):
|
||||||
|
"""
|
||||||
|
---
|
||||||
|
description: Zip and download craftbeerpi.service log
|
||||||
|
tags:
|
||||||
|
- System
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: successful operation
|
||||||
|
content: # Response body
|
||||||
|
application/zip: # Media type
|
||||||
|
"""
|
||||||
|
logtime = request.match_info['logtime']
|
||||||
|
await self.controller.downloadlog(logtime)
|
||||||
|
filename = "cbpi4_log.zip"
|
||||||
|
file_name = pathlib.Path(os.path.join(".", filename))
|
||||||
|
|
||||||
|
response = web.StreamResponse(
|
||||||
|
status=200,
|
||||||
|
reason='OK',
|
||||||
|
headers={'Content-Type': 'application/zip'},
|
||||||
|
)
|
||||||
|
await response.prepare(request)
|
||||||
|
with open(file_name, 'rb') as file:
|
||||||
|
for line in file.readlines():
|
||||||
|
await response.write(line)
|
||||||
|
|
||||||
|
await response.write_eof()
|
||||||
|
os.remove(file_name)
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
@request_mapping("/restore", method="POST", name="RestoreConfig", auth_required=False)
|
@request_mapping("/restore", method="POST", name="RestoreConfig", auth_required=False)
|
||||||
async def restore(self, request):
|
async def restore(self, request):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue