feat: send simple command ON/OFF to machine

This commit is contained in:
Alberto Lerda 2023-02-09 18:55:54 +01:00
parent ee81c95b32
commit e74b2ed725
5 changed files with 44 additions and 13 deletions

8
example/send_off.json Normal file
View File

@ -0,0 +1,8 @@
{
"command": "OFF",
"eddsa_public_key": "BmW1a6x43P4Rae9B4hS67PhHTCUShXAGy4K8tQtUfa8L",
"eddsa_signature": "3YUhX39JihVQySNAQVD4zS1PMhjsVdyndffoZmBQGUiKEH8EuAU4b2hF9fYSYutjPGdVnHtNhq3fgo9PChsp1455",
"service": "urn:fabaccess:resource:Another",
"timestamp": "1675964281",
"token": "todo"
}

3
example/send_off.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
curl -X POST -H 'Content-Type:application/json' -d "@send_off.json" "http://localhost:8000/command"

8
example/send_on.json Normal file
View File

@ -0,0 +1,8 @@
{
"command": "ON",
"eddsa_public_key": "BmW1a6x43P4Rae9B4hS67PhHTCUShXAGy4K8tQtUfa8L",
"eddsa_signature": "GJxBHa1wtb2WhnwPZjs4tCTavy7tQAA7py46xMtTfDtuqwUhMHdzmfqoKCbMzUHJvbcRxkcJD7Zz1qP3d6AETMc",
"service": "urn:fabaccess:resource:Another",
"timestamp": "1675964281",
"token": "todo"
}

3
example/send_on.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
curl -X POST -H 'Content-Type:application/json' -d "@send_on.json" "http://localhost:8000/command"

35
main.py
View File

@ -4,7 +4,7 @@ import pyfabapi.fabapi.user_system
from typing import Union from typing import Union
from fastapi import FastAPI from fastapi import FastAPI, HTTPException
from pydantic import BaseModel from pydantic import BaseModel
from zenroom import zencode_exec from zenroom import zencode_exec
@ -17,10 +17,10 @@ from dotenv import load_dotenv
load_dotenv() load_dotenv()
class Config: class Config:
fab_host: string fab_host: str
fab_port: int fab_port: int
fab_user: string fab_user: str
fab_pass: string fab_pass: str
def __init__(self): def __init__(self):
self.fab_host = os.getenv("FAB_HOST") self.fab_host = os.getenv("FAB_HOST")
@ -66,7 +66,7 @@ async def new_session(cmd: NewSession):
return {"token": "todo"} return {"token": "todo"}
@app.get("/command") @app.post("/command")
async def read_root(cmd: Command): async def read_root(cmd: Command):
zen_result = zencode_exec(zen_verify_cmd, keys=cmd.json()) zen_result = zencode_exec(zen_verify_cmd, keys=cmd.json())
@ -79,15 +79,24 @@ async def read_root(cmd: Command):
raise HTTPException(status_code=500, detail="Invalid signature") raise HTTPException(status_code=500, detail="Invalid signature")
# a service "urn:fabaccess:resource:Another"
session = await fabapi.connect(conf.fab_host, conf.fab_port, conf.fab_user, conf.fab_pass) session = await fabapi.connect(conf.fab_host, conf.fab_port, conf.fab_user, conf.fab_pass)
if session == None:
raise HTTPException(status_code=500, detail="Fabaccess not available")
info = session.machineSystem.info info = session.machineSystem.info
ma = await info.getMachineURN(cmd.service).a_wait() ma = await info.getMachineURN(cmd.service).a_wait()
if ma.just: try:
print(ma) if ma.just:
print(ma.just) if cmd.command == "ON":
await ma.just.use.use().a_wait() await ma.just.use.use().a_wait()
else: elif cmd.command == "OFF":
raise HTTPException(status_code=500, detail="No such resource") await ma.just.inuse.giveBack().a_wait()
else:
raise HTTPException(status_code=500, detail="Fabaccess not available")
else:
raise HTTPException(status_code=500, detail="No such resource")
except:
raise HTTPException(status_code=500, detail="Could not complete command")
return {"success": True}