diff options
author | Yuriy Piskarev <yuriy.piskarev@gmail.com> | 2023-08-24 14:16:17 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-24 14:16:17 +0300 |
commit | ae406c7b49a72de24d81fd74386d9638342c90ee (patch) | |
tree | 6fbcf557375b98e926c78af9c757e62c79d72a1b /src/main/java/org/traccar/model | |
parent | 56ff656c908b19feb2fa3dcffa48cc3bcdfe9b3b (diff) | |
parent | 9aeedc90da24848ff97227d6f281eb4d1e1506ef (diff) | |
download | trackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.tar.gz trackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.tar.bz2 trackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.zip |
Merge branch 'traccar:master' into master
Diffstat (limited to 'src/main/java/org/traccar/model')
23 files changed, 698 insertions, 221 deletions
diff --git a/src/main/java/org/traccar/model/BaseCommand.java b/src/main/java/org/traccar/model/BaseCommand.java new file mode 100644 index 000000000..f87b8ef65 --- /dev/null +++ b/src/main/java/org/traccar/model/BaseCommand.java @@ -0,0 +1,30 @@ +/* + * Copyright 2022 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.model; + +public class BaseCommand extends Message { + + private boolean textChannel; + + public boolean getTextChannel() { + return textChannel; + } + + public void setTextChannel(boolean textChannel) { + this.textChannel = textChannel; + } + +} diff --git a/src/main/java/org/traccar/model/BaseModel.java b/src/main/java/org/traccar/model/BaseModel.java index 8bdb916e8..acde0f83d 100644 --- a/src/main/java/org/traccar/model/BaseModel.java +++ b/src/main/java/org/traccar/model/BaseModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2022 Anton Tananaev (anton@traccar.org) * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,11 +20,11 @@ public class BaseModel { private long id; - public final long getId() { + public long getId() { return id; } - public final void setId(long id) { + public void setId(long id) { this.id = id; } diff --git a/src/main/java/org/traccar/model/Calendar.java b/src/main/java/org/traccar/model/Calendar.java index 102c0be52..03f1995ba 100644 --- a/src/main/java/org/traccar/model/Calendar.java +++ b/src/main/java/org/traccar/model/Calendar.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2021 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org) * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,6 +24,7 @@ import net.fortuna.ical4j.filter.predicate.PeriodRule; import net.fortuna.ical4j.model.DateTime; import net.fortuna.ical4j.model.Period; import net.fortuna.ical4j.model.component.CalendarComponent; +import net.fortuna.ical4j.model.component.VEvent; import org.traccar.storage.QueryIgnore; import org.traccar.storage.StorageName; @@ -32,6 +33,8 @@ import java.io.IOException; import java.time.Duration; import java.util.Collection; import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; @StorageName("tc_calendars") public class Calendar extends ExtendedModel { @@ -49,13 +52,13 @@ public class Calendar extends ExtendedModel { private byte[] data; public byte[] getData() { - return data.clone(); + return data; } public void setData(byte[] data) throws IOException, ParserException { CalendarBuilder builder = new CalendarBuilder(); calendar = builder.build(new ByteArrayInputStream(data)); - this.data = data.clone(); + this.data = data; } private net.fortuna.ical4j.model.Calendar calendar; @@ -66,14 +69,24 @@ public class Calendar extends ExtendedModel { return calendar; } - public boolean checkMoment(Date date) { + private Collection<VEvent> findEvents(Date date) { if (calendar != null) { - Period period = new Period(new DateTime(date), Duration.ZERO); - Filter<CalendarComponent> filter = new Filter<>(new PeriodRule<>(period)); - Collection<CalendarComponent> events = filter.filter(calendar.getComponents(CalendarComponent.VEVENT)); - return events != null && !events.isEmpty(); + var filter = new Filter<VEvent>(new PeriodRule<>(new Period(new DateTime(date), Duration.ZERO))); + return filter.filter(calendar.getComponents(CalendarComponent.VEVENT)); + } else { + return List.of(); } - return false; + } + + public Collection<Period> findPeriods(Date date) { + var calendarDate = new net.fortuna.ical4j.model.Date(date); + return findEvents(date).stream() + .flatMap((event) -> event.getConsumedTime(calendarDate, calendarDate).stream()) + .collect(Collectors.toSet()); + } + + public boolean checkMoment(Date date) { + return !findEvents(date).isEmpty(); } } diff --git a/src/main/java/org/traccar/model/CellTower.java b/src/main/java/org/traccar/model/CellTower.java index 254487471..355594c64 100644 --- a/src/main/java/org/traccar/model/CellTower.java +++ b/src/main/java/org/traccar/model/CellTower.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2022 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. @@ -16,9 +16,11 @@ package org.traccar.model; import com.fasterxml.jackson.annotation.JsonInclude; -import org.traccar.Context; +import org.traccar.config.Config; import org.traccar.config.Keys; +import java.util.Objects; + @JsonInclude(JsonInclude.Include.NON_NULL) public class CellTower { @@ -37,14 +39,12 @@ public class CellTower { return cellTower; } - public static CellTower fromLacCid(int lac, long cid) { - return from( - Context.getConfig().getInteger(Keys.GEOLOCATION_MCC), - Context.getConfig().getInteger(Keys.GEOLOCATION_MCC), lac, cid); + public static CellTower fromLacCid(Config config, int lac, long cid) { + return from(config.getInteger(Keys.GEOLOCATION_MCC), config.getInteger(Keys.GEOLOCATION_MNC), lac, cid); } - public static CellTower fromCidLac(long cid, int lac) { - return fromLacCid(lac, cid); + public static CellTower fromCidLac(Config config, long cid, int lac) { + return fromLacCid(config, lac, cid); } private String radioType; @@ -113,4 +113,26 @@ public class CellTower { mobileNetworkCode = Integer.parseInt(operatorString.substring(3)); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + CellTower cellTower = (CellTower) o; + return Objects.equals(radioType, cellTower.radioType) + && Objects.equals(cellId, cellTower.cellId) + && Objects.equals(locationAreaCode, cellTower.locationAreaCode) + && Objects.equals(mobileCountryCode, cellTower.mobileCountryCode) + && Objects.equals(mobileNetworkCode, cellTower.mobileNetworkCode) + && Objects.equals(signalStrength, cellTower.signalStrength); + } + + @Override + public int hashCode() { + return Objects.hash(radioType, cellId, locationAreaCode, mobileCountryCode, mobileNetworkCode, signalStrength); + } + } diff --git a/src/main/java/org/traccar/model/Command.java b/src/main/java/org/traccar/model/Command.java index 03961c7b2..99988dd82 100644 --- a/src/main/java/org/traccar/model/Command.java +++ b/src/main/java/org/traccar/model/Command.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2022 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. @@ -22,7 +22,7 @@ import org.traccar.storage.StorageName; @StorageName("tc_commands") @JsonIgnoreProperties(ignoreUnknown = true) -public class Command extends Message implements Cloneable { +public class Command extends BaseCommand { public static final String TYPE_CUSTOM = "custom"; public static final String TYPE_IDENTIFICATION = "deviceIdentification"; @@ -58,11 +58,10 @@ public class Command extends Message implements Cloneable { public static final String TYPE_GET_MODEM_STATUS = "getModemStatus"; public static final String TYPE_GET_DEVICE_STATUS = "getDeviceStatus"; public static final String TYPE_SET_SPEED_LIMIT = "setSpeedLimit"; - public static final String TYPE_MODE_POWER_SAVING = "modePowerSaving"; public static final String TYPE_MODE_DEEP_SLEEP = "modeDeepSleep"; - public static final String TYPE_ALARM_GEOFENCE = "movementAlarm"; + public static final String TYPE_ALARM_GEOFENCE = "alarmGeofence"; public static final String TYPE_ALARM_BATTERY = "alarmBattery"; public static final String TYPE_ALARM_SOS = "alarmSos"; public static final String TYPE_ALARM_REMOVE = "alarmRemove"; @@ -85,21 +84,6 @@ public class Command extends Message implements Cloneable { public static final String KEY_SERVER = "server"; public static final String KEY_PORT = "port"; - @Override - public Command clone() throws CloneNotSupportedException { - return (Command) super.clone(); - } - - private boolean textChannel; - - public boolean getTextChannel() { - return textChannel; - } - - public void setTextChannel(boolean textChannel) { - this.textChannel = textChannel; - } - @QueryIgnore @Override public long getDeviceId() { diff --git a/src/main/java/org/traccar/model/Device.java b/src/main/java/org/traccar/model/Device.java index 219f078ed..e07815976 100644 --- a/src/main/java/org/traccar/model/Device.java +++ b/src/main/java/org/traccar/model/Device.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2023 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,15 +15,26 @@ */ package org.traccar.model; -import java.util.Date; -import java.util.List; - -import org.traccar.storage.QueryExtended; +import com.fasterxml.jackson.annotation.JsonIgnore; import org.traccar.storage.QueryIgnore; import org.traccar.storage.StorageName; +import java.util.Date; + @StorageName("tc_devices") -public class Device extends GroupedModel { +public class Device extends GroupedModel implements Disableable, Schedulable { + + private long calendarId; + + @Override + public long getCalendarId() { + return calendarId; + } + + @Override + public void setCalendarId(long calendarId) { + this.calendarId = calendarId; + } private String name; @@ -42,7 +53,7 @@ public class Device extends GroupedModel { } public void setUniqueId(String uniqueId) { - this.uniqueId = uniqueId; + this.uniqueId = uniqueId.trim(); } public static final String STATUS_UNKNOWN = "unknown"; @@ -56,18 +67,17 @@ public class Device extends GroupedModel { return status != null ? status : STATUS_OFFLINE; } - @QueryIgnore public void setStatus(String status) { - this.status = status; + this.status = status != null ? status.trim() : null; } private Date lastUpdate; + @QueryIgnore public Date getLastUpdate() { return this.lastUpdate; } - @QueryExtended public void setLastUpdate(Date lastUpdate) { this.lastUpdate = lastUpdate; } @@ -79,23 +89,10 @@ public class Device extends GroupedModel { return positionId; } - @QueryIgnore public void setPositionId(long positionId) { this.positionId = positionId; } - private List<Long> geofenceIds; - - @QueryIgnore - public List<Long> getGeofenceIds() { - return geofenceIds; - } - - @QueryIgnore - public void setGeofenceIds(List<Long> geofenceIds) { - this.geofenceIds = geofenceIds; - } - private String phone; public String getPhone() { @@ -103,7 +100,7 @@ public class Device extends GroupedModel { } public void setPhone(String phone) { - this.phone = phone; + this.phone = phone != null ? phone.trim() : null; } private String model; @@ -138,12 +135,117 @@ public class Device extends GroupedModel { private boolean disabled; + @Override public boolean getDisabled() { return disabled; } + @Override public void setDisabled(boolean disabled) { this.disabled = disabled; } + private Date expirationTime; + + @Override + public Date getExpirationTime() { + return expirationTime; + } + + @Override + public void setExpirationTime(Date expirationTime) { + this.expirationTime = expirationTime; + } + + private boolean motionStreak; + + @QueryIgnore + @JsonIgnore + public boolean getMotionStreak() { + return motionStreak; + } + + @JsonIgnore + public void setMotionStreak(boolean motionStreak) { + this.motionStreak = motionStreak; + } + + private boolean motionState; + + @QueryIgnore + @JsonIgnore + public boolean getMotionState() { + return motionState; + } + + @JsonIgnore + public void setMotionState(boolean motionState) { + this.motionState = motionState; + } + + private Date motionTime; + + @QueryIgnore + @JsonIgnore + public Date getMotionTime() { + return motionTime; + } + + @JsonIgnore + public void setMotionTime(Date motionTime) { + this.motionTime = motionTime; + } + + private double motionDistance; + + @QueryIgnore + @JsonIgnore + public double getMotionDistance() { + return motionDistance; + } + + @JsonIgnore + public void setMotionDistance(double motionDistance) { + this.motionDistance = motionDistance; + } + + private boolean overspeedState; + + @QueryIgnore + @JsonIgnore + public boolean getOverspeedState() { + return overspeedState; + } + + @JsonIgnore + public void setOverspeedState(boolean overspeedState) { + this.overspeedState = overspeedState; + } + + private Date overspeedTime; + + @QueryIgnore + @JsonIgnore + public Date getOverspeedTime() { + return overspeedTime; + } + + @JsonIgnore + public void setOverspeedTime(Date overspeedTime) { + this.overspeedTime = overspeedTime; + } + + private long overspeedGeofenceId; + + @QueryIgnore + @JsonIgnore + public long getOverspeedGeofenceId() { + return overspeedGeofenceId; + } + + @JsonIgnore + public void setOverspeedGeofenceId(long overspeedGeofenceId) { + this.overspeedGeofenceId = overspeedGeofenceId; + } + } diff --git a/src/main/java/org/traccar/model/DeviceState.java b/src/main/java/org/traccar/model/DeviceState.java deleted file mode 100644 index 75d6726ee..000000000 --- a/src/main/java/org/traccar/model/DeviceState.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 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.model; - -public class DeviceState { - - private Boolean motionState; - - public void setMotionState(boolean motionState) { - this.motionState = motionState; - } - - public Boolean getMotionState() { - return motionState; - } - - private Position motionPosition; - - public void setMotionPosition(Position motionPosition) { - this.motionPosition = motionPosition; - } - - public Position getMotionPosition() { - return motionPosition; - } - - private Boolean overspeedState; - - public void setOverspeedState(boolean overspeedState) { - this.overspeedState = overspeedState; - } - - public Boolean getOverspeedState() { - return overspeedState; - } - - private Position overspeedPosition; - - public void setOverspeedPosition(Position overspeedPosition) { - this.overspeedPosition = overspeedPosition; - } - - public Position getOverspeedPosition() { - return overspeedPosition; - } - - private long overspeedGeofenceId; - - public void setOverspeedGeofenceId(long overspeedGeofenceId) { - this.overspeedGeofenceId = overspeedGeofenceId; - } - - public long getOverspeedGeofenceId() { - return overspeedGeofenceId; - } - -} diff --git a/src/main/java/org/traccar/model/Disableable.java b/src/main/java/org/traccar/model/Disableable.java new file mode 100644 index 000000000..1145d6279 --- /dev/null +++ b/src/main/java/org/traccar/model/Disableable.java @@ -0,0 +1,39 @@ +/* + * Copyright 2022 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.model; + +import java.util.Date; + +public interface Disableable { + + boolean getDisabled(); + + void setDisabled(boolean disabled); + + Date getExpirationTime(); + + void setExpirationTime(Date expirationTime); + + default void checkDisabled() throws SecurityException { + if (getDisabled()) { + throw new SecurityException(getClass().getSimpleName() + " is disabled"); + } + if (getExpirationTime() != null && System.currentTimeMillis() > getExpirationTime().getTime()) { + throw new SecurityException(getClass().getSimpleName() + " has expired"); + } + } + +} diff --git a/src/main/java/org/traccar/model/Driver.java b/src/main/java/org/traccar/model/Driver.java index b9e023088..ca5714e51 100644 --- a/src/main/java/org/traccar/model/Driver.java +++ b/src/main/java/org/traccar/model/Driver.java @@ -38,7 +38,7 @@ public class Driver extends ExtendedModel { } public void setUniqueId(String uniqueId) { - this.uniqueId = uniqueId; + this.uniqueId = uniqueId.trim(); } } diff --git a/src/main/java/org/traccar/model/Event.java b/src/main/java/org/traccar/model/Event.java index 6e3953fda..0e851d748 100644 --- a/src/main/java/org/traccar/model/Event.java +++ b/src/main/java/org/traccar/model/Event.java @@ -52,6 +52,7 @@ public class Event extends Message { public static final String TYPE_DEVICE_OVERSPEED = "deviceOverspeed"; public static final String TYPE_DEVICE_FUEL_DROP = "deviceFuelDrop"; + public static final String TYPE_DEVICE_FUEL_INCREASE = "deviceFuelIncrease"; public static final String TYPE_GEOFENCE_ENTER = "geofenceEnter"; public static final String TYPE_GEOFENCE_EXIT = "geofenceExit"; @@ -62,10 +63,9 @@ public class Event extends Message { public static final String TYPE_IGNITION_OFF = "ignitionOff"; public static final String TYPE_MAINTENANCE = "maintenance"; - public static final String TYPE_TEXT_MESSAGE = "textMessage"; - public static final String TYPE_DRIVER_CHANGED = "driverChanged"; + public static final String TYPE_MEDIA = "media"; private Date eventTime; diff --git a/src/main/java/org/traccar/model/ExtendedModel.java b/src/main/java/org/traccar/model/ExtendedModel.java index 8353d0e66..d5cd094da 100644 --- a/src/main/java/org/traccar/model/ExtendedModel.java +++ b/src/main/java/org/traccar/model/ExtendedModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2022 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. @@ -17,17 +17,22 @@ package org.traccar.model; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Objects; public class ExtendedModel extends BaseModel { private Map<String, Object> attributes = new LinkedHashMap<>(); + public boolean hasAttribute(String key) { + return attributes.containsKey(key); + } + public Map<String, Object> getAttributes() { return attributes; } public void setAttributes(Map<String, Object> attributes) { - this.attributes = attributes; + this.attributes = Objects.requireNonNullElseGet(attributes, LinkedHashMap::new); } public void set(String key, Boolean value) { @@ -84,17 +89,27 @@ public class ExtendedModel extends BaseModel { } } - public String getString(String key) { + public String getString(String key, String defaultValue) { if (attributes.containsKey(key)) { - return (String) attributes.get(key); + Object value = attributes.get(key); + return value != null ? value.toString() : null; } else { - return null; + return defaultValue; } } + public String getString(String key) { + return getString(key, null); + } + public double getDouble(String key) { if (attributes.containsKey(key)) { - return ((Number) attributes.get(key)).doubleValue(); + Object value = attributes.get(key); + if (value instanceof Number) { + return ((Number) attributes.get(key)).doubleValue(); + } else { + return Double.parseDouble(value.toString()); + } } else { return 0.0; } @@ -102,7 +117,12 @@ public class ExtendedModel extends BaseModel { public boolean getBoolean(String key) { if (attributes.containsKey(key)) { - return (Boolean) attributes.get(key); + Object value = attributes.get(key); + if (value instanceof Boolean) { + return (Boolean) attributes.get(key); + } else { + return Boolean.parseBoolean(value.toString()); + } } else { return false; } @@ -110,7 +130,12 @@ public class ExtendedModel extends BaseModel { public int getInteger(String key) { if (attributes.containsKey(key)) { - return ((Number) attributes.get(key)).intValue(); + Object value = attributes.get(key); + if (value instanceof Number) { + return ((Number) attributes.get(key)).intValue(); + } else { + return Integer.parseInt(value.toString()); + } } else { return 0; } @@ -118,7 +143,12 @@ public class ExtendedModel extends BaseModel { public long getLong(String key) { if (attributes.containsKey(key)) { - return ((Number) attributes.get(key)).longValue(); + Object value = attributes.get(key); + if (value instanceof Number) { + return ((Number) attributes.get(key)).longValue(); + } else { + return Long.parseLong(value.toString()); + } } else { return 0; } diff --git a/src/main/java/org/traccar/model/Geofence.java b/src/main/java/org/traccar/model/Geofence.java index 5b857580d..ca6293651 100644 --- a/src/main/java/org/traccar/model/Geofence.java +++ b/src/main/java/org/traccar/model/Geofence.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2023 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,25 +15,30 @@ */ package org.traccar.model; -import java.text.ParseException; - -import org.traccar.Context; -import org.traccar.config.Keys; -import org.traccar.storage.QueryIgnore; +import com.fasterxml.jackson.annotation.JsonIgnore; import org.traccar.geofence.GeofenceCircle; import org.traccar.geofence.GeofenceGeometry; import org.traccar.geofence.GeofencePolygon; import org.traccar.geofence.GeofencePolyline; - -import com.fasterxml.jackson.annotation.JsonIgnore; +import org.traccar.storage.QueryIgnore; import org.traccar.storage.StorageName; +import java.text.ParseException; + @StorageName("tc_geofences") -public class Geofence extends ScheduledModel { +public class Geofence extends ExtendedModel implements Schedulable { - public static final String TYPE_GEOFENCE_CILCLE = "geofenceCircle"; - public static final String TYPE_GEOFENCE_POLYGON = "geofencePolygon"; - public static final String TYPE_GEOFENCE_POLYLINE = "geofencePolyline"; + private long calendarId; + + @Override + public long getCalendarId() { + return calendarId; + } + + @Override + public void setCalendarId(long calendarId) { + this.calendarId = calendarId; + } private String name; @@ -68,9 +73,7 @@ public class Geofence extends ScheduledModel { } else if (area.startsWith("POLYGON")) { geometry = new GeofencePolygon(area); } else if (area.startsWith("LINESTRING")) { - final double distance = getDouble("polylineDistance"); - geometry = new GeofencePolyline(area, distance > 0 ? distance - : Context.getConfig().getDouble(Keys.GEOFENCE_POLYLINE_DISTANCE)); + geometry = new GeofencePolyline(area); } else { throw new ParseException("Unknown geometry type", 0); } @@ -92,4 +95,5 @@ public class Geofence extends ScheduledModel { area = geometry.toWkt(); this.geometry = geometry; } + } diff --git a/src/main/java/org/traccar/model/Network.java b/src/main/java/org/traccar/model/Network.java index 4d67fc5d8..b31c53c38 100644 --- a/src/main/java/org/traccar/model/Network.java +++ b/src/main/java/org/traccar/model/Network.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2019 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2022 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,6 +19,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import java.util.ArrayList; import java.util.Collection; +import java.util.Objects; @JsonInclude(JsonInclude.Include.NON_NULL) public class Network { @@ -118,4 +119,27 @@ public class Network { wifiAccessPoints.add(wifiAccessPoint); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Network network = (Network) o; + return Objects.equals(homeMobileCountryCode, network.homeMobileCountryCode) + && Objects.equals(homeMobileNetworkCode, network.homeMobileNetworkCode) + && Objects.equals(radioType, network.radioType) + && Objects.equals(carrier, network.carrier) + && Objects.equals(cellTowers, network.cellTowers) + && Objects.equals(wifiAccessPoints, network.wifiAccessPoints); + } + + @Override + public int hashCode() { + return Objects.hash( + homeMobileCountryCode, homeMobileNetworkCode, radioType, carrier, cellTowers, wifiAccessPoints); + } + } diff --git a/src/main/java/org/traccar/model/Notification.java b/src/main/java/org/traccar/model/Notification.java index 95e446132..6dcd9c9de 100644 --- a/src/main/java/org/traccar/model/Notification.java +++ b/src/main/java/org/traccar/model/Notification.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2023 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. @@ -24,7 +24,19 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import org.traccar.storage.StorageName; @StorageName("tc_notifications") -public class Notification extends ScheduledModel { +public class Notification extends ExtendedModel implements Schedulable { + + private long calendarId; + + @Override + public long getCalendarId() { + return calendarId; + } + + @Override + public void setCalendarId(long calendarId) { + this.calendarId = calendarId; + } private boolean always; @@ -46,6 +58,16 @@ public class Notification extends ScheduledModel { this.type = type; } + private long commandId; + + public long getCommandId() { + return commandId; + } + + public void setCommandId(long commandId) { + this.commandId = commandId; + } + private String notificators; public String getNotificators() { diff --git a/src/main/java/org/traccar/model/Permission.java b/src/main/java/org/traccar/model/Permission.java index ad0176b39..0b2f0584f 100644 --- a/src/main/java/org/traccar/model/Permission.java +++ b/src/main/java/org/traccar/model/Permission.java @@ -31,12 +31,12 @@ import org.traccar.storage.QueryIgnore; public class Permission { - private static final Map<String, Class<?>> CLASSES = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + private static final Map<String, Class<? extends BaseModel>> CLASSES = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); static { try { for (Class<?> clazz : ClassScanner.findSubclasses(BaseModel.class)) { - CLASSES.put(clazz.getSimpleName(), clazz); + CLASSES.put(clazz.getSimpleName(), (Class<? extends BaseModel>) clazz); } } catch (IOException | ReflectiveOperationException | URISyntaxException e) { throw new RuntimeException(e); @@ -45,23 +45,25 @@ public class Permission { private final LinkedHashMap<String, Long> data; - private final Class<?> ownerClass; + private final Class<? extends BaseModel> ownerClass; private final long ownerId; - private final Class<?> propertyClass; + private final Class<? extends BaseModel> propertyClass; private final long propertyId; public Permission(LinkedHashMap<String, Long> data) { this.data = data; var iterator = data.entrySet().iterator(); var owner = iterator.next(); - ownerClass = CLASSES.get(owner.getKey().substring(0, owner.getKey().length() - 2)); + ownerClass = getKeyClass(owner.getKey()); ownerId = owner.getValue(); var property = iterator.next(); - propertyClass = CLASSES.get(property.getKey().substring(0, property.getKey().length() - 2)); + propertyClass = getKeyClass(property.getKey()); propertyId = property.getValue(); } - public Permission(Class<?> ownerClass, long ownerId, Class<?> propertyClass, long propertyId) { + public Permission( + Class<? extends BaseModel> ownerClass, long ownerId, + Class<? extends BaseModel> propertyClass, long propertyId) { this.ownerClass = ownerClass; this.ownerId = ownerId; this.propertyClass = propertyClass; @@ -71,7 +73,11 @@ public class Permission { data.put(getKey(propertyClass), propertyId); } - private static String getKey(Class<?> clazz) { + public static Class<? extends BaseModel> getKeyClass(String key) { + return CLASSES.get(key.substring(0, key.length() - 2)); + } + + public static String getKey(Class<?> clazz) { return Introspector.decapitalize(clazz.getSimpleName()) + "Id"; } @@ -105,7 +111,7 @@ public class Permission { @QueryIgnore @JsonIgnore - public Class<?> getOwnerClass() { + public Class<? extends BaseModel> getOwnerClass() { return ownerClass; } @@ -117,7 +123,7 @@ public class Permission { @QueryIgnore @JsonIgnore - public Class<?> getPropertyClass() { + public Class<? extends BaseModel> getPropertyClass() { return propertyClass; } diff --git a/src/main/java/org/traccar/model/Position.java b/src/main/java/org/traccar/model/Position.java index 348370e2c..6685cab95 100644 --- a/src/main/java/org/traccar/model/Position.java +++ b/src/main/java/org/traccar/model/Position.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2022 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. @@ -16,6 +16,8 @@ package org.traccar.model; import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonIgnore; import org.traccar.storage.QueryIgnore; @@ -40,7 +42,7 @@ public class Position extends Message { public static final String KEY_ODOMETER = "odometer"; // meters public static final String KEY_ODOMETER_SERVICE = "serviceOdometer"; // meters public static final String KEY_ODOMETER_TRIP = "tripOdometer"; // meters - public static final String KEY_HOURS = "hours"; + public static final String KEY_HOURS = "hours"; // milliseconds public static final String KEY_STEPS = "steps"; public static final String KEY_HEART_RATE = "heartRate"; public static final String KEY_INPUT = "input"; @@ -83,12 +85,14 @@ public class Position extends Message { public static final String KEY_OPERATOR = "operator"; public static final String KEY_COMMAND = "command"; public static final String KEY_BLOCKED = "blocked"; + public static final String KEY_LOCK = "lock"; public static final String KEY_DOOR = "door"; public static final String KEY_AXLE_WEIGHT = "axleWeight"; public static final String KEY_G_SENSOR = "gSensor"; public static final String KEY_ICCID = "iccid"; public static final String KEY_PHONE = "phone"; public static final String KEY_SPEED_LIMIT = "speedLimit"; + public static final String KEY_DRIVING_TIME = "drivingTime"; public static final String KEY_DTCS = "dtcs"; public static final String KEY_OBD_SPEED = "obdSpeed"; // knots @@ -97,6 +101,7 @@ public class Position extends Message { public static final String KEY_RESULT = "result"; public static final String KEY_DRIVER_UNIQUE_ID = "driverUniqueId"; + public static final String KEY_CARD = "card"; // Start with 1 not 0 public static final String PREFIX_TEMP = "temp"; @@ -150,7 +155,6 @@ public class Position extends Message { public Position(String protocol) { this.protocol = protocol; - this.serverTime = new Date(); } private String protocol; @@ -228,6 +232,9 @@ public class Position extends Message { } public void setLatitude(double latitude) { + if (latitude < -90 || latitude > 90) { + throw new IllegalArgumentException("Latitude out of range"); + } this.latitude = latitude; } @@ -238,6 +245,9 @@ public class Position extends Message { } public void setLongitude(double longitude) { + if (longitude < -180 || longitude > 180) { + throw new IllegalArgumentException("Longitude out of range"); + } this.longitude = longitude; } @@ -301,6 +311,20 @@ public class Position extends Message { this.network = network; } + private List<Long> geofenceIds; + + public List<Long> getGeofenceIds() { + return geofenceIds; + } + + public void setGeofenceIds(List<? extends Number> geofenceIds) { + if (geofenceIds != null) { + this.geofenceIds = geofenceIds.stream().map(Number::longValue).collect(Collectors.toList()); + } else { + this.geofenceIds = null; + } + } + @JsonIgnore @QueryIgnore @Override diff --git a/src/main/java/org/traccar/model/QueuedCommand.java b/src/main/java/org/traccar/model/QueuedCommand.java new file mode 100644 index 000000000..96a1eca4b --- /dev/null +++ b/src/main/java/org/traccar/model/QueuedCommand.java @@ -0,0 +1,46 @@ +/* + * Copyright 2022 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.model; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import org.traccar.storage.StorageName; + +import java.util.HashMap; + +@StorageName("tc_commands_queue") +@JsonIgnoreProperties(ignoreUnknown = true) +public class QueuedCommand extends BaseCommand { + + public static QueuedCommand fromCommand(Command command) { + QueuedCommand queuedCommand = new QueuedCommand(); + queuedCommand.setDeviceId(command.getDeviceId()); + queuedCommand.setType(command.getType()); + queuedCommand.setTextChannel(command.getTextChannel()); + queuedCommand.setAttributes(new HashMap<>(command.getAttributes())); + return queuedCommand; + } + + public Command toCommand() { + Command command = new Command(); + command.setDeviceId(getDeviceId()); + command.setType(getType()); + command.setDescription(""); + command.setTextChannel(getTextChannel()); + command.setAttributes(new HashMap<>(getAttributes())); + return command; + } + +} diff --git a/src/main/java/org/traccar/model/Report.java b/src/main/java/org/traccar/model/Report.java new file mode 100644 index 000000000..2ee7ae288 --- /dev/null +++ b/src/main/java/org/traccar/model/Report.java @@ -0,0 +1,55 @@ +/* + * Copyright 2022 - 2023 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.model; + +import org.traccar.storage.StorageName; + +@StorageName("tc_reports") +public class Report extends ExtendedModel implements Schedulable { + + private long calendarId; + + @Override + public long getCalendarId() { + return calendarId; + } + + @Override + public void setCalendarId(long calendarId) { + this.calendarId = calendarId; + } + + private String type; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + private String description; + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/src/main/java/org/traccar/model/ScheduledModel.java b/src/main/java/org/traccar/model/Schedulable.java index 9e6a4b9a6..331e77583 100644 --- a/src/main/java/org/traccar/model/ScheduledModel.java +++ b/src/main/java/org/traccar/model/Schedulable.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2018 - 2023 Anton Tananaev (anton@traccar.org) * Copyright 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,15 +16,7 @@ */ package org.traccar.model; -public class ScheduledModel extends ExtendedModel { - - private long calendarId; - - public long getCalendarId() { - return calendarId; - } - - public void setCalendarId(long calendarId) { - this.calendarId = calendarId; - } +public interface Schedulable { + long getCalendarId(); + void setCalendarId(long calendarId); } diff --git a/src/main/java/org/traccar/model/Server.java b/src/main/java/org/traccar/model/Server.java index b48e84939..6442186b6 100644 --- a/src/main/java/org/traccar/model/Server.java +++ b/src/main/java/org/traccar/model/Server.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2023 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. @@ -16,13 +16,13 @@ package org.traccar.model; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import org.traccar.Context; + import org.traccar.storage.QueryIgnore; import org.traccar.storage.StorageName; @StorageName("tc_servers") @JsonIgnoreProperties(ignoreUnknown = true) -public class Server extends ExtendedModel { +public class Server extends ExtendedModel implements UserRestrictions { private boolean registration; @@ -36,6 +36,7 @@ public class Server extends ExtendedModel { private boolean readonly; + @Override public boolean getReadonly() { return readonly; } @@ -46,6 +47,7 @@ public class Server extends ExtendedModel { private boolean deviceReadonly; + @Override public boolean getDeviceReadonly() { return deviceReadonly; } @@ -84,6 +86,16 @@ public class Server extends ExtendedModel { this.mapUrl = mapUrl; } + private String overlayUrl; + + public String getOverlayUrl() { + return overlayUrl; + } + + public void setOverlayUrl(String overlayUrl) { + this.overlayUrl = overlayUrl; + } + private double latitude; public double getLatitude() { @@ -146,6 +158,7 @@ public class Server extends ExtendedModel { private boolean limitCommands; + @Override public boolean getLimitCommands() { return limitCommands; } @@ -156,6 +169,7 @@ public class Server extends ExtendedModel { private boolean disableReports; + @Override public boolean getDisableReports() { return disableReports; } @@ -164,6 +178,17 @@ public class Server extends ExtendedModel { this.disableReports = disableReports; } + private boolean fixedEmail; + + @Override + public boolean getFixedEmail() { + return fixedEmail; + } + + public void setFixedEmail(boolean fixedEmail) { + this.fixedEmail = fixedEmail; + } + private String poiLayer; public String getPoiLayer() { @@ -189,9 +214,87 @@ public class Server extends ExtendedModel { return getClass().getPackage().getImplementationVersion(); } + private boolean emailEnabled; + + @QueryIgnore + public void setEmailEnabled(boolean emailEnabled) { + this.emailEnabled = emailEnabled; + } + @QueryIgnore public Boolean getEmailEnabled() { - return Context.getMailManager().getEmailEnabled(); + return emailEnabled; + } + + private boolean geocoderEnabled; + + private boolean textEnabled; + + @QueryIgnore + public void setTextEnabled(boolean textEnabled) { + this.textEnabled = textEnabled; + } + + @QueryIgnore + public Boolean getTextEnabled() { + return textEnabled; + } + + @QueryIgnore + public void setGeocoderEnabled(boolean geocoderEnabled) { + this.geocoderEnabled = geocoderEnabled; + } + + @QueryIgnore + public boolean getGeocoderEnabled() { + return geocoderEnabled; + } + + private long[] storageSpace; + + @QueryIgnore + public long[] getStorageSpace() { + return storageSpace; + } + + @QueryIgnore + public void setStorageSpace(long[] storageSpace) { + this.storageSpace = storageSpace; + } + + private boolean newServer; + + @QueryIgnore + public boolean getNewServer() { + return newServer; + } + + @QueryIgnore + public void setNewServer(boolean newServer) { + this.newServer = newServer; + } + + private boolean openIdEnabled; + + @QueryIgnore + public boolean getOpenIdEnabled() { + return openIdEnabled; + } + + @QueryIgnore + public void setOpenIdEnabled(boolean openIdEnabled) { + this.openIdEnabled = openIdEnabled; } + private boolean openIdForce; + + @QueryIgnore + public boolean getOpenIdForce() { + return openIdForce; + } + + @QueryIgnore + public void setOpenIdForce(boolean openIdForce) { + this.openIdForce = openIdForce; + } } diff --git a/src/main/java/org/traccar/model/User.java b/src/main/java/org/traccar/model/User.java index 464d0cbfe..0540f16d7 100644 --- a/src/main/java/org/traccar/model/User.java +++ b/src/main/java/org/traccar/model/User.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2022 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. @@ -17,7 +17,6 @@ package org.traccar.model; import com.fasterxml.jackson.annotation.JsonIgnore; -import org.traccar.storage.QueryExtended; import org.traccar.storage.QueryIgnore; import org.traccar.helper.Hashing; import org.traccar.storage.StorageName; @@ -25,7 +24,7 @@ import org.traccar.storage.StorageName; import java.util.Date; @StorageName("tc_users") -public class User extends ExtendedModel { +public class User extends ExtendedModel implements UserRestrictions, Disableable { private String name; @@ -64,11 +63,12 @@ public class User extends ExtendedModel { } public void setPhone(String phone) { - this.phone = phone; + this.phone = phone != null ? phone.trim() : null; } private boolean readonly; + @Override public boolean getReadonly() { return readonly; } @@ -79,6 +79,12 @@ public class User extends ExtendedModel { private boolean administrator; + @QueryIgnore + @JsonIgnore + public boolean getManager() { + return userLimit != 0; + } + public boolean getAdministrator() { return administrator; } @@ -149,20 +155,24 @@ public class User extends ExtendedModel { private boolean disabled; + @Override public boolean getDisabled() { return disabled; } + @Override public void setDisabled(boolean disabled) { this.disabled = disabled; } private Date expirationTime; + @Override public Date getExpirationTime() { return expirationTime; } + @Override public void setExpirationTime(Date expirationTime) { this.expirationTime = expirationTime; } @@ -189,6 +199,7 @@ public class User extends ExtendedModel { private boolean deviceReadonly; + @Override public boolean getDeviceReadonly() { return deviceReadonly; } @@ -197,25 +208,9 @@ public class User extends ExtendedModel { this.deviceReadonly = deviceReadonly; } - private String token; - - public String getToken() { - return token; - } - - public void setToken(String token) { - if (token != null && !token.isEmpty()) { - if (!token.matches("^[a-zA-Z0-9-]{16,}$")) { - throw new IllegalArgumentException("Illegal token"); - } - this.token = token; - } else { - this.token = null; - } - } - private boolean limitCommands; + @Override public boolean getLimitCommands() { return limitCommands; } @@ -224,10 +219,9 @@ public class User extends ExtendedModel { this.limitCommands = limitCommands; } - private String poiLayer; - private boolean disableReports; + @Override public boolean getDisableReports() { return disableReports; } @@ -236,6 +230,19 @@ public class User extends ExtendedModel { this.disableReports = disableReports; } + private boolean fixedEmail; + + @Override + public boolean getFixedEmail() { + return fixedEmail; + } + + public void setFixedEmail(boolean fixedEmail) { + this.fixedEmail = fixedEmail; + } + + private String poiLayer; + public String getPoiLayer() { return poiLayer; } @@ -261,12 +268,12 @@ public class User extends ExtendedModel { private String hashedPassword; @JsonIgnore - @QueryExtended + @QueryIgnore public String getHashedPassword() { return hashedPassword; } - @QueryExtended + @QueryIgnore public void setHashedPassword(String hashedPassword) { this.hashedPassword = hashedPassword; } @@ -274,12 +281,12 @@ public class User extends ExtendedModel { private String salt; @JsonIgnore - @QueryExtended + @QueryIgnore public String getSalt() { return salt; } - @QueryExtended + @QueryIgnore public void setSalt(String salt) { this.salt = salt; } diff --git a/src/main/java/org/traccar/model/UserRestrictions.java b/src/main/java/org/traccar/model/UserRestrictions.java new file mode 100644 index 000000000..1fcc5682e --- /dev/null +++ b/src/main/java/org/traccar/model/UserRestrictions.java @@ -0,0 +1,24 @@ +/* + * Copyright 2022 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.model; + +public interface UserRestrictions { + boolean getReadonly(); + boolean getDeviceReadonly(); + boolean getLimitCommands(); + boolean getDisableReports(); + boolean getFixedEmail(); +} diff --git a/src/main/java/org/traccar/model/WifiAccessPoint.java b/src/main/java/org/traccar/model/WifiAccessPoint.java index 87a77f3c0..e28c1b935 100644 --- a/src/main/java/org/traccar/model/WifiAccessPoint.java +++ b/src/main/java/org/traccar/model/WifiAccessPoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2022 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. @@ -17,6 +17,8 @@ package org.traccar.model; import com.fasterxml.jackson.annotation.JsonInclude; +import java.util.Objects; + @JsonInclude(JsonInclude.Include.NON_NULL) public class WifiAccessPoint { @@ -63,4 +65,23 @@ public class WifiAccessPoint { this.channel = channel; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + WifiAccessPoint that = (WifiAccessPoint) o; + return Objects.equals(macAddress, that.macAddress) + && Objects.equals(signalStrength, that.signalStrength) + && Objects.equals(channel, that.channel); + } + + @Override + public int hashCode() { + return Objects.hash(macAddress, signalStrength, channel); + } + } |