Spaces:
Runtime error
Runtime error
Commit
·
e685fff
0
Parent(s):
Adding files
Browse files- .gitattributes +42 -0
- Dockerfile +36 -0
- README.md +12 -0
- backend/csv_data/containers.csv +57 -0
- backend/csv_data/input_items.csv +0 -0
- backend/csv_data/placements.csv +21 -0
- backend/final_cpp_codes/3dBinPakckingAlgo.cpp +3 -0
- backend/final_cpp_codes/json.hpp +3 -0
- backend/final_cpp_codes/placingItem.cpp +3 -0
- backend/final_cpp_codes/priorityCalculationEngine.cpp +3 -0
- backend/final_cpp_codes/retrievalPathPlanning.cpp +3 -0
- backend/final_cpp_codes/wasteManagement.cpp +3 -0
- backend/main.py +1250 -0
- backend/requirements.txt +9 -0
- prisma/.env +1 -0
- prisma/.gitignore +1 -0
- prisma/schema.prisma +69 -0
.gitattributes
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
backend/final_cpp_codes/* filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
backend/final_cpp_codes/retrievalPathPlanning filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
backend/final_cpp_codes/3dBinPakckingAlgo filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
backend/final_cpp_codes/placingItem filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
backend/final_cpp_codes/priorityCalculationEngine filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
backend/final_cpp_codes/priortiyCalculationEngine filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
backend/final_cpp_codes/wasteManagement filter=lfs diff=lfs merge=lfs -text
|
Dockerfile
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM ubuntu:22.04
|
| 2 |
+
|
| 3 |
+
ENV PORT=8000
|
| 4 |
+
ENV PYTHONPATH=/app/backend
|
| 5 |
+
ENV NODE_VERSION=20
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
RUN apt-get update && apt-get install -y \
|
| 10 |
+
g++ \
|
| 11 |
+
build-essential \
|
| 12 |
+
python3 \
|
| 13 |
+
python3-pip \
|
| 14 |
+
python3-dev \
|
| 15 |
+
libstdc++6 \
|
| 16 |
+
curl \
|
| 17 |
+
&& apt-get clean && rm -rf /var/lib/apt/lists/*
|
| 18 |
+
|
| 19 |
+
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
|
| 20 |
+
|
| 21 |
+
ENV NVM_DIR=/root/.nvm
|
| 22 |
+
RUN . $NVM_DIR/nvm.sh
|
| 23 |
+
|
| 24 |
+
RUN . $NVM_DIR/nvm.sh && nvm install 20 && nvm use 20
|
| 25 |
+
|
| 26 |
+
COPY backend/requirements.txt /app/
|
| 27 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 28 |
+
|
| 29 |
+
COPY backend /app/backend
|
| 30 |
+
COPY prisma /app/prisma
|
| 31 |
+
|
| 32 |
+
RUN prisma db push --schema=prisma/schema.prisma
|
| 33 |
+
|
| 34 |
+
EXPOSE 8000
|
| 35 |
+
|
| 36 |
+
CMD ["python3", "-m", "uvicorn", "backend.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--reload-dir", "/app/backend"]
|
README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: PHYDRA
|
| 3 |
+
emoji: 📉
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
license: apache-2.0
|
| 9 |
+
short_description: 🚢 Cargo Stowage Management System — PHYDRA
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
backend/csv_data/containers.csv
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
zone,container_id,width_cm,depth_cm,height_cm
|
| 2 |
+
Sanitation_Bay,SB01,25.0,42.5,200.0
|
| 3 |
+
Sanitation_Bay,SB02,100.0,85.0,200.0
|
| 4 |
+
Sanitation_Bay,SB03,100.0,85.0,200.0
|
| 5 |
+
Sanitation_Bay,SB04,200.0,85.0,200.0
|
| 6 |
+
Command_Center,CC01,100.0,85.0,200.0
|
| 7 |
+
Command_Center,CC02,100.0,170.0,200.0
|
| 8 |
+
Command_Center,CC03,100.0,85.0,200.0
|
| 9 |
+
Engineering_Bay,EB01,100.0,85.0,50.0
|
| 10 |
+
Engineering_Bay,EB02,25.0,85.0,200.0
|
| 11 |
+
Power_Bay,PB01,100.0,85.0,200.0
|
| 12 |
+
Power_Bay,PB02,200.0,21.25,100.0
|
| 13 |
+
Power_Bay,PB03,100.0,85.0,200.0
|
| 14 |
+
Power_Bay,PB04,100.0,42.5,200.0
|
| 15 |
+
Power_Bay,PB05,200.0,85.0,200.0
|
| 16 |
+
External_Storage,ES01,100.0,42.5,50.0
|
| 17 |
+
External_Storage,ES02,100.0,42.5,200.0
|
| 18 |
+
External_Storage,ES03,100.0,170.0,200.0
|
| 19 |
+
External_Storage,ES04,25.0,85.0,200.0
|
| 20 |
+
Lab,L01,100.0,21.25,400.0
|
| 21 |
+
Lab,L02,100.0,85.0,200.0
|
| 22 |
+
Lab,L03,100.0,85.0,200.0
|
| 23 |
+
Lab,L04,200.0,85.0,200.0
|
| 24 |
+
Storage_Bay,TA01,100.0,170.0,200.0
|
| 25 |
+
Storage_Bay,TA02,100.0,85.0,200.0
|
| 26 |
+
Storage_Bay,TA03,100.0,85.0,50.0
|
| 27 |
+
Engine_Bay,NA01,200.0,21.25,200.0
|
| 28 |
+
Engine_Bay,NA02,100.0,170.0,200.0
|
| 29 |
+
Engine_Bay,NA03,200.0,85.0,400.0
|
| 30 |
+
Life_Support,LS01,25.0,42.5,200.0
|
| 31 |
+
Life_Support,LS02,25.0,85.0,200.0
|
| 32 |
+
Life_Support,LS03,100.0,85.0,200.0
|
| 33 |
+
Life_Support,LS04,100.0,42.5,100.0
|
| 34 |
+
Life_Support,LS05,100.0,170.0,200.0
|
| 35 |
+
Maintenance_Bay,MB01,100.0,85.0,100.0
|
| 36 |
+
Maintenance_Bay,MB02,100.0,85.0,200.0
|
| 37 |
+
Maintenance_Bay,MB03,50.0,85.0,50.0
|
| 38 |
+
Maintenance_Bay,MB04,100.0,85.0,200.0
|
| 39 |
+
Crew_Quarters,CQ01,50.0,21.25,400.0
|
| 40 |
+
Crew_Quarters,CQ02,50.0,170.0,200.0
|
| 41 |
+
Crew_Quarters,CQ03,100.0,85.0,200.0
|
| 42 |
+
Crew_Quarters,CQ04,100.0,85.0,200.0
|
| 43 |
+
Medical_Bay,EA01,100.0,85.0,200.0
|
| 44 |
+
Medical_Bay,EA02,100.0,21.25,400.0
|
| 45 |
+
Medical_Bay,EA03,100.0,85.0,200.0
|
| 46 |
+
Cockpit,C01,100.0,85.0,200.0
|
| 47 |
+
Cockpit,C02,50.0,85.0,200.0
|
| 48 |
+
Cockpit,C03,100.0,85.0,200.0
|
| 49 |
+
Cockpit,C04,25.0,42.5,200.0
|
| 50 |
+
Cockpit,C05,100.0,21.25,200.0
|
| 51 |
+
Airlock,A01,100.0,170.0,200.0
|
| 52 |
+
Airlock,A02,100.0,85.0,200.0
|
| 53 |
+
Airlock,A03,100.0,170.0,400.0
|
| 54 |
+
Greenhouse,G01,200.0,85.0,200.0
|
| 55 |
+
Greenhouse,G02,100.0,85.0,400.0
|
| 56 |
+
Greenhouse,G03,100.0,85.0,400.0
|
| 57 |
+
Greenhouse,G04,100.0,42.5,200.0
|
backend/csv_data/input_items.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
backend/csv_data/placements.csv
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Item ID,Container ID,"Coordinates (W1,D1,H1)","Coordinates (W2,D2,H2)"
|
| 2 |
+
000002,MB01,"(0,0,0)","(49,36,44)"
|
| 3 |
+
000003,A01,"(0,0,0)","(48,33,43)"
|
| 4 |
+
000015,EB01,"(0,0,0)","(33,43,48)"
|
| 5 |
+
000013,A01,"(0,0,43)","(32,35,91)"
|
| 6 |
+
000020,TA01,"(0,0,0)","(33,39,33)"
|
| 7 |
+
000006,CC01,"(0,0,0)","(11,39,46)"
|
| 8 |
+
000008,G01,"(0,0,0)","(39,13,36)"
|
| 9 |
+
000009,CQ01,"(0,0,0)","(31,16,33)"
|
| 10 |
+
000019,LS01,"(0,0,0)","(23,22,32)"
|
| 11 |
+
000017,MB01,"(0,36,0)","(27,68,17)"
|
| 12 |
+
000011,TA01,"(0,0,33)","(11,31,76)"
|
| 13 |
+
000005,ES01,"(0,0,0)","(23,31,18)"
|
| 14 |
+
000004,EA01,"(0,0,0)","(15,46,17)"
|
| 15 |
+
000018,EB01,"(0,43,0)","(15,78,22)"
|
| 16 |
+
000014,CQ01,"(0,0,33)","(27,16,57)"
|
| 17 |
+
000001,TA01,"(0,39,0)","(26,56,19)"
|
| 18 |
+
000010,TA01,"(33,0,0)","(48,18,29)"
|
| 19 |
+
000016,EB01,"(33,0,0)","(77,11,14)"
|
| 20 |
+
000007,EA01,"(0,0,17)","(17,15,41)"
|
| 21 |
+
000012,G01,"(0,13,0)","(26,31,11)"
|
backend/final_cpp_codes/3dBinPakckingAlgo.cpp
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8ef191511979fe1b80957517ca17e9f66d38a4f4d4fece0bfabb7bade4818113
|
| 3 |
+
size 29855
|
backend/final_cpp_codes/json.hpp
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5f49960a57b014bc38a100f0c119dba49a349452ab263481375bf7d18fcceaf7
|
| 3 |
+
size 952699
|
backend/final_cpp_codes/placingItem.cpp
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:69b1ea369bc82053a0e9dd9f237156cce1cc2c54c605d10e4be3fac3a2233603
|
| 3 |
+
size 26437
|
backend/final_cpp_codes/priorityCalculationEngine.cpp
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:76ea848fa34d768bcc69f97e2e6f4b40fa5050ccdb4dcfaf2769513422cf5ad8
|
| 3 |
+
size 12423
|
backend/final_cpp_codes/retrievalPathPlanning.cpp
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7914aff053fbdaeb4f87987e553de2a67811369206354a18c9159bf3d74dcf6f
|
| 3 |
+
size 19109
|
backend/final_cpp_codes/wasteManagement.cpp
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:affd0c37c7cc1567ccb1514acd45b8004c80d6d07e90f56b1302e212588a37b7
|
| 3 |
+
size 20016
|
backend/main.py
ADDED
|
@@ -0,0 +1,1250 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from hmac import new
|
| 2 |
+
from os import times
|
| 3 |
+
from sys import stdin
|
| 4 |
+
from textwrap import indent
|
| 5 |
+
from fastapi import FastAPI,Request,Response, HTTPException, UploadFile, Query
|
| 6 |
+
import json
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
# from sqlmodel import Field, Session, SQLModel, create_engine, select
|
| 9 |
+
from typing import List, Optional, Type
|
| 10 |
+
import requests
|
| 11 |
+
import pandas as pd
|
| 12 |
+
import csv
|
| 13 |
+
from prisma import Prisma
|
| 14 |
+
from prisma.models import Item, Container as PrismaContainer, Zone, Placement, Log
|
| 15 |
+
from datetime import datetime, date, timedelta
|
| 16 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 17 |
+
|
| 18 |
+
import subprocess
|
| 19 |
+
import re
|
| 20 |
+
|
| 21 |
+
from sympy import content
|
| 22 |
+
from tomlkit import item
|
| 23 |
+
from fastapi.responses import FileResponse
|
| 24 |
+
import uvicorn
|
| 25 |
+
|
| 26 |
+
prisma = Prisma()
|
| 27 |
+
|
| 28 |
+
app = FastAPI()
|
| 29 |
+
|
| 30 |
+
# origins = [
|
| 31 |
+
# "https://localhost:5173/*",
|
| 32 |
+
# ]
|
| 33 |
+
|
| 34 |
+
app.add_middleware(
|
| 35 |
+
CORSMiddleware,
|
| 36 |
+
allow_origins=['*'],
|
| 37 |
+
allow_credentials=True,
|
| 38 |
+
allow_methods=["*"], # Allows all methods
|
| 39 |
+
allow_headers=["*"], # Allows all headers
|
| 40 |
+
)
|
| 41 |
+
curr_date_iso_format = date.today().isoformat()
|
| 42 |
+
curr_date = date.today()
|
| 43 |
+
|
| 44 |
+
@app.get("/")
|
| 45 |
+
def welcome():
|
| 46 |
+
return Response("Welcome")
|
| 47 |
+
|
| 48 |
+
@app.on_event("startup")
|
| 49 |
+
async def startup():
|
| 50 |
+
await prisma.connect()
|
| 51 |
+
print("Prisma connected....")
|
| 52 |
+
|
| 53 |
+
@app.on_event("shutdown")
|
| 54 |
+
async def shutdown():
|
| 55 |
+
await prisma.disconnect()
|
| 56 |
+
print("Prisma disconnected....")
|
| 57 |
+
|
| 58 |
+
class PlacementItem(BaseModel):
|
| 59 |
+
itemId: str
|
| 60 |
+
itemName: str
|
| 61 |
+
itemWidth: float
|
| 62 |
+
itemDepth: float
|
| 63 |
+
itemHeight: float
|
| 64 |
+
itemMass: float
|
| 65 |
+
itemPriority: float
|
| 66 |
+
itemExpiryDate: str
|
| 67 |
+
itemUsageLimit: float
|
| 68 |
+
itemPreferredZone: str
|
| 69 |
+
|
| 70 |
+
class PlacementContainer(BaseModel):
|
| 71 |
+
containerId: str
|
| 72 |
+
zone: str
|
| 73 |
+
width: float
|
| 74 |
+
depth: float
|
| 75 |
+
height: float
|
| 76 |
+
|
| 77 |
+
class Container(BaseModel):
|
| 78 |
+
containerId: str
|
| 79 |
+
zone: str
|
| 80 |
+
width: float
|
| 81 |
+
depth: float
|
| 82 |
+
height: float
|
| 83 |
+
items: List[str] = []
|
| 84 |
+
|
| 85 |
+
class PlacementRequest(BaseModel):
|
| 86 |
+
items: List[PlacementItem]
|
| 87 |
+
containers: List[PlacementContainer]
|
| 88 |
+
|
| 89 |
+
class RetrieveRequest(BaseModel):
|
| 90 |
+
itemId:str
|
| 91 |
+
userId:str
|
| 92 |
+
timestamp:str
|
| 93 |
+
class Coordinates(BaseModel):
|
| 94 |
+
width: float
|
| 95 |
+
depth: float
|
| 96 |
+
height: float
|
| 97 |
+
|
| 98 |
+
class Position(BaseModel):
|
| 99 |
+
startCoordinates: Coordinates
|
| 100 |
+
endCoordinates: Coordinates
|
| 101 |
+
|
| 102 |
+
class PlaceRequest(BaseModel):
|
| 103 |
+
itemId: str
|
| 104 |
+
userId: str
|
| 105 |
+
timestamp: str
|
| 106 |
+
containerId: str
|
| 107 |
+
position: Position
|
| 108 |
+
|
| 109 |
+
class WasteReturnPlanRequest(BaseModel):
|
| 110 |
+
undockingContainerId: str
|
| 111 |
+
undockingDate: str # ISO format
|
| 112 |
+
maxWeight: float
|
| 113 |
+
|
| 114 |
+
class WasteCompleteUndocking(BaseModel):
|
| 115 |
+
undockingContainerId: str
|
| 116 |
+
timestamp: str # ISO format
|
| 117 |
+
|
| 118 |
+
class SimulateItemUsage(BaseModel):
|
| 119 |
+
itemId: str
|
| 120 |
+
name: Optional[str] = None # Making it optional since either itemId or name can be used
|
| 121 |
+
|
| 122 |
+
class SimulateDayRequest(BaseModel):
|
| 123 |
+
numOfDays: Optional[int] = None # Either this or toTimestamp
|
| 124 |
+
toTimestamp: Optional[str] = None # ISO format
|
| 125 |
+
itemsToBeUsedPerDay: List[SimulateItemUsage]
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
class ContainerImportRequest(BaseModel):
|
| 129 |
+
fileUrl: str
|
| 130 |
+
|
| 131 |
+
containerState : List[Container] = []
|
| 132 |
+
|
| 133 |
+
# placements: List[dict] = []
|
| 134 |
+
|
| 135 |
+
@app.post("/api/placement")
|
| 136 |
+
async def placement(data: PlacementRequest):
|
| 137 |
+
items = data.items
|
| 138 |
+
containers = data.containers
|
| 139 |
+
|
| 140 |
+
for i in items:
|
| 141 |
+
i_data = {
|
| 142 |
+
"itemId": i.itemId,
|
| 143 |
+
"name": i.itemName,
|
| 144 |
+
"width": int(i.itemWidth),
|
| 145 |
+
"depth": int(i.itemDepth),
|
| 146 |
+
"height": int(i.itemHeight),
|
| 147 |
+
"mass":float(i.itemMass),
|
| 148 |
+
"priority": int(i.itemPriority),
|
| 149 |
+
"expiryDate": i.itemExpiryDate,
|
| 150 |
+
"usageLimit": int(i.itemUsageLimit),
|
| 151 |
+
"preferredZone": i.itemPreferredZone
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
try:
|
| 155 |
+
existing_item = await prisma.item.find_first(where={"itemId": i.itemId}) # type: ignore
|
| 156 |
+
if not existing_item:
|
| 157 |
+
created_item = await prisma.item.create(data=i_data) # type: ignore
|
| 158 |
+
print("Created Item")
|
| 159 |
+
print("Created Item")
|
| 160 |
+
except Exception as e:
|
| 161 |
+
print(f"Error creating item: {e}")
|
| 162 |
+
|
| 163 |
+
input_data = json.dumps({
|
| 164 |
+
"items": [
|
| 165 |
+
{
|
| 166 |
+
"itemId": item.itemId,
|
| 167 |
+
"name": item.itemName,
|
| 168 |
+
"width": item.itemWidth,
|
| 169 |
+
"depth": item.itemDepth,
|
| 170 |
+
"height": item.itemHeight,
|
| 171 |
+
"priority": item.itemPriority,
|
| 172 |
+
"expiryDate": "N/A" if item.itemExpiryDate is None else item.itemExpiryDate,
|
| 173 |
+
"usageLimit": item.itemUsageLimit,
|
| 174 |
+
"preferredZone": item.itemPreferredZone
|
| 175 |
+
} for item in items
|
| 176 |
+
]
|
| 177 |
+
}, indent=4)
|
| 178 |
+
|
| 179 |
+
command = "g++ -std=c++20 final_cpp_codes/priorityCalculationEngine.cpp -o final_cpp_codes/priorityCalculationEngine && ./final_cpp_codes/priorityCalculationEngine"
|
| 180 |
+
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
|
| 181 |
+
stdout, stderr = process.communicate(input=input_data)
|
| 182 |
+
|
| 183 |
+
print("C++ Program Output:", stdout)
|
| 184 |
+
print("C++ Program Errors:", stderr)
|
| 185 |
+
|
| 186 |
+
output_data = json.loads(stdout)
|
| 187 |
+
raw_items = output_data.get("items", [])
|
| 188 |
+
|
| 189 |
+
new_items = []
|
| 190 |
+
for item in raw_items:
|
| 191 |
+
corrected_item = {
|
| 192 |
+
"itemId": item.get("itemId", "").strip(),
|
| 193 |
+
"name": item.get("name", "").strip(),
|
| 194 |
+
"width": int(item.get("width", 0)),
|
| 195 |
+
"depth": int(item.get("depth", 0)),
|
| 196 |
+
"height": int(item.get("height", 0)),
|
| 197 |
+
"priority": int(item.get("priority", 0)),
|
| 198 |
+
"expiryDate": item.get("expiryDate", "").strip(),
|
| 199 |
+
"usageLimit": int(item.get("usageLimit", 0)),
|
| 200 |
+
"preferredZone": item.get("preferredZone", "").strip(),
|
| 201 |
+
"priorityScore": float(item.get("priorityScore", 0.0))
|
| 202 |
+
}
|
| 203 |
+
new_items.append(corrected_item)
|
| 204 |
+
|
| 205 |
+
for c in containers:
|
| 206 |
+
c_data = {
|
| 207 |
+
"containerId": c.containerId,
|
| 208 |
+
"zone": c.zone,
|
| 209 |
+
"width": float(c.width),
|
| 210 |
+
"depth": float(c.depth),
|
| 211 |
+
"height": float(c.height),
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
try:
|
| 215 |
+
existing_container = await prisma.container.find_first(where={"containerId": c.containerId}) # type: ignore
|
| 216 |
+
if not existing_container:
|
| 217 |
+
created_container = await prisma.container.create(data=c_data) # type: ignore
|
| 218 |
+
print("Created Container")
|
| 219 |
+
except Exception as e:
|
| 220 |
+
print(f"Error creating container: {e}")
|
| 221 |
+
|
| 222 |
+
final_json = json.dumps({
|
| 223 |
+
"items" : new_items,
|
| 224 |
+
"containers": [c.dict() for c in containers]
|
| 225 |
+
}, indent=4)
|
| 226 |
+
|
| 227 |
+
command = "g++ -std=c++20 final_cpp_codes/3dBinPakckingAlgo.cpp -o final_cpp_codes/3dBinPakckingAlgo && ./final_cpp_codes/3dBinPakckingAlgo"
|
| 228 |
+
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
|
| 229 |
+
stdout, stderr = process.communicate(input=final_json)
|
| 230 |
+
|
| 231 |
+
print("C++ Program Output:", stdout)
|
| 232 |
+
print("C++ Program Errors:", stderr)
|
| 233 |
+
|
| 234 |
+
stdout = stdout.strip()
|
| 235 |
+
|
| 236 |
+
if not stdout:
|
| 237 |
+
raise HTTPException(status_code=500, detail="C++ program did not return any output. Check for errors.")
|
| 238 |
+
|
| 239 |
+
print("-"*50,stdout)
|
| 240 |
+
# Clean the stdout string to make it valid JSON
|
| 241 |
+
stdout = stdout.replace('\\n', '\n').replace('\\\"', '\"')
|
| 242 |
+
|
| 243 |
+
# Remove any invalid escape sequences
|
| 244 |
+
stdout = re.sub(r'\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})', r'', stdout)
|
| 245 |
+
|
| 246 |
+
try:
|
| 247 |
+
output_data = json.loads(stdout)
|
| 248 |
+
except json.JSONDecodeError as e:
|
| 249 |
+
print(f"JSON decode error: {e}")
|
| 250 |
+
print(f"Problematic JSON: {stdout}")
|
| 251 |
+
raise HTTPException(status_code=500, detail=f"Invalid JSON response from algorithm: {str(e)}")
|
| 252 |
+
output_data = json.loads(stdout)
|
| 253 |
+
|
| 254 |
+
newContainerState = output_data.get("finalContainers", [])
|
| 255 |
+
|
| 256 |
+
for x in newContainerState:
|
| 257 |
+
items = []
|
| 258 |
+
newContainer = Container(
|
| 259 |
+
containerId = x.get("containerId", ""),
|
| 260 |
+
zone=x.get("zone", ""),
|
| 261 |
+
width=float(x.get("width", 0)),
|
| 262 |
+
depth=float(x.get("depth", 0)),
|
| 263 |
+
height=float(x.get("height", 0)),
|
| 264 |
+
items = x.get("itemIds", [])
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
containerState.append(newContainer)
|
| 268 |
+
|
| 269 |
+
placements = output_data.get("placements", [])
|
| 270 |
+
|
| 271 |
+
print(f"placements: {placements}")
|
| 272 |
+
|
| 273 |
+
for placement in placements:
|
| 274 |
+
placement_data = {
|
| 275 |
+
"containerId": placement["containerId"],
|
| 276 |
+
"itemId": placement["itemId"],
|
| 277 |
+
"startPos": json.dumps({
|
| 278 |
+
"x": placement["startPos"][0],
|
| 279 |
+
"y": placement["startPos"][1],
|
| 280 |
+
"z": placement["startPos"][2],
|
| 281 |
+
}),
|
| 282 |
+
"endPos": json.dumps({
|
| 283 |
+
"x": placement["endPos"][0],
|
| 284 |
+
"y": placement["endPos"][1],
|
| 285 |
+
"z": placement["endPos"][2],
|
| 286 |
+
}),
|
| 287 |
+
}
|
| 288 |
+
try:
|
| 289 |
+
created_placement = await prisma.placement.create(data=placement_data) # type: ignore
|
| 290 |
+
print(f"Created Placement: {created_placement}")
|
| 291 |
+
except Exception as e:
|
| 292 |
+
print(f"Error creating placement: {e}")
|
| 293 |
+
|
| 294 |
+
print(f"new container state: {containerState}")
|
| 295 |
+
output_refined = {
|
| 296 |
+
"placements" : output_data["placements"],
|
| 297 |
+
"rearrangements" : output_data["rearrangements"]
|
| 298 |
+
}
|
| 299 |
+
|
| 300 |
+
return {"status": "success", "message" : output_refined}
|
| 301 |
+
|
| 302 |
+
class SearchRequest(BaseModel):
|
| 303 |
+
itemId: Optional[str] = None
|
| 304 |
+
itemName: Optional[str] = None
|
| 305 |
+
userId: Optional[str] = None
|
| 306 |
+
|
| 307 |
+
@app.get("/api/search")
|
| 308 |
+
async def search(data:SearchRequest):
|
| 309 |
+
itemId = data.itemId
|
| 310 |
+
itemName = data.itemName
|
| 311 |
+
userId = data.userId
|
| 312 |
+
if not itemId and not itemName:
|
| 313 |
+
raise HTTPException(status_code=400, detail="Either itemId or itemName must be provided")
|
| 314 |
+
if not itemId and not itemName:
|
| 315 |
+
raise HTTPException(status_code=400, detail="Either itemId or itemName must be provided")
|
| 316 |
+
|
| 317 |
+
if itemId:
|
| 318 |
+
item = await prisma.item.find_first(where={"itemId": itemId})
|
| 319 |
+
elif itemName:
|
| 320 |
+
item = await prisma.item.find_first(where={"name": itemName})
|
| 321 |
+
if not item:
|
| 322 |
+
raise HTTPException(status_code=404, detail="Item not found")
|
| 323 |
+
|
| 324 |
+
# i_data = {
|
| 325 |
+
# "itemId": item.itemId,
|
| 326 |
+
# "name": item.name,
|
| 327 |
+
# "width": item.width,
|
| 328 |
+
# "depth": item.depth,
|
| 329 |
+
# "height": item.height,
|
| 330 |
+
# "priority": item.priority,
|
| 331 |
+
# "expiryDate": item.expiryDate,
|
| 332 |
+
# "usageLimit": item.usageLimit,
|
| 333 |
+
# "preferredZone": item.preferredZone,
|
| 334 |
+
# "startPos": None,
|
| 335 |
+
# }
|
| 336 |
+
|
| 337 |
+
placements = await prisma.placement.find_first(where={"itemId": item.itemId})
|
| 338 |
+
print(f"placements: {placements}")
|
| 339 |
+
|
| 340 |
+
containerId = placements.containerId if placements else None
|
| 341 |
+
|
| 342 |
+
placements1 = []
|
| 343 |
+
# container = []
|
| 344 |
+
item_ids = []
|
| 345 |
+
item_start_pos_map = {}
|
| 346 |
+
|
| 347 |
+
if placements:
|
| 348 |
+
placements1 = await prisma.placement.find_many(where={"containerId": placements.containerId})
|
| 349 |
+
for placement in placements1:
|
| 350 |
+
item_ids.append(placement.itemId)
|
| 351 |
+
item_start_pos_map[placement.itemId] = placement.startPos
|
| 352 |
+
|
| 353 |
+
containerData = await prisma.container.find_first(where={"containerId": placements.containerId})
|
| 354 |
+
if containerData:
|
| 355 |
+
container = {
|
| 356 |
+
"containerId" : containerData.containerId,
|
| 357 |
+
"zone" : containerData.zone,
|
| 358 |
+
"width" : containerData.width,
|
| 359 |
+
"depth" : containerData.depth,
|
| 360 |
+
"height" : containerData.height,
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
print(f"placements1: {placements1}")
|
| 364 |
+
print(f"Container ID: {containerId}")
|
| 365 |
+
print(f"Container: {container}")
|
| 366 |
+
print(type(container))
|
| 367 |
+
print(f"Item Start Position Map: {item_start_pos_map}")
|
| 368 |
+
|
| 369 |
+
item_data = []
|
| 370 |
+
|
| 371 |
+
# item = await prisma.item.find_first(where={"itemId": itemId})
|
| 372 |
+
if not item:
|
| 373 |
+
raise HTTPException(status_code=404, detail="Item not found")
|
| 374 |
+
|
| 375 |
+
for x in item_ids:
|
| 376 |
+
item = await prisma.item.find_first(where={"itemId": x})
|
| 377 |
+
if item:
|
| 378 |
+
i = {
|
| 379 |
+
"itemId": item.itemId,
|
| 380 |
+
"name": item.name,
|
| 381 |
+
"width": item.width,
|
| 382 |
+
"depth": item.depth,
|
| 383 |
+
"height": item.height,
|
| 384 |
+
"startPos": item_start_pos_map[x],
|
| 385 |
+
}
|
| 386 |
+
item_data.append(i)
|
| 387 |
+
else:
|
| 388 |
+
print(f"Item with ID {x} not found in the database.")
|
| 389 |
+
print(f"item data: {item_data}")
|
| 390 |
+
|
| 391 |
+
if container:
|
| 392 |
+
container_data = {
|
| 393 |
+
"containerId": container.get("containerId", ""),
|
| 394 |
+
"zone": container.get("zone", ""),
|
| 395 |
+
"width": container.get("width", 0),
|
| 396 |
+
"depth": container.get("depth", 0),
|
| 397 |
+
"height": container.get("height", 0),
|
| 398 |
+
"items": item_data
|
| 399 |
+
}
|
| 400 |
+
else:
|
| 401 |
+
raise HTTPException(status_code=404, detail="Container not found")
|
| 402 |
+
|
| 403 |
+
input_json = json.dumps({
|
| 404 |
+
"container": container_data,
|
| 405 |
+
"itemId": itemId
|
| 406 |
+
}, indent=4)
|
| 407 |
+
|
| 408 |
+
command = "g++ -std=c++20 final_cpp_codes/retrievalPathPlanning.cpp -o final_cpp_codes/retrievalPathPlanning && ./final_cpp_codes/retrievalPathPlanning"
|
| 409 |
+
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
|
| 410 |
+
stdout, stderr = process.communicate(input=input_json)
|
| 411 |
+
|
| 412 |
+
print("C++ Program Output:", stdout)
|
| 413 |
+
print("C++ Program Errors:", stderr)
|
| 414 |
+
|
| 415 |
+
stdout = stdout.strip()
|
| 416 |
+
|
| 417 |
+
if not stdout:
|
| 418 |
+
raise HTTPException(status_code=500, detail="C++ program did not return any output. Check for errors.")
|
| 419 |
+
|
| 420 |
+
print("-" * 50, stdout)
|
| 421 |
+
stdout = stdout.replace('\\n', '\n').replace('\\\"', '\"')
|
| 422 |
+
stdout = re.sub(r'\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})', r'', stdout)
|
| 423 |
+
|
| 424 |
+
try:
|
| 425 |
+
output_data = json.loads(stdout)
|
| 426 |
+
except json.JSONDecodeError as e:
|
| 427 |
+
print(f"JSON decode error: {e}")
|
| 428 |
+
print(f"Problematic JSON: {stdout}")
|
| 429 |
+
raise HTTPException(status_code=500, detail=f"Invalid JSON response from algorithm: {str(e)}")
|
| 430 |
+
|
| 431 |
+
return {"status": "success", "found": container, "item": item, "message": output_data}
|
| 432 |
+
|
| 433 |
+
@app.post("/api/retrieve")
|
| 434 |
+
async def retrieve(data: RetrieveRequest):
|
| 435 |
+
try:
|
| 436 |
+
item_data = {
|
| 437 |
+
"itemId": data.itemId,
|
| 438 |
+
"userId": data.userId,
|
| 439 |
+
"timestamp": data.timestamp
|
| 440 |
+
}
|
| 441 |
+
|
| 442 |
+
placements = await prisma.placement.find_first(where={"itemId": data.itemId})
|
| 443 |
+
print(f"placements: {placements}")
|
| 444 |
+
|
| 445 |
+
containerId = placements.containerId if placements else None
|
| 446 |
+
|
| 447 |
+
itemThing = await prisma.item.find_first(where={"itemId": data.itemId})
|
| 448 |
+
print(f"itemThing: {itemThing}")
|
| 449 |
+
if not itemThing:
|
| 450 |
+
raise HTTPException(status_code=404, detail="Item not found")
|
| 451 |
+
|
| 452 |
+
if curr_date_iso_format == item_data["timestamp"]:
|
| 453 |
+
print(f"Item {data.itemId} retrieved by user {data.userId} at {datetime.now().isoformat()}")
|
| 454 |
+
if itemThing.usageLimit is not None and itemThing.usageLimit > 0:
|
| 455 |
+
updated_item = await prisma.item.update(
|
| 456 |
+
where={"itemId": data.itemId},
|
| 457 |
+
data={
|
| 458 |
+
"usageLimit": itemThing.usageLimit - 1
|
| 459 |
+
}
|
| 460 |
+
)
|
| 461 |
+
if updated_item:
|
| 462 |
+
print(f"Updated usageLimit for item {data.itemId}: {updated_item.usageLimit}")
|
| 463 |
+
else:
|
| 464 |
+
print(f"Failed to update usageLimit for item {data.itemId}")
|
| 465 |
+
# itemThing.usageLimit -= 1
|
| 466 |
+
|
| 467 |
+
print(f"Item {data.itemId} retrieved by user {data.userId} at {datetime.now().isoformat()}")
|
| 468 |
+
print(f"Item usage limit: {itemThing.usageLimit}")
|
| 469 |
+
|
| 470 |
+
itemData = {
|
| 471 |
+
"itemId": itemThing.itemId,
|
| 472 |
+
"name": itemThing.name,
|
| 473 |
+
"width": itemThing.width,
|
| 474 |
+
"depth": itemThing.depth,
|
| 475 |
+
"height": itemThing.height,
|
| 476 |
+
"priority": itemThing.priority,
|
| 477 |
+
"expiryDate": itemThing.expiryDate,
|
| 478 |
+
"usageLimit": itemThing.usageLimit,
|
| 479 |
+
"preferredZone": itemThing.preferredZone
|
| 480 |
+
}
|
| 481 |
+
|
| 482 |
+
log_data = {
|
| 483 |
+
"timestamp" : datetime.now(),
|
| 484 |
+
"userId": data.userId,
|
| 485 |
+
"actionType" : "retrieve",
|
| 486 |
+
"itemId": data.itemId,
|
| 487 |
+
"details" : json.dumps({
|
| 488 |
+
"fromContainer" : containerId,
|
| 489 |
+
"toContainer" : None,
|
| 490 |
+
"reason" : "Item retrieved"
|
| 491 |
+
})
|
| 492 |
+
}
|
| 493 |
+
|
| 494 |
+
print(f"Log data: {log_data}")
|
| 495 |
+
|
| 496 |
+
xm = await prisma.log.create(data=log_data) #type: ignore
|
| 497 |
+
print(f"Log created: {xm}")
|
| 498 |
+
|
| 499 |
+
if not xm:
|
| 500 |
+
raise HTTPException(status_code=500, detail="Log creation failed")
|
| 501 |
+
|
| 502 |
+
return {
|
| 503 |
+
"status": "success",
|
| 504 |
+
"message": f"Item {data.itemId} retrieved by user {data.userId} at {datetime.now().isoformat()} and updated the usage limit",
|
| 505 |
+
"item": itemData
|
| 506 |
+
}
|
| 507 |
+
except Exception as e:
|
| 508 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
@app.post("/api/place")
|
| 512 |
+
async def place(data:PlaceRequest):
|
| 513 |
+
# {
|
| 514 |
+
# "itemId": "string",
|
| 515 |
+
# "userId": "string",
|
| 516 |
+
# "timestamp": "string", // ISO format
|
| 517 |
+
# "containerId": "string", // Where the item is kept
|
| 518 |
+
# "position": {
|
| 519 |
+
# "startCoordinates": {
|
| 520 |
+
# "width": number,
|
| 521 |
+
# "depth": number,
|
| 522 |
+
# "height": number
|
| 523 |
+
# },
|
| 524 |
+
# "endCoordinates": {
|
| 525 |
+
# "width": number,
|
| 526 |
+
# "depth": number,
|
| 527 |
+
# "height": number
|
| 528 |
+
# }
|
| 529 |
+
# }
|
| 530 |
+
# }
|
| 531 |
+
itemId = data.itemId
|
| 532 |
+
userId = data.userId
|
| 533 |
+
timestamp1 = data.timestamp
|
| 534 |
+
containerId = data.containerId
|
| 535 |
+
startCoordinates = data.position.startCoordinates
|
| 536 |
+
|
| 537 |
+
startCoo = {
|
| 538 |
+
"x" : startCoordinates.width,
|
| 539 |
+
"y" : startCoordinates.depth,
|
| 540 |
+
"z" :startCoordinates.height
|
| 541 |
+
}
|
| 542 |
+
|
| 543 |
+
endCoordinates = data.position.endCoordinates
|
| 544 |
+
|
| 545 |
+
endCoo = {
|
| 546 |
+
"x" : endCoordinates.width,
|
| 547 |
+
"y" : endCoordinates.depth,
|
| 548 |
+
"z" : endCoordinates.height
|
| 549 |
+
}
|
| 550 |
+
|
| 551 |
+
item_to_retrieve = {
|
| 552 |
+
"itemId" : itemId,
|
| 553 |
+
"userId" : userId,
|
| 554 |
+
"timeStamp" : timestamp1,
|
| 555 |
+
"containerId" : containerId,
|
| 556 |
+
"startCoordinates" : startCoo,
|
| 557 |
+
"endCoordinates" : endCoo
|
| 558 |
+
}
|
| 559 |
+
|
| 560 |
+
# print(f"Item {itemId} placed by user {userId} at {timestamp} in container {containerId} from {startCoordinates} to {endCoordinates}")
|
| 561 |
+
|
| 562 |
+
items = await prisma.item.find_many()
|
| 563 |
+
containers = await prisma.container.find_many()
|
| 564 |
+
placements = await prisma.placement.find_many()
|
| 565 |
+
|
| 566 |
+
item_data = []
|
| 567 |
+
|
| 568 |
+
for item in items:
|
| 569 |
+
i_data = {
|
| 570 |
+
"itemId" : item.itemId,
|
| 571 |
+
"name" : item.name,
|
| 572 |
+
"width" : item.width,
|
| 573 |
+
"depth" : item.depth,
|
| 574 |
+
"height" : item.height,
|
| 575 |
+
"priority" : item.priority,
|
| 576 |
+
"expiryDate" : item.expiryDate,
|
| 577 |
+
"usageLimit" : item.usageLimit,
|
| 578 |
+
"preferredZone" : item.preferredZone,
|
| 579 |
+
}
|
| 580 |
+
|
| 581 |
+
item_data.append(i_data)
|
| 582 |
+
|
| 583 |
+
container_data = []
|
| 584 |
+
|
| 585 |
+
for container in containers:
|
| 586 |
+
c_data = {
|
| 587 |
+
"containerId" : container.containerId,
|
| 588 |
+
"zone" : container.zone,
|
| 589 |
+
"width" : container.width,
|
| 590 |
+
"depth" : container.depth,
|
| 591 |
+
"height" : container.height
|
| 592 |
+
}
|
| 593 |
+
|
| 594 |
+
container_data.append(c_data)
|
| 595 |
+
|
| 596 |
+
placement_data = []
|
| 597 |
+
|
| 598 |
+
for placement in placements:
|
| 599 |
+
p_data = {
|
| 600 |
+
"containerId": placement.containerId,
|
| 601 |
+
"itemId": placement.itemId,
|
| 602 |
+
"startPos": {
|
| 603 |
+
"x": placement.startPos["x"],
|
| 604 |
+
"y": placement.startPos["y"],
|
| 605 |
+
"z": placement.startPos["z"],
|
| 606 |
+
},
|
| 607 |
+
"endPos": {
|
| 608 |
+
"x": placement.endPos["x"],
|
| 609 |
+
"y": placement.endPos["y"],
|
| 610 |
+
"z": placement.endPos["z"],
|
| 611 |
+
},
|
| 612 |
+
}
|
| 613 |
+
|
| 614 |
+
placement_data.append(p_data)
|
| 615 |
+
|
| 616 |
+
# print(f"Placement data : {placement_data}")
|
| 617 |
+
|
| 618 |
+
input_json = json.dumps({
|
| 619 |
+
"items": item_data,
|
| 620 |
+
"containers": container_data,
|
| 621 |
+
"placements": placement_data,
|
| 622 |
+
"priorityItem" : item_to_retrieve
|
| 623 |
+
}, indent=4)
|
| 624 |
+
|
| 625 |
+
print(input_json)
|
| 626 |
+
command = "g++ -std=c++20 final_cpp_codes/placingItem.cpp -o final_cpp_codes/placingItem && ./final_cpp_codes/placingItem"
|
| 627 |
+
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
|
| 628 |
+
stdout, stderr = process.communicate(input=input_json)
|
| 629 |
+
|
| 630 |
+
print("C++ Program Output:", stdout)
|
| 631 |
+
print("C++ Program Errors:", stderr)
|
| 632 |
+
|
| 633 |
+
log_data = {
|
| 634 |
+
"timestamp": datetime.now(),
|
| 635 |
+
"userId" : data.userId,
|
| 636 |
+
"actionType" : "placed",
|
| 637 |
+
"itemId" : data.itemId,
|
| 638 |
+
"details" : json.dumps({
|
| 639 |
+
"fromContainer" : None,
|
| 640 |
+
"toContainer" : data.containerId,
|
| 641 |
+
"reason" : "Item placed in container"
|
| 642 |
+
})
|
| 643 |
+
}
|
| 644 |
+
|
| 645 |
+
xm = await prisma.log.create(data=log_data) #type: ignore
|
| 646 |
+
print(f"Log created: {xm}")
|
| 647 |
+
|
| 648 |
+
if not xm:
|
| 649 |
+
raise HTTPException(status_code=500, detail="Log creation failed")
|
| 650 |
+
|
| 651 |
+
return {
|
| 652 |
+
"status": "success",
|
| 653 |
+
"message": f"Item {data.itemId} placed by user {data.userId} at {data.timestamp} in container {data.containerId} from {data.position.startCoordinates} to {data.position.endCoordinates}",
|
| 654 |
+
"item": item_to_retrieve
|
| 655 |
+
}
|
| 656 |
+
|
| 657 |
+
@app.get("/api/waste/identify")
|
| 658 |
+
async def waste_identify():
|
| 659 |
+
items = await prisma.item.find_many()
|
| 660 |
+
placements = await prisma.placement.find_many()
|
| 661 |
+
|
| 662 |
+
waste_items_data = []
|
| 663 |
+
curr_date = date.today()
|
| 664 |
+
|
| 665 |
+
for item in items:
|
| 666 |
+
expire = False
|
| 667 |
+
reason = None
|
| 668 |
+
if(item.expiryDate and item.expiryDate != "N/A"):
|
| 669 |
+
expiry_date = datetime.strptime(item.expiryDate, "%Y-%m-%d").date()
|
| 670 |
+
if expiry_date <= curr_date:
|
| 671 |
+
# items_data.append(item)
|
| 672 |
+
expire = True
|
| 673 |
+
print(f"Item {item.itemId} has expired on {expiry_date}")
|
| 674 |
+
reason = "Expired"
|
| 675 |
+
elif(item.usageLimit and item.usageLimit != 0):
|
| 676 |
+
if item.usageLimit <= 0:
|
| 677 |
+
# items_data.append(item)
|
| 678 |
+
expire = True
|
| 679 |
+
print(f"Item {item.itemId} has usage limit reached")
|
| 680 |
+
reason = "Out of uses"
|
| 681 |
+
|
| 682 |
+
if expire:
|
| 683 |
+
placements = await prisma.placement.find_first(where={"itemId": item.itemId})
|
| 684 |
+
if placements:
|
| 685 |
+
thing = {
|
| 686 |
+
"itemId": item.itemId,
|
| 687 |
+
"name": item.name,
|
| 688 |
+
"reason" : reason,
|
| 689 |
+
"containerId" : placements.containerId,
|
| 690 |
+
"positions" : {
|
| 691 |
+
"startPos": {
|
| 692 |
+
"width": placements.startPos["x"],
|
| 693 |
+
"depth": placements.startPos["y"],
|
| 694 |
+
"height": placements.startPos["z"],
|
| 695 |
+
},
|
| 696 |
+
"endPos": {
|
| 697 |
+
"width": placements.endPos["x"],
|
| 698 |
+
"depth": placements.endPos["y"],
|
| 699 |
+
"height": placements.endPos["z"],
|
| 700 |
+
}
|
| 701 |
+
}
|
| 702 |
+
}
|
| 703 |
+
|
| 704 |
+
waste_items_data.append(thing)
|
| 705 |
+
|
| 706 |
+
log_data = {
|
| 707 |
+
"timestamp": datetime.now().isoformat(),
|
| 708 |
+
"userId" : "system",
|
| 709 |
+
"actionType" : "waste identified",
|
| 710 |
+
"itemId" : item.itemId,
|
| 711 |
+
"details" : json.dumps({
|
| 712 |
+
"fromContainer" : placements.containerId,
|
| 713 |
+
"toContainer" : None,
|
| 714 |
+
"reason" : reason
|
| 715 |
+
})
|
| 716 |
+
}
|
| 717 |
+
|
| 718 |
+
xm = await prisma.log.create(data=log_data) #type: ignore
|
| 719 |
+
print(f"Log created: {xm}")
|
| 720 |
+
|
| 721 |
+
if not xm:
|
| 722 |
+
raise HTTPException(status_code=500, detail="Log creation failed")
|
| 723 |
+
|
| 724 |
+
|
| 725 |
+
|
| 726 |
+
return {
|
| 727 |
+
"status": "success",
|
| 728 |
+
"wasteItems": waste_items_data}
|
| 729 |
+
|
| 730 |
+
@app.post("/api/waste/return‐plan")
|
| 731 |
+
async def waste_return_plan(data: WasteReturnPlanRequest):
|
| 732 |
+
container_id = data.undockingContainerId
|
| 733 |
+
undocking_date = data.undockingDate
|
| 734 |
+
max_weight = data.maxWeight
|
| 735 |
+
container = await prisma.container.find_first(where={"containerId": container_id})
|
| 736 |
+
if not container:
|
| 737 |
+
raise HTTPException(status_code=404, detail="Container not found")
|
| 738 |
+
|
| 739 |
+
zone = await prisma.zone.find_first(where={"name": container.zone})
|
| 740 |
+
if not zone:
|
| 741 |
+
raise HTTPException(status_code=404, detail="Zone not found")
|
| 742 |
+
|
| 743 |
+
containers = await prisma.container.find_many(where={"zone": zone.name})
|
| 744 |
+
if not containers:
|
| 745 |
+
raise HTTPException(status_code=404, detail="Containers not found")
|
| 746 |
+
|
| 747 |
+
placements = await prisma.placement.find_many(where={"containerId": container_id})
|
| 748 |
+
if not placements:
|
| 749 |
+
raise HTTPException(status_code=404, detail="Placements not found")
|
| 750 |
+
item_ids = [placement.itemId for placement in placements]
|
| 751 |
+
|
| 752 |
+
items = []
|
| 753 |
+
for item_id in item_ids:
|
| 754 |
+
item = await prisma.item.find_first(where={"itemId": item_id})
|
| 755 |
+
if item:
|
| 756 |
+
i_data = {
|
| 757 |
+
"itemId": item.itemId,
|
| 758 |
+
"name": item.name,
|
| 759 |
+
"width": item.width,
|
| 760 |
+
"depth": item.depth,
|
| 761 |
+
"height": item.height,
|
| 762 |
+
"mass": item.mass,
|
| 763 |
+
"priority": item.priority,
|
| 764 |
+
"expiryDate": item.expiryDate,
|
| 765 |
+
"usageLimit": item.usageLimit,
|
| 766 |
+
"preferredZone": item.preferredZone
|
| 767 |
+
}
|
| 768 |
+
items.append(i_data)
|
| 769 |
+
|
| 770 |
+
items_to_remove = []
|
| 771 |
+
total_removed_weight = 0.0
|
| 772 |
+
|
| 773 |
+
items.sort(key=lambda x: x["priority"], reverse=True)
|
| 774 |
+
print(f"Items: {items}")
|
| 775 |
+
|
| 776 |
+
for item in items:
|
| 777 |
+
if total_removed_weight + item["mass"] <= max_weight:
|
| 778 |
+
items_to_remove.append(item)
|
| 779 |
+
total_removed_weight += item["mass"]
|
| 780 |
+
else:
|
| 781 |
+
break
|
| 782 |
+
|
| 783 |
+
for item in items_to_remove:
|
| 784 |
+
placement = await prisma.placement.find_first(where={"itemId": item["itemId"]})
|
| 785 |
+
if placement:
|
| 786 |
+
await prisma.placement.delete(where={"id": placement.id})
|
| 787 |
+
await prisma.item.delete(where={"itemId": item["itemId"]})
|
| 788 |
+
|
| 789 |
+
# Add log entry for the removed item
|
| 790 |
+
log_data = {
|
| 791 |
+
"timestamp": datetime.now(),
|
| 792 |
+
"userId": "system", # Assuming the system is performing the operation
|
| 793 |
+
"actionType": "return-plan",
|
| 794 |
+
"itemId": item["itemId"],
|
| 795 |
+
"details": json.dumps({
|
| 796 |
+
"fromContainer": container_id,
|
| 797 |
+
"toContainer": None,
|
| 798 |
+
"reason": "Item removed as part of return plan"
|
| 799 |
+
})
|
| 800 |
+
}
|
| 801 |
+
try:
|
| 802 |
+
created_log = await prisma.log.create(data=log_data) # type: ignore
|
| 803 |
+
print(f"Log created: {created_log}")
|
| 804 |
+
except Exception as e:
|
| 805 |
+
print(f"Error creating log: {e}")
|
| 806 |
+
|
| 807 |
+
remaining_items = [item for item in items if item not in items_to_remove]
|
| 808 |
+
container_data = [
|
| 809 |
+
{
|
| 810 |
+
"containerId": c.containerId,
|
| 811 |
+
"zone": c.zone,
|
| 812 |
+
"width": c.width,
|
| 813 |
+
"depth": c.depth,
|
| 814 |
+
"height": c.height
|
| 815 |
+
}
|
| 816 |
+
for c in containers if c.containerId != container_id
|
| 817 |
+
]
|
| 818 |
+
|
| 819 |
+
input_json = json.dumps({
|
| 820 |
+
"items": remaining_items,
|
| 821 |
+
"containers": container_data
|
| 822 |
+
}, indent=4)
|
| 823 |
+
print("*", input_json)
|
| 824 |
+
command = "g++ -std=c++20 final_cpp_codes/3dBinPakckingAlgo.cpp -o final_cpp_codes/3dBinPakckingAlgo && ./final_cpp_codes/3dBinPakckingAlgo"
|
| 825 |
+
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
|
| 826 |
+
stdout, stderr = process.communicate(input=input_json)
|
| 827 |
+
|
| 828 |
+
print("3d algo C++ Program Output:", stdout)
|
| 829 |
+
print("3d algo C++ Program Errors:", stderr)
|
| 830 |
+
|
| 831 |
+
if not stdout:
|
| 832 |
+
raise HTTPException(status_code=500, detail="C++ program did not return any output. Check for errors.")
|
| 833 |
+
|
| 834 |
+
try:
|
| 835 |
+
output_data = json.loads(stdout)
|
| 836 |
+
print(type(output_data))
|
| 837 |
+
print(f"Output data: {output_data}")
|
| 838 |
+
except json.JSONDecodeError as e:
|
| 839 |
+
print(f"JSON decode error: {e}")
|
| 840 |
+
print(f"Problematic JSON: {stdout}")
|
| 841 |
+
raise HTTPException(status_code=500, detail=f"Invalid JSON response from algorithm: {str(e)}")
|
| 842 |
+
|
| 843 |
+
retrieval_steps = []
|
| 844 |
+
items_to_remove_with_coordinates = []
|
| 845 |
+
for item in items_to_remove:
|
| 846 |
+
placement = await prisma.placement.find_first(where={"itemId": item["itemId"]})
|
| 847 |
+
if placement:
|
| 848 |
+
x_data = {
|
| 849 |
+
"itemId": item["itemId"],
|
| 850 |
+
"name": item["name"],
|
| 851 |
+
"startPos": {
|
| 852 |
+
"x": placement.startPos["x"],
|
| 853 |
+
"y": placement.startPos["y"],
|
| 854 |
+
"z": placement.startPos["z"]
|
| 855 |
+
},
|
| 856 |
+
"width": item["width"],
|
| 857 |
+
"depth": item["depth"],
|
| 858 |
+
"height": item["height"],
|
| 859 |
+
}
|
| 860 |
+
|
| 861 |
+
items_to_remove_with_coordinates.append(x_data)
|
| 862 |
+
print(f"Items to remove: {items_to_remove_with_coordinates}")
|
| 863 |
+
for item in items_to_remove_with_coordinates:
|
| 864 |
+
input_json = json.dumps({
|
| 865 |
+
"container": {
|
| 866 |
+
"containerId": container.containerId,
|
| 867 |
+
"zone": container.zone,
|
| 868 |
+
"width": container.width,
|
| 869 |
+
"depth": container.depth,
|
| 870 |
+
"height": container.height,
|
| 871 |
+
"items": items
|
| 872 |
+
},
|
| 873 |
+
"itemId": item["itemId"]
|
| 874 |
+
}, indent=4)
|
| 875 |
+
|
| 876 |
+
print("*" * 50, "\nInput JSON:", input_json)
|
| 877 |
+
|
| 878 |
+
command = "g++ -std=c++20 final_cpp_codes/retrievalPathPlanning.cpp -o final_cpp_codes/retrievalPathPlanning && ./final_cpp_codes/retrievalPathPlanning"
|
| 879 |
+
process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
|
| 880 |
+
stdout, stderr = process.communicate(input=input_json)
|
| 881 |
+
|
| 882 |
+
print("retrieval C++ Retrieval Output:", stdout)
|
| 883 |
+
print("retrieval C++ Retrieval Errors:", stderr)
|
| 884 |
+
|
| 885 |
+
if not stdout:
|
| 886 |
+
raise HTTPException(status_code=500, detail="C++ retrieval program did not return any output. Check for errors.")
|
| 887 |
+
|
| 888 |
+
stdout = stdout.strip()
|
| 889 |
+
|
| 890 |
+
print("-" * 50, stdout)
|
| 891 |
+
stdout = stdout.replace('\\n', '\n').replace('\\\"', '\"')
|
| 892 |
+
stdout = re.sub(r'\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})', r'', stdout)
|
| 893 |
+
|
| 894 |
+
try:
|
| 895 |
+
output_data = json.loads(stdout)
|
| 896 |
+
except json.JSONDecodeError as e:
|
| 897 |
+
print(f"JSON decode error: {e}")
|
| 898 |
+
print(f"Problematic JSON: {stdout}")
|
| 899 |
+
raise HTTPException(status_code=500, detail=f"Invalid JSON response from algorithm: {str(e)}")
|
| 900 |
+
|
| 901 |
+
retrieval_steps.append(output_data)
|
| 902 |
+
|
| 903 |
+
return_items = [
|
| 904 |
+
{
|
| 905 |
+
"itemId": item["itemId"],
|
| 906 |
+
"name": item["name"],
|
| 907 |
+
"reason": "Undocking"
|
| 908 |
+
}
|
| 909 |
+
for item in items_to_remove_with_coordinates
|
| 910 |
+
]
|
| 911 |
+
|
| 912 |
+
total_volume = sum(item["width"] * item["depth"] * item["height"] for item in items_to_remove_with_coordinates)
|
| 913 |
+
total_weight = sum(item["mass"] for item in items_to_remove_with_coordinates if item["mass"])
|
| 914 |
+
|
| 915 |
+
return_manifest = {
|
| 916 |
+
"undockingContainerId": container_id,
|
| 917 |
+
"undockingDate": undocking_date,
|
| 918 |
+
"returnItems": return_items,
|
| 919 |
+
"totalVolume": total_volume,
|
| 920 |
+
"totalWeight": total_weight
|
| 921 |
+
}
|
| 922 |
+
|
| 923 |
+
return {
|
| 924 |
+
"success": True,
|
| 925 |
+
"returnPlan": output_data.get("placements", []),
|
| 926 |
+
"retrievalSteps": retrieval_steps,
|
| 927 |
+
"returnManifest": return_manifest
|
| 928 |
+
}
|
| 929 |
+
|
| 930 |
+
@app.post("/api/waste/complete‐undocking")
|
| 931 |
+
async def waste_complete_undocking(data:WasteCompleteUndocking):
|
| 932 |
+
undockingContainerId = data.undockingContainerId
|
| 933 |
+
timestamp = data.timestamp
|
| 934 |
+
|
| 935 |
+
placements = await prisma.placement.find_many(where={"containerId": undockingContainerId})
|
| 936 |
+
|
| 937 |
+
item_ids = []
|
| 938 |
+
|
| 939 |
+
for placement in placements:
|
| 940 |
+
item_ids.append(placement.itemId)
|
| 941 |
+
if(timestamp == date.today()):
|
| 942 |
+
await prisma.item.delete(where={"itemId": placement.itemId})
|
| 943 |
+
print(f"Deleted item {placement.itemId}")
|
| 944 |
+
|
| 945 |
+
await prisma.placement.delete_many(where={"containerId": undockingContainerId})
|
| 946 |
+
print(f"Deleted placements for container {undockingContainerId}")
|
| 947 |
+
|
| 948 |
+
return {
|
| 949 |
+
"status": "success",
|
| 950 |
+
"itemsRemoved" : len(item_ids)
|
| 951 |
+
}
|
| 952 |
+
|
| 953 |
+
|
| 954 |
+
@app.post("/api/simulate/day")
|
| 955 |
+
async def simulate_day(data: SimulateDayRequest):
|
| 956 |
+
num_days = data.numOfDays
|
| 957 |
+
target_date = None
|
| 958 |
+
|
| 959 |
+
if data.toTimestamp:
|
| 960 |
+
target_date = datetime.fromisoformat(data.toTimestamp.split('T')[0]).date()
|
| 961 |
+
days_to_simulate = (target_date - curr_date).days
|
| 962 |
+
if days_to_simulate < 0:
|
| 963 |
+
raise HTTPException(status_code=400, detail="Target date must be in the future")
|
| 964 |
+
elif num_days:
|
| 965 |
+
days_to_simulate = num_days
|
| 966 |
+
target_date = curr_date + timedelta(days=days_to_simulate)
|
| 967 |
+
else:
|
| 968 |
+
raise HTTPException(status_code=400, detail="Either numOfDays or toTimestamp must be provided")
|
| 969 |
+
|
| 970 |
+
items_to_use = data.itemsToBeUsedPerDay
|
| 971 |
+
|
| 972 |
+
id_map = {item.itemId: item for item in items_to_use if item.itemId}
|
| 973 |
+
name_map = {item.name: item for item in items_to_use if item.name}
|
| 974 |
+
|
| 975 |
+
all_items = await prisma.item.find_many()
|
| 976 |
+
|
| 977 |
+
items_expired = []
|
| 978 |
+
items_depleted_today = []
|
| 979 |
+
items_used = []
|
| 980 |
+
|
| 981 |
+
for item in all_items:
|
| 982 |
+
if item.expiryDate and item.expiryDate != "N/A":
|
| 983 |
+
expiry_date = datetime.strptime(item.expiryDate, "%Y-%m-%d").date()
|
| 984 |
+
if expiry_date <= target_date:
|
| 985 |
+
items_expired.append({
|
| 986 |
+
"itemId": item.itemId,
|
| 987 |
+
"name": item.name
|
| 988 |
+
})
|
| 989 |
+
|
| 990 |
+
if item.itemId in id_map or item.name in name_map:
|
| 991 |
+
remaining_usage = item.usageLimit - days_to_simulate
|
| 992 |
+
items_used.append({
|
| 993 |
+
"itemId": item.itemId,
|
| 994 |
+
"name": item.name,
|
| 995 |
+
"remainingUses": max(0, remaining_usage)
|
| 996 |
+
})
|
| 997 |
+
|
| 998 |
+
if item.usageLimit > 0 and remaining_usage <= 0:
|
| 999 |
+
items_depleted_today.append({
|
| 1000 |
+
"itemId": item.itemId,
|
| 1001 |
+
"name": item.name
|
| 1002 |
+
})
|
| 1003 |
+
|
| 1004 |
+
return {
|
| 1005 |
+
"success": True,
|
| 1006 |
+
"newDate": target_date.isoformat(),
|
| 1007 |
+
"changes": {
|
| 1008 |
+
"itemsUsed": items_used,
|
| 1009 |
+
"itemsExpired": items_expired,
|
| 1010 |
+
"itemsDepletedToday": items_depleted_today
|
| 1011 |
+
}
|
| 1012 |
+
}
|
| 1013 |
+
|
| 1014 |
+
|
| 1015 |
+
@app.post("/api/import/items")
|
| 1016 |
+
async def import_items(file:UploadFile):
|
| 1017 |
+
# item_id,name,width_cm,depth_cm,height_cm,mass_kg,priority,expiry_date,usage_limit,preferred_zone
|
| 1018 |
+
count = 0
|
| 1019 |
+
contents = await file.read()
|
| 1020 |
+
text_contents = contents.decode("utf-8").splitlines()
|
| 1021 |
+
reader = csv.reader(text_contents)
|
| 1022 |
+
header = next(reader)
|
| 1023 |
+
for row in reader:
|
| 1024 |
+
count+=1
|
| 1025 |
+
item_id = row[0]
|
| 1026 |
+
name = row[1]
|
| 1027 |
+
width = float(row[2]) if row[2] else None
|
| 1028 |
+
depth = float(row[3]) if row[3] else None
|
| 1029 |
+
height = float(row[4]) if row[4] else None
|
| 1030 |
+
mass = float(row[5]) if row[5] else None
|
| 1031 |
+
priority = int(row[6]) if row[6] else None
|
| 1032 |
+
expiry_date = row[7]
|
| 1033 |
+
usage_limit = int(row[8]) if row[8] else None
|
| 1034 |
+
preferred_zone = row[9]
|
| 1035 |
+
|
| 1036 |
+
|
| 1037 |
+
item_data = {
|
| 1038 |
+
"itemId": item_id,
|
| 1039 |
+
"name": name,
|
| 1040 |
+
"width": width,
|
| 1041 |
+
"depth": depth,
|
| 1042 |
+
"height": height,
|
| 1043 |
+
"mass": mass,
|
| 1044 |
+
"priority": priority,
|
| 1045 |
+
"expiryDate": expiry_date,
|
| 1046 |
+
"usageLimit": usage_limit,
|
| 1047 |
+
"preferredZone": preferred_zone
|
| 1048 |
+
}
|
| 1049 |
+
|
| 1050 |
+
existing_item = await prisma.item.find_first(where={"itemId": item_id})
|
| 1051 |
+
if existing_item:
|
| 1052 |
+
await prisma.item.update(where={"itemId": item_id}, data=item_data) # type: ignore
|
| 1053 |
+
print("Updated Item")
|
| 1054 |
+
continue
|
| 1055 |
+
try:
|
| 1056 |
+
created_item = await prisma.item.create(data=item_data) # type: ignore
|
| 1057 |
+
print("Created Item")
|
| 1058 |
+
except Exception as e:
|
| 1059 |
+
print(f"Error creating item: {e}")
|
| 1060 |
+
return Response(json.dumps({"success":True,"itemsImported":count,"errors":[]} ), media_type="application/json")
|
| 1061 |
+
|
| 1062 |
+
@app.post("/api/import/containers")
|
| 1063 |
+
async def import_containers(file: UploadFile):
|
| 1064 |
+
zone_map = {}
|
| 1065 |
+
count = 0
|
| 1066 |
+
contents = await file.read()
|
| 1067 |
+
text_contents = contents.decode("utf-8").splitlines()
|
| 1068 |
+
reader = csv.reader(text_contents)
|
| 1069 |
+
header = next(reader)
|
| 1070 |
+
for row in reader:
|
| 1071 |
+
count+=1
|
| 1072 |
+
zone = row[0]
|
| 1073 |
+
container_id = row[1]
|
| 1074 |
+
width = float(row[2]) if row[2] else None
|
| 1075 |
+
depth = float(row[3]) if row[3] else None
|
| 1076 |
+
height = float(row[4]) if row[4] else None
|
| 1077 |
+
|
| 1078 |
+
if zone not in zone_map:
|
| 1079 |
+
zone_map[zone] = []
|
| 1080 |
+
zone_map[zone].append(container_id)
|
| 1081 |
+
container_data = {
|
| 1082 |
+
"containerId": container_id,
|
| 1083 |
+
"zone": zone,
|
| 1084 |
+
"width": width,
|
| 1085 |
+
"depth": depth,
|
| 1086 |
+
"height": height
|
| 1087 |
+
}
|
| 1088 |
+
|
| 1089 |
+
try:
|
| 1090 |
+
created_container = await prisma.container.create(data=container_data) #type: ignore
|
| 1091 |
+
print("Created Container")
|
| 1092 |
+
except Exception as e:
|
| 1093 |
+
print(f"Error creating container: {e}")
|
| 1094 |
+
|
| 1095 |
+
print(f"Zone Map: {zone_map}")
|
| 1096 |
+
|
| 1097 |
+
for zone_name, container_ids in zone_map.items():
|
| 1098 |
+
zone_data = {
|
| 1099 |
+
"name": zone_name,
|
| 1100 |
+
"containersIds": container_ids
|
| 1101 |
+
}
|
| 1102 |
+
|
| 1103 |
+
try:
|
| 1104 |
+
created_zone = await prisma.zone.create(data=zone_data) #type: ignore
|
| 1105 |
+
print("Created Zone")
|
| 1106 |
+
except Exception as e:
|
| 1107 |
+
print(f"Error creating zone: {e}")
|
| 1108 |
+
|
| 1109 |
+
return Response(json.dumps({"success":True,"containersImported":count,"errors":[]} ), media_type="application/json")
|
| 1110 |
+
|
| 1111 |
+
|
| 1112 |
+
@app.get("/api/export/arrangement")
|
| 1113 |
+
async def export_arrangement():
|
| 1114 |
+
placements = await prisma.placement.find_many()
|
| 1115 |
+
if not placements:
|
| 1116 |
+
raise HTTPException(status_code=404, detail="No placements found")
|
| 1117 |
+
|
| 1118 |
+
file_path = "csv_data/placements.csv"
|
| 1119 |
+
with open(file_path, mode="w", newline="") as file:
|
| 1120 |
+
writer = csv.writer(file)
|
| 1121 |
+
writer.writerow(["Item ID", "Container ID", "Coordinates (W1,D1,H1)", "Coordinates (W2,D2,H2)"])
|
| 1122 |
+
for p in placements:
|
| 1123 |
+
writer.writerow([
|
| 1124 |
+
p.itemId,
|
| 1125 |
+
p.containerId,
|
| 1126 |
+
f"({p.startPos['x']},{p.startPos['y']},{p.startPos['z']})",
|
| 1127 |
+
f"({p.endPos['x']},{p.endPos['y']},{p.endPos['z']})"
|
| 1128 |
+
])
|
| 1129 |
+
|
| 1130 |
+
return FileResponse(file_path, media_type="text/csv", filename="/csv_data/placements.csv")
|
| 1131 |
+
|
| 1132 |
+
class LogWhereInput(BaseModel):
|
| 1133 |
+
createdAt: Optional[dict[str, datetime]] = None
|
| 1134 |
+
itemId: Optional[str] = None
|
| 1135 |
+
userId: Optional[str] = None
|
| 1136 |
+
actionType: Optional[str] = None
|
| 1137 |
+
|
| 1138 |
+
class LogsRequest(BaseModel):
|
| 1139 |
+
startDate: str
|
| 1140 |
+
endDate: str
|
| 1141 |
+
itemId: Optional[str] = None
|
| 1142 |
+
userId: Optional[str] = None
|
| 1143 |
+
actionType: Optional[str] = None
|
| 1144 |
+
@app.get("/api/logs")
|
| 1145 |
+
async def logs(request: LogsRequest):
|
| 1146 |
+
start_date = datetime.fromisoformat(request.startDate)
|
| 1147 |
+
end_date = datetime.fromisoformat(request.endDate)
|
| 1148 |
+
item_id = request.itemId
|
| 1149 |
+
user_id = request.userId
|
| 1150 |
+
action_type = request.actionType
|
| 1151 |
+
|
| 1152 |
+
where = LogWhereInput(
|
| 1153 |
+
createdAt={"gte": start_date, "lte": end_date} if start_date and end_date else None,
|
| 1154 |
+
itemId=item_id,
|
| 1155 |
+
userId=user_id,
|
| 1156 |
+
actionType=action_type
|
| 1157 |
+
)
|
| 1158 |
+
|
| 1159 |
+
logs = await prisma.log.find_many(where=where.dict(exclude_none=True)) #type: ignore
|
| 1160 |
+
if(not logs):
|
| 1161 |
+
raise HTTPException(status_code=404, detail="No logs found")
|
| 1162 |
+
|
| 1163 |
+
log_data = []
|
| 1164 |
+
for log in logs:
|
| 1165 |
+
l_data = {
|
| 1166 |
+
"timestamp": log.timestamp,
|
| 1167 |
+
"userId" : log.userId,
|
| 1168 |
+
"actionType": log.actionType,
|
| 1169 |
+
"itemId": log.itemId,
|
| 1170 |
+
"details" : log.details
|
| 1171 |
+
}
|
| 1172 |
+
log_data.append(l_data)
|
| 1173 |
+
|
| 1174 |
+
return Response(json.dumps({"success":True,"logs":log_data,"errors":[]} ), media_type="application/json")
|
| 1175 |
+
|
| 1176 |
+
@app.get("/api/get-logs")
|
| 1177 |
+
async def get_logs():
|
| 1178 |
+
logs = await prisma.log.find_many(
|
| 1179 |
+
take=3,
|
| 1180 |
+
order=[{"timestamp": "desc"}]
|
| 1181 |
+
)
|
| 1182 |
+
if not logs:
|
| 1183 |
+
raise HTTPException(status_code=404, detail="No logs found")
|
| 1184 |
+
|
| 1185 |
+
log_data = []
|
| 1186 |
+
for log in logs:
|
| 1187 |
+
l_data = {
|
| 1188 |
+
"timestamp": str(log.timestamp),
|
| 1189 |
+
"userId" : log.userId,
|
| 1190 |
+
"actionType": log.actionType,
|
| 1191 |
+
"itemId": log.itemId,
|
| 1192 |
+
"details" : log.details
|
| 1193 |
+
}
|
| 1194 |
+
log_data.append(l_data)
|
| 1195 |
+
|
| 1196 |
+
return Response(json.dumps({"success":True,"logs":log_data,"errors":[]} ))
|
| 1197 |
+
class GetContainers(BaseModel):
|
| 1198 |
+
zoneName:str
|
| 1199 |
+
class GetItems(BaseModel):
|
| 1200 |
+
containerId:str
|
| 1201 |
+
|
| 1202 |
+
@app.get("/api/get-zones")
|
| 1203 |
+
async def get_zones():
|
| 1204 |
+
zones = await prisma.zone.find_many()
|
| 1205 |
+
|
| 1206 |
+
print(zones)
|
| 1207 |
+
zone_names = set()
|
| 1208 |
+
for zone in zones:
|
| 1209 |
+
zone_names.add(zone.name)
|
| 1210 |
+
return Response(json.dumps({"Response":"SUCCESS","zones":list(zone_names)}))
|
| 1211 |
+
|
| 1212 |
+
@app.get("/api/get-items")
|
| 1213 |
+
async def get_items(containerId: str = Query(..., title="Container ID")):
|
| 1214 |
+
placements = await prisma.placement.find_many(where={"containerId": containerId})
|
| 1215 |
+
items_ids = []
|
| 1216 |
+
for placement in placements:
|
| 1217 |
+
print(placement)
|
| 1218 |
+
item_data = await prisma.item.find_first(where={"itemId": placement.itemId})
|
| 1219 |
+
if item_data:
|
| 1220 |
+
items_ids.append({
|
| 1221 |
+
"itemId": placement.itemId,
|
| 1222 |
+
"itemName": item_data.name
|
| 1223 |
+
})
|
| 1224 |
+
print(items_ids)
|
| 1225 |
+
return Response(json.dumps({"Response": "Success", "items": items_ids}))
|
| 1226 |
+
|
| 1227 |
+
@app.get("/api/getItemData")
|
| 1228 |
+
async def get_item_data(itemId: str = Query(..., title="Item ID")):
|
| 1229 |
+
item_data = await prisma.item.find_first(where={"itemId": itemId})
|
| 1230 |
+
if item_data:
|
| 1231 |
+
return {"Response": "Success", "Item": item_data}
|
| 1232 |
+
else:
|
| 1233 |
+
return {"Response": "Item not found"}
|
| 1234 |
+
|
| 1235 |
+
|
| 1236 |
+
@app.get("/api/get-containers")
|
| 1237 |
+
async def get_containers(zoneName: str = Query(..., title="Zone Name")):
|
| 1238 |
+
container_data = await prisma.container.find_many(where={"zone": zoneName})
|
| 1239 |
+
|
| 1240 |
+
print(container_data)
|
| 1241 |
+
|
| 1242 |
+
containers = set()
|
| 1243 |
+
for container in container_data:
|
| 1244 |
+
containers.add(container.containerId)
|
| 1245 |
+
|
| 1246 |
+
return {"Response": "Success", "Containers": list(containers)}
|
| 1247 |
+
|
| 1248 |
+
|
| 1249 |
+
if __name__ == "__main__":
|
| 1250 |
+
uvicorn.run(app,host="0.0.0.0",port=8000)
|
backend/requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.115.12
|
| 2 |
+
pandas==2.2.3
|
| 3 |
+
prisma==0.15.0
|
| 4 |
+
pydantic==2.11.2
|
| 5 |
+
Requests==2.32.3
|
| 6 |
+
sympy==1.13.3
|
| 7 |
+
tomlkit==0.13.2
|
| 8 |
+
uvicorn
|
| 9 |
+
python-multipart
|
prisma/.env
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
MONGODB_URI=mongodb+srv://mantissa6789:Mantis%[email protected]/dbName=iss
|
prisma/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# .env
|
prisma/schema.prisma
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
generator client {
|
| 2 |
+
provider = "prisma-client-py"
|
| 3 |
+
interface = "asyncio"
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
datasource db {
|
| 7 |
+
provider = "mongodb"
|
| 8 |
+
url = env("MONGODB_URI")
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
// wdh
|
| 12 |
+
|
| 13 |
+
// type Dimensions {
|
| 14 |
+
// width Int
|
| 15 |
+
// depth Int
|
| 16 |
+
// height Int
|
| 17 |
+
// }
|
| 18 |
+
|
| 19 |
+
model Item {
|
| 20 |
+
id String @id @default(uuid()) @map("_id")
|
| 21 |
+
itemId String @unique @map("itemId")
|
| 22 |
+
name String
|
| 23 |
+
width Int
|
| 24 |
+
depth Int
|
| 25 |
+
height Int
|
| 26 |
+
mass Int
|
| 27 |
+
priority Int
|
| 28 |
+
expiryDate String
|
| 29 |
+
usageLimit Int
|
| 30 |
+
preferredZone String
|
| 31 |
+
createdAt DateTime @default(now())
|
| 32 |
+
updatedAt DateTime @updatedAt
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
model Container {
|
| 36 |
+
id String @id @default(uuid()) @map("_id")
|
| 37 |
+
containerId String @unique @map("containerId")
|
| 38 |
+
zone String @map("zone")
|
| 39 |
+
width Int
|
| 40 |
+
depth Int
|
| 41 |
+
height Int
|
| 42 |
+
itemsIds String[]
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
model Zone{
|
| 46 |
+
id String @id @default(uuid()) @map("_id")
|
| 47 |
+
name String @map("name")
|
| 48 |
+
containersIds String[]
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
model Placement {
|
| 52 |
+
id String @id @default(uuid()) @map("_id")
|
| 53 |
+
containerId String @map("containerId")
|
| 54 |
+
itemId String @map("itemId")
|
| 55 |
+
startPos Json
|
| 56 |
+
endPos Json
|
| 57 |
+
createdAt DateTime @default(now())
|
| 58 |
+
updatedAt DateTime @updatedAt
|
| 59 |
+
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
model Log {
|
| 63 |
+
id String @id @default(uuid()) @map("_id")
|
| 64 |
+
timestamp DateTime @default(now())
|
| 65 |
+
userId String
|
| 66 |
+
actionType String
|
| 67 |
+
itemId String
|
| 68 |
+
details Json
|
| 69 |
+
}
|