Skip to content

Commit

Permalink
FDP-2359: more specific command urc handling
Browse files Browse the repository at this point in the history
Signed-off-by: Loes Immens <[email protected]>
  • Loading branch information
loesimmens committed Aug 22, 2024
1 parent 0f4391a commit 9a59d1f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ data class Command(
val commandValue: String?,
@Enumerated(EnumType.STRING) var status: CommandStatus,
) {
enum class CommandType {
REBOOT
enum class CommandType(val downlink: String, val urcsSuccess: List<String>, val urcsFailure: List<String>) {
REBOOT("CMD:REBOOT", listOf("INIT", "WDR"), listOf())
}

enum class CommandStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class UrcService(
) {
companion object {
private const val URC_FIELD = "URC"
private const val DL_FIELD = "DL"
private const val URC_PSK_SUCCESS = "PSK:SET"
}

Expand All @@ -49,13 +50,17 @@ class UrcService(

val commandInProgress = commandService.getFirstCommandInProgressForDevice(deviceId)
if (commandInProgress != null) {
handleUrcsForCommand(urcs, deviceId, commandInProgress)
val downlink = getDownlinkFromMessage(body)
handleUrcsForCommand(urcs, deviceId, commandInProgress, downlink)
}
}

private fun getUrcsFromMessage(body: JsonNode) =
body[URC_FIELD].filter { it.isTextual }.map { it.asText() }

private fun getDownlinkFromMessage(body: JsonNode) =
body[URC_FIELD].first { it.isObject }[DL_FIELD].asText()

private fun urcsContainPskError(urcs: List<String>) = urcs.any { urc -> isPskErrorUrc(urc) }

private fun handlePskErrors(deviceId: String, urcs: List<String>) {
Expand Down Expand Up @@ -90,17 +95,26 @@ class UrcService(
private fun handleUrcsForCommand(
urcs: List<String>,
deviceId: String,
commandInProgress: Command
commandInProgress: Command,
downlink: String
) {
if (urcsContainErrors(urcs)) {
handleCommandError(deviceId, commandInProgress, urcs)
} else {
handleCommandUrcs(deviceId, commandInProgress, urcs)
// check if urc is about this command
if(downlink.contains(commandInProgress.type.downlink)) {
if (urcsContainErrors(urcs)) {
handleCommandError(deviceId, commandInProgress, urcs)
} else if(urcsContainSuccesses(urcs, commandInProgress)){
handleCommandUrcs(deviceId, commandInProgress, urcs)
}
}
}

private fun urcsContainErrors(urcs: List<String>) = urcs.any { urc -> isErrorUrc(urc) }

private fun urcsContainSuccesses(urcs: List<String>, command: Command) =
command.type.urcsSuccess.any {
successUrc -> urcs.contains(successUrc)
}

private fun handleCommandError(deviceId: String, command: Command, urcs: List<String>) {
val errorUrcs = urcs.filter { urc -> isErrorUrc(urc) }
val message =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ class UrcServiceTest {
companion object {
private const val URC_FIELD = "URC"
private const val DL_FIELD = "DL"
private const val PSK_COMMAND =
private const val PSK_DOWNLINK =
"!PSK:umU6KJ4g7Ye5ZU6o:4a3cfdd487298e2f048ebfd703a1da4800c18f2167b62192cf7dc9fd6cc4bcd3;PSK:umU6KJ4g7Ye5ZU6o:4a3cfdd487298e2f048ebfd703a1da4800c18f2167b62192cf7dc9fd6cc4bcd3:SET"
private const val REBOOT_DOWNLINK = "CMD:REBOOT"
private const val DEVICE_ID = "867787050253370"

@JvmStatic
Expand Down Expand Up @@ -72,9 +73,6 @@ class UrcServiceTest {
listOf("EXR"),
listOf("POR"),
listOf("INIT", "BOR", "POR"))

@JvmStatic
private fun containingRebootSuccesUrc() = Stream.of(listOf("INIT"), listOf("INIT", "WDR"))
}

@Test
Expand Down Expand Up @@ -103,7 +101,7 @@ class UrcServiceTest {
fun handleErrorUrcForCommand(urcs: List<String>) {
val commandInProgress = TestHelper.rebootCommandInProgress()
val commandError = commandInProgress.copy(status = Command.CommandStatus.ERROR)
val message = updatePskCommandInMessage(urcs)
val message = updateUrcInMessage(urcs, REBOOT_DOWNLINK)

whenever(pskService.needsKeyChange(DEVICE_ID)).thenReturn(false)
whenever(pskService.isPendingKeyPresent(DEVICE_ID)).thenReturn(false)
Expand All @@ -118,19 +116,18 @@ class UrcServiceTest {
.sendFeedback(eq(commandError), eq(CommandStatus.Error), any<String>())
}

@ParameterizedTest(name = "should handle success urcs for command")
@MethodSource("containingRebootSuccesUrc")
fun handleSuccessUrcForCommand(urcs: List<String>) {
@Test
fun handleSuccessUrcForCommand() {
val urcs = listOf("INIT", "WDR")
val commandInProgress = TestHelper.rebootCommandInProgress()
val commandSuccessful = commandInProgress.copy(status = Command.CommandStatus.SUCCESSFUL)
val message = updatePskCommandInMessage(urcs)
val message = updateUrcInMessage(urcs, REBOOT_DOWNLINK)

whenever(pskService.needsKeyChange(DEVICE_ID)).thenReturn(false)
whenever(pskService.isPendingKeyPresent(DEVICE_ID)).thenReturn(false)
whenever(commandService.getFirstCommandInProgressForDevice(DEVICE_ID))
.thenReturn(commandInProgress)
//
// whenever(commandService.saveCommandEntity(commandSuccessful)).thenReturn(commandSuccessful)
whenever(commandService.saveCommandEntity(commandSuccessful)).thenReturn(commandSuccessful)

urcService.interpretURCInMessage(DEVICE_ID, message)

Expand All @@ -139,27 +136,44 @@ class UrcServiceTest {
.sendFeedback(eq(commandSuccessful), eq(CommandStatus.Successful), any<String>())
}

@Test
fun shouldDoNothingIfUrcDoesNotConcernCommandInProgress() {
val urcs = listOf("ENPD")
val commandInProgress = TestHelper.rebootCommandInProgress()
val message = updateUrcInMessage(urcs, REBOOT_DOWNLINK)

whenever(pskService.needsKeyChange(DEVICE_ID)).thenReturn(false)
whenever(pskService.isPendingKeyPresent(DEVICE_ID)).thenReturn(false)
whenever(commandService.getFirstCommandInProgressForDevice(DEVICE_ID))
.thenReturn(commandInProgress)

urcService.interpretURCInMessage(DEVICE_ID, message)

verify(commandService, times(0)).saveCommandEntity(any<Command>())
verify(commandFeedbackService, times(0)).sendFeedback(any<Command>(), any<CommandStatus>(), any<String>())
}

private fun interpretURCWhileNewKeyIsPending(urcs: List<String>) {
whenever(pskService.needsKeyChange(DEVICE_ID)).thenReturn(false)
whenever(pskService.isPendingKeyPresent(DEVICE_ID)).thenReturn(true)

val message = updatePskCommandInMessage(urcs)
val message = updateUrcInMessage(urcs, PSK_DOWNLINK)

urcService.interpretURCInMessage(DEVICE_ID, message)
}

private fun updatePskCommandInMessage(urcs: List<String>): JsonNode {
private fun updateUrcInMessage(urcs: List<String>, downlink: String): JsonNode {
val message = TestHelper.messageTemplate()
val urcFieldValue = urcFieldValue(urcs)
val urcFieldValue = urcFieldValue(urcs, downlink)

message.replace(URC_FIELD, urcFieldValue)
return message
}

private fun urcFieldValue(urcs: List<String>): ArrayNode? {
private fun urcFieldValue(urcs: List<String>, downlink: String): ArrayNode? {
val urcNodes = urcs.map { urc -> TextNode(urc) }
val downlinkNode =
ObjectNode(JsonNodeFactory.instance, mapOf(DL_FIELD to TextNode(PSK_COMMAND)))
ObjectNode(JsonNodeFactory.instance, mapOf(DL_FIELD to TextNode(downlink)))
val urcsPlusReceivedDownlink: MutableList<BaseJsonNode> = mutableListOf()
urcsPlusReceivedDownlink.addAll(urcNodes)
urcsPlusReceivedDownlink.add(downlinkNode)
Expand Down

0 comments on commit 9a59d1f

Please sign in to comment.