ein-dateien backend erstellt
This commit is contained in:
1
archiv/NETWORK-api-backend_blueprint/.env
Normal file
1
archiv/NETWORK-api-backend_blueprint/.env
Normal file
@ -0,0 +1 @@
|
||||
PRINTER_IPS=192.168.0.10,192.168.0.11,192.168.0.12
|
106
archiv/NETWORK-api-backend_blueprint/README.md
Normal file
106
archiv/NETWORK-api-backend_blueprint/README.md
Normal file
@ -0,0 +1,106 @@
|
||||
# 🖨️ 3D-Drucker Status API 📊
|
||||
|
||||
Willkommen beim Blueprint der 3D-Drucker Status API! Diese API ermöglicht es Ihnen, den Status mehrerer über LAN verbundener 3D-Drucker zu überwachen und Druckaufträge an sie zu senden.
|
||||
|
||||
## 🌟 Funktionen
|
||||
|
||||
- 🔍 Abrufen des Status von 3D-Druckern, einschließlich ihres aktuellen Status, Fortschrittes und Temperatur.
|
||||
- 📥 Senden von Druckaufträgen an verfügbare 3D-Drucker.
|
||||
- 💾 Speichern und Aktualisieren des Status jedes Druckers in einer SQLite-Datenbank.
|
||||
|
||||
## 🛠️Verwendete Technologien
|
||||
|
||||
- 🐍 Python
|
||||
- 🌶️ Flask
|
||||
- 🗄️ SQLite
|
||||
- 🌐 HTTP-Anfragen
|
||||
|
||||
## 📋 Verordnungen
|
||||
|
||||
Bevor Sie die API starten, stellen Sie sicher, dass Sie folgendes haben:
|
||||
|
||||
- Python 3.x installiert
|
||||
- Flask und python-dotenv-Bibliotheken installiert (`pip install flask python-dotenv`)
|
||||
- Eine Liste von IP-Adressen der 3D-Drucker, die Sie überwachen möchten
|
||||
|
||||
## 🚀 Erste Schritte
|
||||
|
||||
1. Klonen Sie das Repository:
|
||||
```
|
||||
git clone https://git.i.mercedes-benz.com/TBA-Berlin-FI/MYP
|
||||
```
|
||||
|
||||
2. Installieren Sie die erforderlichen Abhängigkeiten:
|
||||
```
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
3. Erstellen Sie eine `.env`-Datei im Projektverzeichnis und geben Sie die IP-Adressen Ihrer 3D-Drucker an:
|
||||
```
|
||||
PRINTER_IPS=192.168.0.10,192.168.0.11,192.168.0.12
|
||||
```
|
||||
|
||||
4. Starten Sie das Skript, um die SQLite-Datenbank zu erstellen:
|
||||
```
|
||||
python create_db.py
|
||||
```
|
||||
|
||||
5. Starten Sie den API-Server:
|
||||
```
|
||||
python app.py
|
||||
```
|
||||
|
||||
6. Die API ist unter `http://localhost:5000` erreichbar.
|
||||
|
||||
## 📡 API-Endpunkte
|
||||
|
||||
- `GET /printer_status`: Rufen Sie den Status aller 3D-Drucker ab.
|
||||
- `POST /print_job`: Senden Sie einen Druckauftrag an einen bestimmten 3D-Drucker.
|
||||
|
||||
## 📝 API-Nutzung
|
||||
|
||||
### Druckerstatus abrufen
|
||||
|
||||
Senden Sie eine `GET`-Anfrage an `/printer_status`, um den Status aller 3D-Drucker abzurufen.
|
||||
|
||||
Antwort:
|
||||
```json
|
||||
[
|
||||
{
|
||||
"ip": "192.168.0.10",
|
||||
"status": "frei",
|
||||
"progress": 0,
|
||||
"temperature": 25
|
||||
},
|
||||
{
|
||||
"ip": "192.168.0.11",
|
||||
"status": "besetzt",
|
||||
"progress": 50,
|
||||
"temperature": 180
|
||||
},
|
||||
...
|
||||
]
|
||||
```
|
||||
|
||||
### Druckauftrag senden
|
||||
|
||||
Senden Sie eine `POST`-Anfrage an `/print_job` mit der folgenden JSON-Last, um einen Druckauftrag an einen bestimmten 3D-Drucker zu senden:
|
||||
|
||||
```json
|
||||
{
|
||||
"printer_ip": "192.168.0.10",
|
||||
"file_url": "http://example.com/print_file.gcode"
|
||||
}
|
||||
```
|
||||
|
||||
Antwort:
|
||||
```json
|
||||
{
|
||||
"message": "Druckauftrag gestartet"
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## 📄 Lizenz
|
||||
|
||||
- --> Noch nicht verfügbar
|
25
archiv/NETWORK-api-backend_blueprint/datenbank_erstellen.py
Normal file
25
archiv/NETWORK-api-backend_blueprint/datenbank_erstellen.py
Normal file
@ -0,0 +1,25 @@
|
||||
import sqlite3
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
printers = os.getenv('PRINTER_IPS').split(',')
|
||||
|
||||
def create_db():
|
||||
conn = sqlite3.connect('printers.db')
|
||||
c = conn.cursor()
|
||||
|
||||
# Tabelle 'printers' erstellen, falls sie nicht existiert
|
||||
c.execute('''CREATE TABLE IF NOT EXISTS printers
|
||||
(ip TEXT PRIMARY KEY, status TEXT)''')
|
||||
|
||||
# Drucker-IPs in die Tabelle einfügen, falls sie noch nicht vorhanden sind
|
||||
for printer_ip in printers:
|
||||
c.execute("INSERT OR IGNORE INTO printers (ip, status) VALUES (?, ?)", (printer_ip, "frei"))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
print("Datenbank 'printers.db' erfolgreich erstellt.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
create_db()
|
3
archiv/NETWORK-api-backend_blueprint/requirements.txt
Normal file
3
archiv/NETWORK-api-backend_blueprint/requirements.txt
Normal file
@ -0,0 +1,3 @@
|
||||
flask==2.1.0
|
||||
requests==2.25.1
|
||||
python-dotenv==0.20.0
|
94
archiv/NETWORK-api-backend_blueprint/server.py
Normal file
94
archiv/NETWORK-api-backend_blueprint/server.py
Normal file
@ -0,0 +1,94 @@
|
||||
from flask import Flask, jsonify, request
|
||||
import requests
|
||||
import sqlite3
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
|
||||
load_dotenv()
|
||||
printers = os.getenv('PRINTER_IPS').split(',')
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# SQLite-Datenbank initialisieren
|
||||
def init_db():
|
||||
conn = sqlite3.connect('printers.db')
|
||||
c = conn.cursor()
|
||||
c.execute('''CREATE TABLE IF NOT EXISTS printers
|
||||
(ip TEXT PRIMARY KEY, status TEXT)''')
|
||||
for printer_ip in printers:
|
||||
c.execute("INSERT OR IGNORE INTO printers (ip, status) VALUES (?, ?)", (printer_ip, "frei"))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@app.route('/printer_status', methods=['GET'])
|
||||
def get_printer_status():
|
||||
printer_status = []
|
||||
conn = sqlite3.connect('printers.db')
|
||||
c = conn.cursor()
|
||||
|
||||
for printer_ip in printers:
|
||||
c.execute("SELECT status FROM printers WHERE ip = ?", (printer_ip,))
|
||||
status = c.fetchone()[0]
|
||||
|
||||
try:
|
||||
response = requests.get(f"http://{printer_ip}/api/printer/status")
|
||||
|
||||
if response.status_code == 200:
|
||||
status_data = response.json()
|
||||
printer_status.append({
|
||||
"ip": printer_ip,
|
||||
"status": status,
|
||||
"progress": status_data["progress"],
|
||||
"temperature": status_data["temperature"]
|
||||
})
|
||||
else:
|
||||
printer_status.append({
|
||||
"ip": printer_ip,
|
||||
"status": "Fehler bei der Abfrage",
|
||||
"progress": None,
|
||||
"temperature": None
|
||||
})
|
||||
except:
|
||||
printer_status.append({
|
||||
"ip": printer_ip,
|
||||
"status": "Drucker nicht erreichbar",
|
||||
"progress": None,
|
||||
"temperature": None
|
||||
})
|
||||
|
||||
conn.close()
|
||||
return jsonify(printer_status)
|
||||
|
||||
@app.route('/print_job', methods=['POST'])
|
||||
def submit_print_job():
|
||||
print_job = request.json
|
||||
printer_ip = print_job["printer_ip"]
|
||||
file_url = print_job["file_url"]
|
||||
|
||||
conn = sqlite3.connect('printers.db')
|
||||
c = conn.cursor()
|
||||
c.execute("SELECT status FROM printers WHERE ip = ?", (printer_ip,))
|
||||
status = c.fetchone()[0]
|
||||
|
||||
if status == "frei":
|
||||
try:
|
||||
response = requests.post(f"http://{printer_ip}/api/print_job", json={"file_url": file_url})
|
||||
|
||||
if response.status_code == 200:
|
||||
c.execute("UPDATE printers SET status = 'besetzt' WHERE ip = ?", (printer_ip,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return jsonify({"message": "Druckauftrag gestartet"}), 200
|
||||
else:
|
||||
conn.close()
|
||||
return jsonify({"message": "Fehler beim Starten des Druckauftrags"}), 500
|
||||
except:
|
||||
conn.close()
|
||||
return jsonify({"message": "Drucker nicht erreichbar"}), 500
|
||||
else:
|
||||
conn.close()
|
||||
return jsonify({"message": "Drucker ist nicht frei"}), 400
|
||||
|
||||
if __name__ == '__main__':
|
||||
init_db()
|
||||
app.run(host='0.0.0.0', port=5000)
|
@ -0,0 +1,38 @@
|
||||
# entwendet aus:
|
||||
# https://github.com/ut-hnl-lab/ultimakerpy
|
||||
|
||||
# auch zum lesen:
|
||||
# https://github.com/MartinBienz/SDPremote?tab=readme-ov-file
|
||||
|
||||
import time
|
||||
from ultimakerpy import UMS3, JobState
|
||||
|
||||
def print_started(state):
|
||||
if state == JobState.PRINTING:
|
||||
time.sleep(6.0)
|
||||
return True
|
||||
return False
|
||||
|
||||
def layer_reached(pos, n):
|
||||
if round(pos / 0.2) >= n: # set layer pitch: 0.2 mm
|
||||
return True
|
||||
return False
|
||||
|
||||
printer = UMS3(name='MyPrinterName')
|
||||
targets = {
|
||||
'job_state': printer.job_state,
|
||||
'bed_pos': printer.bed.position,
|
||||
}
|
||||
|
||||
printer.print_from_dialog() # select file to print
|
||||
printer.peripherals.camera_streaming()
|
||||
with printer.data_logger('output2.csv', targets) as dl:
|
||||
timer = dl.get_timer()
|
||||
|
||||
# sleep until active leveling finishes
|
||||
timer.wait_for_datalog('job_state', print_started)
|
||||
|
||||
for n in range(1, 101):
|
||||
# sleep until the printing of specified layer to start
|
||||
timer.wait_for_datalog('bed_pos', lambda v: layer_reached(v, n))
|
||||
print('printing layer:', n)
|
Reference in New Issue
Block a user