Skip to content

Commit 26af2a3

Browse files
committed
sensor-backend endpoint tested
1 parent 02ab1e2 commit 26af2a3

File tree

6 files changed

+32
-15
lines changed

6 files changed

+32
-15
lines changed

backend/controller/moisture_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def add_moisture_entry(
2020
):
2121
try:
2222
# Call the service layer to add sensor moisture data
23-
response = service.receive_moisture_data(sensors.data)
23+
response = service.receive_moisture_data(sensors.sensor_data)
2424

2525
# Check if the response contains an error
2626
if "error" in response:

backend/dal/sensor_dal.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,29 @@ def __init__(self):
1212

1313
def receive_moisture_data(self, sensors: List[MoistureDataSchema]):
1414
try:
15+
# Ensure the table exists
16+
self.cursor.execute("""
17+
CREATE TABLE IF NOT EXISTS raw_sensors (
18+
id INTEGER NOT NULL,
19+
timestamp TIMESTAMP NOT NULL,
20+
sensor_id INTEGER NOT NULL,
21+
adc_value FLOAT NOT NULL,
22+
moisture_level FLOAT NOT NULL,
23+
digital_status VARCHAR(255) NOT NULL,
24+
weather_temp FLOAT,
25+
weather_humidity FLOAT,
26+
weather_sunlight FLOAT,
27+
weather_wind_speed FLOAT,
28+
location VARCHAR(255),
29+
weather_fetched TEXT,
30+
device_id VARCHAR(255) NOT NULL
31+
);
32+
""")
33+
self.conn.commit()
34+
1535
# Bulk insert query
1636
insert_query = """
17-
INSERT INTO sensors (
37+
INSERT INTO raw_sensors(
1838
id, timestamp, sensor_id, adc_value, moisture_level, digital_status,
1939
weather_temp, weather_humidity, weather_sunlight, weather_wind_speed, location, weather_fetched, device_id
2040
) VALUES %s RETURNING id;
@@ -28,11 +48,11 @@ def receive_moisture_data(self, sensors: List[MoistureDataSchema]):
2848
)
2949
for sensor in sensors
3050
]
31-
# Execute bulk insert
51+
# Execute bulk insert for sensors table
3252
psycopg2.extras.execute_values(self.cursor, insert_query, values)
3353
self.conn.commit()
3454

35-
# Get the returned sensor_id
55+
# Get the returned id
3656
inserted_ids = [row[0] for row in self.cursor.fetchall()]
3757
return {"status": "success", "inserted_ids": inserted_ids}
3858

backend/models/models.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ class Plant(Base):
1212
class Sensor(Base):
1313
__tablename__ = "sensors"
1414

15-
id = Column(Integer, primary_key=True, index=True)
15+
id = Column(Integer, nullable=False)
1616
timestamp = Column(DateTime, nullable=False)
1717
sensor_id = Column(Integer, nullable=False)
18-
adc_value = Column(Integer, nullable=False)
18+
adc_value = Column(Float, nullable=False)
1919
moisture_level = Column(Float, nullable=False)
2020
digital_status = Column(String, nullable=False)
2121
weather_temp = Column(Float, nullable=False)
22-
weather_humidity = Column(Integer, nullable=False)
22+
weather_humidity = Column(Float, nullable=False)
2323
weather_sunlight = Column(Float, nullable=False)
2424
weather_wind_speed = Column(Float, nullable=False)
2525
location = Column(String, nullable=False)
26-
weather_fetched = Column(DateTime, nullable=False)
26+
weather_fetched = Column(String, nullable=False)
2727
device_id = Column(String, nullable=False)

backend/repository/plant_repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ def get_plants(self):
1212
return self.dal.get_plants()
1313

1414
def update_plant_image(self, plant_id: int, new_image_filename: str, file_content: bytes):
15-
return self.dal.update_plant_image(plant_id, new_image_filename)
15+
return self.dal.update_plant_image(plant_id, new_image_filename, file_content)

backend/schemas/sensor_schema.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@ class MoistureDataSchema(BaseModel):
66
id: int
77
timestamp: datetime
88
sensor_id: int
9-
adc_value: int
9+
adc_value: float
1010
moisture_level: float
1111
digital_status: str
1212
weather_temp: float
1313
weather_humidity: float
1414
weather_sunlight: float
1515
weather_wind_speed: float
1616
location: str # city, state, country
17-
weather_fetched: datetime
17+
weather_fetched: str
1818
device_id: str
19-
20-
2119

2220
class MoistureDataListSchema(BaseModel):
23-
data: List[MoistureDataSchema] # Accept array of sensor data
21+
sensor_data: List[MoistureDataSchema] # Accept array of sensor data
2422

2523

2624
class SensorDataSchema(BaseModel):

backend/services/plant_service.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def get_plants(self):
1414
return self.repository.get_plants()
1515

1616
def update_plant_image(self, plant_id: int, new_image_filename: str, file_content: bytes):
17-
# Pass the file content to the repository
1817
return self.repository.update_plant_image(plant_id, new_image_filename, file_content)
1918

2019
def get_service():

0 commit comments

Comments
 (0)