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;
}
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));
}
}
}
}
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);
}
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));
}
Aggregations