Skip to content

Commit

Permalink
Merge branch 'traccar:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-wolf-gb authored Mar 3, 2024
2 parents b85988a + 75b404d commit eca1638
Show file tree
Hide file tree
Showing 11 changed files with 474 additions and 297 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/traccar/api/resource/ReportResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.traccar.model.Report;
import org.traccar.model.UserRestrictions;
import org.traccar.reports.CombinedReportProvider;
import org.traccar.reports.DevicesReportProvider;
import org.traccar.reports.EventsReportProvider;
import org.traccar.reports.RouteReportProvider;
import org.traccar.reports.StopsReportProvider;
Expand Down Expand Up @@ -77,6 +78,9 @@ public class ReportResource extends SimpleObjectResource<Report> {
@Inject
private TripsReportProvider tripsReportProvider;

@Inject
private DevicesReportProvider devicesReportProvider;

@Inject
private ReportMailer reportMailer;

Expand Down Expand Up @@ -319,4 +323,15 @@ public Response getStopsExcel(
return getStopsExcel(deviceIds, groupIds, from, to, type.equals("mail"));
}

@Path("devices/{type:xlsx|mail}")
@GET
@Produces(EXCEL)
public Response geDevicesExcel(
@PathParam("type") String type) throws StorageException {
permissionsService.checkRestriction(getUserId(), UserRestrictions::getDisableReports);
return executeReport(getUserId(), type.equals("mail"), stream -> {
devicesReportProvider.getExcel(stream, getUserId());
});
}

}
524 changes: 256 additions & 268 deletions src/main/java/org/traccar/protocol/Gl200TextProtocolDecoder.java

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions src/main/java/org/traccar/protocol/KhdProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.helper.BufferUtil;
import org.traccar.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
Expand Down Expand Up @@ -169,7 +170,9 @@ protected Object decode(
position.set(Position.KEY_FUEL_LEVEL, BitUtil.from(odometer, 16));
}

position.set(Position.KEY_STATUS, buf.readUnsignedInt());
long status = buf.readUnsignedInt();
position.set(Position.KEY_IGNITION, BitUtil.check(status, 7 + 3 * 8));
position.set(Position.KEY_STATUS, status);

