Skip to content

Commit 822cf57

Browse files
committed
Add actuator service to CommandController and implement UV light activation command
Enhance CommandMapper with date conversion methods and update CommandServiceImpl to set dateExecuted.
1 parent 3cc735a commit 822cf57

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

src/main/java/app/smartpot/api/commands/controller/CommandController.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package app.smartpot.api.commands.controller;
22

3+
import app.smartpot.api.actuators.model.dto.ActuatorDTO;
34
import app.smartpot.api.commands.model.dto.CommandDTO;
45
import app.smartpot.api.commands.service.CommandService;
56
import app.smartpot.api.crops.model.dto.CropDTO;
67
import app.smartpot.api.responses.DeleteResponse;
78
import app.smartpot.api.responses.ErrorResponse;
9+
import app.smartpot.api.actuators.model.entity.ActuatorType;
10+
import app.smartpot.api.actuators.service.ActuatorService;
811
import io.swagger.v3.oas.annotations.Operation;
912
import io.swagger.v3.oas.annotations.Parameter;
1013
import io.swagger.v3.oas.annotations.media.ArraySchema;
@@ -21,12 +24,13 @@
2124
public class CommandController {
2225

2326
private final CommandService commandService;
27+
private final ActuatorService actuatorService;
2428

25-
@Autowired
26-
public CommandController(CommandService commandService) {
29+
@Autowired
30+
public CommandController(CommandService commandService, ActuatorService actuatorService) {
2731
this.commandService = commandService;
32+
this.actuatorService = actuatorService;
2833
}
29-
3034
@PostMapping("/Create")
3135
@Operation(summary = "Crear un nuevo comando",
3236
description = "Crea un nuevo comando utilizando los datos proporcionados en el objeto CommandDTO. "
@@ -171,4 +175,30 @@ public ResponseEntity<?> updateCommand(@PathVariable String id, @RequestBody Com
171175
}
172176
}
173177

178+
@PostMapping("/ActivateUVLight/{cropId}")
179+
@Operation(summary = "Activar luz ultravioleta",
180+
description = "Crea un comando para activar la luz UV del cultivo especificado")
181+
public ResponseEntity<?> activateUVLight(@PathVariable String cropId) {
182+
try {
183+
184+
ActuatorDTO uvLight = actuatorService.getActuatorsByCrop(cropId)
185+
.stream()
186+
.filter(a -> a.getType() == ActuatorType.UV_LIGHT)
187+
.findFirst()
188+
.orElseThrow(() -> new Exception("No se encontró luz UV para este cultivo"));
189+
190+
CommandDTO command = new CommandDTO();
191+
command.setCommandType("ACTIVATE_UV_LIGHT");
192+
command.setActuator(uvLight.getId());
193+
command.setCrop(cropId);
194+
195+
return new ResponseEntity<>(commandService.createCommand(command), HttpStatus.CREATED);
196+
} catch (Exception e) {
197+
return new ResponseEntity<>(
198+
new ErrorResponse("Error al activar la luz UV [" + e.getMessage() + "]",
199+
HttpStatus.BAD_REQUEST.value()),
200+
HttpStatus.BAD_REQUEST);
201+
}
202+
}
203+
174204
}

src/main/java/app/smartpot/api/commands/mapper/CommandMapper.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77
import org.mapstruct.Mapping;
88
import org.mapstruct.factory.Mappers;
99

10+
import java.text.ParseException;
11+
import java.text.SimpleDateFormat;
12+
import java.util.Date;
13+
1014
@Mapper(componentModel = "spring")
1115
public interface CommandMapper {
1216
CommandMapper INSTANCE = Mappers.getMapper(CommandMapper.class);
1317

1418
@Mapping(source = "id", target = "id", qualifiedByName = "stringToObjectId")
1519
@Mapping(source = "crop", target = "crop", qualifiedByName = "stringToObjectId")
1620
@Mapping(source = "actuator", target = "actuator", qualifiedByName = "stringToObjectId")
21+
@Mapping(source = "dateCreated", target = "dateCreated", qualifiedByName = "stringToDate")
22+
@Mapping(source = "dateExecuted", target = "dateExecuted", qualifiedByName = "stringToDate")
1723
Command toEntity(CommandDTO commandDTO);
1824

1925
@Mapping(source = "id", target = "id", qualifiedByName = "objectIdToString")
2026
@Mapping(source = "crop", target = "crop", qualifiedByName = "objectIdToString")
2127
@Mapping(source = "actuator", target = "actuator", qualifiedByName = "objectIdToString")
28+
@Mapping(source = "dateCreated", target = "dateCreated", qualifiedByName = "dateToString")
29+
@Mapping(source = "dateExecuted", target = "dateExecuted", qualifiedByName = "dateToString")
2230
CommandDTO toDTO(Command command);
2331

2432
@org.mapstruct.Named("objectIdToString")
@@ -30,4 +38,26 @@ default String objectIdToString(ObjectId objectId) {
3038
default ObjectId stringToObjectId(String id) {
3139
return id != null ? new ObjectId(id) : null;
3240
}
41+
42+
@org.mapstruct.Named("stringToDate")
43+
default Date stringToDate(String dateString) {
44+
if (dateString == null || dateString.isEmpty()) {
45+
return null;
46+
}
47+
try {
48+
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
49+
return formatter.parse(dateString);
50+
} catch (ParseException e) {
51+
throw new RuntimeException("Error al convertir la fecha: " + dateString, e);
52+
}
53+
}
54+
55+
@org.mapstruct.Named("dateToString")
56+
default String dateToString(Date date) {
57+
if (date == null) {
58+
return null;
59+
}
60+
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
61+
return formatter.format(date);
62+
}
3363
}

src/main/java/app/smartpot/api/commands/service/CommandServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public CommandDTO executeCommand(String id, String response) throws Exception {
213213
return Optional.of(getCommandById(id))
214214
.map( commandDTO -> {
215215
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
216-
commandDTO.setDateCreated(formatter.format(new Date()));
216+
commandDTO.setDateExecuted(formatter.format(new Date()));
217217
commandDTO.setStatus(CommandStatus.EXECUTED);
218218
commandDTO.setResponse(response);
219219
return commandDTO;

0 commit comments

Comments
 (0)