Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 152b541a6c | |||
| 44fe3361a3 | |||
| e43aee9b13 | |||
| d4c69e4b32 | |||
| 28457eb200 | |||
| f1c530e5b1 | |||
| 1e3198d09b | |||
| 965d6dde63 | |||
| e5a65f2dcf | |||
| ad6d369dcf | |||
| 5a4d0b9348 | |||
| 42979daaf2 | |||
| f8172d9323 | |||
| 4256f65161 | |||
| c113139303 | |||
| 6ea0479059 | |||
| 0c673ab5f1 |
@@ -1,4 +1,4 @@
|
||||
name: Build Nuxt App
|
||||
name: Build
|
||||
|
||||
on:
|
||||
push:
|
||||
@@ -6,7 +6,7 @@ on:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build:
|
||||
build-web:
|
||||
runs-on: docker
|
||||
|
||||
defaults:
|
||||
@@ -43,12 +43,63 @@ jobs:
|
||||
cd dist_package
|
||||
zip -r ../quibot-web.zip .
|
||||
|
||||
# Create or update release and upload asset
|
||||
- name: Upload Release
|
||||
uses: softprops/action-gh-release@v1
|
||||
- name: Upload Web artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: quibot-web
|
||||
path: quibot-web/quibot-web.zip
|
||||
|
||||
build-backend:
|
||||
runs-on: docker
|
||||
defaults:
|
||||
run:
|
||||
working-directory: backend
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
apt update
|
||||
apt install -y zip
|
||||
|
||||
- name: Create zip
|
||||
run: |
|
||||
zip -r backend.zip .
|
||||
|
||||
- name: Upload Backend artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: backend
|
||||
path: backend/backend.zip
|
||||
|
||||
|
||||
|
||||
release:
|
||||
runs-on: docker
|
||||
needs: [build-web, build-backend]
|
||||
steps:
|
||||
- name: Download Web Artifact
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: quibot-web
|
||||
path: dist
|
||||
|
||||
- name: Download Backend Artifact
|
||||
uses: actions/download-artifact@v3
|
||||
with:
|
||||
name: backend
|
||||
path: dist
|
||||
|
||||
- name: Create Release
|
||||
uses: https://gitea.com/actions/gitea-release-action@v1
|
||||
with:
|
||||
tag_name: latest
|
||||
name: Latest Build
|
||||
files: quibot-web/quibot-web.zip
|
||||
overwrite_files: true
|
||||
files: |
|
||||
dist/quibot-web.zip
|
||||
dist/backend.zip
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITEATOKEN }}
|
||||
GITEA_TOKEN: ${{ secrets.GITEATOKEN }}
|
||||
|
||||
2
backend/.gitignore
vendored
Normal file
2
backend/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
__pycache__/
|
||||
venv/
|
||||
112
backend/main.py
Normal file
112
backend/main.py
Normal file
@@ -0,0 +1,112 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import RPi.GPIO as GPIO
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
# -------------------------
|
||||
# GPIO SETUP
|
||||
# -------------------------
|
||||
STEP = 23
|
||||
DIR = 24
|
||||
EN = 25
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(STEP, GPIO.OUT)
|
||||
GPIO.setup(DIR, GPIO.OUT)
|
||||
GPIO.setup(EN, GPIO.OUT)
|
||||
|
||||
GPIO.output(EN, GPIO.LOW)
|
||||
|
||||
|
||||
motor_thread = None
|
||||
|
||||
def step_motor(steps, direction, delay=0.001):
|
||||
GPIO.output(DIR, direction)
|
||||
|
||||
for _ in range(steps):
|
||||
GPIO.output(STEP, GPIO.HIGH)
|
||||
time.sleep(delay)
|
||||
GPIO.output(STEP, GPIO.LOW)
|
||||
time.sleep(delay)
|
||||
|
||||
def motor_step(dir):
|
||||
dir_pin = GPIO.HIGH if dir == "forward" else GPIO.LOW
|
||||
time.sleep(0.02) # small delay before starting
|
||||
print("Motor running...")
|
||||
step_motor(200, dir_pin, 0.001)
|
||||
|
||||
|
||||
# -------------------------
|
||||
# SAFE COMMAND WHITELIST
|
||||
# -------------------------
|
||||
COMMANDS = {
|
||||
"restart_nginx": ["sudo", "systemctl", "restart", "nginx"],
|
||||
"uptime": ["uptime"],
|
||||
"update": ["sudo", "apt", "update"]
|
||||
}
|
||||
|
||||
|
||||
# -------------------------
|
||||
# API ENDPOINTS
|
||||
# -------------------------
|
||||
|
||||
@app.post("/run")
|
||||
def run_task(task: str, token: str):
|
||||
if token != "MY_SECRET_TOKEN":
|
||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
||||
|
||||
if task not in COMMANDS:
|
||||
raise HTTPException(status_code=400, detail="Invalid task")
|
||||
|
||||
try:
|
||||
result = subprocess.check_output(COMMANDS[task], text=True)
|
||||
return {"output": result}
|
||||
except subprocess.CalledProcessError as e:
|
||||
return {"error": e.output}
|
||||
|
||||
|
||||
@app.post("/motor/step/forward")
|
||||
def start_motor(token: str):
|
||||
global motor_thread
|
||||
|
||||
if token != "MY_SECRET_TOKEN":
|
||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
||||
|
||||
|
||||
motor_thread = threading.Thread(target=motor_step, args=("forward",), daemon=True)
|
||||
motor_thread.start()
|
||||
|
||||
return {"status": "motor started"}
|
||||
|
||||
@app.post("/motor/step/backwards")
|
||||
def start_motor(token: str):
|
||||
global motor_thread
|
||||
|
||||
if token != "MY_SECRET_TOKEN":
|
||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
||||
|
||||
|
||||
motor_thread = threading.Thread(target=motor_step, args=("backwards",), daemon=True)
|
||||
motor_thread.start()
|
||||
|
||||
return {"status": "motor started"}
|
||||
|
||||
@app.post("/motor/stop")
|
||||
def stop_motor(token: str):
|
||||
if token != "MY_SECRET_TOKEN":
|
||||
raise HTTPException(status_code=403, detail="Unauthorized")
|
||||
|
||||
GPIO.output(EN, GPIO.HIGH) # disable driver
|
||||
|
||||
return {"status": "motor stopped"}
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
def shutdown():
|
||||
global motor_running
|
||||
motor_running = False
|
||||
GPIO.output(EN, GPIO.HIGH)
|
||||
GPIO.cleanup()
|
||||
@@ -1,3 +1,120 @@
|
||||
<template>
|
||||
<h1>Test</h1>
|
||||
</template>
|
||||
<main class="panel">
|
||||
<h1>Quibot Motor Control</h1>
|
||||
|
||||
<div class="actions">
|
||||
<button :disabled="pending" @click="callMotor('step/forward')">
|
||||
{{ pending && action === 'step/forward' ? 'Starting...' : 'Step Forward' }}
|
||||
</button>
|
||||
|
||||
<button :disabled="pending" @click="callMotor('step/backwards')">
|
||||
{{ pending && action === 'step/backwards' ? 'Starting...' : 'Step Backwards' }}
|
||||
</button>
|
||||
|
||||
<button :disabled="pending" class="stop" @click="callMotor('stop')">
|
||||
{{ pending && action === 'stop' ? 'Stopping...' : 'Stop' }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p v-if="message" class="message">
|
||||
{{ message }}
|
||||
</p>
|
||||
|
||||
<p v-if="error" class="error">
|
||||
{{ error }}
|
||||
</p>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
|
||||
type MotorAction = 'step/forward' | 'step/backwards' | 'stop'
|
||||
|
||||
const pending = ref(false)
|
||||
const action = ref<MotorAction | null>(null)
|
||||
const message = ref('')
|
||||
const error = ref('')
|
||||
|
||||
async function callMotor(nextAction: MotorAction) {
|
||||
pending.value = true
|
||||
action.value = nextAction
|
||||
message.value = ''
|
||||
error.value = ''
|
||||
|
||||
try {
|
||||
const response = await $fetch<{ status?: string }>(`/api/motor/${nextAction}`, {
|
||||
method: 'POST',
|
||||
})
|
||||
|
||||
message.value = response.status || `${nextAction} request sent`
|
||||
} catch (err: any) {
|
||||
error.value = err?.data?.statusMessage || err?.data?.message || err?.message || 'Request failed'
|
||||
} finally {
|
||||
pending.value = false
|
||||
action.value = null
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
:global(body) {
|
||||
margin: 0;
|
||||
font-family: Helvetica, Arial, sans-serif;
|
||||
background: #f3f4f6;
|
||||
}
|
||||
|
||||
.panel {
|
||||
min-height: 100vh;
|
||||
display: grid;
|
||||
place-content: center;
|
||||
gap: 1rem;
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
h1 {
|
||||
margin: 0;
|
||||
color: #111827;
|
||||
}
|
||||
|
||||
.actions {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
button {
|
||||
border: 0;
|
||||
border-radius: 0.75rem;
|
||||
padding: 0.9rem 1.4rem;
|
||||
font-size: 1rem;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
background: #2563eb;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button.stop {
|
||||
background: #dc2626;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
opacity: 0.7;
|
||||
cursor: wait;
|
||||
}
|
||||
|
||||
.message,
|
||||
.error {
|
||||
margin: 0;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
|
||||
.message {
|
||||
color: #166534;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #b91c1c;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
export default defineNuxtConfig({
|
||||
compatibilityDate: '2025-07-15',
|
||||
devtools: { enabled: true },
|
||||
runtimeConfig: {
|
||||
quibotBaseUrl: process.env.QUIBOT_BASE_URL || 'http://quibot:8000',
|
||||
quibotToken: process.env.QUIBOT_TOKEN || 'MY_SECRET_TOKEN',
|
||||
},
|
||||
vite: {
|
||||
optimizeDeps: {
|
||||
include: [
|
||||
|
||||
18
quibot-web/server/api/motor/step/[direction].post.ts
Normal file
18
quibot-web/server/api/motor/step/[direction].post.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig()
|
||||
const direction = getRouterParam(event, 'direction')
|
||||
|
||||
if (direction !== 'forward' && direction !== 'backwards') {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: 'Invalid motor direction',
|
||||
})
|
||||
}
|
||||
|
||||
return await $fetch(`${config.quibotBaseUrl}/motor/step/${direction}`, {
|
||||
method: 'POST',
|
||||
query: {
|
||||
token: config.quibotToken,
|
||||
},
|
||||
})
|
||||
})
|
||||
10
quibot-web/server/api/motor/stop.post.ts
Normal file
10
quibot-web/server/api/motor/stop.post.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export default defineEventHandler(async () => {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
return await $fetch(`${config.quibotBaseUrl}/motor/stop`, {
|
||||
method: 'POST',
|
||||
query: {
|
||||
token: config.quibotToken,
|
||||
},
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user