diff options
Diffstat (limited to 'src/org/traccar/helper')
-rw-r--r-- | src/org/traccar/helper/BcdUtil.java | 63 | ||||
-rw-r--r-- | src/org/traccar/helper/BitBuffer.java | 101 | ||||
-rw-r--r-- | src/org/traccar/helper/BitUtil.java | 51 | ||||
-rw-r--r-- | src/org/traccar/helper/Checksum.java | 254 | ||||
-rw-r--r-- | src/org/traccar/helper/DateBuilder.java | 126 | ||||
-rw-r--r-- | src/org/traccar/helper/DateUtil.java | 66 | ||||
-rw-r--r-- | src/org/traccar/helper/DistanceCalculator.java | 53 | ||||
-rw-r--r-- | src/org/traccar/helper/Hashing.java | 101 | ||||
-rw-r--r-- | src/org/traccar/helper/LocationTree.java | 125 | ||||
-rw-r--r-- | src/org/traccar/helper/Log.java | 268 | ||||
-rw-r--r-- | src/org/traccar/helper/ObdDecoder.java | 110 | ||||
-rw-r--r-- | src/org/traccar/helper/Parser.java | 348 | ||||
-rw-r--r-- | src/org/traccar/helper/PatternBuilder.java | 98 | ||||
-rw-r--r-- | src/org/traccar/helper/PatternUtil.java | 76 | ||||
-rw-r--r-- | src/org/traccar/helper/StringFinder.java | 41 | ||||
-rw-r--r-- | src/org/traccar/helper/UnitsConverter.java | 74 |
16 files changed, 0 insertions, 1955 deletions
diff --git a/src/org/traccar/helper/BcdUtil.java b/src/org/traccar/helper/BcdUtil.java deleted file mode 100644 index 495f94104..000000000 --- a/src/org/traccar/helper/BcdUtil.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2012 - 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import org.jboss.netty.buffer.ChannelBuffer; - -public final class BcdUtil { - - private BcdUtil() { - } - - public static int readInteger(ChannelBuffer buf, int digits) { - int result = 0; - - for (int i = 0; i < digits / 2; i++) { - int b = buf.readUnsignedByte(); - result *= 10; - result += b >>> 4; - result *= 10; - result += b & 0x0f; - } - - if (digits % 2 != 0) { - int b = buf.getUnsignedByte(buf.readerIndex()); - result *= 10; - result += b >>> 4; - } - - return result; - } - - public static double readCoordinate(ChannelBuffer buf) { - int b1 = buf.readUnsignedByte(); - int b2 = buf.readUnsignedByte(); - int b3 = buf.readUnsignedByte(); - int b4 = buf.readUnsignedByte(); - - double value = (b2 & 0xf) * 10 + (b3 >> 4); - value += (((b3 & 0xf) * 10 + (b4 >> 4)) * 10 + (b4 & 0xf)) / 1000.0; - value /= 60; - value += ((b1 >> 4 & 0x7) * 10 + (b1 & 0xf)) * 10 + (b2 >> 4); - - if ((b1 & 0x80) != 0) { - value = -value; - } - - return value; - } - -} diff --git a/src/org/traccar/helper/BitBuffer.java b/src/org/traccar/helper/BitBuffer.java deleted file mode 100644 index ac307efdf..000000000 --- a/src/org/traccar/helper/BitBuffer.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; - -public class BitBuffer { - - private final ChannelBuffer buffer; - - private int writeByte; - private int writeCount; - - private int readByte; - private int readCount; - - public BitBuffer() { - buffer = ChannelBuffers.dynamicBuffer(); - } - - public BitBuffer(ChannelBuffer buffer) { - this.buffer = buffer; - } - - public void writeEncoded(byte[] bytes) { - for (byte b : bytes) { - b -= 48; - if (b > 40) { - b -= 8; - } - write(b); - } - } - - public void write(int b) { - if (writeCount == 0) { - writeByte |= b; - writeCount = 6; - } else { - int remaining = 8 - writeCount; - writeByte <<= remaining; - writeByte |= b >> (6 - remaining); - buffer.writeByte(writeByte); - writeByte = b & ((1 << (6 - remaining)) - 1); - writeCount = 6 - remaining; - } - } - - public int readUnsigned(int length) { - int result = 0; - - while (length > 0) { - if (readCount == 0) { - readByte = buffer.readUnsignedByte(); - readCount = 8; - } - if (readCount >= length) { - result <<= length; - result |= readByte >> (readCount - length); - readByte &= (1 << (readCount - length)) - 1; - readCount -= length; - length = 0; - } else { - result <<= readCount; - result |= readByte; - length -= readCount; - readByte = 0; - readCount = 0; - } - } - - return result; - } - - public int readSigned(int length) { - int result = readUnsigned(length); - int signBit = 1 << (length - 1); - if ((result & signBit) == 0) { - return result; - } else { - result &= signBit - 1; - result += ~(signBit - 1); - return result; - } - } - -} diff --git a/src/org/traccar/helper/BitUtil.java b/src/org/traccar/helper/BitUtil.java deleted file mode 100644 index b6108edff..000000000 --- a/src/org/traccar/helper/BitUtil.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -public final class BitUtil { - - private BitUtil() { - } - - public static boolean check(long number, int index) { - return (number & (1 << index)) != 0; - } - - public static int between(int number, int from, int to) { - return (number >> from) & ((1 << to - from) - 1); - } - - public static int from(int number, int from) { - return number >> from; - } - - public static int to(int number, int to) { - return between(number, 0, to); - } - - public static long between(long number, int from, int to) { - return (number >> from) & ((1L << to - from) - 1L); - } - - public static long from(long number, int from) { - return number >> from; - } - - public static long to(long number, int to) { - return between(number, 0, to); - } - -} diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java deleted file mode 100644 index 44cef9635..000000000 --- a/src/org/traccar/helper/Checksum.java +++ /dev/null @@ -1,254 +0,0 @@ -/* - * Copyright 2012 - 2015 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.zip.CRC32; - -public final class Checksum { - - private Checksum() { - } - - private static final int[] CRC16_CCITT_TABLE_REVERSE = { - 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, - 0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, - 0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, - 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876, - 0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD, - 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5, - 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C, - 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974, - 0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB, - 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3, - 0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A, - 0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72, - 0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9, - 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, - 0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738, - 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70, - 0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7, - 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF, - 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036, - 0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, - 0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5, - 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD, - 0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134, - 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C, - 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3, - 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB, - 0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232, - 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A, - 0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1, - 0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9, - 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, - 0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78 - }; - - private static final int[] CRC16_CCITT_TABLE = { - 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, - 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, - 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, - 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, - 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, - 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, - 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, - 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, - 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, - 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, - 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, - 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, - 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, - 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, - 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, - 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, - 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, - 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, - 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, - 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, - 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, - 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, - 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, - 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, - 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, - 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, - 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, - 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, - 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, - 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, - 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, - 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 - }; - - private static final int[] CRC16_IBM_TABLE = { - 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, - 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440, - 0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40, - 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841, - 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40, - 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41, - 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, - 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040, - 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240, - 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441, - 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41, - 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840, - 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41, - 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40, - 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640, - 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041, - 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240, - 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441, - 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41, - 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840, - 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41, - 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40, - 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640, - 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041, - 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241, - 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440, - 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40, - 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841, - 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40, - 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41, - 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, - 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040, - }; - - // More info: http://reveng.sourceforge.net/crc-catalogue/16.htm - public static final String CRC16_IBM = "IBM"; - public static final String CRC16_MODBUS = "MODBUS"; - public static final String CRC16_X25 = "X-25"; - public static final String CRC16_CCITT_FALSE = "CCITT-FALSE"; - public static final String CRC16_KERMIT = "KERMIT"; - public static final String CRC16_XMODEM = "XMODEM"; - public static final String CRC16_AUG_CCITT = "AUG-CCITT"; - public static final String CRC16_GENIBUS = "GENIBUS"; - public static final String CRC16_MCRF4XX = "MCRF4XX"; - - private static int crc16Unreflected(ByteBuffer buf, int crcIn, int[] table) { - int crc16 = crcIn; - while (buf.hasRemaining()) { - crc16 = table[((crc16 >> 8) ^ buf.get()) & 0xff] ^ (crc16 << 8); - } - return crc16 & 0xFFFF; - } - - private static int crc16Reflected(ByteBuffer buf, int crcIn, int[] table) { - int crc16 = crcIn; - while (buf.hasRemaining()) { - crc16 = table[(crc16 ^ buf.get()) & 0xff] ^ (crc16 >> 8); - } - return crc16 & 0xFFFF; - } - - public static int crc16(String type, ByteBuffer buf) { - switch (type) { - case CRC16_IBM: - return crc16Reflected(buf, 0, CRC16_IBM_TABLE); - case CRC16_MODBUS: - return crc16Reflected(buf, 0xFFFF, CRC16_IBM_TABLE); - case CRC16_X25: - return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE) ^ 0xFFFF; - case CRC16_CCITT_FALSE: - return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE); - case CRC16_KERMIT: - return crc16Reflected(buf, 0, CRC16_CCITT_TABLE_REVERSE); - case CRC16_XMODEM: - return crc16Unreflected(buf, 0, CRC16_CCITT_TABLE); - case CRC16_AUG_CCITT: - return crc16Unreflected(buf, 0x1d0f, CRC16_CCITT_TABLE); - case CRC16_GENIBUS: - return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE) ^ 0xFFFF; - case CRC16_MCRF4XX: - return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE); - default: - throw new UnsupportedOperationException(type); - } - } - - public static int crc32(ByteBuffer buf) { - CRC32 checksum = new CRC32(); - while (buf.hasRemaining()) { - checksum.update(buf.get()); - } - return (int) checksum.getValue(); - } - - public static int xor(ByteBuffer buf) { - int checksum = 0; - while (buf.hasRemaining()) { - checksum ^= buf.get(); - } - return checksum; - } - - public static int xor(String string) { - byte checksum = 0; - for (byte b : string.getBytes(StandardCharsets.US_ASCII)) { - checksum ^= b; - } - return checksum; - } - - public static String nmea(String msg) { - int checksum = 0; - byte[] bytes = msg.getBytes(StandardCharsets.US_ASCII); - for (int i = 1; i < bytes.length; i++) { - checksum ^= bytes[i]; - } - return String.format("*%02x", checksum).toUpperCase(); - } - - public static String sum(String msg) { - byte checksum = 0; - for (byte b : msg.getBytes(StandardCharsets.US_ASCII)) { - checksum += b; - } - return String.format("%02X", checksum).toUpperCase(); - } - - public static long luhn(long imei) { - long checksum = 0; - long remain = imei; - - for (int i = 0; remain != 0; i++) { - long digit = remain % 10; - - if (i % 2 == 0) { - digit *= 2; - if (digit >= 10) { - digit = 1 + (digit % 10); - } - } - - checksum += digit; - remain /= 10; - } - - return (10 - (checksum % 10)) % 10; - } - - public static int modulo256(byte[] bytes) { - int sum = 0; - for (byte b : bytes) { - sum = (sum + b) & 0xFF; - } - return sum; - } - -} diff --git a/src/org/traccar/helper/DateBuilder.java b/src/org/traccar/helper/DateBuilder.java deleted file mode 100644 index 6e1b779f0..000000000 --- a/src/org/traccar/helper/DateBuilder.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import java.util.Calendar; -import java.util.Date; -import java.util.TimeZone; - -public class DateBuilder { - - private Calendar calendar; - - public DateBuilder() { - this(TimeZone.getTimeZone("UTC")); - } - - public DateBuilder(Date time) { - this(time, TimeZone.getTimeZone("UTC")); - } - - public DateBuilder(TimeZone timeZone) { - this(new Date(0), timeZone); - } - - public DateBuilder(Date time, TimeZone timeZone) { - calendar = Calendar.getInstance(timeZone); - calendar.clear(); - calendar.setTimeInMillis(time.getTime()); - } - - public DateBuilder setYear(int year) { - if (year < 100) { - year += 2000; - } - calendar.set(Calendar.YEAR, year); - return this; - } - - public DateBuilder setMonth(int month) { - calendar.set(Calendar.MONTH, month - 1); - return this; - } - - public DateBuilder setDay(int day) { - calendar.set(Calendar.DAY_OF_MONTH, day); - return this; - } - - public DateBuilder setDate(int year, int month, int day) { - return setYear(year).setMonth(month).setDay(day); - } - - public DateBuilder setDateReverse(int day, int month, int year) { - return setDate(year, month, day); - } - - public DateBuilder setCurrentDate() { - Calendar now = Calendar.getInstance(calendar.getTimeZone()); - return setYear(now.get(Calendar.YEAR)).setMonth(now.get(Calendar.MONTH)).setDay(now.get(Calendar.DAY_OF_MONTH)); - } - - public DateBuilder setHour(int hour) { - calendar.set(Calendar.HOUR_OF_DAY, hour); - return this; - } - - public DateBuilder setMinute(int minute) { - calendar.set(Calendar.MINUTE, minute); - return this; - } - - public DateBuilder addMinute(int minute) { - calendar.add(Calendar.MINUTE, minute); - return this; - } - - public DateBuilder setSecond(int second) { - calendar.set(Calendar.SECOND, second); - return this; - } - - public DateBuilder addSeconds(long seconds) { - calendar.setTimeInMillis(calendar.getTimeInMillis() + seconds * 1000); - return this; - } - - public DateBuilder setMillis(int millis) { - calendar.set(Calendar.MILLISECOND, millis); - return this; - } - - public DateBuilder addMillis(long millis) { - calendar.setTimeInMillis(calendar.getTimeInMillis() + millis); - return this; - } - - public DateBuilder setTime(int hour, int minute, int second) { - return setHour(hour).setMinute(minute).setSecond(second); - } - - public DateBuilder setTimeReverse(int second, int minute, int hour) { - return setHour(hour).setMinute(minute).setSecond(second); - } - - public DateBuilder setTime(int hour, int minute, int second, int millis) { - return setHour(hour).setMinute(minute).setSecond(second).setMillis(millis); - } - - public Date getDate() { - return calendar.getTime(); - } - -} diff --git a/src/org/traccar/helper/DateUtil.java b/src/org/traccar/helper/DateUtil.java deleted file mode 100644 index 30bb1b2fa..000000000 --- a/src/org/traccar/helper/DateUtil.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import java.util.Calendar; -import java.util.Date; - -import org.joda.time.format.DateTimeFormatter; -import org.joda.time.format.ISODateTimeFormat; - -public final class DateUtil { - - private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTimeParser(); - - private DateUtil() { - } - - public static Date correctDay(Date guess) { - return correctDate(new Date(), guess, Calendar.DAY_OF_MONTH); - } - - public static Date correctYear(Date guess) { - return correctDate(new Date(), guess, Calendar.YEAR); - } - - public static Date correctDate(Date now, Date guess, int field) { - - if (guess.getTime() > now.getTime()) { - Date previous = dateAdd(guess, field, -1); - if (now.getTime() - previous.getTime() < guess.getTime() - now.getTime()) { - return previous; - } - } else if (guess.getTime() < now.getTime()) { - Date next = dateAdd(guess, field, 1); - if (next.getTime() - now.getTime() < now.getTime() - guess.getTime()) { - return next; - } - } - - return guess; - } - - private static Date dateAdd(Date guess, int field, int amount) { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(guess); - calendar.add(field, amount); - return calendar.getTime(); - } - - public static Date parseDate(String value) { - return DATE_FORMAT.parseDateTime(value).toDate(); - } -} diff --git a/src/org/traccar/helper/DistanceCalculator.java b/src/org/traccar/helper/DistanceCalculator.java deleted file mode 100644 index 88d4ef8a4..000000000 --- a/src/org/traccar/helper/DistanceCalculator.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2014 - 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -public final class DistanceCalculator { - - private DistanceCalculator() { - } - - private static final double EQUATORIAL_EARTH_RADIUS = 6378.1370; - private static final double DEG_TO_RAD = Math.PI / 180; - - public static double distance(double lat1, double lon1, double lat2, double lon2) { - double dlong = (lon2 - lon1) * DEG_TO_RAD; - double dlat = (lat2 - lat1) * DEG_TO_RAD; - double a = Math.pow(Math.sin(dlat / 2), 2) - + Math.cos(lat1 * DEG_TO_RAD) * Math.cos(lat2 * DEG_TO_RAD) * Math.pow(Math.sin(dlong / 2), 2); - double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); - double d = EQUATORIAL_EARTH_RADIUS * c; - return d * 1000; - } - - public static double distanceToLine( - double pointLat, double pointLon, double lat1, double lon1, double lat2, double lon2) { - double d0 = distance(pointLat, pointLon, lat1, lon1); - double d1 = distance(lat1, lon1, lat2, lon2); - double d2 = distance(lat2, lon2, pointLat, pointLon); - if (Math.pow(d0, 2) > Math.pow(d1, 2) + Math.pow(d2, 2)) { - return d2; - } - if (Math.pow(d2, 2) > Math.pow(d1, 2) + Math.pow(d0, 2)) { - return d0; - } - double halfP = (d0 + d1 + d2) * 0.5; - double area = Math.sqrt(halfP * (halfP - d0) * (halfP - d1) * (halfP - d2)); - return 2 * area / d1; - } - -} diff --git a/src/org/traccar/helper/Hashing.java b/src/org/traccar/helper/Hashing.java deleted file mode 100644 index 3fcc124fa..000000000 --- a/src/org/traccar/helper/Hashing.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.PBEKeySpec; -import javax.xml.bind.DatatypeConverter; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.security.spec.InvalidKeySpecException; - -public final class Hashing { - - public static final int ITERATIONS = 1000; - public static final int SALT_SIZE = 24; - public static final int HASH_SIZE = 24; - - private static SecretKeyFactory factory; - static { - try { - factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - } - - public static class HashingResult { - - private final String hash; - private final String salt; - - public HashingResult(String hash, String salt) { - this.hash = hash; - this.salt = salt; - } - - public String getHash() { - return hash; - } - - public String getSalt() { - return salt; - } - } - - private Hashing() { - } - - private static byte[] function(char[] password, byte[] salt) { - try { - PBEKeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, HASH_SIZE * Byte.SIZE); - return factory.generateSecret(spec).getEncoded(); - } catch (InvalidKeySpecException e) { - throw new SecurityException(e); - } - } - - private static final SecureRandom RANDOM = new SecureRandom(); - - public static HashingResult createHash(String password) { - byte[] salt = new byte[SALT_SIZE]; - RANDOM.nextBytes(salt); - byte[] hash = function(password.toCharArray(), salt); - return new HashingResult( - DatatypeConverter.printHexBinary(hash), - DatatypeConverter.printHexBinary(salt)); - } - - public static boolean validatePassword(String password, String hashHex, String saltHex) { - byte[] hash = DatatypeConverter.parseHexBinary(hashHex); - byte[] salt = DatatypeConverter.parseHexBinary(saltHex); - return slowEquals(hash, function(password.toCharArray(), salt)); - } - - /** - * Compares two byte arrays in length-constant time. This comparison method - * is used so that password hashes cannot be extracted from an on-line - * system using a timing attack and then attacked off-line. - */ - private static boolean slowEquals(byte[] a, byte[] b) { - int diff = a.length ^ b.length; - for (int i = 0; i < a.length && i < b.length; i++) { - diff |= a[i] ^ b[i]; - } - return diff == 0; - } - -} diff --git a/src/org/traccar/helper/LocationTree.java b/src/org/traccar/helper/LocationTree.java deleted file mode 100644 index 3aff3ce33..000000000 --- a/src/org/traccar/helper/LocationTree.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -public class LocationTree { - - public static class Item { - - private Item left, right; - private float x, y; - private String data; - - public Item(float x, float y) { - this(x, y, null); - } - - public Item(float x, float y, String data) { - this.x = x; - this.y = y; - this.data = data; - } - - public String getData() { - return data; - } - - private float squaredDistance(Item item) { - return (x - item.x) * (x - item.x) + (y - item.y) * (y - item.y); - } - - private float axisSquaredDistance(Item item, int axis) { - if (axis == 0) { - return (x - item.x) * (x - item.x); - } else { - return (y - item.y) * (y - item.y); - } - } - - } - - private Item root; - - private ArrayList<Comparator<Item>> comparators = new ArrayList<>(); - - public LocationTree(List<Item> items) { - comparators.add(new Comparator<Item>() { - @Override - public int compare(Item o1, Item o2) { - return Float.compare(o1.x, o2.x); - } - }); - comparators.add(new Comparator<Item>() { - @Override - public int compare(Item o1, Item o2) { - return Float.compare(o1.y, o2.y); - } - }); - root = createTree(items, 0); - } - - private Item createTree(List<Item> items, int depth) { - if (items.isEmpty()) { - return null; - } - Collections.sort(items, comparators.get(depth % 2)); - int currentIndex = items.size() / 2; - Item median = items.get(currentIndex); - median.left = createTree(new ArrayList<>(items.subList(0, currentIndex)), depth + 1); - median.right = createTree(new ArrayList<>(items.subList(currentIndex + 1, items.size())), depth + 1); - return median; - } - - public Item findNearest(Item search) { - return findNearest(root, search, 0); - } - - private Item findNearest(Item current, Item search, int depth) { - int direction = comparators.get(depth % 2).compare(search, current); - - Item next, other; - if (direction < 0) { - next = current.left; - other = current.right; - } else { - next = current.right; - other = current.left; - } - - Item best = current; - if (next != null) { - best = findNearest(next, search, depth + 1); - } - - if (current.squaredDistance(search) < best.squaredDistance(search)) { - best = current; - } - if (other != null && current.axisSquaredDistance(search, depth % 2) < best.squaredDistance(search)) { - Item possibleBest = findNearest(other, search, depth + 1); - if (possibleBest.squaredDistance(search) < best.squaredDistance(search)) { - best = possibleBest; - } - } - - return best; - } - -} diff --git a/src/org/traccar/helper/Log.java b/src/org/traccar/helper/Log.java deleted file mode 100644 index d74246a64..000000000 --- a/src/org/traccar/helper/Log.java +++ /dev/null @@ -1,268 +0,0 @@ -/* - * Copyright 2012 - 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import org.apache.log4j.Appender; -import org.apache.log4j.DailyRollingFileAppender; -import org.apache.log4j.Layout; -import org.apache.log4j.Level; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; -import org.apache.log4j.PatternLayout; -import org.apache.log4j.varia.NullAppender; -import org.jboss.netty.logging.AbstractInternalLogger; -import org.jboss.netty.logging.InternalLogger; -import org.jboss.netty.logging.InternalLoggerFactory; -import org.traccar.Config; - -import java.io.IOException; -import java.lang.management.ManagementFactory; -import java.lang.management.MemoryMXBean; -import java.lang.management.OperatingSystemMXBean; -import java.lang.management.RuntimeMXBean; -import java.nio.charset.Charset; - -public final class Log { - - private Log() { - } - - public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; - - private static final String LOGGER_NAME = "traccar"; - - private static final String STACK_PACKAGE = "org.traccar"; - private static final int STACK_LIMIT = 3; - - private static Logger logger = null; - - public static String getAppVersion() { - return Log.class.getPackage().getImplementationVersion(); - } - - public static void setupLogger(Config config) throws IOException { - - Layout layout = new PatternLayout("%d{" + DATE_FORMAT + "} %5p: %m%n"); - - Appender appender = new DailyRollingFileAppender( - layout, config.getString("logger.file"), "'.'yyyyMMdd"); - - LogManager.resetConfiguration(); - LogManager.getRootLogger().addAppender(new NullAppender()); - - logger = Logger.getLogger(LOGGER_NAME); - logger.addAppender(appender); - logger.setLevel(Level.toLevel(config.getString("logger.level"), Level.ALL)); - - // Workaround for "Bug 745866 - (EDG-45) Possible netty logging config problem" - InternalLoggerFactory.setDefaultFactory(new InternalLoggerFactory() { - @Override - public InternalLogger newInstance(String string) { - return new NettyInternalLogger(); - } - }); - - Log.logSystemInfo(); - Log.info("Version: " + getAppVersion()); - } - - public static Logger getLogger() { - if (logger == null) { - logger = Logger.getLogger(LOGGER_NAME); - logger.setLevel(Level.OFF); - } - return logger; - } - - public static void logSystemInfo() { - try { - OperatingSystemMXBean operatingSystemBean = ManagementFactory.getOperatingSystemMXBean(); - Log.info("Operating system" - + " name: " + operatingSystemBean.getName() - + " version: " + operatingSystemBean.getVersion() - + " architecture: " + operatingSystemBean.getArch()); - - RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean(); - Log.info("Java runtime" - + " name: " + runtimeBean.getVmName() - + " vendor: " + runtimeBean.getVmVendor() - + " version: " + runtimeBean.getVmVersion()); - - MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); - Log.info("Memory limit" - + " heap: " + memoryBean.getHeapMemoryUsage().getMax() / (1024 * 1024) + "mb" - + " non-heap: " + memoryBean.getNonHeapMemoryUsage().getMax() / (1024 * 1024) + "mb"); - - Log.info("Character encoding: " - + System.getProperty("file.encoding") + " charset: " + Charset.defaultCharset()); - - } catch (Exception error) { - Log.warning("Failed to get system info"); - } - } - - public static void error(String msg) { - getLogger().error(msg); - } - - public static void warning(String msg) { - getLogger().warn(msg); - } - - public static void warning(Throwable exception) { - warning(null, exception); - } - - public static void warning(String msg, Throwable exception) { - StringBuilder s = new StringBuilder(); - if (msg != null) { - s.append(msg); - } - if (exception != null) { - if (msg != null) { - s.append(" - "); - } - s.append(exceptionStack(exception)); - } - getLogger().warn(s.toString()); - } - - public static void info(String msg) { - getLogger().info(msg); - } - - public static void debug(String msg) { - getLogger().debug(msg); - } - - public static String exceptionStack(Throwable exception) { - StringBuilder s = new StringBuilder(); - String exceptionMsg = exception.getMessage(); - if (exceptionMsg != null) { - s.append(exceptionMsg); - s.append(" - "); - } - s.append(exception.getClass().getSimpleName()); - StackTraceElement[] stack = exception.getStackTrace(); - - if (stack.length > 0) { - int count = STACK_LIMIT; - boolean first = true; - boolean skip = false; - String file = ""; - s.append(" ("); - for (StackTraceElement element : stack) { - if (count > 0 && element.getClassName().startsWith(STACK_PACKAGE)) { - if (!first) { - s.append(" < "); - } else { - first = false; - } - - if (skip) { - s.append("... < "); - skip = false; - } - - if (file.equals(element.getFileName())) { - s.append("*"); - } else { - file = element.getFileName(); - s.append(file.substring(0, file.length() - 5)); // remove ".java" - count -= 1; - } - s.append(":").append(element.getLineNumber()); - } else { - skip = true; - } - } - if (skip) { - if (!first) { - s.append(" < "); - } - s.append("..."); - } - s.append(")"); - } - return s.toString(); - } - - /** - * Netty logger implementation - */ - private static class NettyInternalLogger extends AbstractInternalLogger { - - @Override - public boolean isDebugEnabled() { - return false; - } - - @Override - public boolean isInfoEnabled() { - return false; - } - - @Override - public boolean isWarnEnabled() { - return true; - } - - @Override - public boolean isErrorEnabled() { - return true; - } - - @Override - public void debug(String string) { - debug(string, null); - } - - @Override - public void debug(String string, Throwable thrwbl) { - } - - @Override - public void info(String string) { - info(string, null); - } - - @Override - public void info(String string, Throwable thrwbl) { - } - - @Override - public void warn(String string) { - warn(string, null); - } - - @Override - public void warn(String string, Throwable thrwbl) { - getLogger().warn("netty warning: " + string); - } - - @Override - public void error(String string) { - error(string, null); - } - - @Override - public void error(String string, Throwable thrwbl) { - getLogger().error("netty error: " + string); - } - - } - -} diff --git a/src/org/traccar/helper/ObdDecoder.java b/src/org/traccar/helper/ObdDecoder.java deleted file mode 100644 index 4bc3bcdfb..000000000 --- a/src/org/traccar/helper/ObdDecoder.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import org.traccar.model.Position; - -import java.util.AbstractMap; -import java.util.Map; - -public final class ObdDecoder { - - private ObdDecoder() { - } - - private static final int MODE_CURRENT = 0x01; - private static final int MODE_FREEZE_FRAME = 0x02; - private static final int MODE_CODES = 0x03; - - private static final int PID_ENGINE_LOAD = 0x04; - private static final int PID_COOLANT_TEMPERATURE = 0x05; - private static final int PID_ENGINE_RPM = 0x0C; - private static final int PID_VEHICLE_SPEED = 0x0D; - private static final int PID_THROTTLE_POSITION = 0x11; - private static final int PID_MIL_DISTANCE = 0x21; - private static final int PID_FUEL_LEVEL = 0x2F; - private static final int PID_DISTANCE_CLEARED = 0x31; - - public static Map.Entry<String, Object> decode(int mode, String value) { - switch (mode) { - case MODE_CURRENT: - case MODE_FREEZE_FRAME: - return decodeData( - Integer.parseInt(value.substring(0, 2), 16), - Integer.parseInt(value.substring(2), 16), true); - case MODE_CODES: - return decodeCodes(value); - default: - return null; - } - } - - private static Map.Entry<String, Object> createEntry(String key, Object value) { - return new AbstractMap.SimpleEntry<>(key, value); - } - - public static Map.Entry<String, Object> decodeCodes(String value) { - StringBuilder codes = new StringBuilder(); - for (int i = 0; i < value.length() / 4; i++) { - int numValue = Integer.parseInt(value.substring(i * 4, (i + 1) * 4), 16); - codes.append(' '); - switch (numValue >> 14) { - case 1: - codes.append('C'); - break; - case 2: - codes.append('B'); - break; - case 3: - codes.append('U'); - break; - default: - codes.append('P'); - break; - } - codes.append(String.format("%04X", numValue & 0x3FFF)); - } - if (codes.length() > 0) { - return createEntry(Position.KEY_DTCS, codes.toString().trim()); - } else { - return null; - } - } - - public static Map.Entry<String, Object> decodeData(int pid, int value, boolean convert) { - switch (pid) { - case PID_ENGINE_LOAD: - return createEntry(Position.KEY_ENGINE_LOAD, convert ? value * 100 / 255 : value); - case PID_COOLANT_TEMPERATURE: - return createEntry(Position.KEY_COOLANT_TEMP, convert ? value - 40 : value); - case PID_ENGINE_RPM: - return createEntry(Position.KEY_RPM, convert ? value / 4 : value); - case PID_VEHICLE_SPEED: - return createEntry(Position.KEY_OBD_SPEED, value); - case PID_THROTTLE_POSITION: - return createEntry(Position.KEY_THROTTLE, convert ? value * 100 / 255 : value); - case PID_MIL_DISTANCE: - return createEntry("milDistance", value); - case PID_FUEL_LEVEL: - return createEntry(Position.KEY_FUEL_LEVEL, convert ? value * 100 / 255 : value); - case PID_DISTANCE_CLEARED: - return createEntry("clearedDistance", value); - default: - return null; - } - } - -} diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java deleted file mode 100644 index 1471ec237..000000000 --- a/src/org/traccar/helper/Parser.java +++ /dev/null @@ -1,348 +0,0 @@ -/* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import java.util.Date; -import java.util.TimeZone; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class Parser { - - private int position; - private final Matcher matcher; - - public Parser(Pattern pattern, String input) { - matcher = pattern.matcher(input); - } - - public boolean matches() { - position = 1; - return matcher.matches(); - } - - public boolean find() { - position = 1; - return matcher.find(); - } - - public void skip(int number) { - position += number; - } - - public boolean hasNext() { - return hasNext(1); - } - - public boolean hasNext(int number) { - String value = matcher.group(position); - if (value != null && !value.isEmpty()) { - return true; - } else { - position += number; - return false; - } - } - - public String next() { - return matcher.group(position++); - } - - public Integer nextInt() { - if (hasNext()) { - return Integer.parseInt(next()); - } else { - return null; - } - } - - public int nextInt(int defaultValue) { - if (hasNext()) { - return Integer.parseInt(next()); - } else { - return defaultValue; - } - } - - public Integer nextHexInt() { - if (hasNext()) { - return Integer.parseInt(next(), 16); - } else { - return null; - } - } - - public int nextHexInt(int defaultValue) { - if (hasNext()) { - return Integer.parseInt(next(), 16); - } else { - return defaultValue; - } - } - - public Integer nextBinInt() { - if (hasNext()) { - return Integer.parseInt(next(), 2); - } else { - return null; - } - } - - public int nextBinInt(int defaultValue) { - if (hasNext()) { - return Integer.parseInt(next(), 2); - } else { - return defaultValue; - } - } - - public Long nextLong() { - if (hasNext()) { - return Long.parseLong(next()); - } else { - return null; - } - } - - public Long nextHexLong() { - if (hasNext()) { - return Long.parseLong(next(), 16); - } else { - return null; - } - } - - public long nextLong(long defaultValue) { - return nextLong(10, defaultValue); - } - - public long nextLong(int radix, long defaultValue) { - if (hasNext()) { - return Long.parseLong(next(), radix); - } else { - return defaultValue; - } - } - - public Double nextDouble() { - if (hasNext()) { - return Double.parseDouble(next()); - } else { - return null; - } - } - - public double nextDouble(double defaultValue) { - if (hasNext()) { - return Double.parseDouble(next()); - } else { - return defaultValue; - } - } - - public enum CoordinateFormat { - DEG_DEG, - DEG_HEM, - DEG_MIN_MIN, - DEG_MIN_HEM, - DEG_MIN_MIN_HEM, - HEM_DEG_MIN_MIN, - HEM_DEG, - HEM_DEG_MIN, - HEM_DEG_MIN_HEM - } - - public double nextCoordinate(CoordinateFormat format) { - double coordinate; - String hemisphere = null; - - switch (format) { - case DEG_DEG: - coordinate = Double.parseDouble(next() + '.' + next()); - break; - case DEG_HEM: - coordinate = nextDouble(0); - hemisphere = next(); - break; - case DEG_MIN_MIN: - coordinate = nextInt(0); - coordinate += Double.parseDouble(next() + '.' + next()) / 60; - break; - case DEG_MIN_MIN_HEM: - coordinate = nextInt(0); - coordinate += Double.parseDouble(next() + '.' + next()) / 60; - hemisphere = next(); - break; - case HEM_DEG: - hemisphere = next(); - coordinate = nextDouble(0); - break; - case HEM_DEG_MIN: - hemisphere = next(); - coordinate = nextInt(0); - coordinate += nextDouble(0) / 60; - break; - case HEM_DEG_MIN_HEM: - hemisphere = next(); - coordinate = nextInt(0); - coordinate += nextDouble(0) / 60; - if (hasNext()) { - hemisphere = next(); - } - break; - case HEM_DEG_MIN_MIN: - hemisphere = next(); - coordinate = nextInt(0); - coordinate += Double.parseDouble(next() + '.' + next()) / 60; - break; - case DEG_MIN_HEM: - default: - coordinate = nextInt(0); - coordinate += nextDouble(0) / 60; - hemisphere = next(); - break; - } - - if (hemisphere != null && (hemisphere.equals("S") || hemisphere.equals("W") || hemisphere.equals("-"))) { - coordinate = -Math.abs(coordinate); - } - - return coordinate; - } - - public double nextCoordinate() { - return nextCoordinate(CoordinateFormat.DEG_MIN_HEM); - } - - public enum DateTimeFormat { - HMS, - SMH, - HMS_YMD, - HMS_DMY, - SMH_YMD, - SMH_DMY, - DMY_HMS, - DMY_HMSS, - YMD_HMS, - YMD_HMSS, - } - - public Date nextDateTime(DateTimeFormat format, String timeZone) { - int year = 0, month = 0, day = 0; - int hour = 0, minute = 0, second = 0, millisecond = 0; - - switch (format) { - case HMS: - hour = nextInt(0); - minute = nextInt(0); - second = nextInt(0); - break; - case SMH: - second = nextInt(0); - minute = nextInt(0); - hour = nextInt(0); - break; - case HMS_YMD: - hour = nextInt(0); - minute = nextInt(0); - second = nextInt(0); - year = nextInt(0); - month = nextInt(0); - day = nextInt(0); - break; - case HMS_DMY: - hour = nextInt(0); - minute = nextInt(0); - second = nextInt(0); - day = nextInt(0); - month = nextInt(0); - year = nextInt(0); - break; - case SMH_YMD: - second = nextInt(0); - minute = nextInt(0); - hour = nextInt(0); - year = nextInt(0); - month = nextInt(0); - day = nextInt(0); - break; - case SMH_DMY: - second = nextInt(0); - minute = nextInt(0); - hour = nextInt(0); - day = nextInt(0); - month = nextInt(0); - year = nextInt(0); - break; - case DMY_HMS: - case DMY_HMSS: - day = nextInt(0); - month = nextInt(0); - year = nextInt(0); - hour = nextInt(0); - minute = nextInt(0); - second = nextInt(0); - break; - case YMD_HMS: - case YMD_HMSS: - default: - year = nextInt(0); - month = nextInt(0); - day = nextInt(0); - hour = nextInt(0); - minute = nextInt(0); - second = nextInt(0); - break; - } - - if (format == DateTimeFormat.YMD_HMSS || format == DateTimeFormat.DMY_HMSS) { - millisecond = nextInt(0); // (ddd) - } - - if (year >= 0 && year < 100) { - year += 2000; - } - - DateBuilder dateBuilder; - if (format != DateTimeFormat.HMS && format != DateTimeFormat.SMH) { - if (timeZone != null) { - dateBuilder = new DateBuilder(TimeZone.getTimeZone(timeZone)); - } else { - dateBuilder = new DateBuilder(); - } - dateBuilder.setDate(year, month, day); - } else { - if (timeZone != null) { - dateBuilder = new DateBuilder(new Date(), TimeZone.getTimeZone(timeZone)); - } else { - dateBuilder = new DateBuilder(new Date()); - } - } - - dateBuilder.setTime(hour, minute, second, millisecond); - - return dateBuilder.getDate(); - } - - public Date nextDateTime(DateTimeFormat format) { - return nextDateTime(format, null); - } - - public Date nextDateTime() { - return nextDateTime(DateTimeFormat.YMD_HMS, null); - } - -} diff --git a/src/org/traccar/helper/PatternBuilder.java b/src/org/traccar/helper/PatternBuilder.java deleted file mode 100644 index f3de5c1e5..000000000 --- a/src/org/traccar/helper/PatternBuilder.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import java.util.ArrayList; -import java.util.regex.Pattern; - -public class PatternBuilder { - - private final ArrayList<String> fragments = new ArrayList<>(); - - public PatternBuilder optional() { - return optional(1); - } - - public PatternBuilder optional(int count) { - fragments.add(fragments.size() - count, "(?:"); - fragments.add(")?"); - return this; - } - - public PatternBuilder expression(String s) { - s = s.replaceAll("\\|$", "\\\\|"); // special case for delimiter - - fragments.add(s); - return this; - } - - public PatternBuilder text(String s) { - fragments.add(s.replaceAll("([\\\\\\.\\[\\{\\(\\)\\*\\+\\?\\^\\$\\|])", "\\\\$1")); - return this; - } - - public PatternBuilder number(String s) { - s = s.replace("dddd", "d{4}").replace("ddd", "d{3}").replace("dd", "d{2}"); - s = s.replace("xxxx", "x{4}").replace("xxx", "x{3}").replace("xx", "x{2}"); - - s = s.replace("d", "\\d").replace("x", "[0-9a-fA-F]").replaceAll("([\\.])", "\\\\$1"); - s = s.replaceAll("\\|$", "\\\\|").replaceAll("^\\|", "\\\\|"); // special case for delimiter - - fragments.add(s); - return this; - } - - public PatternBuilder any() { - fragments.add(".*?"); - return this; - } - - public PatternBuilder binary(String s) { - fragments.add(s.replaceAll("(\\p{XDigit}{2})", "\\\\$1")); - return this; - } - - public PatternBuilder or() { - fragments.add("|"); - return this; - } - - public PatternBuilder groupBegin() { - return expression("(?:"); - } - - public PatternBuilder groupEnd() { - return expression(")"); - } - - public PatternBuilder groupEnd(String s) { - return expression(")" + s); - } - - public Pattern compile() { - return Pattern.compile(toString(), Pattern.DOTALL); - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - for (String fragment : fragments) { - builder.append(fragment); - } - return builder.toString(); - } - -} diff --git a/src/org/traccar/helper/PatternUtil.java b/src/org/traccar/helper/PatternUtil.java deleted file mode 100644 index 1bbb166a6..000000000 --- a/src/org/traccar/helper/PatternUtil.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import java.lang.management.ManagementFactory; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.regex.PatternSyntaxException; - -public final class PatternUtil { - - private PatternUtil() { - } - - public static class MatchResult { - private String patternMatch; - private String patternTail; - private String stringMatch; - private String stringTail; - - public String getPatternMatch() { - return patternMatch; - } - - public String getPatternTail() { - return patternTail; - } - - public String getStringMatch() { - return stringMatch; - } - - public String getStringTail() { - return stringTail; - } - } - - public static MatchResult checkPattern(String pattern, String input) { - - if (!ManagementFactory.getRuntimeMXBean().getInputArguments().toString().contains("-agentlib:jdwp")) { - throw new RuntimeException("PatternUtil usage detected"); - } - - MatchResult result = new MatchResult(); - - for (int i = 0; i < pattern.length(); i++) { - try { - Matcher matcher = Pattern.compile("(" + pattern.substring(0, i) + ").*").matcher(input); - if (matcher.matches()) { - result.patternMatch = pattern.substring(0, i); - result.patternTail = pattern.substring(i); - result.stringMatch = matcher.group(1); - result.stringTail = input.substring(matcher.group(1).length()); - } - } catch (PatternSyntaxException error) { - Log.warning(error); - } - } - - return result; - } - -} diff --git a/src/org/traccar/helper/StringFinder.java b/src/org/traccar/helper/StringFinder.java deleted file mode 100644 index 2fa0aa9a4..000000000 --- a/src/org/traccar/helper/StringFinder.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBufferIndexFinder; - -import java.nio.charset.StandardCharsets; - -public class StringFinder implements ChannelBufferIndexFinder { - - private String string; - - public StringFinder(String string) { - this.string = string; - } - - @Override - public boolean find(ChannelBuffer buffer, int guessedIndex) { - - if (buffer.writerIndex() - guessedIndex < string.length()) { - return false; - } - - return string.equals(buffer.toString(guessedIndex, string.length(), StandardCharsets.US_ASCII)); - } - -} diff --git a/src/org/traccar/helper/UnitsConverter.java b/src/org/traccar/helper/UnitsConverter.java deleted file mode 100644 index 56d15e4e7..000000000 --- a/src/org/traccar/helper/UnitsConverter.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.helper; - -public final class UnitsConverter { - - private static final double KNOTS_TO_KPH_RATIO = 0.539957; - private static final double KNOTS_TO_MPH_RATIO = 0.868976; - private static final double KNOTS_TO_MPS_RATIO = 1.94384; - private static final double KNOTS_TO_CPS_RATIO = 0.0194384449; - private static final double METERS_TO_FEET_RATIO = 0.3048; - private static final double METERS_TO_MILE_RATIO = 1609.34; - - private UnitsConverter() { - } - - public static double knotsFromKph(double value) { // km/h - return value * KNOTS_TO_KPH_RATIO; - } - - public static double kphFromKnots(double value) { - return value / KNOTS_TO_KPH_RATIO; - } - - public static double knotsFromMph(double value) { - return value * KNOTS_TO_MPH_RATIO; - } - - public static double mphFromKnots(double value) { - return value / KNOTS_TO_MPH_RATIO; - } - - public static double knotsFromMps(double value) { // m/s - return value * KNOTS_TO_MPS_RATIO; - } - - public static double mpsFromKnots(double value) { - return value / KNOTS_TO_MPS_RATIO; - } - - public static double knotsFromCps(double value) { // cm/s - return value * KNOTS_TO_CPS_RATIO; - } - - public static double feetFromMeters(double value) { - return value / METERS_TO_FEET_RATIO; - } - - public static double metersFromFeet(double value) { - return value * METERS_TO_FEET_RATIO; - } - - public static double milesFromMeters(double value) { - return value / METERS_TO_MILE_RATIO; - } - - public static double metersFromMiles(double value) { - return value * METERS_TO_MILE_RATIO; - } - -} |