Skip to content

Commit

Permalink
Send file (#59)
Browse files Browse the repository at this point in the history
* send a file with smbclient

* upgrade fastapi

* add python-multipart

* upgrade dev deps

* fix fastapi

* fix ruff args

* Add TMP_PATH to config.py
  • Loading branch information
yuichiroaoki authored Dec 26, 2023
1 parent 90a3e0d commit bd00961
Show file tree
Hide file tree
Showing 10 changed files with 734 additions and 629 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ jobs:
- name: Lint with ruff
run: |
# stop the build if there are Python syntax errors or undefined names
poetry run ruff --format=github --select=E9,F63,F7,F82 --target-version=py37 .
poetry run ruff --output-format=github --select=E9,F63,F7,F82 --target-version=py37 .
# default set of ruff rules with GitHub Annotations
poetry run ruff --format=github --target-version=py37 .
poetry run ruff --output-format=github --target-version=py37 .
- name: Run the automated tests
run: poetry run pytest -v --cov=server
Expand Down
2 changes: 2 additions & 0 deletions server/data/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
1,244 changes: 625 additions & 619 deletions server/poetry.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.11,<3.13"
fastapi = {extras = ["all"], version = "^0.101.0"}
fastapi = {extras = ["all"], version = "^0.106.0"}
uvicorn = {extras = ["standard"], version = "^0.23.2"}
scipy = "^1.11.1"
paho-mqtt = "^1.6.1"
Expand All @@ -22,9 +22,9 @@ networkx = "^3.2.1"


[tool.poetry.group.dev.dependencies]
black = "^23.7.0"
pytest = "^7.4.0"
ruff = "^0.0.283"
black = "^23.12.1"
pytest = "^7.4.3"
ruff = "^0.1.9"
pytest-cov = "^4.1.0"

[build-system]
Expand Down
1 change: 1 addition & 0 deletions server/server/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

MODEL_PATH = "data/3dmodel"
GCODE_PATH = "data/gcode"
TMP_PATH = "data/tmp"

SENSOR_HOSTNAME = "opencmm"
SENSOR_IP = f"{SENSOR_HOSTNAME}.local"
Expand Down
20 changes: 18 additions & 2 deletions server/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
update_mtct_latency,
)
from server.measure.gcode import get_gcode_line_path
from server.config import MYSQL_CONFIG, MODEL_PATH, get_config, update_conf
from server.config import MYSQL_CONFIG, MODEL_PATH, TMP_PATH, get_config, update_conf
from server.model import (
get_3dmodel_data,
get_recent_3dmodel_data,
Expand Down Expand Up @@ -179,7 +179,6 @@ def update_trace_config(_conf: TraceConfig):
@app.post("/upload/3dmodel")
async def upload_3dmodel(file: UploadFile):
"""Upload 3d model file"""
# save image
file_extension = file.filename.split(".")[-1]
if file_extension not in ["stl", "STL"]:
raise HTTPException(status_code=400, detail="File extension not supported")
Expand Down Expand Up @@ -585,6 +584,23 @@ def get_first_machine():
return machines[0]


@app.post("/send/file")
async def send_file(file: UploadFile):
"""Send a file to the CNC machine"""
filepath = f"{TMP_PATH}/{file.filename}"
with open(filepath, "wb") as buffer:
buffer.write(await file.read())

machines = machine.get_machines(MYSQL_CONFIG)
if len(machines) == 0:
raise HTTPException(status_code=400, detail="No machine registered")

machine_info = machines[0]
machine.send_file_with_smbclient(machine_info, filepath)
os.remove(filepath)
return {"status": "ok", "filename": file.filename}


@app.post("/update_machine")
def update_machine(machine_info: machine.MachineInfo):
machine.update_machine(MYSQL_CONFIG, machine_info)
Expand Down
5 changes: 4 additions & 1 deletion ui/src/lib/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
}
},
"menu": {
"settings": "Settings"
"settings": "Settings",
"sendFile": "Send a file"
},
"settings": {
"language": {
Expand Down Expand Up @@ -180,6 +181,8 @@
"apply": "Apply",
"saveSuccess": "Saved successfully",
"saveFailed": "Save failed",
"sendSuccess": "Sent successfully",
"sendFailed": "Send failed",
"delete": "Delete",
"ok": "OK",
"seconds": "seconds",
Expand Down
5 changes: 4 additions & 1 deletion ui/src/lib/i18n/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@
}
},
"menu": {
"settings": "設定"
"settings": "設定",
"sendFile": "ファイル送信"
},
"settings": {
"language": {
Expand Down Expand Up @@ -180,6 +181,8 @@
"apply": "適用",
"saveSuccess": "保存しました",
"saveFailed": "保存に失敗しました",
"sendSuccess": "送信しました",
"sendFailed": "送信に失敗しました",
"delete": "削除",
"ok": "OK",
"seconds": "",
Expand Down
1 change: 1 addition & 0 deletions ui/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<HeaderAction bind:isOpen>
<HeaderPanelLinks>
<HeaderPanelLink href="/settings/language">{$_('menu.settings')}</HeaderPanelLink>
<HeaderPanelLink href="/sendfile">{$_('menu.sendFile')}</HeaderPanelLink>
</HeaderPanelLinks>
</HeaderAction>
</HeaderUtilities>
Expand Down
73 changes: 73 additions & 0 deletions ui/src/routes/sendfile/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<script lang="ts">
import axios from 'axios';
import { BACKEND_URL } from '$lib/constants/backend';
import { Content, FileUploader, ToastNotification } from 'carbon-components-svelte';
import { _ } from 'svelte-i18n';
let updateSuccess = false;
let error: string | null = null;
const handleFileChange = async (event: CustomEvent) => {
console.log(event);
const fileInput = event.detail;
if (fileInput === null) return;
const file = fileInput[0];
if (!file) return;
const formData = new FormData();
formData.append('file', file);
try {
const res = await axios.post(`${BACKEND_URL}/send/file`, formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
});
console.log(res);
if (res.status === 200 && res.data['status'] === 'ok') {
updateSuccess = true;
} else {
error = res.data['message'];
}
} catch (err) {
console.error(err);
}
};
</script>

<Content>
<div id="file-uploader">
<FileUploader
labelTitle={$_('menu.sendFile')}
buttonLabel={$_('home.upload3dmodel.buttonLabel')}
status="complete"
on:change={handleFileChange}
/>
</div>

{#if updateSuccess}
<ToastNotification
kind="success"
title={$_('common.sendSuccess')}
timeout={3000}
on:close={() => (updateSuccess = false)}
/>
{/if}

{#if error}
<ToastNotification
kind="error"
title={$_('common.sendFailed')}
subtitle={error}
timeout={3000}
on:close={() => (error = null)}
/>
{/if}
</Content>

<style>
#file-uploader {
margin: 1rem;
}
</style>

0 comments on commit bd00961

Please sign in to comment.