use of org.traccar.model.Device in project traccar by tananaev.
the class DeviceManager method lookupAttribute.
private Object lookupAttribute(long deviceId, String attributeName, boolean lookupConfig) {
Object result = null;
Device device = getById(deviceId);
if (device != null) {
result = device.getAttributes().get(attributeName);
if (result == null && lookupGroupsAttribute) {
long groupId = device.getGroupId();
while (groupId != 0) {
Group group = Context.getGroupsManager().getById(groupId);
if (group != null) {
result = group.getAttributes().get(attributeName);
if (result != null) {
break;
}
groupId = group.getGroupId();
} else {
groupId = 0;
}
}
}
if (result == null) {
if (lookupConfig) {
result = Context.getConfig().getString(attributeName);
} else {
Server server = Context.getPermissionsManager().getServer();
result = server.getAttributes().get(attributeName);
}
}
}
return result;
}
use of org.traccar.model.Device in project traccar by tananaev.
the class ExtendedObjectManager method refreshExtendedPermissions.
public void refreshExtendedPermissions() {
if (getDataManager() != null) {
try {
Collection<Permission> databaseGroupPermissions = getDataManager().getPermissions(Group.class, getBaseClass());
groupItems.clear();
for (Permission groupPermission : databaseGroupPermissions) {
getGroupItems(groupPermission.getOwnerId()).add(groupPermission.getPropertyId());
}
Collection<Permission> databaseDevicePermissions = getDataManager().getPermissions(Device.class, getBaseClass());
deviceItems.clear();
deviceItemsWithGroups.clear();
for (Permission devicePermission : databaseDevicePermissions) {
getDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
getAllDeviceItems(devicePermission.getOwnerId()).add(devicePermission.getPropertyId());
}
for (Device device : Context.getDeviceManager().getAllDevices()) {
long groupId = device.getGroupId();
while (groupId != 0) {
getAllDeviceItems(device.getId()).addAll(getGroupItems(groupId));
Group group = Context.getGroupsManager().getById(groupId);
if (group != null) {
groupId = group.getGroupId();
} else {
groupId = 0;
}
}
}
} catch (SQLException | ClassNotFoundException error) {
Log.warning(error);
}
}
}
use of org.traccar.model.Device in project traccar by tananaev.
the class GeofenceEventHandler method analyzePosition.
@Override
protected Map<Event, Position> analyzePosition(Position position) {
Device device = Context.getIdentityManager().getById(position.getDeviceId());
if (device == null) {
return null;
}
if (!Context.getIdentityManager().isLatestPosition(position) || !position.getValid()) {
return null;
}
List<Long> currentGeofences = geofenceManager.getCurrentDeviceGeofences(position);
List<Long> oldGeofences = new ArrayList<>();
if (device.getGeofenceIds() != null) {
oldGeofences.addAll(device.getGeofenceIds());
}
List<Long> newGeofences = new ArrayList<>(currentGeofences);
newGeofences.removeAll(oldGeofences);
oldGeofences.removeAll(currentGeofences);
device.setGeofenceIds(currentGeofences);
Map<Event, Position> events = new HashMap<>();
for (long geofenceId : newGeofences) {
long calendarId = geofenceManager.getById(geofenceId).getCalendarId();
Calendar calendar = calendarId != 0 ? Context.getCalendarManager().getById(calendarId) : null;
if (calendar == null || calendar.checkMoment(position.getFixTime())) {
Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId());
event.setGeofenceId(geofenceId);
events.put(event, position);
}
}
for (long geofenceId : oldGeofences) {
long calendarId = geofenceManager.getById(geofenceId).getCalendarId();
Calendar calendar = calendarId != 0 ? Context.getCalendarManager().getById(calendarId) : null;
if (calendar == null || calendar.checkMoment(position.getFixTime())) {
Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId());
event.setGeofenceId(geofenceId);
events.put(event, position);
}
}
return events;
}
use of org.traccar.model.Device in project traccar by tananaev.
the class Gt06ProtocolDecoder method decodeExtended.
private Object decodeExtended(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) throws Exception {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
if (deviceSession.getTimeZone() == null) {
deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId()));
}
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
// length
buf.readUnsignedShort();
int type = buf.readUnsignedByte();
if (type == MSG_STRING_INFO) {
// server flag
buf.readUnsignedInt();
String data;
if (buf.readUnsignedByte() == 1) {
data = buf.readBytes(buf.readableBytes() - 6).toString(StandardCharsets.US_ASCII);
} else {
data = buf.readBytes(buf.readableBytes() - 6).toString(StandardCharsets.UTF_16BE);
}
if (decodeLocationString(position, data) == null) {
getLastLocation(position, null);
position.set(Position.KEY_RESULT, data);
}
return position;
} else if (type == MSG_INFO) {
int subType = buf.readUnsignedByte();
getLastLocation(position, null);
if (subType == 0x00) {
position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);
return position;
} else if (subType == 0x05) {
int flags = buf.readUnsignedByte();
position.set(Position.KEY_DOOR, BitUtil.check(flags, 0));
position.set(Position.PREFIX_IO + 1, BitUtil.check(flags, 2));
return position;
} else if (subType == 0x0d) {
buf.skipBytes(6);
return decodeFuelData(position, buf.toString(buf.readerIndex(), buf.readableBytes() - 4 - 2, StandardCharsets.US_ASCII));
}
} else if (type == MSG_X1_PHOTO_DATA) {
int pictureId = buf.readInt();
ChannelBuffer photo = photos.get(pictureId);
// offset
buf.readUnsignedInt();
buf.readBytes(photo, buf.readUnsignedShort());
if (photo.writableBytes() > 0) {
sendPhotoRequest(channel, pictureId);
} else {
Device device = Context.getDeviceManager().getById(deviceSession.getDeviceId());
position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(device.getUniqueId(), photo, "jpg"));
photos.remove(pictureId);
}
} else if (type == MSG_AZ735_GPS || type == MSG_AZ735_ALARM) {
if (!decodeGps(position, buf, true, deviceSession.getTimeZone())) {
getLastLocation(position, position.getDeviceTime());
}
if (decodeLbs(position, buf, true)) {
position.set(Position.KEY_RSSI, buf.readUnsignedByte());
}
// additional cell towers
buf.skipBytes(buf.readUnsignedByte());
// wifi access point
buf.skipBytes(buf.readUnsignedByte());
int status = buf.readUnsignedByte();
position.set(Position.KEY_STATUS, status);
if (type == MSG_AZ735_ALARM) {
switch(status) {
case 0xA0:
position.set(Position.KEY_ARMED, true);
break;
case 0xA1:
position.set(Position.KEY_ARMED, false);
break;
case 0xA2:
case 0xA3:
position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY);
break;
case 0xA4:
position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
break;
case 0xA5:
position.set(Position.KEY_ALARM, Position.ALARM_DOOR);
break;
default:
break;
}
}
// reserved extension
buf.skipBytes(buf.readUnsignedByte());
sendResponse(channel, true, type, buf.getShort(buf.writerIndex() - 6), null);
return position;
}
return null;
}
use of org.traccar.model.Device in project traccar by tananaev.
the class Events method getExcel.
public static void getExcel(OutputStream outputStream, long userId, Collection<Long> deviceIds, Collection<Long> groupIds, Collection<String> types, Date from, Date to) throws SQLException, IOException {
ReportUtils.checkPeriodLimit(from, to);
ArrayList<DeviceReport> devicesEvents = new ArrayList<>();
ArrayList<String> sheetNames = new ArrayList<>();
HashMap<Long, String> geofenceNames = new HashMap<>();
for (long deviceId : ReportUtils.getDeviceList(deviceIds, groupIds)) {
Context.getPermissionsManager().checkDevice(userId, deviceId);
Collection<Event> events = Context.getDataManager().getEvents(deviceId, from, to);
boolean all = types.isEmpty() || types.contains(Event.ALL_EVENTS);
for (Iterator<Event> iterator = events.iterator(); iterator.hasNext(); ) {
Event event = iterator.next();
if (all || types.contains(event.getType())) {
long geofenceId = event.getGeofenceId();
if (geofenceId != 0) {
if (Context.getGeofenceManager().checkItemPermission(userId, geofenceId)) {
Geofence geofence = Context.getGeofenceManager().getById(geofenceId);
if (geofence != null) {
geofenceNames.put(geofenceId, geofence.getName());
}
} else {
iterator.remove();
}
}
} else {
iterator.remove();
}
}
DeviceReport deviceEvents = new DeviceReport();
Device device = Context.getIdentityManager().getById(deviceId);
deviceEvents.setDeviceName(device.getName());
sheetNames.add(WorkbookUtil.createSafeSheetName(deviceEvents.getDeviceName()));
if (device.getGroupId() != 0) {
Group group = Context.getGroupsManager().getById(device.getGroupId());
if (group != null) {
deviceEvents.setGroupName(group.getName());
}
}
deviceEvents.setObjects(events);
devicesEvents.add(deviceEvents);
}
String templatePath = Context.getConfig().getString("report.templatesPath", "templates/export/");
try (InputStream inputStream = new FileInputStream(templatePath + "/events.xlsx")) {
org.jxls.common.Context jxlsContext = ReportUtils.initializeContext(userId);
jxlsContext.putVar("devices", devicesEvents);
jxlsContext.putVar("sheetNames", sheetNames);
jxlsContext.putVar("geofenceNames", geofenceNames);
jxlsContext.putVar("from", from);
jxlsContext.putVar("to", to);
ReportUtils.processTemplateWithSheets(inputStream, outputStream, jxlsContext);
}
}
Aggregations