buf.readUnsignedShort();
buf.readUnsignedByte();
Expand All @@ -185,8 +188,7 @@ protected Object decode(
buf.readUnsignedShort(); // data length

int dataType = buf.readUnsignedByte();

buf.readUnsignedByte(); // content length
int dataLength = buf.readUnsignedByte();

switch (dataType) {
case 0x01:
Expand All @@ -197,6 +199,20 @@ protected Object decode(
position.set(Position.PREFIX_TEMP + 1,
buf.readUnsignedByte() * 100 + buf.readUnsignedByte());
break;
case 0x05:
int sign = buf.readUnsignedByte();
switch (sign) {
case 1:
position.set("sign", true);
break;
case 2:
position.set("sign", false);
break;
default:
break;
}
position.set(Position.KEY_DRIVER_UNIQUE_ID, BufferUtil.readString(buf, dataLength - 1));
break;
case 0x18:
for (int i = 1; i <= 4; i++) {
double value = buf.readUnsignedShort();
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/traccar/protocol/Tlt2hProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ public Tlt2hProtocolDecoder(Protocol protocol) {
.text("#")
.number("(?:(dd|dddd)|x*)") // cell or voltage
.groupBegin()
.number("#(d+),") // mcc
.text("#")
.groupBegin()
.number("(d+),") // mcc
.number("(d+),") // mnc
.number("(x+),") // lac
.number("(x+)") // cell id
.groupEnd("?")
.groupEnd("?")
.text("$GPRMC,")
.number("(?:(dd)(dd)(dd).d+)?,") // time (hhmmss.sss)
.expression("([AVL]),") // validity
Expand Down
51 changes: 35 additions & 16 deletions src/main/java/org/traccar/protocol/Xrb28ProtocolDecoder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Anton Tananaev (anton@traccar.org)
* Copyright 2018 - 2024 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.
Expand Down Expand Up @@ -27,6 +27,7 @@
import org.traccar.model.Position;

import java.net.SocketAddress;
import java.util.Arrays;
import java.util.regex.Pattern;

public class Xrb28ProtocolDecoder extends BaseProtocolDecoder {
Expand All @@ -46,6 +47,7 @@ public Xrb28ProtocolDecoder(Protocol protocol) {
.expression("....,")
.expression("..,") // vendor
.number("d{15},") // imei
.number("d{12},").optional() // time
.expression("..,") // type
.number("[01],") // reserved
.number("(dd)(dd)(dd).d+,") // time (hhmmss)
Expand All @@ -67,23 +69,44 @@ protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

String sentence = (String) msg;
String[] values = sentence.replaceAll("#$", "").split(",");

DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(9, 24));
int index = 0;
String header = values[index++];
String vendor = values[index++];

String imei = values[index++];
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
if (deviceSession == null) {
return null;
}

String type = sentence.substring(25, 27);
String time;
if (values[index].length() == 12) {
time = values[index++];
} else {
time = null;
}

String type = values[index++];
if (channel != null) {
StringBuilder response = new StringBuilder("\u00ff\u00ff");
response.append(header.replaceAll("R$", "S")).append(',');
response.append(vendor).append(',');
response.append(imei).append(',');
if (time != null) {
response.append(time).append(',');
}
if (type.matches("L0|L1|W0|E1")) {
channel.write(new NetworkMessage(
"\u00ff\u00ff*SCOS" + sentence.substring(5, 27) + "#\n",
remoteAddress));
response.append(type).append("#\n");
channel.write(new NetworkMessage(response.toString(), remoteAddress));
} else if (type.equals("R0") && pendingCommand != null) {
String command = pendingCommand.equals(Command.TYPE_ALARM_ARM) ? "L1," : "L0,";
channel.write(new NetworkMessage(
"\u00ff\u00ff*SCOS" + sentence.substring(5, 25) + command + sentence.substring(30) + "\n",
remoteAddress));
String command = pendingCommand.equals(Command.TYPE_ALARM_ARM) ? "L1" : "L0";
response.append(command);
String[] remaining = Arrays.copyOfRange(values, index, values.length);
response.append(String.join(",", remaining));
response.append("#\n");
channel.write(new NetworkMessage(response.toString(), remoteAddress));
pendingCommand = null;
}
}
Expand All @@ -95,11 +118,6 @@ protected Object decode(

getLastLocation(position, null);

String payload = sentence.substring(25, sentence.length() - 1);

int index = 0;
String[] values = payload.substring(3).split(",");

switch (type) {
case "Q0":
position.set(Position.KEY_BATTERY, Integer.parseInt(values[index++]) * 0.01);
Expand Down Expand Up @@ -146,7 +164,8 @@ protected Object decode(
case "K0":
case "I0":
case "M0":
position.set(Position.KEY_RESULT, payload);
String[] remaining = Arrays.copyOfRange(values, index, values.length);
position.set(Position.KEY_RESULT, String.join(",", remaining));
break;
default:
break;
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/org/traccar/reports/DevicesReportProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2024 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.reports;

import jakarta.inject.Inject;
import org.jxls.util.JxlsHelper;
import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.helper.model.PositionUtil;
import org.traccar.model.Device;
import org.traccar.model.Message;
import org.traccar.model.User;
import org.traccar.reports.common.ReportUtils;
import org.traccar.reports.model.DeviceReportItem;
import org.traccar.storage.Storage;
import org.traccar.storage.StorageException;
import org.traccar.storage.query.Columns;
import org.traccar.storage.query.Condition;
import org.traccar.storage.query.Request;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.stream.Collectors;

public class DevicesReportProvider {

private final Config config;
private final ReportUtils reportUtils;
private final Storage storage;

@Inject
public DevicesReportProvider(Config config, ReportUtils reportUtils, Storage storage) {
this.config = config;
this.reportUtils = reportUtils;
this.storage = storage;
}

public Collection<DeviceReportItem> getObjects(long userId) throws StorageException {

var positions = PositionUtil.getLatestPositions(storage, userId).stream()
.collect(Collectors.toMap(Message::getDeviceId, p -> p));

return storage.getObjects(Device.class, new Request(
new Columns.All(),
new Condition.Permission(User.class, userId, Device.class))).stream()
.map(device -> new DeviceReportItem(device, positions.get(device.getId())))
.collect(Collectors.toUnmodifiableList());
}

public void getExcel(OutputStream outputStream, long userId) throws StorageException, IOException {

File file = Paths.get(config.getString(Keys.TEMPLATES_ROOT), "export", "devices.xlsx").toFile();
try (InputStream inputStream = new FileInputStream(file)) {
var context = reportUtils.initializeContext(userId);
context.putVar("items", getObjects(userId));
JxlsHelper.getInstance().setUseFastFormulaProcessor(false)
.processTemplate(inputStream, outputStream, context);
}
}
}
48 changes: 48 additions & 0 deletions src/main/java/org/traccar/reports/model/DeviceReportItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2024 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.reports.model;

import org.traccar.model.Device;
import org.traccar.model.Position;

public class DeviceReportItem {

public DeviceReportItem(Device device, Position position) {
this.device = device;
this.position = position;
}

private Device device;

public Device getDevice() {
return device;
}

public void setDevice(Device device) {
this.device = device;
}

private Position position;

public Position getPosition() {
return position;
}

public void setPosition(Position position) {
this.position = position;
}

}
Loading

0 comments on commit eca1638

Please sign in to comment.