From 267c6e1651b6e0e91dd7c3463f86de929d7bc73c Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 9 Feb 2018 12:11:39 +0500 Subject: Implement unified device timezone handling --- src/org/traccar/BaseProtocolDecoder.java | 22 ++++++++++- src/org/traccar/DeviceSession.java | 14 ++++++- src/org/traccar/protocol/Gt06ProtocolDecoder.java | 45 +++++++++++++--------- .../traccar/protocol/UlbotechProtocolDecoder.java | 12 +++--- 4 files changed, 67 insertions(+), 26 deletions(-) diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 18b0bc04a..3ea1208ca 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 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. @@ -29,6 +29,7 @@ import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.TimeZone; import java.sql.SQLException; public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { @@ -81,6 +82,25 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } } + protected TimeZone getTimeZone(long deviceId) { + TimeZone result = TimeZone.getTimeZone("UTC"); + String timeZoneName = null; + if (Context.getDeviceManager() != null) { + timeZoneName = Context.getDeviceManager().lookupAttributeString( + deviceId, "decoder.timezone", null, true); + } + if (timeZoneName != null) { + result = TimeZone.getTimeZone(timeZoneName); + } else { + int timeZoneOffset = Context.getConfig().getInteger(getProtocolName() + ".timezone", 0); + if (timeZoneOffset != 0) { + result.setRawOffset(timeZoneOffset * 1000); + Log.warning("Config parameter " + getProtocolName() + ".timezone is deprecated"); + } + } + return result; + } + private DeviceSession channelDeviceSession; // connection-based protocols private Map addressDeviceSessions = new HashMap<>(); // connectionless protocols diff --git a/src/org/traccar/DeviceSession.java b/src/org/traccar/DeviceSession.java index 36958287d..322381807 100644 --- a/src/org/traccar/DeviceSession.java +++ b/src/org/traccar/DeviceSession.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2018 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. @@ -15,6 +15,8 @@ */ package org.traccar; +import java.util.TimeZone; + public class DeviceSession { private final long deviceId; @@ -27,4 +29,14 @@ public class DeviceSession { return deviceId; } + private TimeZone timeZone; + + public void setTimeZone(TimeZone timeZone) { + this.timeZone = timeZone; + } + + public TimeZone getTimeZone() { + return timeZone; + } + } diff --git a/src/org/traccar/protocol/Gt06ProtocolDecoder.java b/src/org/traccar/protocol/Gt06ProtocolDecoder.java index 4173b4d5b..3e5ebc8c5 100644 --- a/src/org/traccar/protocol/Gt06ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt06ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 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. @@ -44,18 +44,10 @@ import java.util.regex.Pattern; public class Gt06ProtocolDecoder extends BaseProtocolDecoder { - private boolean forceTimeZone = false; - private final TimeZone timeZone = TimeZone.getTimeZone("UTC"); - private final Map photos = new HashMap<>(); public Gt06ProtocolDecoder(Gt06Protocol protocol) { super(protocol); - - if (Context.getConfig().hasKey(getProtocolName() + ".timezone")) { - forceTimeZone = true; - timeZone.setRawOffset(Context.getConfig().getInteger(getProtocolName() + ".timezone") * 1000); - } } public static final int MSG_LOGIN = 0x01; @@ -202,9 +194,9 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, false, MSG_X1_PHOTO_DATA, 0, content); } - private boolean decodeGps(Position position, ChannelBuffer buf, boolean hasLength) { + private boolean decodeGps(Position position, ChannelBuffer buf, boolean hasLength, TimeZone timezone) { - DateBuilder dateBuilder = new DateBuilder(timeZone) + DateBuilder dateBuilder = new DateBuilder(timezone) .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); position.setTime(dateBuilder.getDate()); @@ -387,6 +379,9 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { if (deviceSession == null) { return null; } + if (deviceSession.getTimeZone() == null) { + deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId())); + } } if (type == MSG_LOGIN) { @@ -394,6 +389,11 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { String imei = ChannelBuffers.hexDump(buf.readBytes(8)).substring(1); buf.readUnsignedShort(); // type + deviceSession = getDeviceSession(channel, remoteAddress, imei); + if (deviceSession != null && deviceSession.getTimeZone() == null) { + deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId())); + } + if (dataLength > 10) { int extensionBits = buf.readUnsignedShort(); int hours = (extensionBits >> 4) / 100; @@ -402,12 +402,17 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { if ((extensionBits & 0x8) != 0) { offset = -offset; } - if (!forceTimeZone) { - timeZone.setRawOffset(offset * 1000); + if (deviceSession != null) { + TimeZone timeZone = deviceSession.getTimeZone(); + if (timeZone.getRawOffset() == 0) { + timeZone.setRawOffset(offset * 1000); + deviceSession.setTimeZone(timeZone); + } } + } - if (getDeviceSession(channel, remoteAddress, imei) != null) { + if (deviceSession != null) { sendResponse(channel, false, type, buf.getShort(buf.writerIndex() - 6), null); } @@ -455,7 +460,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedInt(); // data and alarm - decodeGps(position, buf, false); + decodeGps(position, buf, false, deviceSession.getTimeZone()); buf.readUnsignedShort(); // terminal info @@ -543,7 +548,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { boolean longFormat = type == MSG_LBS_2 || type == MSG_WIFI_3; - DateBuilder dateBuilder = new DateBuilder(timeZone) + DateBuilder dateBuilder = new DateBuilder(deviceSession.getTimeZone()) .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); @@ -589,7 +594,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { } else if (isSupported(type)) { if (hasGps(type)) { - decodeGps(position, buf, false); + decodeGps(position, buf, false, deviceSession.getTimeZone()); } else { getLastLocation(position, null); } @@ -642,6 +647,10 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { return null; } + if (deviceSession.getTimeZone() == null) { + deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId())); + } + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); @@ -711,7 +720,7 @@ public class Gt06ProtocolDecoder extends BaseProtocolDecoder { } else if (type == MSG_AZ735_GPS || type == MSG_AZ735_ALARM) { - if (!decodeGps(position, buf, true)) { + if (!decodeGps(position, buf, true, deviceSession.getTimeZone())) { getLastLocation(position, position.getDeviceTime()); } diff --git a/src/org/traccar/protocol/UlbotechProtocolDecoder.java b/src/org/traccar/protocol/UlbotechProtocolDecoder.java index f5ca9190b..5499518a1 100644 --- a/src/org/traccar/protocol/UlbotechProtocolDecoder.java +++ b/src/org/traccar/protocol/UlbotechProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -19,7 +19,6 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; -import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.Checksum; @@ -39,11 +38,8 @@ import java.util.regex.Pattern; public class UlbotechProtocolDecoder extends BaseProtocolDecoder { - private final long timeZone; - public UlbotechProtocolDecoder(UlbotechProtocol protocol) { super(protocol); - timeZone = Context.getConfig().getInteger(getProtocolName() + ".timezone", 0); } private static final short DATA_GPS = 0x01; @@ -215,12 +211,16 @@ public class UlbotechProtocolDecoder extends BaseProtocolDecoder { return null; } + if (deviceSession.getTimeZone() == null) { + deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId())); + } + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); long seconds = buf.readUnsignedInt() & 0x7fffffffL; seconds += 946684800L; // 2000-01-01 00:00 - seconds -= timeZone; + seconds -= deviceSession.getTimeZone().getRawOffset() / 1000; Date time = new Date(seconds * 1000); boolean hasLocation = false; -- cgit v1.2.3