package art.servers.etdserver.protocols.rtms; import art.library.model.devices.etd.Etd; import art.library.model.devices.etd.status.EtdStatusVehicle; import java.io.ByteArrayOutputStream; import java.rmi.ServerException; import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.List; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Konstantin */ public class RTMS_Functions { public static byte[] getRtc(int deviceAddress)throws ServerException { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(0xFF); bos.write(0xAA); bos.write(0xF9); bos.write(0x02); bos.write((byte) ((deviceAddress >> 8) & 0xFF)); bos.write((byte) (deviceAddress & 0xFF)); bos.write(checkSum(bos.toByteArray())); return bos.toByteArray(); } catch (Exception ex) { throw new ServerException("Unable to construct protocol packet"); } } public static byte[] setRtc(int deviceAddress, Calendar cal)throws ServerException { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(0xFF); bos.write(0xAA); bos.write(0x49); bos.write(0x10); bos.write((byte) ((deviceAddress >> 8) & 0xFF)); bos.write((byte) (deviceAddress & 0xFF)); bos.write(decToBcd((byte) cal.get(Calendar.SECOND))); bos.write(decToBcd((byte) cal.get(Calendar.MINUTE))); bos.write(decToBcd((byte) cal.get(Calendar.HOUR))); bos.write((byte) 0x00); bos.write(decToBcd((byte) cal.get(Calendar.DATE))); bos.write(decToBcd((byte) cal.get(Calendar.MONTH)) + 1); bos.write(decToBcd((byte) (cal.get(Calendar.YEAR) % 100))); bos.write(decToBcd((byte) cal.get(Calendar.SECOND))); bos.write(decToBcd((byte) cal.get(Calendar.MINUTE))); bos.write(decToBcd((byte) cal.get(Calendar.HOUR))); bos.write((byte) 0x00); bos.write(decToBcd((byte) cal.get(Calendar.DATE))); bos.write(decToBcd((byte) cal.get(Calendar.MONTH)) + 1); bos.write(decToBcd((byte) (cal.get(Calendar.YEAR) % 100))); bos.write(checkSum(bos.toByteArray())); return bos.toByteArray(); } catch (Exception ex) { throw new ServerException("Unable to construct protocol packet"); } } public Calendar rtcToCalendar(byte[] rtcResponse)throws ServerException { try { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2000 + bcdToDec(rtcResponse[12])); cal.set(Calendar.MONTH, bcdToDec(rtcResponse[11]) -1 ); cal.set(Calendar.DATE, bcdToDec(rtcResponse[10])); cal.set(Calendar.HOUR, bcdToDec(rtcResponse[8])); cal.set(Calendar.MINUTE, bcdToDec(rtcResponse[7])); cal.set(Calendar.SECOND, bcdToDec(rtcResponse[6])); return cal; } catch (Exception ex) { throw new ServerException("Unable to retrieve date from packet"); } } //Operation mode public static byte[] getCurrentSetup(int deviceAddress) throws ServerException { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(0xFF); bos.write(0xAA); bos.write(0xC1); bos.write(0x02); bos.write((byte) ((deviceAddress >> 8) & 0xFF)); bos.write((byte) (deviceAddress & 0xFF)); bos.write(checkSum(bos.toByteArray())); return bos.toByteArray(); } catch (Exception ex) { throw new ServerException("Unable to construct protocol packet"); } } public static byte[] setPolledMode(byte[] currentSetup)throws ServerException { try { byte[] currentDataSetup = Arrays.copyOfRange(currentSetup, 0, currentSetup.length - 3); if ((currentDataSetup[13] & 0x02) < 1 || (currentDataSetup[15] & 0x02) < 1) { currentDataSetup[13] = (byte) (currentDataSetup[13] & 0xF1); currentDataSetup[13] ^= 1 << 1; currentDataSetup[15] = (byte) (currentDataSetup[13] & 0xF1); currentDataSetup[15] ^= 1 << 1; ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(currentDataSetup); bos.write(checkSum(bos.toByteArray())); return bos.toByteArray(); } else { return currentSetup; } } catch (Exception ex) { throw new ServerException("Unable to construct protocol packet"); } } // Per Vehicle public static byte[] getDataPeriod(int deviceAddress, Calendar cal) throws ServerException { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); bos.write(0xFF); bos.write(0xAA); bos.write(0x68); bos.write(0x0E); bos.write((byte) ((deviceAddress >> 8) & 0xFF)); bos.write((byte) (deviceAddress & 0xFF)); bos.write(decToBcd((byte) 0)); bos.write(decToBcd((byte) cal.get(Calendar.MINUTE))); bos.write(decToBcd((byte) cal.get(Calendar.HOUR))); bos.write(decToBcd((byte) cal.get(Calendar.DATE))); bos.write(decToBcd((byte) cal.get(Calendar.MONTH))+1); bos.write(decToBcd((byte) cal.get(Calendar.YEAR % 100))); bos.write(decToBcd((byte) 59)); bos.write(decToBcd((byte) cal.get(Calendar.MINUTE))); bos.write(decToBcd((byte) cal.get(Calendar.HOUR))); bos.write(decToBcd((byte) cal.get(Calendar.DATE))); bos.write(decToBcd((byte) cal.get(Calendar.MONTH))+1); bos.write(decToBcd((byte) cal.get(Calendar.YEAR % 100))); bos.write(checkSum(bos.toByteArray())); return bos.toByteArray(); } catch (Exception ex) { throw new ServerException("Unable to construct protocol packet"); } } public static List getPerFrames(Etd etd, byte[] response) { List output = new ArrayList(); // for (int i = 0; i < response.length; i++) // { // try // { // if ((response[i] & 0xFF) == 0xFF && (response[i + 1] & 0xFF) == 0xAA) // { // if ((response[i + 2] & 0xFF) == 0x5E) // { // byte[] data = Arrays.copyOfRange(response, i, (i + 6 + response[i + 3])); // EtdStatusVehicle vehicle = getVehicle(data); // if (vehicle != null) output.add(vehicle); // // i += (5 + (response[i + 3])); // } // } // } // catch (Exception ex) // { // } // } return output; } //Utils public static byte decToBcd(byte val) { return (byte) (((val / 10) << 4) | (val % 10)); } public static byte bcdToDec(byte bcd) { return (byte) (((bcd >> 4) * 10) + (bcd & 0xF)); } public static byte[] checkSum(byte[] data) { int sum = 0; for (int i = 4; i < data.length; i++) { sum += data[i]; } return new byte[]{(byte) ((sum >> 8) & 0xFF), (byte) (sum & 0xFF)}; } }