Search in sources :

Example 1 with Calendar

use of org.traccar.model.Calendar 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;
}
Also used : Position(org.traccar.model.Position) HashMap(java.util.HashMap) Device(org.traccar.model.Device) Calendar(org.traccar.model.Calendar) ArrayList(java.util.ArrayList) Event(org.traccar.model.Event)

Example 2 with Calendar

use of org.traccar.model.Calendar in project traccar by tananaev.

the class CalendarTest method testCalendar.

@Test
public void testCalendar() throws IOException, ParserException, ParseException, SQLException {
    String calendarString = "BEGIN:VCALENDAR\n" + "PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN\n" + "VERSION:2.0\n" + "BEGIN:VTIMEZONE\n" + "TZID:Asia/Yekaterinburg\n" + "BEGIN:STANDARD\n" + "TZOFFSETFROM:+0500\n" + "TZOFFSETTO:+0500\n" + "TZNAME:YEKT\n" + "DTSTART:19700101T000000\n" + "END:STANDARD\n" + "END:VTIMEZONE\n" + "BEGIN:VEVENT\n" + "CREATED:20161213T045151Z\n" + "LAST-MODIFIED:20161213T045242Z\n" + "DTSTAMP:20161213T045242Z\n" + "UID:9d000df0-6354-479d-a407-218dac62c7c9\n" + "SUMMARY:Every night\n" + "RRULE:FREQ=DAILY\n" + "DTSTART;TZID=Asia/Yekaterinburg:20161130T230000\n" + "DTEND;TZID=Asia/Yekaterinburg:20161201T070000\n" + "TRANSP:OPAQUE\n" + "END:VEVENT\n" + "END:VCALENDAR";
    Calendar calendar = new Calendar();
    calendar.setData(calendarString.getBytes());
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ssX");
    Date date = format.parse("2016-12-13 22:59:59+05");
    assertTrue(!calendar.checkMoment(date));
    date = format.parse("2016-12-13 23:00:01+05");
    assertTrue(calendar.checkMoment(date));
    date = format.parse("2016-12-13 06:59:59+05");
    assertTrue(calendar.checkMoment(date));
    date = format.parse("2016-12-13 07:00:01+05");
    assertTrue(!calendar.checkMoment(date));
}
Also used : Calendar(org.traccar.model.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 3 with Calendar

use of org.traccar.model.Calendar in project traccar by tananaev.

the class NotificationManager method getEffectiveNotifications.

private Set<Long> getEffectiveNotifications(long userId, long deviceId, Date time) {
    Set<Long> result = new HashSet<>();
    Set<Long> deviceNotifications = getAllDeviceItems(deviceId);
    for (long itemId : getUserItems(userId)) {
        if (getById(itemId).getAlways() || deviceNotifications.contains(itemId)) {
            long calendarId = getById(itemId).getCalendarId();
            Calendar calendar = calendarId != 0 ? Context.getCalendarManager().getById(calendarId) : null;
            if (calendar == null || calendar.checkMoment(time)) {
                result.add(itemId);
            }
        }
    }
    return result;
}
Also used : Calendar(org.traccar.model.Calendar) HashSet(java.util.HashSet)

Aggregations

Calendar (org.traccar.model.Calendar)3 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 Device (org.traccar.model.Device)1 Event (org.traccar.model.Event)1 Position (org.traccar.model.Position)1