craftbeerpi4-pione/cbpi/http_endpoints/http_step.py

277 lines
6.8 KiB
Python
Raw Normal View History

2021-02-16 20:37:51 +01:00
from cbpi.controller.step_controller import StepController
from cbpi.api.dataclasses import Props, Step
2019-01-02 21:20:44 +01:00
from aiohttp import web
2019-01-05 20:43:48 +01:00
from cbpi.api import *
2019-01-02 21:20:44 +01:00
2021-01-22 23:25:20 +01:00
class StepHttpEndpoints():
2019-01-02 21:20:44 +01:00
def __init__(self, cbpi):
2021-01-22 23:25:20 +01:00
self.cbpi = cbpi
2021-02-16 20:37:51 +01:00
self.controller : StepController = cbpi.step
2021-01-22 23:25:20 +01:00
self.cbpi.register(self, "/step2")
2019-01-02 21:20:44 +01:00
2021-01-22 23:25:20 +01:00
def create_dict(self, data):
return dict(name=data["name"], id=data["id"], type=data.get("type"), status=data["status"],props=data["props"], state_text=data["instance"].get_state())
2019-01-02 21:20:44 +01:00
@request_mapping(path="/", auth_required=False)
async def http_get_all(self, request):
2019-07-27 21:08:19 +02:00
2019-01-02 21:20:44 +01:00
"""
---
2019-07-27 21:08:19 +02:00
description: Get all steps
2019-01-02 21:20:44 +01:00
tags:
- Step
responses:
2021-01-22 23:25:20 +01:00
"200":
2019-01-02 21:20:44 +01:00
description: successful operation
"""
2021-01-22 23:25:20 +01:00
return web.json_response(data=self.controller.get_state())
2019-01-02 21:20:44 +01:00
@request_mapping(path="/", method="POST", auth_required=False)
async def http_add(self, request):
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
"""
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
---
2021-01-22 23:25:20 +01:00
description: Add
2019-01-02 21:20:44 +01:00
tags:
- Step
parameters:
2019-01-02 21:20:44 +01:00
- in: body
name: body
description: Created an step
2021-01-22 23:25:20 +01:00
required: true
2019-01-02 21:20:44 +01:00
schema:
type: object
responses:
2021-01-22 23:25:20 +01:00
"200":
2019-01-02 21:20:44 +01:00
description: successful operation
"""
2021-02-16 20:37:51 +01:00
2021-01-22 23:25:20 +01:00
data = await request.json()
2021-02-16 20:37:51 +01:00
step = Step(name=data.get("name"), props=Props(data.get("props", {})), type=data.get("type"))
response_data = await self.controller.add(step)
return web.json_response(data=response_data.to_dict())
2019-01-02 21:20:44 +01:00
@request_mapping(path="/{id}", method="PUT", auth_required=False)
async def http_update(self, request):
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
"""
---
2021-01-22 23:25:20 +01:00
description: Update
2019-01-02 21:20:44 +01:00
tags:
- Step
parameters:
- in: body
name: body
2021-01-22 23:25:20 +01:00
description: Created an kettle
2019-01-02 21:20:44 +01:00
required: false
schema:
type: object
responses:
"200":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
data = await request.json()
id = request.match_info['id']
2021-02-16 20:37:51 +01:00
step = Step(id, data.get("name"), Props(data.get("props", {})), data.get("type"))
return web.json_response((await self.controller.update(step)).to_dict())
2019-01-02 21:20:44 +01:00
@request_mapping(path="/{id}", method="DELETE", auth_required=False)
2021-01-22 23:25:20 +01:00
async def http_delete(self, request):
2019-01-02 21:20:44 +01:00
"""
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
---
2021-01-22 23:25:20 +01:00
description: Delete
2019-01-02 21:20:44 +01:00
tags:
- Step
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
id = request.match_info['id']
await self.controller.delete(id)
return web.Response(status=204)
2019-01-02 21:20:44 +01:00
2021-01-22 23:25:20 +01:00
@request_mapping(path="/next", method="POST", auth_required=False)
async def http_next(self, request):
"""
2021-01-22 23:25:20 +01:00
---
2021-01-22 23:25:20 +01:00
description: Next
tags:
- Step
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
await self.controller.next()
return web.Response(status=204)
2021-01-22 23:25:20 +01:00
@request_mapping(path="/move", method="PUT", auth_required=False)
async def http_move(self, request):
"""
---
description: Move
tags:
- Step
parameters:
- in: body
name: body
description: Created an kettle
required: false
schema:
type: object
properties:
id:
type: string
direction:
type: "integer"
format: "int64"
responses:
"204":
description: successful operation
2019-01-02 21:20:44 +01:00
"""
2019-07-31 07:58:54 +02:00
data = await request.json()
2021-01-22 23:25:20 +01:00
await self.controller.move(data["id"], data["direction"])
return web.Response(status=204)
2019-01-02 21:20:44 +01:00
2021-01-22 23:25:20 +01:00
@request_mapping(path="/start", method="POST", auth_required=False)
2019-01-02 21:20:44 +01:00
async def http_start(self, request):
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
"""
---
2021-01-22 23:25:20 +01:00
description: Move
2019-01-02 21:20:44 +01:00
tags:
- Step
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
await self.controller.start()
2019-01-04 09:29:09 +01:00
return web.Response(status=204)
2021-01-22 23:25:20 +01:00
@request_mapping(path="/stop", method="POST", auth_required=False)
async def http_stop(self, request):
2019-01-02 21:20:44 +01:00
"""
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
---
2021-01-22 23:25:20 +01:00
description: Stop Step
2019-01-02 21:20:44 +01:00
tags:
- Step
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
await self.controller.stop()
return web.Response(status=204)
2019-01-02 21:20:44 +01:00
2021-01-22 23:25:20 +01:00
@request_mapping(path="/reset", method="POST", auth_required=False)
async def http_reset(self, request):
2019-01-02 21:20:44 +01:00
"""
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
---
2021-01-22 23:25:20 +01:00
description: Move
2019-01-02 21:20:44 +01:00
tags:
- Step
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
await self.controller.reset_all()
2019-01-02 21:20:44 +01:00
return web.Response(status=204)
2021-01-22 23:25:20 +01:00
@request_mapping(path="/basic", method="PUT", auth_required=False)
async def http_save_basic(self, request):
2019-01-02 21:20:44 +01:00
"""
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
---
2021-01-22 23:25:20 +01:00
description: Move
2019-01-02 21:20:44 +01:00
tags:
- Step
responses:
"204":
description: successful operation
"""
2019-01-21 22:33:29 +01:00
data = await request.json()
2021-01-22 23:25:20 +01:00
await self.controller.save_basic(data)
return web.Response(status=204)
@request_mapping(path="/action/{id}", method="POST", auth_required=False)
async def http_call_action(self, request):
"""
---
description: Call action
tags:
- Step
parameters:
- name: "id"
in: "path"
description: "Step id"
required: true
type: "integer"
format: "int64"
- in: body
name: body
description: call action
required: false
schema:
type: object
properties:
action:
type: string
parameter:
type: "array"
items:
type: string
responses:
"204":
description: successful operation
"""
data = await request.json()
id = request.match_info['id']
await self.controller.call_action(id,data.get("action"), data.get("parameter",[]))
return web.Response(status=204)
2021-02-27 20:09:19 +01:00
@request_mapping(path="/clear", method="POST", auth_required=False)
async def http_clear(self, request):
"""
---
description: Clear ALll
tags:
- Step
responses:
"204":
description: successful operation
"""
await self.controller.clear()
return web.Response(status=204)