Browse Source

完善系统相关接口

master
白凤吉 4 months ago
parent
commit
6b392093c3
  1. 6
      app/src/main/java/com/iflytop/profilometer/api/ble/BleWebsocketManager.java
  2. 28
      app/src/main/java/com/iflytop/profilometer/api/system/SystemApi.java
  3. 12
      app/src/main/java/com/iflytop/profilometer/api/system/SystemRoutes.kt
  4. 5
      app/src/main/java/com/iflytop/profilometer/common/constant/SystemConfigType.java
  5. 6
      app/src/main/java/com/iflytop/profilometer/common/result/Result.java
  6. 3
      app/src/main/java/com/iflytop/profilometer/dao/SystemConfigDao.java
  7. 13
      app/src/main/java/com/iflytop/profilometer/model/vo/SystemConfigVO.java

6
app/src/main/java/com/iflytop/profilometer/api/ble/BleWebsocketManager.java

@ -14,6 +14,8 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import cn.hutool.json.JSONUtil;
public class BleWebsocketManager {
private static BleWebsocketManager instance;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
@ -40,10 +42,10 @@ public class BleWebsocketManager {
scheduledTask = scheduler.scheduleWithFixedDelay(() -> {
List<BluetoothDevice> scannedDevices = BleManager.getInstance(context).getScannedDevices();
List<String> bleList = new ArrayList<>();
for(BluetoothDevice bluetoothDevice : scannedDevices){
for (BluetoothDevice bluetoothDevice : scannedDevices) {
bleList.add(bluetoothDevice.getName() + "_" + bluetoothDevice.getAddress());
}
WebSocketManager.send(GsonUtil.toJson(bleList));
WebSocketManager.send(JSONUtil.toJsonStr(bleList));
}, 2, 2, TimeUnit.SECONDS);
}

28
app/src/main/java/com/iflytop/profilometer/api/system/SystemApi.java

@ -2,6 +2,12 @@ package com.iflytop.profilometer.api.system;
import android.content.Context;
import com.iflytop.profilometer.common.constant.SystemConfigType;
import com.iflytop.profilometer.common.result.Result;
import com.iflytop.profilometer.dao.SystemConfigDao;
import com.iflytop.profilometer.model.entity.SystemConfig;
import com.iflytop.profilometer.model.vo.SystemConfigVO;
/**
* 系统相关接口
*/
@ -12,5 +18,27 @@ public class SystemApi {
this.context = context.getApplicationContext();
}
public String config() {
SystemConfigDao systemConfigDao = new SystemConfigDao(context);
SystemConfig serverConfig = systemConfigDao.getSystemConfigByKey(SystemConfigType.SERVER);
SystemConfigVO systemConfigVO = new SystemConfigVO();
if (serverConfig != null) {
systemConfigVO.setServer(serverConfig.getConfigValue());
}
return Result.success(systemConfigVO);
}
public String save(String server) {
SystemConfigDao systemConfigDao = new SystemConfigDao(context);
SystemConfig systemConfig = new SystemConfig();
systemConfig.setConfigKey(SystemConfigType.SERVER);
systemConfig.setConfigValue(server);
int rows = systemConfigDao.updateSystemConfig(systemConfig);
if (rows < 1) {
systemConfigDao.insertSystemConfig(systemConfig);
}
return Result.success();
}
}

12
app/src/main/java/com/iflytop/profilometer/api/system/SystemRoutes.kt

@ -1,6 +1,11 @@
package com.iflytop.profilometer.api.system
import android.content.Context
import cn.hutool.json.JSONUtil
import io.ktor.http.ContentType
import io.ktor.server.application.call
import io.ktor.server.request.receiveText
import io.ktor.server.response.respondText
import io.ktor.server.routing.Routing
import io.ktor.server.routing.post
@ -11,12 +16,19 @@ fun Routing.systemRoutes(context: Context) {
*
*/
post("/api/system/config") {
val jsonResponse = api.config()
call.respondText(jsonResponse, ContentType.Application.Json)
}
/**
*
*/
post("/api/system/config/save") {
val requestBody = call.receiveText()
val jsonObj = JSONUtil.parseObj(requestBody)
val server = jsonObj.getStr("server")
val jsonResponse = api.save(server)
call.respondText(jsonResponse, ContentType.Application.Json)
}
}

5
app/src/main/java/com/iflytop/profilometer/common/constant/SystemConfigType.java

@ -0,0 +1,5 @@
package com.iflytop.profilometer.common.constant;
public class SystemConfigType {
public static final String SERVER = "sync_server";
}

6
app/src/main/java/com/iflytop/profilometer/common/result/Result.java

@ -4,6 +4,8 @@ package com.iflytop.profilometer.common.result;
import java.util.HashMap;
import java.util.Map;
import cn.hutool.json.JSONUtil;
public final class Result {
private Result() {
}
@ -15,7 +17,7 @@ public final class Result {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("success", true);
resultMap.put("data", data);
return GsonUtil.toJson(resultMap);
return JSONUtil.toJsonStr(resultMap);
}
/**
@ -33,7 +35,7 @@ public final class Result {
resultMap.put("success", false);
resultMap.put("data", data);
resultMap.put("message", message == null ? "请求失败" : message);
return GsonUtil.toJson(resultMap);
return JSONUtil.toJsonStr(resultMap);
}

3
app/src/main/java/com/iflytop/profilometer/dao/SystemConfigDao.java

@ -41,9 +41,8 @@ public class SystemConfigDao {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("update_time", getCurrentTime());
values.put("config_key", config.getConfigKey());
values.put("config_value", config.getConfigValue());
int rows = db.update(MyDatabaseHelper.TABLE_SYSTEM_CONFIG, values, "id = ?", new String[]{String.valueOf(config.getId())});
int rows = db.update(MyDatabaseHelper.TABLE_SYSTEM_CONFIG, values, "config_key = ?", new String[]{String.valueOf(config.getConfigKey())});
db.close();
return rows;
}

13
app/src/main/java/com/iflytop/profilometer/model/vo/SystemConfigVO.java

@ -0,0 +1,13 @@
package com.iflytop.profilometer.model.vo;
public class SystemConfigVO {
private String server;
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
}
Loading…
Cancel
Save