package z21Drive.responses;

import java.util.ArrayList;
import java.util.List;

/**
 * Handels a RailCom-Response
 * @author sven
 *
 */
public class Z21ResponseRailcomDatachanged extends Z21Response {

	private List<RailComData> data = new ArrayList<RailComData>();
	
    public Z21ResponseRailcomDatachanged(byte[] initArray) {
        super(initArray);
        boundType = ResponseTypes.LAN_RAILCOM_DATACHANGED;
        if (byteRepresentation != null)
            populateFields();
    }

    private void populateFields(){
    	int nrOfDecoders = ((getByteRepresentation()[0] & 0xFF) - 4) / 13; 
    	for (int idx = 0; idx < nrOfDecoders; idx++) {
    		data.add(analyse(4 + idx * 13));
    	}
    }

	private RailComData analyse(int i) {
		Integer[] data = new Integer[13];
		for (int idx = i; idx < (i + 13); idx++) {
//            System.out.print("0x" + String.format("%02X ", byteRepresentation[idx]));
            data[idx - i] = (int) ( byteRepresentation[idx] & 0xFF);
		}

		int locId = data[0] 
				    + (data[1]  << 8);
		int receiveCounter = data[2] 
             			   + (data[3] << 8)
							+ (data[4] << 16)
							+ (data[5] << 24);
		int errorCounter = data[6] 
 			    + (data[7]<< 8)
				+ (data[8] << 16)
				+ (data[9]<< 24);
		int speed = data[10];
		int options = data[11];
		int temp = data[12];
		return new RailComData(locId, receiveCounter, errorCounter, speed, options, temp);
	} 

	public class RailComData {
		public RailComData(int locId, int receiveCounter, int errorCounter, int speed, int options, int temp) {
			super();
			this.locId = locId;
			this.receiveCounter = receiveCounter;
			this.errorCounter = errorCounter;
			this.speed = speed;
			this.options = options;
			this.temp = temp;
		}
		private int locId;
		private int receiveCounter;
		private int errorCounter;
		private int speed;
		private int options;
		private int temp;
		public int getLocId() {
			return locId;
		}
		public int getReceiveCounter() {
			return receiveCounter;
		}
		public int getErrorCounter() {
			return errorCounter;
		}
		public int getSpeed() {
			return speed;
		}
		public int getOptions() {
			return options;
		}
		public int getTemp() {
			return temp;
		}
		
		@Override
		public String toString() {
			return "RailComData [locId=" + locId + ", receiveCounter=" + receiveCounter + ", errorCounter="
					+ errorCounter + ", speed=" + speed + ", options=" + options + ", temp=" + temp + "]";
		}
		
	}

}

@@ -265,14 +265,10 @@
 ();%0A
+%0A
 %09
-%0A    
 publ
@@ -320,24 +320,18 @@
 rray) %7B%0A
-        
+%09%09
 super(in
@@ -340,24 +340,18 @@
 Array);%0A
-        
+%09%09
 boundTyp
@@ -393,24 +393,18 @@
 HANGED;%0A
-        
+%09%09
 if (byte
@@ -427,28 +427,19 @@
 = null)%0A
-            
+%09%09%09
 populate
@@ -452,19 +452,13 @@
 ();%0A
-    %7D%0A%0A    
+%09%7D%0A%0A%09
 priv
@@ -484,20 +484,17 @@
 elds()%7B%0A
-    
+%09
 %09int nrO
@@ -554,20 +554,17 @@
  / 13; %0A
-    
+%09
 %09for (in
@@ -603,20 +603,17 @@
 dx++) %7B%0A
-    
+%09
 %09%09data.a
@@ -643,19 +643,13 @@
 ));%0A
-    %09%7D%0A    
+%09%09%7D%0A%09
 %7D%0A%0A%09
@@ -766,16 +766,19 @@
 dx++) %7B%0A
+%09%09%09
 //      
@@ -857,28 +857,19 @@
 idx%5D));%0A
-            
+%09%09%09
 data%5Bidx
@@ -949,20 +949,16 @@
 0%5D %0A%09%09%09%09
-    
 + (data%5B
@@ -1004,27 +1004,12 @@
 2%5D %0A
-             %09%09%09   
+%09%09%09%09
 + (d
@@ -1021,27 +1021,24 @@
 %5D %3C%3C 8)%0A%09%09%09%09
-%09
-%09%09
 + (data%5B4%5D %3C
@@ -1043,19 +1043,16 @@
  %3C%3C 16)%0A
-%09%09%09
 %09%09%09%09+ (d
@@ -1100,16 +1100,12 @@
 6%5D %0A
- 
 %09%09%09
-    
+%09
 + (d
@@ -1323,16 +1323,78 @@
 );%0A%09%7D %0A%0A
+%09/**%0A%09 * Class for the Railcom-Data%0A%09 * @author sven%0A%09 *%0A%09 */%0A
 %09public 
@@ -2166,18 +2166,16 @@
 mp;%0A%09%09%7D%0A
-%09%09
 %0A%09%09@Over
@@ -2403,17 +2403,15 @@
 %5D%22;%0A%09%09%7D%0A
-%09%09
 %0A%09%7D%0A%0A%7D%0A

Indent corrected