craftbeerpi4-pione/cbpi/http_endpoints/http_step.py

211 lines
5.1 KiB
Python
Raw Permalink Normal View History

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
2019-01-02 21:20:44 +01:00
self.controller = 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:
- 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-01-22 23:25:20 +01:00
data = await request.json()
result = await self.controller.add(data)
return web.json_response(self.create_dict(result))
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']
result = await self.controller.update(id, data)
print("RESULT", result)
return web.json_response(self.create_dict(result))
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)