Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added initial station info test and not requiere sensor data in Creat… #126

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ class CreateStationDataAPIView(APIView):
def post(self, request, *args, **kwargs):
# Parse the incoming JSON data
try:
station_data = request.data.get('station')
sensors_data = request.data.get('sensors')
station_data = request.data.get('station', None)
sensors_data = request.data.get('sensors', None)

if not station_data or not sensors_data:
if not station_data:
raise ValidationError("Both 'station' and 'sensors' are required.")

# Use the get_or_create_station function to get or create the station
Expand All @@ -165,6 +165,9 @@ def post(self, request, *args, **kwargs):
# Record the time when the request was received
time_received = datetime.datetime.now(datetime.timezone.utc)

if sensors_data:
return JsonResponse({"status": "success, but no sensor data found"}, status=200)

try:
with transaction.atomic():
# Iterate through all sensors
Expand Down
18 changes: 18 additions & 0 deletions app/devices/migrations/0014_devicestatus_sensor_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.2 on 2025-01-28 13:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('devices', '0013_device_current_user_measurement_user_and_more'),
]

operations = [
migrations.AddField(
model_name='devicestatus',
name='sensor_list',
field=models.JSONField(null=True),
),
]
1 change: 1 addition & 0 deletions app/devices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DeviceStatus(models.Model):
device = models.ForeignKey(Device, on_delete=models.CASCADE, related_name='status_list')
battery_voltage = models.FloatField(null=True, blank=True)
battery_soc = models.FloatField(null=True, blank=True)
sensor_list = models.JSONField(null=True)

def save(self, *args, **kwargs):
if not self.pk: # Check if the instance is new
Expand Down
13 changes: 10 additions & 3 deletions app/main/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def get_or_create_station(station_info: dict):
"voltage": 0,
"percentage": 0
}
"sensor_list": [
{
"model_id": 1,
"dimension_list": [1, 2, 3]
}
]
}

creates a station_status entry with the information in station_info
Expand All @@ -31,13 +37,14 @@ def get_or_create_station(station_info: dict):
station.model = station_info['model']
station.firmware = station_info['firmware']
station.api_key = station_info['apikey']

# add a new DeviceStatus
station_status = DeviceStatus.objects.create(
time_received = datetime.datetime.now(datetime.timezone.utc),
device = station,
battery_voltage = station_info['battery']['voltage'],
battery_soc = station_info['battery']['percentage'],
battery_voltage = station_info.get('battery', {}).get('voltage', None),
battery_soc = station_info.get('battery', {}).get('percentage', None),
sensor_list = station_info.get('sensor_list', None)
)

# update firmware field
Expand Down
3 changes: 3 additions & 0 deletions app/staticfiles/debug_toolbar/css/print.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#djDebug {
display: none !important;
}
Loading
Loading