diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index eb84834..7564ae9 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -54,7 +54,7 @@ size = inStream!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -73,7 +73,7 @@ size = inStream1!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -92,7 +92,7 @@ size = inStream2!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -139,10 +139,10 @@ /** * 串口读数 * */ - abstract fun onDataReceived(buffer: ByteArray?, size: Int) + abstract fun onDataReceived(buffer: ByteArray?) override fun onDestroy() { - BaseApplication.get().closeSerialPort() super.onDestroy() + BaseApplication.get().closeSerialPort() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index eb84834..7564ae9 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -54,7 +54,7 @@ size = inStream!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -73,7 +73,7 @@ size = inStream1!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -92,7 +92,7 @@ size = inStream2!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -139,10 +139,10 @@ /** * 串口读数 * */ - abstract fun onDataReceived(buffer: ByteArray?, size: Int) + abstract fun onDataReceived(buffer: ByteArray?) override fun onDestroy() { - BaseApplication.get().closeSerialPort() super.onDestroy() + BaseApplication.get().closeSerialPort() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/extensions/String.kt b/app/src/main/java/com/casic/detector/extensions/String.kt index b5b9afc..b76d83d 100644 --- a/app/src/main/java/com/casic/detector/extensions/String.kt +++ b/app/src/main/java/com/casic/detector/extensions/String.kt @@ -2,14 +2,11 @@ import android.content.Context import com.casic.detector.callback.OnImageCompressListener -import com.casic.detector.model.ErrorMessageModel import com.casic.detector.utils.FileType import com.casic.detector.utils.LocaleConstant -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.google.gson.JsonParser import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.utils.SaveKeyValues -import org.json.JSONObject import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File @@ -17,18 +14,22 @@ /** * String扩展方法 */ -fun String.separateResponseState(): Boolean { +fun String.getResponseState(): Boolean { if (this.isBlank()) { return false } - return JSONObject(this).getBoolean("success") + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("success").asBoolean } -fun String.toErrorMessage(): String { - val errorModel = Gson().fromJson( - this, object : TypeToken() {}.type - ) - return errorModel.message.toString() +fun String.getResponseMessage(): String { + if (this.isBlank()) { + return "" + } + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("message").asString } /** diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index eb84834..7564ae9 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -54,7 +54,7 @@ size = inStream!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -73,7 +73,7 @@ size = inStream1!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -92,7 +92,7 @@ size = inStream2!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -139,10 +139,10 @@ /** * 串口读数 * */ - abstract fun onDataReceived(buffer: ByteArray?, size: Int) + abstract fun onDataReceived(buffer: ByteArray?) override fun onDestroy() { - BaseApplication.get().closeSerialPort() super.onDestroy() + BaseApplication.get().closeSerialPort() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/extensions/String.kt b/app/src/main/java/com/casic/detector/extensions/String.kt index b5b9afc..b76d83d 100644 --- a/app/src/main/java/com/casic/detector/extensions/String.kt +++ b/app/src/main/java/com/casic/detector/extensions/String.kt @@ -2,14 +2,11 @@ import android.content.Context import com.casic.detector.callback.OnImageCompressListener -import com.casic.detector.model.ErrorMessageModel import com.casic.detector.utils.FileType import com.casic.detector.utils.LocaleConstant -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.google.gson.JsonParser import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.utils.SaveKeyValues -import org.json.JSONObject import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File @@ -17,18 +14,22 @@ /** * String扩展方法 */ -fun String.separateResponseState(): Boolean { +fun String.getResponseState(): Boolean { if (this.isBlank()) { return false } - return JSONObject(this).getBoolean("success") + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("success").asBoolean } -fun String.toErrorMessage(): String { - val errorModel = Gson().fromJson( - this, object : TypeToken() {}.type - ) - return errorModel.message.toString() +fun String.getResponseMessage(): String { + if (this.isBlank()) { + return "" + } + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("message").asString } /** diff --git a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java b/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java deleted file mode 100644 index 7dbe1c5..0000000 --- a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.casic.detector.model; - -public class ErrorMessageModel { - private int code; - private String data; - private String exceptionClazz; - private String message; - private boolean isSuccess; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public String getExceptionClazz() { - return exceptionClazz; - } - - public void setExceptionClazz(String exceptionClazz) { - this.exceptionClazz = exceptionClazz; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public boolean isSuccess() { - return isSuccess; - } - - public void setSuccess(boolean success) { - isSuccess = success; - } -} diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index eb84834..7564ae9 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -54,7 +54,7 @@ size = inStream!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -73,7 +73,7 @@ size = inStream1!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -92,7 +92,7 @@ size = inStream2!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -139,10 +139,10 @@ /** * 串口读数 * */ - abstract fun onDataReceived(buffer: ByteArray?, size: Int) + abstract fun onDataReceived(buffer: ByteArray?) override fun onDestroy() { - BaseApplication.get().closeSerialPort() super.onDestroy() + BaseApplication.get().closeSerialPort() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/extensions/String.kt b/app/src/main/java/com/casic/detector/extensions/String.kt index b5b9afc..b76d83d 100644 --- a/app/src/main/java/com/casic/detector/extensions/String.kt +++ b/app/src/main/java/com/casic/detector/extensions/String.kt @@ -2,14 +2,11 @@ import android.content.Context import com.casic.detector.callback.OnImageCompressListener -import com.casic.detector.model.ErrorMessageModel import com.casic.detector.utils.FileType import com.casic.detector.utils.LocaleConstant -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.google.gson.JsonParser import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.utils.SaveKeyValues -import org.json.JSONObject import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File @@ -17,18 +14,22 @@ /** * String扩展方法 */ -fun String.separateResponseState(): Boolean { +fun String.getResponseState(): Boolean { if (this.isBlank()) { return false } - return JSONObject(this).getBoolean("success") + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("success").asBoolean } -fun String.toErrorMessage(): String { - val errorModel = Gson().fromJson( - this, object : TypeToken() {}.type - ) - return errorModel.message.toString() +fun String.getResponseMessage(): String { + if (this.isBlank()) { + return "" + } + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("message").asString } /** diff --git a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java b/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java deleted file mode 100644 index 7dbe1c5..0000000 --- a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.casic.detector.model; - -public class ErrorMessageModel { - private int code; - private String data; - private String exceptionClazz; - private String message; - private boolean isSuccess; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public String getExceptionClazz() { - return exceptionClazz; - } - - public void setExceptionClazz(String exceptionClazz) { - this.exceptionClazz = exceptionClazz; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public boolean isSuccess() { - return isSuccess; - } - - public void setSuccess(boolean success) { - isSuccess = success; - } -} diff --git a/app/src/main/java/com/casic/detector/model/MarkerModel.kt b/app/src/main/java/com/casic/detector/model/MarkerModel.kt new file mode 100644 index 0000000..dab32c3 --- /dev/null +++ b/app/src/main/java/com/casic/detector/model/MarkerModel.kt @@ -0,0 +1,6 @@ +package com.casic.detector.model + +/** + * 上传标识器的数据模型 + * */ +data class MarkerModel(private val id: String, private val imagePath: String) \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index eb84834..7564ae9 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -54,7 +54,7 @@ size = inStream!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -73,7 +73,7 @@ size = inStream1!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -92,7 +92,7 @@ size = inStream2!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -139,10 +139,10 @@ /** * 串口读数 * */ - abstract fun onDataReceived(buffer: ByteArray?, size: Int) + abstract fun onDataReceived(buffer: ByteArray?) override fun onDestroy() { - BaseApplication.get().closeSerialPort() super.onDestroy() + BaseApplication.get().closeSerialPort() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/extensions/String.kt b/app/src/main/java/com/casic/detector/extensions/String.kt index b5b9afc..b76d83d 100644 --- a/app/src/main/java/com/casic/detector/extensions/String.kt +++ b/app/src/main/java/com/casic/detector/extensions/String.kt @@ -2,14 +2,11 @@ import android.content.Context import com.casic.detector.callback.OnImageCompressListener -import com.casic.detector.model.ErrorMessageModel import com.casic.detector.utils.FileType import com.casic.detector.utils.LocaleConstant -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.google.gson.JsonParser import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.utils.SaveKeyValues -import org.json.JSONObject import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File @@ -17,18 +14,22 @@ /** * String扩展方法 */ -fun String.separateResponseState(): Boolean { +fun String.getResponseState(): Boolean { if (this.isBlank()) { return false } - return JSONObject(this).getBoolean("success") + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("success").asBoolean } -fun String.toErrorMessage(): String { - val errorModel = Gson().fromJson( - this, object : TypeToken() {}.type - ) - return errorModel.message.toString() +fun String.getResponseMessage(): String { + if (this.isBlank()) { + return "" + } + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("message").asString } /** diff --git a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java b/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java deleted file mode 100644 index 7dbe1c5..0000000 --- a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.casic.detector.model; - -public class ErrorMessageModel { - private int code; - private String data; - private String exceptionClazz; - private String message; - private boolean isSuccess; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public String getExceptionClazz() { - return exceptionClazz; - } - - public void setExceptionClazz(String exceptionClazz) { - this.exceptionClazz = exceptionClazz; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public boolean isSuccess() { - return isSuccess; - } - - public void setSuccess(boolean success) { - isSuccess = success; - } -} diff --git a/app/src/main/java/com/casic/detector/model/MarkerModel.kt b/app/src/main/java/com/casic/detector/model/MarkerModel.kt new file mode 100644 index 0000000..dab32c3 --- /dev/null +++ b/app/src/main/java/com/casic/detector/model/MarkerModel.kt @@ -0,0 +1,6 @@ +package com.casic.detector.model + +/** + * 上传标识器的数据模型 + * */ +data class MarkerModel(private val id: String, private val imagePath: String) \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt index a5c3bc6..db640cc 100644 --- a/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt @@ -84,4 +84,23 @@ @Query("userName") userId: String, @Query("taskDetailId") taskId: String, ): String + + /** + * 新建自由巡检任务 + * */ + @GET("") + suspend fun createFreeTask( + @Query("patrollerId") patrollerId: String, + @Query("description") description: String, + ): String + + /** + * 上传自由巡检过程中发现的标识器 + * */ + @Multipart + @POST("") + suspend fun uploadTaskMarker( + @Field("taskId") taskId: String, + @Body requestBody: RequestBody, + ): String } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index eb84834..7564ae9 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -54,7 +54,7 @@ size = inStream!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -73,7 +73,7 @@ size = inStream1!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -92,7 +92,7 @@ size = inStream2!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -139,10 +139,10 @@ /** * 串口读数 * */ - abstract fun onDataReceived(buffer: ByteArray?, size: Int) + abstract fun onDataReceived(buffer: ByteArray?) override fun onDestroy() { - BaseApplication.get().closeSerialPort() super.onDestroy() + BaseApplication.get().closeSerialPort() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/extensions/String.kt b/app/src/main/java/com/casic/detector/extensions/String.kt index b5b9afc..b76d83d 100644 --- a/app/src/main/java/com/casic/detector/extensions/String.kt +++ b/app/src/main/java/com/casic/detector/extensions/String.kt @@ -2,14 +2,11 @@ import android.content.Context import com.casic.detector.callback.OnImageCompressListener -import com.casic.detector.model.ErrorMessageModel import com.casic.detector.utils.FileType import com.casic.detector.utils.LocaleConstant -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.google.gson.JsonParser import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.utils.SaveKeyValues -import org.json.JSONObject import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File @@ -17,18 +14,22 @@ /** * String扩展方法 */ -fun String.separateResponseState(): Boolean { +fun String.getResponseState(): Boolean { if (this.isBlank()) { return false } - return JSONObject(this).getBoolean("success") + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("success").asBoolean } -fun String.toErrorMessage(): String { - val errorModel = Gson().fromJson( - this, object : TypeToken() {}.type - ) - return errorModel.message.toString() +fun String.getResponseMessage(): String { + if (this.isBlank()) { + return "" + } + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("message").asString } /** diff --git a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java b/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java deleted file mode 100644 index 7dbe1c5..0000000 --- a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.casic.detector.model; - -public class ErrorMessageModel { - private int code; - private String data; - private String exceptionClazz; - private String message; - private boolean isSuccess; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public String getExceptionClazz() { - return exceptionClazz; - } - - public void setExceptionClazz(String exceptionClazz) { - this.exceptionClazz = exceptionClazz; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public boolean isSuccess() { - return isSuccess; - } - - public void setSuccess(boolean success) { - isSuccess = success; - } -} diff --git a/app/src/main/java/com/casic/detector/model/MarkerModel.kt b/app/src/main/java/com/casic/detector/model/MarkerModel.kt new file mode 100644 index 0000000..dab32c3 --- /dev/null +++ b/app/src/main/java/com/casic/detector/model/MarkerModel.kt @@ -0,0 +1,6 @@ +package com.casic.detector.model + +/** + * 上传标识器的数据模型 + * */ +data class MarkerModel(private val id: String, private val imagePath: String) \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt index a5c3bc6..db640cc 100644 --- a/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt @@ -84,4 +84,23 @@ @Query("userName") userId: String, @Query("taskDetailId") taskId: String, ): String + + /** + * 新建自由巡检任务 + * */ + @GET("") + suspend fun createFreeTask( + @Query("patrollerId") patrollerId: String, + @Query("description") description: String, + ): String + + /** + * 上传自由巡检过程中发现的标识器 + * */ + @Multipart + @POST("") + suspend fun uploadTaskMarker( + @Field("taskId") taskId: String, + @Body requestBody: RequestBody, + ): String } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt index c96ef36..e0a45c3 100644 --- a/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt @@ -1,7 +1,10 @@ package com.casic.detector.retrofit +import com.casic.detector.model.MarkerModel import com.casic.detector.utils.LocaleConstant +import com.google.gson.Gson import com.google.gson.JsonObject +import com.google.gson.reflect.TypeToken import com.pengxh.kt.lite.utils.RetrofitFactory import com.pengxh.kt.lite.utils.SaveKeyValues import okhttp3.MediaType.Companion.toMediaType @@ -13,43 +16,35 @@ object RetrofitServiceManager { + private val gson by lazy { Gson() } + private val typeToken = object : TypeToken>() {}.type + + private fun createApi(): RetrofitService { + val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String + val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String + val httpConfig = "http://${serverIp}:${serverPort}" + return RetrofitFactory.createRetrofit(httpConfig) + } + /** * 登录 */ suspend fun login(account: String, password: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.login(account, password) + return createApi().login(account, password) } /** * 更新版本 * */ suspend fun getApplicationVersion(): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getApplicationVersion() + return createApi().getApplicationVersion() } /** * 获取标识器信息文件 */ suspend fun getMarkerFile(userId: String, companyId: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getMarkerFile(userId, companyId) + return createApi().getMarkerFile(userId, companyId) } /** @@ -62,39 +57,21 @@ "application/json;charset=UTF-8".toMediaType() ) - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getLabelPicture(requestBody) + return createApi().getLabelPicture(requestBody) } /** * 获取巡检工单 */ suspend fun getTask(userName: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getTask(userName) + return createApi().getTask(userName) } /** * 提交工单 */ suspend fun uploadTask(userId: String, taskId: String?, state: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.uploadTask(userId, taskId.toString(), state) + return createApi().uploadTask(userId, taskId.toString(), state) } /** @@ -172,24 +149,21 @@ multiParts.add(fileMultipart) } - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } return when (multiParts.size) { 3 -> { - api.installLabel(res, multiParts[0], multiParts[1], multiParts[2]) + createApi().installLabel(res, multiParts[0], multiParts[1], multiParts[2]) } + 2 -> { - api.installLabel(res, multiParts[0], multiParts[1], null) + createApi().installLabel(res, multiParts[0], multiParts[1], null) } + 1 -> { - api.installLabel(res, multiParts[0], null, null) + createApi().installLabel(res, multiParts[0], null, null) } + else -> { - api.installLabel(res, null, null, null) + createApi().installLabel(res, null, null, null) } } } @@ -218,24 +192,27 @@ multiParts.add(fileMultipart) } - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } return when (multiParts.size) { 3 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], multiParts[2]) + createApi().uploadEvent( + taskIdMap, + eventMap, + multiParts[0], + multiParts[1], + multiParts[2] + ) } + 2 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], null) + createApi().uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], null) } + 1 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], null, null) + createApi().uploadEvent(taskIdMap, eventMap, multiParts[0], null, null) } + else -> { - api.uploadEvent(taskIdMap, eventMap, null, null, null) + createApi().uploadEvent(taskIdMap, eventMap, null, null, null) } } } @@ -244,12 +221,25 @@ * 上传工单中标识器信息 */ suspend fun uploadMarker(userName: String, taskDetailId: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.uploadMarker(userName, taskDetailId) + return createApi().uploadMarker(userName, taskDetailId) + } + + /** + * 上传工单中标识器信息 + */ + suspend fun createFreeTask(patrollerId: String, description: String): String { + return createApi().createFreeTask(patrollerId, description) + } + + /** + * 上传自由巡检过程中发现的标识器 + */ + suspend fun uploadTaskMarker(taskId: String, models: List): String { + val param = JsonObject() + param.add("markers", gson.toJsonTree(models, typeToken).asJsonArray) + val requestBody = param.toString().toRequestBody( + "application/json;charset=UTF-8".toMediaType() + ) + return createApi().uploadTaskMarker(taskId, requestBody) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index eb84834..7564ae9 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -54,7 +54,7 @@ size = inStream!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -73,7 +73,7 @@ size = inStream1!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -92,7 +92,7 @@ size = inStream2!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -139,10 +139,10 @@ /** * 串口读数 * */ - abstract fun onDataReceived(buffer: ByteArray?, size: Int) + abstract fun onDataReceived(buffer: ByteArray?) override fun onDestroy() { - BaseApplication.get().closeSerialPort() super.onDestroy() + BaseApplication.get().closeSerialPort() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/extensions/String.kt b/app/src/main/java/com/casic/detector/extensions/String.kt index b5b9afc..b76d83d 100644 --- a/app/src/main/java/com/casic/detector/extensions/String.kt +++ b/app/src/main/java/com/casic/detector/extensions/String.kt @@ -2,14 +2,11 @@ import android.content.Context import com.casic.detector.callback.OnImageCompressListener -import com.casic.detector.model.ErrorMessageModel import com.casic.detector.utils.FileType import com.casic.detector.utils.LocaleConstant -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.google.gson.JsonParser import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.utils.SaveKeyValues -import org.json.JSONObject import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File @@ -17,18 +14,22 @@ /** * String扩展方法 */ -fun String.separateResponseState(): Boolean { +fun String.getResponseState(): Boolean { if (this.isBlank()) { return false } - return JSONObject(this).getBoolean("success") + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("success").asBoolean } -fun String.toErrorMessage(): String { - val errorModel = Gson().fromJson( - this, object : TypeToken() {}.type - ) - return errorModel.message.toString() +fun String.getResponseMessage(): String { + if (this.isBlank()) { + return "" + } + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("message").asString } /** diff --git a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java b/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java deleted file mode 100644 index 7dbe1c5..0000000 --- a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.casic.detector.model; - -public class ErrorMessageModel { - private int code; - private String data; - private String exceptionClazz; - private String message; - private boolean isSuccess; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public String getExceptionClazz() { - return exceptionClazz; - } - - public void setExceptionClazz(String exceptionClazz) { - this.exceptionClazz = exceptionClazz; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public boolean isSuccess() { - return isSuccess; - } - - public void setSuccess(boolean success) { - isSuccess = success; - } -} diff --git a/app/src/main/java/com/casic/detector/model/MarkerModel.kt b/app/src/main/java/com/casic/detector/model/MarkerModel.kt new file mode 100644 index 0000000..dab32c3 --- /dev/null +++ b/app/src/main/java/com/casic/detector/model/MarkerModel.kt @@ -0,0 +1,6 @@ +package com.casic.detector.model + +/** + * 上传标识器的数据模型 + * */ +data class MarkerModel(private val id: String, private val imagePath: String) \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt index a5c3bc6..db640cc 100644 --- a/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt @@ -84,4 +84,23 @@ @Query("userName") userId: String, @Query("taskDetailId") taskId: String, ): String + + /** + * 新建自由巡检任务 + * */ + @GET("") + suspend fun createFreeTask( + @Query("patrollerId") patrollerId: String, + @Query("description") description: String, + ): String + + /** + * 上传自由巡检过程中发现的标识器 + * */ + @Multipart + @POST("") + suspend fun uploadTaskMarker( + @Field("taskId") taskId: String, + @Body requestBody: RequestBody, + ): String } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt index c96ef36..e0a45c3 100644 --- a/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt @@ -1,7 +1,10 @@ package com.casic.detector.retrofit +import com.casic.detector.model.MarkerModel import com.casic.detector.utils.LocaleConstant +import com.google.gson.Gson import com.google.gson.JsonObject +import com.google.gson.reflect.TypeToken import com.pengxh.kt.lite.utils.RetrofitFactory import com.pengxh.kt.lite.utils.SaveKeyValues import okhttp3.MediaType.Companion.toMediaType @@ -13,43 +16,35 @@ object RetrofitServiceManager { + private val gson by lazy { Gson() } + private val typeToken = object : TypeToken>() {}.type + + private fun createApi(): RetrofitService { + val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String + val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String + val httpConfig = "http://${serverIp}:${serverPort}" + return RetrofitFactory.createRetrofit(httpConfig) + } + /** * 登录 */ suspend fun login(account: String, password: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.login(account, password) + return createApi().login(account, password) } /** * 更新版本 * */ suspend fun getApplicationVersion(): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getApplicationVersion() + return createApi().getApplicationVersion() } /** * 获取标识器信息文件 */ suspend fun getMarkerFile(userId: String, companyId: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getMarkerFile(userId, companyId) + return createApi().getMarkerFile(userId, companyId) } /** @@ -62,39 +57,21 @@ "application/json;charset=UTF-8".toMediaType() ) - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getLabelPicture(requestBody) + return createApi().getLabelPicture(requestBody) } /** * 获取巡检工单 */ suspend fun getTask(userName: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getTask(userName) + return createApi().getTask(userName) } /** * 提交工单 */ suspend fun uploadTask(userId: String, taskId: String?, state: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.uploadTask(userId, taskId.toString(), state) + return createApi().uploadTask(userId, taskId.toString(), state) } /** @@ -172,24 +149,21 @@ multiParts.add(fileMultipart) } - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } return when (multiParts.size) { 3 -> { - api.installLabel(res, multiParts[0], multiParts[1], multiParts[2]) + createApi().installLabel(res, multiParts[0], multiParts[1], multiParts[2]) } + 2 -> { - api.installLabel(res, multiParts[0], multiParts[1], null) + createApi().installLabel(res, multiParts[0], multiParts[1], null) } + 1 -> { - api.installLabel(res, multiParts[0], null, null) + createApi().installLabel(res, multiParts[0], null, null) } + else -> { - api.installLabel(res, null, null, null) + createApi().installLabel(res, null, null, null) } } } @@ -218,24 +192,27 @@ multiParts.add(fileMultipart) } - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } return when (multiParts.size) { 3 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], multiParts[2]) + createApi().uploadEvent( + taskIdMap, + eventMap, + multiParts[0], + multiParts[1], + multiParts[2] + ) } + 2 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], null) + createApi().uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], null) } + 1 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], null, null) + createApi().uploadEvent(taskIdMap, eventMap, multiParts[0], null, null) } + else -> { - api.uploadEvent(taskIdMap, eventMap, null, null, null) + createApi().uploadEvent(taskIdMap, eventMap, null, null, null) } } } @@ -244,12 +221,25 @@ * 上传工单中标识器信息 */ suspend fun uploadMarker(userName: String, taskDetailId: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.uploadMarker(userName, taskDetailId) + return createApi().uploadMarker(userName, taskDetailId) + } + + /** + * 上传工单中标识器信息 + */ + suspend fun createFreeTask(patrollerId: String, description: String): String { + return createApi().createFreeTask(patrollerId, description) + } + + /** + * 上传自由巡检过程中发现的标识器 + */ + suspend fun uploadTaskMarker(taskId: String, models: List): String { + val param = JsonObject() + param.add("markers", gson.toJsonTree(models, typeToken).asJsonArray) + val requestBody = param.toString().toRequestBody( + "application/json;charset=UTF-8".toMediaType() + ) + return createApi().uploadTaskMarker(taskId, requestBody) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/vm/TaskViewModel.kt b/app/src/main/java/com/casic/detector/vm/TaskViewModel.kt index ee7e5b1..483cebd 100644 --- a/app/src/main/java/com/casic/detector/vm/TaskViewModel.kt +++ b/app/src/main/java/com/casic/detector/vm/TaskViewModel.kt @@ -2,10 +2,10 @@ import androidx.lifecycle.MutableLiveData import com.casic.detector.base.BaseApplication -import com.casic.detector.extensions.convertChinese -import com.casic.detector.extensions.separateResponseState -import com.casic.detector.extensions.toErrorMessage +import com.casic.detector.extensions.getResponseMessage +import com.casic.detector.extensions.getResponseState import com.casic.detector.model.MarkerFileModel +import com.casic.detector.model.MarkerModel import com.casic.detector.model.TaskModel import com.casic.detector.retrofit.RetrofitServiceManager import com.casic.detector.utils.DataBaseManager @@ -23,31 +23,57 @@ val markerFileResult = MutableLiveData() val taskResult = MutableLiveData() + val freeTaskResult = MutableLiveData() + + fun createFreeTask(patrollerId: String, description: String) = launch({ + val response = RetrofitServiceManager.createFreeTask(patrollerId, description) + if (response.getResponseState()) { +// freeTaskResult.value = + } else { + response.getResponseMessage().show(BaseApplication.get()) + } + }, { + it.printStackTrace() + }) + + fun uploadTaskMarker(taskId: String, models: List) = launch({ + loadState.value = LoadState.Loading + val response = RetrofitServiceManager.uploadTaskMarker(taskId, models) + if (response.getResponseState()) { + loadState.value = LoadState.Success + } else { + response.getResponseMessage().show(BaseApplication.get()) + loadState.value = LoadState.Fail + } + }, { + it.printStackTrace() + loadState.value = LoadState.Fail + }) fun getMarkerFile(userId: String, companyId: String) = launch({ val response = RetrofitServiceManager.getMarkerFile(userId, companyId) - if (response.separateResponseState()) { + if (response.getResponseState()) { markerFileResult.value = gson.fromJson( response, object : TypeToken() {}.type ) } else { - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { - it.convertChinese().show(BaseApplication.get()) + it.printStackTrace() }) fun getTask(userName: String) = launch({ val response = RetrofitServiceManager.getTask(userName) - if (response.separateResponseState()) { + if (response.getResponseState()) { taskResult.value = gson.fromJson( response, object : TypeToken() {}.type ) } else { - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { - it.convertChinese().show(BaseApplication.get()) + it.printStackTrace() }) fun installLabel( @@ -109,21 +135,21 @@ memo, realPaths ) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { + it.printStackTrace() loadState.value = LoadState.Fail - it.convertChinese().show(BaseApplication.get()) }) fun uploadTask(userId: String, taskId: String?, state: String) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.uploadTask(userId, taskId, state) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success if (state == "2") { "工单${taskId}已提交!".show(BaseApplication.get()) @@ -133,38 +159,38 @@ } } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { + it.printStackTrace() loadState.value = LoadState.Fail - it.convertChinese().show(BaseApplication.get()) }) fun uploadEvent(taskId: String, event: String, realPaths: ArrayList) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.uploadEvent(taskId, event, realPaths) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { + it.printStackTrace() loadState.value = LoadState.Fail - it.convertChinese().show(BaseApplication.get()) }) fun uploadMarker(userName: String, taskDetailId: String) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.uploadMarker(userName, taskDetailId) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { + it.printStackTrace() loadState.value = LoadState.Fail - it.convertChinese().show(BaseApplication.get()) }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt index eb84834..7564ae9 100644 --- a/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt +++ b/app/src/main/java/com/casic/detector/base/SerialPortActivity.kt @@ -54,7 +54,7 @@ size = inStream!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -73,7 +73,7 @@ size = inStream1!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -92,7 +92,7 @@ size = inStream2!!.read(buffer) if (size > 0) { withContext(Dispatchers.Main) { - onDataReceived(buffer, size) + onDataReceived(buffer) } } } catch (e: IOException) { @@ -139,10 +139,10 @@ /** * 串口读数 * */ - abstract fun onDataReceived(buffer: ByteArray?, size: Int) + abstract fun onDataReceived(buffer: ByteArray?) override fun onDestroy() { - BaseApplication.get().closeSerialPort() super.onDestroy() + BaseApplication.get().closeSerialPort() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/extensions/String.kt b/app/src/main/java/com/casic/detector/extensions/String.kt index b5b9afc..b76d83d 100644 --- a/app/src/main/java/com/casic/detector/extensions/String.kt +++ b/app/src/main/java/com/casic/detector/extensions/String.kt @@ -2,14 +2,11 @@ import android.content.Context import com.casic.detector.callback.OnImageCompressListener -import com.casic.detector.model.ErrorMessageModel import com.casic.detector.utils.FileType import com.casic.detector.utils.LocaleConstant -import com.google.gson.Gson -import com.google.gson.reflect.TypeToken +import com.google.gson.JsonParser import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.utils.SaveKeyValues -import org.json.JSONObject import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File @@ -17,18 +14,22 @@ /** * String扩展方法 */ -fun String.separateResponseState(): Boolean { +fun String.getResponseState(): Boolean { if (this.isBlank()) { return false } - return JSONObject(this).getBoolean("success") + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("success").asBoolean } -fun String.toErrorMessage(): String { - val errorModel = Gson().fromJson( - this, object : TypeToken() {}.type - ) - return errorModel.message.toString() +fun String.getResponseMessage(): String { + if (this.isBlank()) { + return "" + } + val element = JsonParser.parseString(this) + val jsonObject = element.asJsonObject + return jsonObject.get("message").asString } /** diff --git a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java b/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java deleted file mode 100644 index 7dbe1c5..0000000 --- a/app/src/main/java/com/casic/detector/model/ErrorMessageModel.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.casic.detector.model; - -public class ErrorMessageModel { - private int code; - private String data; - private String exceptionClazz; - private String message; - private boolean isSuccess; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getData() { - return data; - } - - public void setData(String data) { - this.data = data; - } - - public String getExceptionClazz() { - return exceptionClazz; - } - - public void setExceptionClazz(String exceptionClazz) { - this.exceptionClazz = exceptionClazz; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public boolean isSuccess() { - return isSuccess; - } - - public void setSuccess(boolean success) { - isSuccess = success; - } -} diff --git a/app/src/main/java/com/casic/detector/model/MarkerModel.kt b/app/src/main/java/com/casic/detector/model/MarkerModel.kt new file mode 100644 index 0000000..dab32c3 --- /dev/null +++ b/app/src/main/java/com/casic/detector/model/MarkerModel.kt @@ -0,0 +1,6 @@ +package com.casic.detector.model + +/** + * 上传标识器的数据模型 + * */ +data class MarkerModel(private val id: String, private val imagePath: String) \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt index a5c3bc6..db640cc 100644 --- a/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/detector/retrofit/RetrofitService.kt @@ -84,4 +84,23 @@ @Query("userName") userId: String, @Query("taskDetailId") taskId: String, ): String + + /** + * 新建自由巡检任务 + * */ + @GET("") + suspend fun createFreeTask( + @Query("patrollerId") patrollerId: String, + @Query("description") description: String, + ): String + + /** + * 上传自由巡检过程中发现的标识器 + * */ + @Multipart + @POST("") + suspend fun uploadTaskMarker( + @Field("taskId") taskId: String, + @Body requestBody: RequestBody, + ): String } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt index c96ef36..e0a45c3 100644 --- a/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/detector/retrofit/RetrofitServiceManager.kt @@ -1,7 +1,10 @@ package com.casic.detector.retrofit +import com.casic.detector.model.MarkerModel import com.casic.detector.utils.LocaleConstant +import com.google.gson.Gson import com.google.gson.JsonObject +import com.google.gson.reflect.TypeToken import com.pengxh.kt.lite.utils.RetrofitFactory import com.pengxh.kt.lite.utils.SaveKeyValues import okhttp3.MediaType.Companion.toMediaType @@ -13,43 +16,35 @@ object RetrofitServiceManager { + private val gson by lazy { Gson() } + private val typeToken = object : TypeToken>() {}.type + + private fun createApi(): RetrofitService { + val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String + val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String + val httpConfig = "http://${serverIp}:${serverPort}" + return RetrofitFactory.createRetrofit(httpConfig) + } + /** * 登录 */ suspend fun login(account: String, password: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.login(account, password) + return createApi().login(account, password) } /** * 更新版本 * */ suspend fun getApplicationVersion(): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getApplicationVersion() + return createApi().getApplicationVersion() } /** * 获取标识器信息文件 */ suspend fun getMarkerFile(userId: String, companyId: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getMarkerFile(userId, companyId) + return createApi().getMarkerFile(userId, companyId) } /** @@ -62,39 +57,21 @@ "application/json;charset=UTF-8".toMediaType() ) - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getLabelPicture(requestBody) + return createApi().getLabelPicture(requestBody) } /** * 获取巡检工单 */ suspend fun getTask(userName: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.getTask(userName) + return createApi().getTask(userName) } /** * 提交工单 */ suspend fun uploadTask(userId: String, taskId: String?, state: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.uploadTask(userId, taskId.toString(), state) + return createApi().uploadTask(userId, taskId.toString(), state) } /** @@ -172,24 +149,21 @@ multiParts.add(fileMultipart) } - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } return when (multiParts.size) { 3 -> { - api.installLabel(res, multiParts[0], multiParts[1], multiParts[2]) + createApi().installLabel(res, multiParts[0], multiParts[1], multiParts[2]) } + 2 -> { - api.installLabel(res, multiParts[0], multiParts[1], null) + createApi().installLabel(res, multiParts[0], multiParts[1], null) } + 1 -> { - api.installLabel(res, multiParts[0], null, null) + createApi().installLabel(res, multiParts[0], null, null) } + else -> { - api.installLabel(res, null, null, null) + createApi().installLabel(res, null, null, null) } } } @@ -218,24 +192,27 @@ multiParts.add(fileMultipart) } - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } return when (multiParts.size) { 3 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], multiParts[2]) + createApi().uploadEvent( + taskIdMap, + eventMap, + multiParts[0], + multiParts[1], + multiParts[2] + ) } + 2 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], null) + createApi().uploadEvent(taskIdMap, eventMap, multiParts[0], multiParts[1], null) } + 1 -> { - api.uploadEvent(taskIdMap, eventMap, multiParts[0], null, null) + createApi().uploadEvent(taskIdMap, eventMap, multiParts[0], null, null) } + else -> { - api.uploadEvent(taskIdMap, eventMap, null, null, null) + createApi().uploadEvent(taskIdMap, eventMap, null, null, null) } } } @@ -244,12 +221,25 @@ * 上传工单中标识器信息 */ suspend fun uploadMarker(userName: String, taskDetailId: String): String { - val api by lazy { - val serverIp = SaveKeyValues.getValue(LocaleConstant.SERVER_IP, "") as String - val serverPort = SaveKeyValues.getValue(LocaleConstant.SERVER_PORT, "") as String - val httpConfig = "http://${serverIp}:${serverPort}" - RetrofitFactory.createRetrofit(httpConfig) - } - return api.uploadMarker(userName, taskDetailId) + return createApi().uploadMarker(userName, taskDetailId) + } + + /** + * 上传工单中标识器信息 + */ + suspend fun createFreeTask(patrollerId: String, description: String): String { + return createApi().createFreeTask(patrollerId, description) + } + + /** + * 上传自由巡检过程中发现的标识器 + */ + suspend fun uploadTaskMarker(taskId: String, models: List): String { + val param = JsonObject() + param.add("markers", gson.toJsonTree(models, typeToken).asJsonArray) + val requestBody = param.toString().toRequestBody( + "application/json;charset=UTF-8".toMediaType() + ) + return createApi().uploadTaskMarker(taskId, requestBody) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/vm/TaskViewModel.kt b/app/src/main/java/com/casic/detector/vm/TaskViewModel.kt index ee7e5b1..483cebd 100644 --- a/app/src/main/java/com/casic/detector/vm/TaskViewModel.kt +++ b/app/src/main/java/com/casic/detector/vm/TaskViewModel.kt @@ -2,10 +2,10 @@ import androidx.lifecycle.MutableLiveData import com.casic.detector.base.BaseApplication -import com.casic.detector.extensions.convertChinese -import com.casic.detector.extensions.separateResponseState -import com.casic.detector.extensions.toErrorMessage +import com.casic.detector.extensions.getResponseMessage +import com.casic.detector.extensions.getResponseState import com.casic.detector.model.MarkerFileModel +import com.casic.detector.model.MarkerModel import com.casic.detector.model.TaskModel import com.casic.detector.retrofit.RetrofitServiceManager import com.casic.detector.utils.DataBaseManager @@ -23,31 +23,57 @@ val markerFileResult = MutableLiveData() val taskResult = MutableLiveData() + val freeTaskResult = MutableLiveData() + + fun createFreeTask(patrollerId: String, description: String) = launch({ + val response = RetrofitServiceManager.createFreeTask(patrollerId, description) + if (response.getResponseState()) { +// freeTaskResult.value = + } else { + response.getResponseMessage().show(BaseApplication.get()) + } + }, { + it.printStackTrace() + }) + + fun uploadTaskMarker(taskId: String, models: List) = launch({ + loadState.value = LoadState.Loading + val response = RetrofitServiceManager.uploadTaskMarker(taskId, models) + if (response.getResponseState()) { + loadState.value = LoadState.Success + } else { + response.getResponseMessage().show(BaseApplication.get()) + loadState.value = LoadState.Fail + } + }, { + it.printStackTrace() + loadState.value = LoadState.Fail + }) fun getMarkerFile(userId: String, companyId: String) = launch({ val response = RetrofitServiceManager.getMarkerFile(userId, companyId) - if (response.separateResponseState()) { + if (response.getResponseState()) { markerFileResult.value = gson.fromJson( response, object : TypeToken() {}.type ) } else { - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { - it.convertChinese().show(BaseApplication.get()) + it.printStackTrace() }) fun getTask(userName: String) = launch({ val response = RetrofitServiceManager.getTask(userName) - if (response.separateResponseState()) { + if (response.getResponseState()) { taskResult.value = gson.fromJson( response, object : TypeToken() {}.type ) } else { - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { - it.convertChinese().show(BaseApplication.get()) + it.printStackTrace() }) fun installLabel( @@ -109,21 +135,21 @@ memo, realPaths ) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { + it.printStackTrace() loadState.value = LoadState.Fail - it.convertChinese().show(BaseApplication.get()) }) fun uploadTask(userId: String, taskId: String?, state: String) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.uploadTask(userId, taskId, state) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success if (state == "2") { "工单${taskId}已提交!".show(BaseApplication.get()) @@ -133,38 +159,38 @@ } } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { + it.printStackTrace() loadState.value = LoadState.Fail - it.convertChinese().show(BaseApplication.get()) }) fun uploadEvent(taskId: String, event: String, realPaths: ArrayList) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.uploadEvent(taskId, event, realPaths) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { + it.printStackTrace() loadState.value = LoadState.Fail - it.convertChinese().show(BaseApplication.get()) }) fun uploadMarker(userName: String, taskDetailId: String) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.uploadMarker(userName, taskDetailId) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { + it.printStackTrace() loadState.value = LoadState.Fail - it.convertChinese().show(BaseApplication.get()) }) } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/vm/UserViewModel.kt b/app/src/main/java/com/casic/detector/vm/UserViewModel.kt index f593aa8..9d8ea24 100644 --- a/app/src/main/java/com/casic/detector/vm/UserViewModel.kt +++ b/app/src/main/java/com/casic/detector/vm/UserViewModel.kt @@ -2,8 +2,8 @@ import androidx.lifecycle.MutableLiveData import com.casic.detector.base.BaseApplication -import com.casic.detector.extensions.separateResponseState -import com.casic.detector.extensions.toErrorMessage +import com.casic.detector.extensions.getResponseMessage +import com.casic.detector.extensions.getResponseState import com.casic.detector.model.LoginResultModel import com.casic.detector.retrofit.RetrofitServiceManager import com.google.gson.Gson @@ -25,14 +25,14 @@ fun login(account: String, password: String) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.login(account, password) - if (response.separateResponseState()) { + if (response.getResponseState()) { loadState.value = LoadState.Success loginResult.value = gson.fromJson( response, object : TypeToken() {}.type ) } else { loadState.value = LoadState.Fail - response.toErrorMessage().show(BaseApplication.get()) + response.getResponseMessage().show(BaseApplication.get()) } }, { loadState.value = LoadState.Fail