Update MFC SessionState from search result

This commit is contained in:
0xb00bface 2021-12-24 12:25:38 +01:00
parent 52bbf78e40
commit 1a0cc73ec3
1 changed files with 54 additions and 45 deletions

View File

@ -407,51 +407,6 @@ public class MyFreeCamsClient {
}
}
private void updateSessionState(SessionState newState) {
if (newState.getUid() <= 0) {
return;
}
SessionState storedState = sessionStates.getIfPresent(newState.getUid());
if (storedState != null) {
storedState.merge(newState);
updateModel(storedState);
} else {
lock.lock();
try {
sessionStates.put(newState.getUid(), newState);
updateModel(newState);
} finally {
lock.unlock();
}
}
}
private void updateModel(SessionState state) {
// essential data not yet available
if (state.getNm() == null || state.getM() == null || state.getU() == null || state.getU().getCamserv() == null
|| state.getU().getCamserv() == 0) {
return;
}
// tokens not yet available
if (ctxenc == null) {
return;
}
// uid not set, we can't identify this model
if (state.getUid() == null || state.getUid() <= 0) {
return;
}
MyFreeCamsModel model = models.getIfPresent(state.getUid());
if (model == null) {
model = mfc.createModel(state.getNm());
model.setUid(state.getUid());
models.put(state.getUid(), model);
}
model.update(state, getStreamUrl(state));
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
@ -461,6 +416,51 @@ public class MyFreeCamsClient {
return websocket;
}
private void updateSessionState(SessionState newState) {
if (newState.getUid() <= 0) {
return;
}
SessionState storedState = sessionStates.getIfPresent(newState.getUid());
if (storedState != null) {
storedState.merge(newState);
updateModel(storedState);
} else {
lock.lock();
try {
sessionStates.put(newState.getUid(), newState);
updateModel(newState);
} finally {
lock.unlock();
}
}
}
private void updateModel(SessionState state) {
// essential data not yet available
if (state.getNm() == null || state.getM() == null || state.getU() == null || state.getU().getCamserv() == null
|| state.getU().getCamserv() == 0) {
return;
}
// tokens not yet available
if (ctxenc == null) {
return;
}
// uid not set, we can't identify this model
if (state.getUid() == null || state.getUid() <= 0) {
return;
}
MyFreeCamsModel model = models.getIfPresent(state.getUid());
if (model == null) {
model = mfc.createModel(state.getNm());
model.setUid(state.getUid());
models.put(state.getUid(), model);
}
model.update(state, getStreamUrl(state));
}
private Message parseMessage(StringBuilder msgBuffer) {
int packetLengthBytes = 6;
if (msgBuffer.length() < packetLengthBytes) {
@ -676,6 +676,15 @@ public class MyFreeCamsClient {
LOG.trace("Search result: {}", msg);
if (StringUtil.isNotBlank(msg.getMessage()) && !Objects.equals(msg.getMessage(), q)) {
JSONObject json = new JSONObject(msg.getMessage());
JsonAdapter<SessionState> adapter = moshi.adapter(SessionState.class);
try {
SessionState sessionState = Objects.requireNonNull(adapter.fromJson(msg.getMessage()));
updateSessionState(sessionState);
} catch (Exception e) {
LOG.error("Couldn't parse session state message {}", msg, e);
}
String name = json.getString("nm");
MyFreeCamsModel model = mfc.createModel(name);
model.setUid(json.getInt("uid"));