Search in sources :

Example 1 with OnlineDevice

use of org.mx.comps.notify.online.OnlineDevice in project main by JohnPeng739.

the class OnlineManagerSimpleImpl method registryDevice.

/**
 * {@inheritDoc}
 *
 * @see OnlineManager#registryDevice(OnlineDevice)
 */
@Override
public boolean registryDevice(OnlineDevice onlineDevice) {
    // TODO 处理终端认证
    synchronized (OnlineManagerSimpleImpl.this.onlineDeviceMutex) {
        String key = String.format("%s@%s", onlineDevice.getDeviceId(), onlineDevice.getConnectKey());
        if (onlineDevices.containsKey(key)) {
            OnlineDevice device = onlineDevices.get(key);
            device.setRegistryTime(System.currentTimeMillis());
            device.update(onlineDevice.getState(), System.currentTimeMillis(), onlineDevice.getLastLongitude(), onlineDevice.getLastLatitude());
            onlineDevices.put(key, device);
        } else {
            onlineDevice.setRegistryTime(System.currentTimeMillis());
            onlineDevice.setLastTime(System.currentTimeMillis());
            onlineDevices.put(key, onlineDevice);
        }
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Registry [%s] successfully.", key));
        }
    }
    return true;
}
Also used : OnlineDevice(org.mx.comps.notify.online.OnlineDevice)

Example 2 with OnlineDevice

use of org.mx.comps.notify.online.OnlineDevice in project main by JohnPeng739.

the class OnlineManagerSimpleImpl method pingDevice.

/**
 * {@inheritDoc}
 *
 * @see OnlineManager#pingDevice(OnlineDevice)
 */
@Override
public void pingDevice(OnlineDevice onlineDevice) {
    synchronized (OnlineManagerSimpleImpl.this.onlineDeviceMutex) {
        String key = String.format("%s@%s", onlineDevice.getDeviceId(), onlineDevice.getConnectKey());
        if (onlineDevices.containsKey(key)) {
            OnlineDevice device = onlineDevices.get(key);
            device.update(onlineDevice.getState(), System.currentTimeMillis(), onlineDevice.getLastLongitude(), onlineDevice.getLastLatitude());
            onlineDevices.put(key, device);
            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Pong [%s] successfully.", key));
            }
        }
    }
}
Also used : OnlineDevice(org.mx.comps.notify.online.OnlineDevice)

Example 3 with OnlineDevice

use of org.mx.comps.notify.online.OnlineDevice in project main by JohnPeng739.

the class DeviceCommandProcessor method processJsonCommand.

/**
 * {@inheritDoc}
 *
 * @see MessageProcessor#processJsonCommand(Session, JSONObject)
 */
@Override
public boolean processJsonCommand(Session session, JSONObject json) {
    String command = json.getString("command");
    String type = json.getString("type");
    JSONObject data = json.getJSONObject("data");
    String ip = TypeUtils.byteArray2Ip(session.getRemoteAddress().getAddress().getAddress());
    int port = session.getRemoteAddress().getPort();
    OnlineDevice device = new OnlineDevice();
    device.setDeviceId(data.getString("deviceId"));
    device.setState(data.getString("state"));
    device.setConnectKey(String.format("%s:%d", ip, port));
    device.setLastTime(data.getLongValue("lastTime"));
    device.setLastLongitude(data.getDoubleValue("lastLongitude"));
    device.setLastLatitude(data.getDoubleValue("lastLatitude"));
    return processCommand(command, type, device);
}
Also used : OnlineDevice(org.mx.comps.notify.online.OnlineDevice) JSONObject(com.alibaba.fastjson.JSONObject)

Example 4 with OnlineDevice

use of org.mx.comps.notify.online.OnlineDevice in project main by JohnPeng739.

the class NotifyServerResource method getOnlineDevices.

/**
 * 获取符合条件的在线设备列表
 *
 * @param state      指定的状态
 * @param later      注册时间晚于指定时间
 * @param early      注册时间早于指定时间
 * @param pagination 分页对象
 * @return 在线设备列表
 */
@Path("onlines")
@POST
public PaginationDataVO<List<OnlineDeviceVO>> getOnlineDevices(@QueryParam("state") String state, @QueryParam("later") long later, @QueryParam("early") long early, Pagination pagination) {
    List<Predicate<OnlineDevice>> filters = new ArrayList<>();
    if (!StringUtils.isBlank(state)) {
        filters.add(onlineDevice -> state.equals(onlineDevice.getState()));
    }
    if (later > 0) {
        filters.add(onlineDevice -> onlineDevice.getRegistryTime() >= later);
    }
    if (early > 0) {
        filters.add(onlineDevice -> onlineDevice.getRegistryTime() <= early);
    }
    Set<OnlineDevice> set = onlineManager.getOnlineDevices(filters);
    if (pagination == null) {
        pagination = new Pagination();
    }
    List<OnlineDevice> list = new ArrayList<>();
    list.addAll(set);
    // 按照注册时间排序
    list.sort((od1, od2) -> (int) (od1.getRegistryTime() - od2.getRegistryTime()));
    pagination.setTotal(set.size());
    List<OnlineDevice> result = new ArrayList<>();
    int skip = (pagination.getPage() - 1) * pagination.getSize();
    int num = Math.min(pagination.getSize(), list.size() - skip);
    for (int index = skip; index < skip + num; index++) {
        result.add(list.get(index));
    }
    return new PaginationDataVO<>(pagination, OnlineDeviceVO.transform(result));
}
Also used : Pagination(org.mx.dal.Pagination) OnlineDevice(org.mx.comps.notify.online.OnlineDevice) PaginationDataVO(org.mx.service.rest.vo.PaginationDataVO) ArrayList(java.util.ArrayList) Predicate(java.util.function.Predicate)

Aggregations

OnlineDevice (org.mx.comps.notify.online.OnlineDevice)4 JSONObject (com.alibaba.fastjson.JSONObject)1 ArrayList (java.util.ArrayList)1 Predicate (java.util.function.Predicate)1 Pagination (org.mx.dal.Pagination)1 PaginationDataVO (org.mx.service.rest.vo.PaginationDataVO)1