DlmsPushNotificationDecoder extends ReplayingDecoder<DlmsPushNotificationDecoder.DecodingState> { @Override protected void decode(final ChannelHandlerContext ctx, final ByteBuf in, final List<Object> out) throws UnrecognizedMessageDataException { DlmsPushNotification pushNotification; final boolean smr5alarm = in.getByte(8) == 0x0F; LOGGER.info("Decoding state: {}, SMR5 alarm: {}", this.state(), smr5alarm); if (smr5alarm) { final Smr5AlarmDecoder alarmDecoder = new Smr5AlarmDecoder(); pushNotification = alarmDecoder.decodeSmr5alarm(in); } else { final Dsmr4AlarmDecoder alarmDecoder = new Dsmr4AlarmDecoder(); pushNotification = alarmDecoder.decodeDsmr4alarm(in); } LOGGER.info("Decoded push notification: {}", pushNotification); out.add(pushNotification); } DlmsPushNotificationDecoder();   }
@Test public void decodeDsmr4AlarmsWithAlarmRegister() throws UnrecognizedMessageDataException { this.decoder = new DlmsPushNotificationDecoder(); final ByteBuf buffer = mock(ByteBuf.class); final byte[] alarmRegister = new byte[] { 0x00, 0x18, 0x00, 0x02 }; final byte[] bytes = this.setupDsmr4Buffer(buffer, IDENTIFIER, alarmRegister); final List<Object> out = new ArrayList<>(); this.decoder.decode(this.ctx, buffer, out); final Object pushNotificationObject = out.get(0); assertThat(pushNotificationObject instanceof DlmsPushNotification).isTrue(); final DlmsPushNotification dlmsPushNotification = (DlmsPushNotification) pushNotificationObject; assertThat(dlmsPushNotification.getEquipmentIdentifier()).isEqualTo(IDENTIFIER); assertThat(dlmsPushNotification.getTriggerType()).isEqualTo(PUSH_ALARM_TRIGGER); assertThat(dlmsPushNotification.toByteArray()).isEqualTo(bytes); final Set<AlarmTypeDto> alarms = dlmsPushNotification.getAlarms(); assertThat(alarms.size()).isEqualTo(3); assertThat(alarms.contains(REPLACE_BATTERY)).isTrue(); assertThat(alarms.contains(COMMUNICATION_ERROR_M_BUS_CHANNEL_4)).isTrue(); assertThat(alarms.contains(FRAUD_ATTEMPT_M_BUS_CHANNEL_1)).isTrue(); this.verifyDsmr4BufferCalls(buffer, alarmRegister); }
@Test public void decodeSmr5AlarmsWithAlarmRegister() throws UnrecognizedMessageDataException { this.decoder = new DlmsPushNotificationDecoder(); final ByteBuf buffer = mock(ByteBuf.class); final byte[] alarmRegister = new byte[] { 0x10, 0x18, 0x00, 0x02 }; this.setupSmr5BufferWithAlarm(buffer, IDENTIFIER, ALARM_OBISCODE_BYTES, alarmRegister); final List<Object> out = new ArrayList<>(); this.decoder.decode(this.ctx, buffer, out); final Object pushNotificationObject = out.get(0); assertThat(pushNotificationObject instanceof DlmsPushNotification).isTrue(); final DlmsPushNotification dlmsPushNotification = (DlmsPushNotification) pushNotificationObject; assertThat(dlmsPushNotification.getEquipmentIdentifier()).isEqualTo(IDENTIFIER); assertThat(dlmsPushNotification.getTriggerType()).isEqualTo(PUSH_ALARM_TRIGGER); final Set<AlarmTypeDto> alarms = dlmsPushNotification.getAlarms(); assertThat(alarms.size()).isEqualTo(4); assertThat(alarms.contains(REPLACE_BATTERY)).isTrue(); assertThat(alarms.contains(COMMUNICATION_ERROR_M_BUS_CHANNEL_4)).isTrue(); assertThat(alarms.contains(FRAUD_ATTEMPT_M_BUS_CHANNEL_1)).isTrue(); assertThat(alarms.contains(PHASE_OUTAGE_DETECTED_L_1)).isTrue(); this.verifySmr5BufferCallsWithAlarm(buffer, alarmRegister); verify(buffer, times(1)) .skipBytes(SMR5_NUMBER_OF_BYTES_FOR_ADDRESSING + 1 + SMR5_NUMBER_OF_BYTES_FOR_INVOKE_ID); }