diff --git a/.idea/workspace.xml b/.idea/workspace.xml index cb85dd7..b5bc4e0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,18 +3,16 @@ + + - - - - - + + + - - - - + + @@ -633,13 +369,6 @@ - - - - - - - @@ -647,6 +376,13 @@ + + + + + + + - @@ -949,18 +689,18 @@ - + - + - - + + - + @@ -1024,82 +764,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1131,13 +795,6 @@ - - - - - - - @@ -1193,20 +850,6 @@ - - - - - - - - - - - - - - @@ -1254,16 +897,6 @@ - - - - - - - - - - @@ -1278,16 +911,6 @@ - - - - - - - - - - @@ -1305,23 +928,6 @@ - - - - - - - - - - - - - - - - - @@ -1342,16 +948,6 @@ - - - - - - - - - - @@ -1359,39 +955,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -1406,6 +972,13 @@ + + + + + + + @@ -1413,13 +986,157 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -1427,10 +1144,17 @@ - + + + + + + + + - - + + diff --git a/cbpi/api/step.py b/cbpi/api/step.py index 08c2626..da350bc 100644 --- a/cbpi/api/step.py +++ b/cbpi/api/step.py @@ -47,6 +47,7 @@ class CBPiSimpleStep(metaclass=ABCMeta): ''' while self.running(): + try: await self.run_cycle() except Exception as e: diff --git a/cbpi/cli.py b/cbpi/cli.py index 0599827..445d853 100644 --- a/cbpi/cli.py +++ b/cbpi/cli.py @@ -1,3 +1,4 @@ +import argparse import logging from cbpi.craftbeerpi import CraftBeerPi @@ -24,18 +25,33 @@ def create_home_folder_structure(): pathlib.Path(os.path.join(".", 'logs/sensors')).mkdir(parents=True, exist_ok=True) pathlib.Path(os.path.join(".", 'config')).mkdir(parents=True, exist_ok=True) +def copy_splash(): + srcfile = os.path.join(os.path.dirname(__file__), "config", "splash.png") + destfile = os.path.join(".", 'config') + shutil.copy(srcfile, destfile) def main(): + parser = argparse.ArgumentParser(description='Welcome to CraftBeerPi 4') + parser.add_argument("action", type=str, help="start,stop,restart,setup") - create_home_folder_structure() - create_plugin_file() - create_config_file() + args = parser.parse_args() - #logging.basicConfig(level=logging.INFO, filename='./logs/app.log', filemode='a', format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') + logging.basicConfig(level=logging.INFO, filename='./logs/app.log', filemode='a', format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(name)s - %(message)s') - cbpi = CraftBeerPi() - cbpi.start() + if args.action == "setup": + create_home_folder_structure() + create_plugin_file() + create_config_file() + copy_splash() + + if args.action == "start": + + cbpi = CraftBeerPi() + cbpi.start() + + parser.print_help() + diff --git a/cbpi/config/splash.png b/cbpi/config/splash.png new file mode 100644 index 0000000..68086b5 Binary files /dev/null and b/cbpi/config/splash.png differ diff --git a/cbpi/controller/actor_controller.py b/cbpi/controller/actor_controller.py index b3957f4..b8170e9 100644 --- a/cbpi/controller/actor_controller.py +++ b/cbpi/controller/actor_controller.py @@ -35,7 +35,7 @@ class ActorController(CRUDController): return dict(items=self.cache,types=self.types) async def _init_actor(self, actor): - print("INit ACTOR") + try: if actor.type in self.types: cfg = actor.config.copy() diff --git a/cbpi/controller/step_controller.py b/cbpi/controller/step_controller.py index f5b1fcc..f51773c 100644 --- a/cbpi/controller/step_controller.py +++ b/cbpi/controller/step_controller.py @@ -204,3 +204,16 @@ class StepController(CRUDController): await self.model.reset_all_steps() + async def sort(self, data): + await self.model.sort(data) + + async def _pre_add_callback(self, data): + order = await self.model.get_max_order() + data["order"] = 1 if order is None else order + 1 + data["state"] = "I" + return await super()._pre_add_callback(data) + + + + + diff --git a/cbpi/craftbeerpi.py b/cbpi/craftbeerpi.py index 1306e51..9e846c2 100644 --- a/cbpi/craftbeerpi.py +++ b/cbpi/craftbeerpi.py @@ -53,7 +53,7 @@ async def error_middleware(request, handler): return web.json_response(status=500, data={'error': message}) except MultipleInvalid as ex: return web.json_response(status=500, data={'error': str(ex)}) - return web.json_response({'error': message}) + return web.json_response(status=500, data={'error': message}) class CraftBeerPi(): @@ -160,7 +160,7 @@ class CraftBeerPi(): } switcher[http_method]() - print("ADDD ", routes) + if url_prefix != "/": logger.debug("URL Prefix: %s " % (url_prefix,)) sub = web.Application() @@ -169,7 +169,7 @@ class CraftBeerPi(): sub.add_routes([web.static('/static', static, show_index=False)]) self.app.add_subapp(url_prefix, sub) else: - print("ADDD ", routes) + self.app.add_routes(routes) def _swagger_setup(self): diff --git a/cbpi/database/model.py b/cbpi/database/model.py index c8fb5f2..c4caa3c 100644 --- a/cbpi/database/model.py +++ b/cbpi/database/model.py @@ -81,11 +81,35 @@ class StepModel(DBModel): @classmethod async def reset_all_steps(cls): - print("RESET ALL STEPS NOW") + async with aiosqlite.connect(DATABASE_FILE) as db: await db.execute("UPDATE %s SET state = 'I', stepstate = NULL , start = NULL, end = NULL " % cls.__table_name__) await db.commit() + @classmethod + async def sort(cls, new_order): + + async with aiosqlite.connect(DATABASE_FILE) as db: + for key, value in new_order.items(): + print("ORDER", key, value) + await db.execute("UPDATE %s SET '%s' = ? WHERE id = ?" % (cls.__table_name__, "order"), (value, key)) + await db.commit() + + @classmethod + async def get_max_order(cls): + + + async with aiosqlite.connect(DATABASE_FILE) as db: + db.row_factory = aiosqlite.Row + db.row_factory = DBModel.dict_factory + async with db.execute("SELECT max(step.'order') as 'order' FROM %s" % cls.__table_name__) as cursor: + row = await cursor.fetchone() + if row is not None: + return row.get("order") + else: + return 0 + + class DashboardModel(DBModel): __fields__ = ["name"] __table_name__ = "dashboard" diff --git a/cbpi/extension/dummystep/__init__.py b/cbpi/extension/dummystep/__init__.py index 670934e..435dc92 100644 --- a/cbpi/extension/dummystep/__init__.py +++ b/cbpi/extension/dummystep/__init__.py @@ -16,9 +16,13 @@ class CustomStepCBPi(CBPiSimpleStep): async def run_cycle(self): print("RUN", self.name) self.i = self.i + 1 - if self.i == 5: + await asyncio.sleep(5) + print("WAIT") + self.next() + #if self.i == 5: # print("NEXT") - self.next() + + #self.cbpi.notify(key="step", message="HELLO FROM STEP") diff --git a/cbpi/http_endpoints/http_curd_endpoints.py b/cbpi/http_endpoints/http_curd_endpoints.py index 4501436..66028aa 100644 --- a/cbpi/http_endpoints/http_curd_endpoints.py +++ b/cbpi/http_endpoints/http_curd_endpoints.py @@ -32,7 +32,7 @@ class HttpCrudEndpoints(): @request_mapping(path="/", method="POST", auth_required=False) async def http_add(self, request): data = await request.json() - print(data) + obj = await self.controller.add(**data) return web.json_response(obj, dumps=json_dumps) diff --git a/cbpi/http_endpoints/http_step.py b/cbpi/http_endpoints/http_step.py index 2d0d86c..e05e82f 100644 --- a/cbpi/http_endpoints/http_step.py +++ b/cbpi/http_endpoints/http_step.py @@ -224,3 +224,10 @@ class StepHttpEndpoints(HttpCrudEndpoints): """ await self.cbpi.bus.fire("step/stop") return web.Response(status=204) + + @request_mapping(path="/sort", method="POST", auth_required=False) + async def http_sort(self, request): + data = await request.json() + print(data) + await self.cbpi.step.sort(data) + return web.Response(status=204) \ No newline at end of file diff --git a/config/config.yaml b/config/config.yaml index 7d36e3b..4d7b7ce 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -2,7 +2,7 @@ name: CraftBeerPi version: 4.0 -index_url: /api/doc +index_url: /ui/static/index.html port: 8080 diff --git a/config/splash.png b/config/splash.png new file mode 100644 index 0000000..68086b5 Binary files /dev/null and b/config/splash.png differ diff --git a/craftbeerpi.db b/craftbeerpi.db index cd41b60..cbd2457 100644 Binary files a/craftbeerpi.db and b/craftbeerpi.db differ