Esempio n. 1
0
 private void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {
   Device device = JsonConverter.objectFromJson(req.getReader(), new Device());
   Context.getPermissionsManager().checkDevice(getUserId(req), device.getId());
   Context.getDataManager().removeDevice(device);
   Context.getPermissionsManager().refresh();
   sendResponse(resp.getWriter(), true);
 }
Esempio n. 2
0
 @Path("{id}")
 @PUT
 public Response update(Geofence entity) throws SQLException {
   Context.getPermissionsManager().checkReadonly(getUserId());
   Context.getPermissionsManager().checkGeofence(getUserId(), entity.getId());
   Context.getGeofenceManager().updateGeofence(entity);
   return Response.ok(entity).build();
 }
Esempio n. 3
0
 @POST
 public Response add(Geofence entity) throws SQLException {
   Context.getPermissionsManager().checkReadonly(getUserId());
   Context.getDataManager().addGeofence(entity);
   Context.getDataManager().linkGeofence(getUserId(), entity.getId());
   Context.getGeofenceManager().refreshGeofences();
   return Response.ok(entity).build();
 }
Esempio n. 4
0
 private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
   Device device = JsonConverter.objectFromJson(req.getReader(), new Device());
   long userId = getUserId(req);
   Context.getDataManager().addDevice(device);
   Context.getDataManager().linkDevice(userId, device.getId());
   Context.getPermissionsManager().refresh();
   sendResponse(resp.getWriter(), JsonConverter.objectToJson(device));
 }
Esempio n. 5
0
 @Path("{id}")
 @DELETE
 public Response remove(@PathParam("id") long id) throws SQLException {
   Context.getPermissionsManager().checkReadonly(getUserId());
   Context.getPermissionsManager().checkGeofence(getUserId(), id);
   Context.getDataManager().removeGeofence(id);
   Context.getGeofenceManager().refreshGeofences();
   return Response.noContent().build();
 }
Esempio n. 6
0
  public AtrackProtocolDecoder(AtrackProtocol protocol) {
    super(protocol);

    longDate = Context.getConfig().getBoolean(getProtocolName() + ".longDate");

    custom = Context.getConfig().getBoolean(getProtocolName() + ".custom");
    form = Context.getConfig().getString(getProtocolName() + ".form");
    if (form != null) {
      custom = true;
    }
  }
Esempio n. 7
0
  public QueryBuilder setObject(Object object) throws SQLException {

    Method[] methods = object.getClass().getMethods();

    for (Method method : methods) {
      if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
        String name = method.getName().substring(3);
        try {
          if (method.getReturnType().equals(boolean.class)) {
            setBoolean(name, (Boolean) method.invoke(object));
          } else if (method.getReturnType().equals(int.class)) {
            setInteger(name, (Integer) method.invoke(object));
          } else if (method.getReturnType().equals(long.class)) {
            setLong(name, (Long) method.invoke(object));
          } else if (method.getReturnType().equals(double.class)) {
            setDouble(name, (Double) method.invoke(object));
          } else if (method.getReturnType().equals(String.class)) {
            setString(name, (String) method.invoke(object));
          } else if (method.getReturnType().equals(Date.class)) {
            setDate(name, (Date) method.invoke(object));
          } else if (method.getReturnType().equals(Map.class)) {
            if (Context.getConfig().getBoolean("database.xml")) {
              setString(name, MiscFormatter.toXmlString((Map) method.invoke(object)));
            } else {
              setString(name, MiscFormatter.toJsonString((Map) method.invoke(object)));
            }
          }
        } catch (IllegalAccessException | InvocationTargetException error) {
          Log.warning(error);
        }
      }
    }

    return this;
  }
Esempio n. 8
0
  @Override
  protected Collection<Event> analyzePosition(Position position) {

    Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId());
    if (device == null) {
      return null;
    }
    if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) {
      return null;
    }

    Collection<Event> result = null;
    double speed = position.getSpeed();
    double oldSpeed = 0;
    Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId());
    if (lastPosition != null) {
      oldSpeed = lastPosition.getSpeed();
    }
    try {
      if (speed > SPEED_THRESHOLD && oldSpeed <= SPEED_THRESHOLD) {
        result = new ArrayList<>();
        result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId()));
      } else if (speed <= SPEED_THRESHOLD && oldSpeed > SPEED_THRESHOLD) {
        result = new ArrayList<>();
        result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId()));
      }

      if (result != null && !result.isEmpty()) {
        for (Event event : result) {
          if (!Context.getDataManager()
              .getLastEvents(position.getDeviceId(), event.getType(), suppressRepeated)
              .isEmpty()) {
            event = null;
          }
        }
      }
    } catch (SQLException error) {
      Log.warning(error);
    }
    return result;
  }
