Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class MyWebSocketClient extends WebSocketClient {
Handler handler;
Expand All @@ -36,6 +35,7 @@ public MyWebSocketClient(Handler handler, TextView mowerLog, Context context, UR

@Override
public void onOpen(ServerHandshake handshakedata) {
Log.d("WebSocket", "Connection opened");
}

@Override
Expand All @@ -45,32 +45,42 @@ public void onClose(int code, String reason, boolean remote) {

@Override
public void onMessage(String message) {
String[] msg_lines = message.split("\n");
String regex = ".*到(\\d{1,2}:\\d{2}:\\d{2})开始工作$";
Pattern pattern = Pattern.compile(regex);
next_time = "";
for (String string : msg_lines) {
Matcher matcher = pattern.matcher(string);
if (matcher.matches()) {
next_time = matcher.group(1);
try {
JSONObject json = new JSONObject(message);
String logData = json.optString("data", "无日志内容");

// 更新日志内容
Collections.addAll(last30, logData.split("\n"));
while (last30.size() > 30) {
last30.remove(0);
}
}
Collections.addAll(last30, msg_lines);
while (last30.size() > 30) {
last30.remove(0);
}
handler.post(new Runnable() {
@Override
public void run() {

// 检查是否有时间信息
String regex = ".*到(\\d{1,2}:\\d{2}:\\d{2})开始工作$";
Pattern pattern = Pattern.compile(regex);
next_time = "";
for (String string : logData.split("\n")) {
Matcher matcher = pattern.matcher(string);
if (matcher.matches()) {
next_time = matcher.group(1);
}
}

// 更新 UI
handler.post(() -> {
mowerLog.setText("");
mowerLog.append(String.join("\n", last30));
});

// 如果有时间信息,启动服务
if (!Objects.equals(next_time, "")) {
Intent intent = new Intent(context, MyService.class);
intent.putExtra("next_time", next_time);
context.stopService(intent);
context.startService(intent);
}
});
if (!Objects.equals(next_time, "")) {
Intent intent = new Intent(context, MyService.class);
intent.putExtra("next_time", next_time);
context.stopService(intent);
context.startService(intent);
} catch (JSONException e) {
handler.post(() -> mowerLog.append("日志解析失败: " + e.getMessage() + "\n"));
}
}

Expand Down