Compare commits

..

10 Commits

Author SHA1 Message Date
sajad-dev 44bfceea37 UPDATE 2026-05-13 03:55:49 +03:30
sajad-dev c140caf8b2 UPDATE 2026-04-27 04:35:19 +03:30
sajad-dev 5a8f852237 UPDATE 2026-04-27 03:25:00 +03:30
sajad-dev 26a587fe09 UPDATE 2026-04-05 00:57:50 +03:30
sajad-dev 24b90a8c81 UPDATE 2026-03-25 01:54:43 +03:30
sajad-dev d572265d9c UPDATE 2026-03-24 16:38:11 +03:30
sajad-dev 9b8452dfbf CI/CD 2026-03-20 23:28:54 +03:30
sajad-dev 04b8776f66 CI/CD 2026-03-20 23:10:10 +03:30
sajad-dev d39acf7272 CI/CD 2026-03-20 17:41:43 +03:30
sajad-dev b3bf936606 CI/CD 2026-03-20 17:31:23 +03:30
17 changed files with 474 additions and 52 deletions
+8 -3
View File
@@ -3,13 +3,18 @@ SECRET_KEY=your-secret-key-change-in-production
DEBUG=1
ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0
# Database (MySQL) - used by Django in Docker
# Database (MySQL)
DB_ENGINE=django.db.backends.mysql
DB_NAME=croplogic
DB_USER=croplogic
DB_PASSWORD=changeme
DB_HOST=db
DB_PORT=3306
DB_ROOT_PASSWORD=root
# Optional: for running manage.py from host (local DB)
# DB_HOST=127.0.0.1
# Cassandra
CASSANDRA_ENABLED=1
CASSANDRA_HOSTS=cassandra
CASSANDRA_PORT=9042
CASSANDRA_KEYSPACE=sensor_hub
CASSANDRA_REPLICATION={'class': 'SimpleStrategy', 'replication_factor': 1}
+3 -3
View File
@@ -71,7 +71,7 @@ jobs:
- name: Checkout repository
run: |
git clone http://gitea:3000/sajad-dev/SensorHub.git .
git clone http://15f3baa28036aa35f8eb707585567d1b87bd8977@git.crop-logic.ir/sajad-dev/SensorHub.git .
- name: Install Python
run: |
@@ -113,8 +113,8 @@ jobs:
- name: Deploy
run: |
ssh ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} -p ${{secrets.SERVER_SSH_PORT}}<< 'EOF'
cd application/Sensor-Hub
cd application/SensorHub
git pull origin production
docker-compose -f docker-compose-prod.yaml down
docker-compose -f docker-compose-prod.yaml down --remove-orphans
docker-compose -f docker-compose-prod.yaml up -d
EOF
+4
View File
@@ -0,0 +1,4 @@
[submodule "Schemas"]
path = Schemas
url = ssh://git@git.crop-logic.ir:2222/sajad-dev/Schemas.git
branch = develop
+16 -10
View File
@@ -1,18 +1,23 @@
FROM python:3.12-slim
FROM docker.iranserver.com/python:3.10
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /app
# Debian/Ubuntu mirrors for apt
RUN rm -f /etc/apt/sources.list.d/debian.sources && \
printf '%s
' \
'deb https://linux-mirror.liara.ir/repository/debian bookworm main' \
'deb https://linux-mirror.liara.ir/repository/debian bookworm-updates main' \
'deb https://linux-mirror.liara.ir/repository/debian-security bookworm-security main' \
> /etc/apt/sources.list
# Debian/debian mirrors for apt
RUN rm -f /etc/apt/sources.list /etc/apt/sources.list.d/* && \
printf '%s\n' \
'deb https://mirror-linux.runflare.com/debian/ bookworm main contrib non-free non-free-firmware' \
'deb https://mirror-linux.runflare.com/debian/ bookworm-updates main contrib non-free non-free-firmware' \
'deb https://mirror-linux.runflare.com/debian-security/ bookworm-security main contrib non-free non-free-firmware' \
'' \
'deb [trusted=yes] https://mirror2.chabokan.net/debian bookworm main contrib non-free non-free-firmware' \
'deb [trusted=yes] https://mirror2.chabokan.net/debian-security bookworm-security main contrib non-free non-free-firmware' \
'' \
'deb http://mirror.iranserver.com/debian/ bookworm main contrib non-free non-free-firmware' \
'deb-src http://mirror.iranserver.com/debian/ bookworm main contrib non-free non-free-firmware' \
> /etc/apt/sources.list
# System deps for MySQL client (pkg-config required by mysqlclient to find libs)
RUN apt-get update && apt-get install -y --no-install-recommends \
@@ -26,11 +31,12 @@ COPY requirements.txt .
# Python mirrors
RUN pip config --user set global.index-url https://package-mirror.liara.ir/repository/pypi/simple && \
pip config --user set global.extra-index-url https://mirror.cdn.ir/repository/pypi/simple && \
pip config --user set global.extra-index-url https://mirror2.chabokan.net/pypi/simple && \
pip config --user set global.trusted-host package-mirror.liara.ir && \
pip config --user set global.trusted-host mirror.cdn.ir && \
pip config --user set global.trusted-host mirror-pypi.runflare.com
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install -r requirements.txt
COPY . .
+6 -7
View File
@@ -18,10 +18,7 @@ INSTALLED_APPS = [
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"auth.apps.AuthConfig",
"account",
"sensor_hub",
"dashboard",
"ingest",
"rest_framework",
"corsheaders",
]
@@ -98,9 +95,11 @@ REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.AllowAny",
],
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework_simplejwt.authentication.JWTAuthentication",
],
}
if "rest_framework_simplejwt" in INSTALLED_APPS:
REST_FRAMEWORK["DEFAULT_AUTHENTICATION_CLASSES"] = [
"rest_framework_simplejwt.authentication.JWTAuthentication",
]
CORS_ALLOW_ALL_ORIGINS = DEBUG
+4 -5
View File
@@ -1,11 +1,10 @@
from django.contrib import admin
from django.urls import include, path
from ingest.views import SensorSimulatorAppView
urlpatterns = [
path("admin/", admin.site.urls),
path("api/auth/", include("auth.urls")),
path("api/account/", include("account.urls")),
path("api/sensor-hub/", include("sensor_hub.urls")),
path("api/farm-dashboard-config/", include("dashboard.urls_config")),
path("api/farm-dashboard/", include("dashboard.urls")),
path("", SensorSimulatorAppView.as_view(), name="home"),
path("api/ingest/", include("ingest.urls")),
]
+36 -24
View File
@@ -1,10 +1,8 @@
# Production: no source mount; image contains code
name: sensor-hub
services:
db:
image: mysql:8.0
image: docker.iranserver.com/mysql:8.0
container_name: sensor-hub-db
restart: always
environment:
MYSQL_DATABASE: ${DB_NAME:-sensor_hub}
MYSQL_USER: ${DB_USER:-sensor_hub}
@@ -12,30 +10,22 @@ services:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
volumes:
- sensor_hub_mysql_data:/var/lib/mysql
restart: unless-stopped
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${DB_ROOT_PASSWORD}"]
test: ["CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p$${MYSQL_ROOT_PASSWORD}"]
interval: 10s
timeout: 5s
retries: 5
phpmyadmin:
image: phpmyadmin:latest
container_name: sensor-hub-phpmyadmin
environment:
PMA_HOST: db
PMA_PORT: 3306
UPLOAD_LIMIT: 64M
ports:
- "8081:80"
depends_on:
db:
condition: service_healthy
restart: unless-stopped
networks:
- sensor-network
web:
build: .
build:
context: .
dockerfile: Dockerfile
container_name: sensor-hub-web
restart: always
ports:
- "8010:8000"
env_file:
- .env
environment:
@@ -43,9 +33,31 @@ services:
depends_on:
db:
condition: service_healthy
restart: unless-stopped
ports:
- "8010:8000"
networks:
- sensor-network
sensor-sender:
build:
context: .
dockerfile: Dockerfile
container_name: sensor-hub-sender
command: python manage.py send_sensor_data
restart: always
env_file:
- .env
environment:
DB_HOST: db
depends_on:
web:
condition: service_started
db:
condition: service_healthy
networks:
- sensor-network
volumes:
sensor_hub_mysql_data:
networks:
sensor-network:
driver: bridge
+36
View File
@@ -2,6 +2,19 @@
name: sensor-hub
services:
cassandra:
image: docker-mirror.liara.ir/cassandra:5.0
container_name: sensor-hub-cassandra
ports:
- "9042:9042"
volumes:
- sensor_hub_cassandra_data:/var/lib/cassandra
healthcheck:
test: ["CMD-SHELL", "cqlsh -e 'DESCRIBE KEYSPACES' || exit 1"]
interval: 20s
timeout: 10s
retries: 10
db:
image: docker-mirror.liara.ir/mysql:8.0
container_name: sensor-hub-db
@@ -43,9 +56,32 @@ services:
- .env
environment:
DB_HOST: db
CASSANDRA_HOSTS: cassandra
depends_on:
db:
condition: service_healthy
cassandra:
condition: service_started
sensor-sender:
build: .
container_name: sensor-hub-sender
command: python manage.py send_sensor_data
volumes:
- .:/app
env_file:
- .env
environment:
DB_HOST: db
CASSANDRA_HOSTS: cassandra
depends_on:
web:
condition: service_started
db:
condition: service_healthy
cassandra:
condition: service_started
volumes:
sensor_hub_mysql_data:
sensor_hub_cassandra_data:
View File
+14
View File
@@ -0,0 +1,14 @@
API_TARGET_URL = "http://backend-web:8000"
API_KEY = "12345"
REQUEST_INTERVAL_SECONDS = 10
STATIC_SENSOR_PAYLOAD = {
"uuid": "11111111111111111111",
"soil_moisture": 42.5,
"soil_temperature": 24.3,
"soil_ph": 6.8,
"soil_ec": 1.4,
"nitrogen": 32,
"phosphorus": 18,
"potassium": 27,
}
View File
@@ -0,0 +1,72 @@
import json
import time
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
from django.core.management.base import BaseCommand
from ingest.constants import API_KEY, API_TARGET_URL, REQUEST_INTERVAL_SECONDS, STATIC_SENSOR_PAYLOAD
class Command(BaseCommand):
help = "Send the static soil sensor payload to the upstream API every 10 seconds."
def add_arguments(self, parser):
parser.add_argument(
"--once",
action="store_true",
help="Send the request once and exit.",
)
def handle(self, *args, **options):
run_once = options["once"]
self.stdout.write(
self.style.SUCCESS(
f"Starting sensor sender -> {API_TARGET_URL} (interval: {REQUEST_INTERVAL_SECONDS}s)"
)
)
while True:
self.send_payload()
if run_once:
break
time.sleep(REQUEST_INTERVAL_SECONDS)
def send_payload(self):
body = json.dumps(STATIC_SENSOR_PAYLOAD).encode("utf-8")
request = Request(
API_TARGET_URL,
data=body,
headers={
"Content-Type": "application/json",
"api_key": API_KEY,
},
method="POST",
)
try:
with urlopen(request, timeout=15) as response:
response_body = response.read().decode("utf-8", errors="replace")
self.stdout.write(
self.style.SUCCESS(
f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Sent payload successfully - status {response.status}"
)
)
if response_body:
self.stdout.write(response_body)
except HTTPError as exc:
error_body = exc.read().decode("utf-8", errors="replace")
self.stderr.write(
self.style.ERROR(
f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Upstream error - status {exc.code}"
)
)
if error_body:
self.stderr.write(error_body)
except URLError as exc:
self.stderr.write(
self.style.ERROR(
f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Connection error - {exc.reason}"
)
)
+166
View File
@@ -0,0 +1,166 @@
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>شبیه ساز سنسور خاک</title>
<style>
:root {
--bg: linear-gradient(135deg, #f4efe6 0%, #dce8d5 100%);
--card: rgba(255, 252, 246, 0.92);
--ink: #203126;
--muted: #5a6b60;
--accent: #2f6a4f;
--accent-dark: #1e4936;
--border: rgba(47, 106, 79, 0.18);
--shadow: 0 24px 60px rgba(32, 49, 38, 0.16);
}
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
font-family: Tahoma, sans-serif;
background: var(--bg);
color: var(--ink);
display: flex;
align-items: center;
justify-content: center;
padding: 24px;
}
.shell {
width: min(980px, 100%);
background: var(--card);
border: 1px solid var(--border);
border-radius: 28px;
box-shadow: var(--shadow);
overflow: hidden;
}
.hero {
padding: 32px;
background: linear-gradient(135deg, rgba(47,106,79,.95), rgba(85,130,96,.88));
color: #f9f6ee;
}
.hero h1 { margin: 0 0 12px; font-size: clamp(28px, 5vw, 42px); }
.hero p { margin: 0; line-height: 1.8; max-width: 700px; }
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 24px;
padding: 24px;
}
.panel {
border: 1px solid var(--border);
border-radius: 22px;
padding: 20px;
background: rgba(255,255,255,.68);
}
label {
display: block;
font-weight: bold;
margin-bottom: 8px;
}
input, textarea {
width: 100%;
border: 1px solid rgba(32,49,38,.14);
border-radius: 14px;
padding: 12px 14px;
font: inherit;
background: rgba(255,255,255,.9);
color: var(--ink);
}
textarea { min-height: 320px; resize: vertical; direction: ltr; text-align: left; }
.hint {
color: var(--muted);
font-size: 14px;
margin-top: 8px;
line-height: 1.8;
}
button {
border: 0;
background: var(--accent);
color: #fff;
padding: 14px 18px;
border-radius: 14px;
font: inherit;
cursor: pointer;
min-width: 180px;
}
button:hover { background: var(--accent-dark); }
pre {
margin: 0;
white-space: pre-wrap;
word-break: break-word;
background: #18241c;
color: #d9f4e2;
border-radius: 18px;
padding: 16px;
min-height: 220px;
direction: ltr;
text-align: left;
}
</style>
</head>
<body>
<main class="shell">
<section class="hero">
<h1>ارسال استاتیک داده سنسور خاک</h1>
<p>
این صفحه بدون هیچ اتصال به دیتابیس، یک payload استاتیک از داده های سنسور خاک را با متد POST
و هدر <code>api_key</code> به API مقصد ارسال می کند.
</p>
</section>
<section class="grid">
<form class="panel" id="sender-form">
<label for="target_url">آدرس API مقصد</label>
<input id="target_url" name="target_url" value="{{ default_url }}" required>
<label for="api_key" style="margin-top: 16px;">API Key</label>
<input id="api_key" name="api_key" value="{{ default_api_key }}" required>
<label for="payload" style="margin-top: 16px;">JSON ارسالی</label>
<textarea id="payload" readonly>{{ default_payload }}</textarea>
<p class="hint">
فیلدها شامل uuid، رطوبت خاک، دمای خاک، pH، EC، نیتروژن، فسفر و پتاسیم هستند و فعلا همه به صورت استاتیک تعریف شده اند.
</p>
<div style="margin-top: 18px; display: flex; gap: 12px; flex-wrap: wrap;">
<button type="submit">ارسال به API</button>
</div>
</form>
<section class="panel">
<label>نتیجه درخواست</label>
<pre id="result">هنوز درخواستی ارسال نشده است.</pre>
<p class="hint">
در پاسخ، payload ارسالی، هدرهای ارسال شده و پاسخ API مقصد نمایش داده می شود.
</p>
</section>
</section>
</main>
<script>
const form = document.getElementById('sender-form');
const result = document.getElementById('result');
form.addEventListener('submit', async (event) => {
event.preventDefault();
result.textContent = 'در حال ارسال...';
const formData = new FormData(form);
try {
const response = await fetch('/api/ingest/forward/', {
method: 'POST',
body: formData,
});
const data = await response.json();
result.textContent = JSON.stringify(data, null, 2);
} catch (error) {
result.textContent = JSON.stringify({ error: error.message }, null, 2);
}
});
</script>
</body>
</html>
+7
View File
@@ -0,0 +1,7 @@
from django.urls import path
from .views import ForwardSensorDataView
urlpatterns = [
path("forward/", ForwardSensorDataView.as_view(), name="forward-sensor-data"),
]
+98
View File
@@ -0,0 +1,98 @@
import json
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from ingest.constants import API_KEY, API_TARGET_URL, STATIC_SENSOR_PAYLOAD
class SensorSimulatorAppView(View):
def get(self, request):
return render(
request,
"ingest/index.html",
{
"default_payload": json.dumps(STATIC_SENSOR_PAYLOAD, indent=2),
"default_url": API_TARGET_URL,
"default_api_key": API_KEY,
},
)
@method_decorator(csrf_exempt, name="dispatch")
class ForwardSensorDataView(View):
def post(self, request):
target_url = request.POST.get("target_url", "").strip()
api_key = request.POST.get("api_key", "").strip()
if not target_url:
return JsonResponse({"error": "target_url is required"}, status=400)
if not api_key:
return JsonResponse({"error": "api_key is required"}, status=400)
payload = STATIC_SENSOR_PAYLOAD
body = json.dumps(payload).encode("utf-8")
outbound_request = Request(
target_url,
data=body,
headers={
"Content-Type": "application/json",
"api_key": api_key,
},
method="POST",
)
try:
with urlopen(outbound_request, timeout=15) as response:
response_body = response.read().decode("utf-8")
content_type = response.headers.get("Content-Type", "")
parsed_body = response_body
if "application/json" in content_type and response_body:
parsed_body = json.loads(response_body)
return JsonResponse(
{
"status": response.status,
"sent_headers": {
"Content-Type": "application/json",
"api_key": api_key,
},
"sent_payload": payload,
"response": parsed_body,
}
)
except HTTPError as exc:
error_body = exc.read().decode("utf-8", errors="replace")
return JsonResponse(
{
"error": "upstream returned an error",
"status": exc.code,
"sent_headers": {
"Content-Type": "application/json",
"api_key": api_key,
},
"sent_payload": payload,
"response": error_body,
},
status=502,
)
except URLError as exc:
return JsonResponse(
{
"error": "could not reach upstream api",
"details": str(exc.reason),
"sent_headers": {
"Content-Type": "application/json",
"api_key": api_key,
},
"sent_payload": payload,
},
status=502,
)
return HttpResponse(status=500)
+4
View File
@@ -10,3 +10,7 @@ redis>=5.0,<5.1
mysqlclient>=2.2,<2.3
gunicorn>=22,<23
python-dotenv>=1.0,<1.1
cassandra-driver>=3.29,<3.30