Esempio n. 9
0
  @GET
  public Collection<Geofence> get(
      @QueryParam("all") boolean all,
      @QueryParam("userId") long userId,
      @QueryParam("groupId") long groupId,
      @QueryParam("deviceId") long deviceId,
      @QueryParam("refresh") boolean refresh)
      throws SQLException {

    GeofenceManager geofenceManager = Context.getGeofenceManager();
    if (refresh) {
      geofenceManager.refreshGeofences();
    }

    Set<Long> result;
    if (all) {
      Context.getPermissionsManager().checkAdmin(getUserId());
      result = new HashSet<>(geofenceManager.getAllGeofencesIds());
    } else {
      if (userId == 0) {
        userId = getUserId();
      }
      Context.getPermissionsManager().checkUser(getUserId(), userId);
      result = new HashSet<>(geofenceManager.getUserGeofencesIds(userId));
    }

    if (groupId != 0) {
      Context.getPermissionsManager().checkGroup(getUserId(), groupId);
      result.retainAll(geofenceManager.getGroupGeofencesIds(groupId));
    }

    if (deviceId != 0) {
      Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
      result.retainAll(geofenceManager.getDeviceGeofencesIds(deviceId));
    }
    return geofenceManager.getGeofences(result);
  }
Esempio n. 10
0
  public final void refresh() {
    if (dataManager != null) {
      try {
        groupGeofencesLock.writeLock().lock();
        deviceGeofencesLock.writeLock().lock();
        try {
          groupGeofences.clear();
          for (GroupGeofence groupGeofence : dataManager.getGroupGeofences()) {
            getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId());
          }

          deviceGeofences.clear();
          deviceGeofencesWithGroups.clear();

          for (DeviceGeofence deviceGeofence : dataManager.getDeviceGeofences()) {
            getDeviceGeofences(deviceGeofences, deviceGeofence.getDeviceId())
                .add(deviceGeofence.getGeofenceId());
            getDeviceGeofences(deviceGeofencesWithGroups, deviceGeofence.getDeviceId())
                .add(deviceGeofence.getGeofenceId());
          }

          for (Device device : dataManager.getAllDevicesCached()) {
            long groupId = device.getGroupId();
            while (groupId != 0) {
              getDeviceGeofences(deviceGeofencesWithGroups, device.getId())
                  .addAll(getGroupGeofences(groupId));
              if (dataManager.getGroupById(groupId) != null) {
                groupId = dataManager.getGroupById(groupId).getGroupId();
              } else {
                groupId = 0;
              }
            }
            List<Long> deviceGeofenceIds = device.getGeofenceIds();
            if (deviceGeofenceIds == null) {
              deviceGeofenceIds = new ArrayList<>();
            } else {
              deviceGeofenceIds.clear();
            }
            Position lastPosition = Context.getConnectionManager().getLastPosition(device.getId());
            if (lastPosition != null && deviceGeofencesWithGroups.containsKey(device.getId())) {
              for (long geofenceId : deviceGeofencesWithGroups.get(device.getId())) {
                Geofence geofence = getGeofence(geofenceId);
                if (geofence != null
                    && geofence
                        .getGeometry()
                        .containsPoint(lastPosition.getLatitude(), lastPosition.getLongitude())) {
                  deviceGeofenceIds.add(geofenceId);
                }
              }
            }
            device.setGeofenceIds(deviceGeofenceIds);
          }

        } finally {
          groupGeofencesLock.writeLock().unlock();
          deviceGeofencesLock.writeLock().unlock();
        }

      } catch (SQLException error) {
        Log.warning(error);
      }
    }
  }
  @Override
  protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg)
      throws Exception {

    ChannelBuffer buf = (ChannelBuffer) msg;

    buf.readUnsignedByte(); // marker
    int version = buf.readUnsignedByte();

    String imei;
    if ((version & 0x80) != 0) {
      imei = String.valueOf((buf.readUnsignedInt() << (3 * 8)) | buf.readUnsignedMedium());
    } else {
      imei = String.valueOf(imeiFromUnitId(buf.readUnsignedMedium()));
    }

    buf.readUnsignedShort(); // length

    int selector = DEFAULT_SELECTOR;
    if ((version & 0x40) != 0) {
      selector = buf.readUnsignedMedium();
    }

    Position position = new Position();
    position.setProtocol(getProtocolName());
    if (!identify(imei, channel, remoteAddress)) {
      return null;
    }

    position.setDeviceId(getDeviceId());

    int event = buf.readUnsignedByte();
    position.set(Event.KEY_EVENT, event);
    position.set("event-info", buf.readUnsignedByte());

    if ((selector & 0x0008) != 0) {
      position.setValid((buf.readUnsignedByte() & 0x40) != 0);
    } else {
      return null; // no location data
    }

    if ((selector & 0x0004) != 0) {
      buf.skipBytes(4); // snapshot time
    }

    if ((selector & 0x0008) != 0) {
      position.setTime(new Date(buf.readUnsignedInt() * 1000));
      position.setLatitude(buf.readInt() / 1000000.0);
      position.setLongitude(buf.readInt() / 1000000.0);
      position.set(Event.KEY_SATELLITES, buf.readUnsignedByte());
    }

    if ((selector & 0x0010) != 0) {
      position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
      buf.readUnsignedByte(); // maximum speed
      position.setCourse(buf.readUnsignedByte() * 2.0);
    }

    if ((selector & 0x0040) != 0) {
      position.set(Event.KEY_INPUT, buf.readUnsignedByte());
    }

    if ((selector & 0x0020) != 0) {
      position.set(Event.PREFIX_ADC + 1, buf.readUnsignedShort());
      position.set(Event.PREFIX_ADC + 2, buf.readUnsignedShort());
      position.set(Event.PREFIX_ADC + 3, buf.readUnsignedShort());
      position.set(Event.PREFIX_ADC + 4, buf.readUnsignedShort());
    }

    if ((selector & 0x8000) != 0) {
      position.set(Event.KEY_POWER, buf.readUnsignedShort() / 1000.0);
      position.set(Event.KEY_BATTERY, buf.readUnsignedShort());
    }

    // Pulse rate 1
    if ((selector & 0x10000) != 0) {
      buf.readUnsignedShort();
      buf.readUnsignedInt();
    }

    // Pulse rate 2
    if ((selector & 0x20000) != 0) {
      buf.readUnsignedShort();
      buf.readUnsignedInt();
    }

    if ((selector & 0x0080) != 0) {
      position.set("trip1", buf.readUnsignedInt());
    }

    if ((selector & 0x0100) != 0) {
      position.set("trip2", buf.readUnsignedInt());
    }

    if ((selector & 0x0040) != 0) {
      position.set(Event.KEY_OUTPUT, buf.readUnsignedByte());
    }

    if ((selector & 0x0200) != 0) {
      position.set(
          Event.KEY_RFID, (((long) buf.readUnsignedShort()) << 32) + buf.readUnsignedInt());
    }

    if ((selector & 0x0400) != 0) {
      buf.readUnsignedByte(); // Keypad
    }

    if ((selector & 0x0800) != 0) {
      position.setAltitude(buf.readShort());
    }

    if ((selector & 0x2000) != 0) {
      buf.readUnsignedShort(); // snapshot counter
    }

    if ((selector & 0x4000) != 0) {
      buf.skipBytes(8); // state flags
    }

    if ((selector & 0x80000) != 0) {
      buf.skipBytes(11); // cell info
    }

    if ((selector & 0x1000) != 0) {
      decodeEventData(event, buf);
    }

    if (Context.getConfig().getBoolean(getProtocolName() + ".can")
        && buf.readable()
        && (selector & 0x1000) != 0
        && event == EVENT_DATA) {

      decodeCanData(buf, position);
    }

    return position;
  }
Esempio n. 12
0
 private void get(HttpServletRequest req, HttpServletResponse resp) throws Exception {
   sendResponse(
       resp.getWriter(),
       JsonConverter.arrayToJson(Context.getDataManager().getDevices(getUserId(req))));
 }
Esempio n. 13
0
 public MotionEventHandler() {
   suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60);
 }