PanelState { protected synchronized void processPacket(Packet packet) { if (packet.getCommand().equals("510")) { int leds = Integer.parseInt(packet.getData().substring(0, 2),16); int mask = 1 << 7; for (int c = 1;c <= 8; c++) { log.debug("LED Status update [LED"+String.valueOf(c)+"="+((leds&mask)==0?"OFF":"ON")+"]"); updateInternalState( StateType.LED, String.valueOf(c), (leds & mask) == 0 ? LedState.OFF : LedState.ON ); leds <<= 1; } } else if (packet.getCommand().equals("511")) { int leds = Integer.parseInt(packet.getData().substring(0, 2),16); int mask = 1 << 7; for (int c = 8;c > 0; c--) { log.debug("LED Status update [LED"+String.valueOf(c)+"="+((leds&mask)==0?"OFF":"FLASH")+"]"); updateInternalState( StateType.LED, String.valueOf(c), (leds & mask) == 0 ? LedState.OFF : LedState.FLASH ); leds <<= 1; } } else if (packet.getCommand().equals("570")) { String num = packet.getData().substring(0, 3); num = trimLeadingZeros(num); String label = packet.getData().substring(3).trim(); log.debug("Broadcast label [number=" + num + ",label=" + label + "]"); updateInternalState(StateType.LABEL, num, new Label(label)); } else if (packet.getCommand().equals("601")) { String partition = packet.getData().substring(0, 1); String zone = packet.getData().substring(1, 4); zone = trimLeadingZeros(zone); log.debug("Zone alarm [partition=" + partition + ",zone=" + zone + "]"); updateInternalState(StateType.ZONE_ALARM, zone, AlarmState.ALARM); } else if (packet.getCommand().equals("602")) { String partition = packet.getData().substring(0, 1); String zone = packet.getData().substring(1, 4); zone = trimLeadingZeros(zone); log.debug("Zone alarm restore [partition=" + partition + ",zone=" + zone + "]"); updateInternalState(StateType.ZONE_ALARM, zone, AlarmState.NORMAL); } else if (packet.getCommand().equals("609")) { String zone = packet.getData().substring(0, 3); zone = trimLeadingZeros(zone); log.debug("Zone open [zone=" + zone + "]"); updateInternalState(StateType.ZONE, zone, ZoneState.OPEN); } else if (packet.getCommand().equals("610")) { String zone = packet.getData().substring(0, 3); zone = trimLeadingZeros(zone); log.debug("Zone restored [zone=" + zone + "]"); updateInternalState(StateType.ZONE, zone, ZoneState.RESTORED); } else if (packet.getCommand().equals("650")) { String partition = packet.getData().substring(0, 1); log.debug("Partition ready [partition=" + partition + "]"); updateInternalState(StateType.PARTITION, partition, PartitionState.READY); } else if (packet.getCommand().equals("651")) { String partition = packet.getData().substring(0, 1); log.debug("Partition not ready [partition=" + partition + "]"); updateInternalState(StateType.PARTITION, partition, PartitionState.NOTREADY); } else if (packet.getCommand().equals("652")) { String partition = packet.getData().substring(0, 1); String mode = packet.getData().substring(1, 2); log.debug("Partition armed [partition=" + partition + ",mode=" + mode + "]"); if (mode.equals("0")) { updateInternalState(StateType.PARTITION, partition, PartitionState.AWAYARMED); } else if (mode.equals("1")) { updateInternalState(StateType.PARTITION, partition, PartitionState.STAYARMED); } else if (mode.equals("2")) { updateInternalState(StateType.PARTITION, partition, PartitionState.AWAYARMEDND); } else if (mode.equals("3")) { updateInternalState(StateType.PARTITION, partition, PartitionState.STAYARMEDND); } } else if (packet.getCommand().equals("654")) { String partition = packet.getData().substring(0, 1); log.debug("Partition in alarm [partition=" + partition + "]"); updateInternalState(StateType.PARTITION, partition, PartitionState.ALARM); } else if (packet.getCommand().equals("655")) { String partition = packet.getData().substring(0, 1); log.debug("Partition disarmed [partition=" + partition + "]"); updateInternalState(StateType.PARTITION, partition, PartitionState.DISARMED); } else if (packet.getCommand().equals("656")) { String partition = packet.getData().substring(0, 1); log.debug("Partition in exit delay [partition=" + partition + "]"); updateInternalState(StateType.PARTITION, partition, PartitionState.EXITDELAY); } else if (packet.getCommand().equals("657")) { String partition = packet.getData().substring(0, 1); log.debug("Partition in entry delay [partition=" + partition + "]"); updateInternalState(StateType.PARTITION, partition, PartitionState.ENTRYDELAY); } else if (packet.getCommand().equals("672")) { String partition = packet.getData().substring(0, 1); log.debug("Partition failed to arm [partition=" + partition + "]"); updateInternalState(StateType.PARTITION, partition, PartitionState.FAILTOARM); } else if (packet.getCommand().equals("673")) { String partition = packet.getData().substring(0, 1); log.debug("Partition busy [partition=" + partition + "]"); updateInternalState(StateType.PARTITION, partition, PartitionState.BUSY); } else if (packet.getCommand().equals("903")) { String led = packet.getData().substring(0,1); int led_state=Integer.parseInt(packet.getData().substring(1,2)); log.debug("LED Status update [LED"+led+"="+String.valueOf(led_state)+"]"); if (led_state==0) { updateInternalState(StateType.LED, led, LedState.OFF); } else { updateInternalState( StateType.LED, led, (led_state) == 1 ? LedState.ON : LedState.FLASH ); } } } PanelState();   }
@Test public void testProcessPacket() { Packet packet; packet = new Packet("570", "001Label"); panelState.processPacket(packet); assertEquals("Label", panelState.getState(new StateDefinition(StateType.LABEL, "1")) .toString()); packet = new Packet("601", "1001"); panelState.processPacket(packet); assertEquals(AlarmState.ALARM, panelState.getState(new StateDefinition(StateType.ZONE_ALARM, "1"))); packet = new Packet("602", "1001"); panelState.processPacket(packet); assertEquals(AlarmState.NORMAL, panelState.getState(new StateDefinition(StateType.ZONE_ALARM, "1"))); packet = new Packet("609", "001"); panelState.processPacket(packet); assertEquals(ZoneState.OPEN, panelState.getState(new StateDefinition(StateType.ZONE, "1"))); packet = new Packet("610", "001"); panelState.processPacket(packet); assertEquals(ZoneState.RESTORED, panelState.getState(new StateDefinition(StateType.ZONE, "1"))); packet = new Packet("650", "1"); panelState.processPacket(packet); assertEquals(PartitionState.READY, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("651", "1"); panelState.processPacket(packet); assertEquals(PartitionState.NOTREADY, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("652", "10"); panelState.processPacket(packet); assertEquals(PartitionState.AWAYARMED, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("652", "11"); panelState.processPacket(packet); assertEquals(PartitionState.STAYARMED, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("652", "12"); panelState.processPacket(packet); assertEquals(PartitionState.AWAYARMEDND, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("652", "13"); panelState.processPacket(packet); assertEquals(PartitionState.STAYARMEDND, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("654", "1"); panelState.processPacket(packet); assertEquals(PartitionState.ALARM, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("655", "1"); panelState.processPacket(packet); assertEquals(PartitionState.DISARMED, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("656", "1"); panelState.processPacket(packet); assertEquals(PartitionState.EXITDELAY, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("657", "1"); panelState.processPacket(packet); assertEquals(PartitionState.ENTRYDELAY, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("672", "1"); panelState.processPacket(packet); assertEquals(PartitionState.FAILTOARM, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); packet = new Packet("673", "1"); panelState.processPacket(packet); assertEquals(PartitionState.BUSY, panelState.getState(new StateDefinition(StateType.PARTITION, "1"))); }