diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt index ff6de36..56382d6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt @@ -56,7 +56,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -98,19 +103,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, 1) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt index ff6de36..56382d6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt @@ -56,7 +56,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -98,19 +103,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, 1) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt index c3b1e6b..df5e1df 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt @@ -64,7 +64,7 @@ //根据id查询详情 id = intent.getStringExtra("id")!! val detailViewModel = ViewModelProvider(this).get(InspectDetailViewModel::class.java) - detailViewModel.getInspectDetail(id) + detailViewModel.getInspectDetail(id = id) detailViewModel.resultModel.observe(this, Observer { val detailData = it.data!! inspectNameView.text = detailData.name @@ -96,7 +96,7 @@ addMarker(LatLng(detailData.endLat, detailData.endLng), R.mipmap.zhongdian) // val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(id) + taskViewModel.searchByInspection(inspectionId = id) taskViewModel.resultModel.observe(this@InspectRouteActivity, Observer { taskModel -> taskModel.data!!.list?.forEach { listBean -> addMarker(LatLng(listBean.lat, listBean.lng), R.mipmap.jingshi) diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt index ff6de36..56382d6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt @@ -56,7 +56,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -98,19 +103,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, 1) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt index c3b1e6b..df5e1df 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt @@ -64,7 +64,7 @@ //根据id查询详情 id = intent.getStringExtra("id")!! val detailViewModel = ViewModelProvider(this).get(InspectDetailViewModel::class.java) - detailViewModel.getInspectDetail(id) + detailViewModel.getInspectDetail(id = id) detailViewModel.resultModel.observe(this, Observer { val detailData = it.data!! inspectNameView.text = detailData.name @@ -96,7 +96,7 @@ addMarker(LatLng(detailData.endLat, detailData.endLng), R.mipmap.zhongdian) // val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(id) + taskViewModel.searchByInspection(inspectionId = id) taskViewModel.resultModel.observe(this@InspectRouteActivity, Observer { taskModel -> taskModel.data!!.list?.forEach { listBean -> addMarker(LatLng(listBean.lat, listBean.lng), R.mipmap.jingshi) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt index 03e0f95..f370c99 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt @@ -50,7 +50,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -93,19 +98,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { if (it.code == 200) { diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt index ff6de36..56382d6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt @@ -56,7 +56,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -98,19 +103,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, 1) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt index c3b1e6b..df5e1df 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt @@ -64,7 +64,7 @@ //根据id查询详情 id = intent.getStringExtra("id")!! val detailViewModel = ViewModelProvider(this).get(InspectDetailViewModel::class.java) - detailViewModel.getInspectDetail(id) + detailViewModel.getInspectDetail(id = id) detailViewModel.resultModel.observe(this, Observer { val detailData = it.data!! inspectNameView.text = detailData.name @@ -96,7 +96,7 @@ addMarker(LatLng(detailData.endLat, detailData.endLng), R.mipmap.zhongdian) // val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(id) + taskViewModel.searchByInspection(inspectionId = id) taskViewModel.resultModel.observe(this@InspectRouteActivity, Observer { taskModel -> taskModel.data!!.list?.forEach { listBean -> addMarker(LatLng(listBean.lat, listBean.lng), R.mipmap.jingshi) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt index 03e0f95..f370c99 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt @@ -50,7 +50,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -93,19 +98,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { if (it.code == 200) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt index dddea49..9ca5f8c 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt @@ -47,7 +47,7 @@ RSAUtils.encryptDataByPublicKey(userPassword.toByteArray(), publicKey!!) //登录并获取Token,POST请求 val loginInViewModel = ViewModelProvider(this).get(LoginInViewModel::class.java) - loginInViewModel.login(account, dataByPublicKey) + loginInViewModel.login(account = account, secretKey = dataByPublicKey) loginInViewModel.resultModel.observe(this, Observer { loginResult -> if (loginResult.code == 200) { AuthenticationHelper.saveToken(loginResult.data!!.token!!) diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt index ff6de36..56382d6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt @@ -56,7 +56,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -98,19 +103,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, 1) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt index c3b1e6b..df5e1df 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt @@ -64,7 +64,7 @@ //根据id查询详情 id = intent.getStringExtra("id")!! val detailViewModel = ViewModelProvider(this).get(InspectDetailViewModel::class.java) - detailViewModel.getInspectDetail(id) + detailViewModel.getInspectDetail(id = id) detailViewModel.resultModel.observe(this, Observer { val detailData = it.data!! inspectNameView.text = detailData.name @@ -96,7 +96,7 @@ addMarker(LatLng(detailData.endLat, detailData.endLng), R.mipmap.zhongdian) // val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(id) + taskViewModel.searchByInspection(inspectionId = id) taskViewModel.resultModel.observe(this@InspectRouteActivity, Observer { taskModel -> taskModel.data!!.list?.forEach { listBean -> addMarker(LatLng(listBean.lat, listBean.lng), R.mipmap.jingshi) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt index 03e0f95..f370c99 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt @@ -50,7 +50,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -93,19 +98,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { if (it.code == 200) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt index dddea49..9ca5f8c 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt @@ -47,7 +47,7 @@ RSAUtils.encryptDataByPublicKey(userPassword.toByteArray(), publicKey!!) //登录并获取Token,POST请求 val loginInViewModel = ViewModelProvider(this).get(LoginInViewModel::class.java) - loginInViewModel.login(account, dataByPublicKey) + loginInViewModel.login(account = account, secretKey = dataByPublicKey) loginInViewModel.resultModel.observe(this, Observer { loginResult -> if (loginResult.code == 200) { AuthenticationHelper.saveToken(loginResult.data!!.token!!) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt index 9e21422..28b7649 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt @@ -4,33 +4,40 @@ import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic +import android.content.Context import android.content.Intent import android.os.Bundle import android.os.Handler import android.os.Message +import android.os.Vibrator import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider +import com.aihook.alertview.library.AlertView +import com.aihook.alertview.library.OnItemClickListener import com.amap.api.location.AMapLocation import com.amap.api.maps.AMap import com.amap.api.maps.AMapOptions +import com.amap.api.maps.AMapUtils +import com.amap.api.maps.model.LatLng import com.amap.api.maps.model.MyLocationStyle import com.casic.birmm.inspect.R import com.casic.birmm.inspect.bean.BlueToothBean import com.casic.birmm.inspect.bean.InspectionBean +import com.casic.birmm.inspect.bean.LocationBean import com.casic.birmm.inspect.extensions.id import com.casic.birmm.inspect.extensions.show import com.casic.birmm.inspect.extensions.toAsciiString import com.casic.birmm.inspect.extensions.toDecStringList -import com.casic.birmm.inspect.model.TaskRecordModel import com.casic.birmm.inspect.model.UserInfoModel import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.utils.callback.ILocationListener import com.casic.birmm.inspect.utils.callback.OnBleConnectListener import com.casic.birmm.inspect.utils.callback.OnDeviceSearchListener -import com.casic.birmm.inspect.vm.TaskRecordViewModel +import com.casic.birmm.inspect.vm.AddInspectionViewModel +import com.casic.birmm.inspect.vm.NewEventViewModel import com.casic.birmm.inspect.widgets.InputDialog import com.casic.birmm.inspect.widgets.SingleChoiceDialog import com.google.gson.Gson @@ -62,15 +69,25 @@ private lateinit var locationStyle: MyLocationStyle private lateinit var inspectionName: String private lateinit var inspectionId: String - private lateinit var inspectionBean: InspectionBean private lateinit var userDataModel: UserInfoModel.DataBean - private var dataBeans: MutableList = ArrayList() - private var routes: MutableList = ArrayList()//路线集和 + private lateinit var inspectionBean: InspectionBean + private lateinit var vibrator: Vibrator + private var routes: MutableList = ArrayList()//路线点集和 private var gson: Gson = Gson() private var isBluetoothOn = true private var blueToothBeans: MutableList = ArrayList()//搜索展示列表 private var currentDevice: BluetoothDevice? = null// 当前蓝牙设备 private var curConnectState = false + private var isInspectionCompleted = + SaveKeyValues.getValue(Constant.INSPECTION_COMPLETED, true) as Boolean + private val isOpenWarning = + SaveKeyValues.getValue(Constant.OPEN_WARNING, false) as Boolean + private val isAutoRecord = + SaveKeyValues.getValue(Constant.AUTO_RECORD, false) as Boolean + private var isGeneratingTask = false + private lateinit var eventViewModel: NewEventViewModel + private var alarmCount = 0 + private lateinit var addInspectionViewModel: AddInspectionViewModel init { weakReferenceHandler = WeakReferenceHandler(this) @@ -158,6 +175,26 @@ mapActivity.currentValueView.text = dataList[0] mapActivity.settingsValueView.text = dataList[2] mapActivity.maxValueView.text = dataList[3] + //判断是否需要报警 + if (mapActivity.isOpenWarning) { + if (dataList[0].toInt() >= dataList[2].toInt()) { + //当前值大于设置值,需要报警 + OtherUtils.playSound(mapActivity, R.raw.alarm) + mapActivity.vibrator.vibrate(1000) + if (mapActivity.isAutoRecord) { + //如果连续超过10个报警,自动生成报警事件 + mapActivity.alarmCount++ + if (mapActivity.alarmCount >= 10) { + mapActivity.generateAlarmTask(dataList[3]) + } + } + } else { + //当测量值小于设置值,报警还未结束,需要停止响铃 + OtherUtils.playSound(mapActivity, 0) + } + } else { + Log.d(Tag, "报警开关未打开,不处理") + } } else { Log.d(Tag, "设备返回值长度异常,无法解析") } @@ -183,6 +220,44 @@ } } + //生成报警事件 + private fun generateAlarmTask(maxValue: String) { + if (isGeneratingTask) return + isGeneratingTask = true + LocationHelper.obtainCurrentLocation(this, object : ILocationListener { + override fun onAMapLocationGet(aMapLocation: AMapLocation) { + eventViewModel.addEventTask( + id = "t".id(), + inspectionId = inspectionId, + name = inspectionName, + createTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + type = "报警事件", + lng = aMapLocation.longitude, + lat = aMapLocation.latitude, + data = maxValue.toDouble(), + images = "", + description = "自动报警记录", + user = userDataModel.name!! + ) + eventViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "保存成功".show(this@MapActivity) + isGeneratingTask = false + alarmCount = 0 + } + }) + eventViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "生成事件中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } + }) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_map) @@ -207,7 +282,9 @@ fun initData() { val userModelJson = SaveKeyValues.getValue(Constant.USER_OBJECT, "") as String userDataModel = gson.fromJson(userModelJson, UserInfoModel::class.java).data!! - + vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator + eventViewModel = ViewModelProvider(this).get(NewEventViewModel::class.java) + addInspectionViewModel = ViewModelProvider(this).get(AddInspectionViewModel::class.java) if (BLEManager.initBle(this)) { if (BLEManager.isEnable()) { BLEManager.openBluetooth(this, false) @@ -222,7 +299,7 @@ if (!curConnectState) { OtherUtils.showLoadingDialog(this, "正在连接[${currentDevice!!.name}]...") BLEManager.connectBleDevice( - this, currentDevice!!, 15000, + this, currentDevice!!, 10000, Constant.SERVICE_UUID, Constant.READ_CHARACTERISTIC_UUID, Constant.WRITE_CHARACTERISTIC_UUID, @@ -232,34 +309,28 @@ } private val onBleConnectListener = object : OnBleConnectListener { - override fun onConnecting( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice? - ) { + override fun onConnecting(bluetoothGatt: BluetoothGatt?) { + } - override fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int - ) { + override fun onConnectSuccess(bluetoothGatt: BluetoothGatt?, status: Int) { + } override fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_FAILURE weakReferenceHandler.sendMessage(message) } - override fun onDisConnecting( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice? - ) { + override fun onDisConnecting(bluetoothGatt: BluetoothGatt?) { } override fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.DISCONNECT_SUCCESS @@ -268,8 +339,7 @@ } override fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_SUCCESS @@ -277,8 +347,7 @@ } override fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_FAILURE @@ -301,8 +370,7 @@ } override fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.SEND_SUCCESS @@ -311,8 +379,7 @@ } override fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.SEND_FAILURE @@ -334,16 +401,14 @@ //显示定位小蓝点 locationStyle = MyLocationStyle() locationStyle.showMyLocation(true)//设置是否显示定位小蓝点 + locationStyle.interval(10 * 1000)////设置连续定位模式下的定位间隔,只在连续定位模式下生效。单位为毫秒 aMap.myLocationStyle = locationStyle aMap.isMyLocationEnabled = true - aMap.addOnMyLocationChangeListener { - // Log.d(Tag, "[" + it.longitude + "," + it.latitude + "]") - } } fun initEvent() { leftBackView.setOnClickListener { this.finish() } - //地图左边五个按钮事件 + //地图左边三个按钮事件 menuButtonEvent() //需要判断是否是上次未完的巡检 selectInspectMode() @@ -383,6 +448,16 @@ } private fun menuButtonEvent() { + //结束巡检 + stopInspectButton.setOnClickListener { + AlertView("温馨提示", "确定结束巡检吗?", "取消", arrayOf("确定"), + null, this, AlertView.Style.Alert, OnItemClickListener { _, position -> + if (position == 0) { + stopInspection() + } + }).show() + } + //蓝牙按钮 if (isBluetoothOn) { bluetoothButton.setImageResource(R.drawable.ic_bluetooth_enable) @@ -432,33 +507,13 @@ message.what = Constant.DISCOVERY_OUT_TIME weakReferenceHandler.sendMessage(message) } - }, 15 * 1000) + }, 5 * 1000) } private fun selectInspectMode() { - val currentInspection = SaveKeyValues.getValue(Constant.INSPECTION_OBJECT, "") as String - if (currentInspection != "") { - inspectionBean = gson.fromJson(currentInspection, InspectionBean::class.java) - /** - * 获取最近的Task - * */ - if (OtherUtils.isNetworkConnected(this)) { - val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(inspectionBean.serverMainId) - taskViewModel.resultModel.observe(this, Observer { - dataBeans = it.data!!.list!! - }) - } else { - dataBeans = - DataBaseManager.instance.queryTaskByInspection(inspectionBean.serverMainId)!! - } - "欢迎回来,继续未完成巡检".show(this) - } else { - InputDialog.Builder().setContext(this) - .setTitle("请输入巡检标签") - .setHintText("如:xxx区间巡检") - .setNegativeButton("放弃") - .setPositiveButton("开始巡检") + if (isInspectionCompleted) { + InputDialog.Builder().setContext(this).setTitle("请输入巡检标签") + .setHintText("如:xxx区间巡检").setNegativeButton("放弃").setPositiveButton("开始巡检") .setOnDialogClickListener(object : InputDialog.OnDialogButtonClickListener { override fun onConfirmClick(input: String) { val currentTimeMillis = System.currentTimeMillis() @@ -466,21 +521,17 @@ inspectionId = currentTimeMillis.id() inspectionName = input - inspectionBean = - InspectionBean( - System.currentTimeMillis(), - inspectionId, - inspectionName, - TimeOrDateUtil.timestampToCompleteDate(currentTimeMillis), - "", - TimeOrDateUtil.timestampToDate(currentTimeMillis), - 0.0, - 0.0, - 0.0, - 0.0, - "", - userDataModel.name - ) + //设置标题和时间 + inspectNameView.text = inspectionName + val completeDate = TimeOrDateUtil.timestampToCompleteDate(currentTimeMillis) + inspectTimeView.text = completeDate + + inspectionBean = InspectionBean( + currentTimeMillis, inspectionId, inspectionName, completeDate, + "", TimeOrDateUtil.timestampToDate(currentTimeMillis), + 0.0, 0.0, 0.0, 0.0, + "", userDataModel.name + ) startInspection() } @@ -489,28 +540,96 @@ } }) .build().show() + } else { + val currentInspection = SaveKeyValues.getValue(Constant.INSPECTION_OBJECT, "") as String + inspectionBean = gson.fromJson(currentInspection, InspectionBean::class.java) + "欢迎回来,继续未完成巡检".show(this) + startInspection() } } /** - * 开始巡检,获取坐标,开定时器,和设备交互 + * 开始巡检 + * 开始巡检,获取坐标,并且一定已经初始化了inspectionBean * */ - fun startInspection() { - LocationHelper.obtainCurrentLocation(this, object : ILocationListener { - override fun onAMapLocationGet(aMapLocation: AMapLocation) { - routes.add(aMapLocation) - inspectionBean.routes = gson.toJson(routes) - if (inspectionBean.startLng == 0.0) { - inspectionBean.startLng = aMapLocation.longitude - inspectionBean.startLat = aMapLocation.latitude + private fun startInspection() { + Log.d(Tag, "开始巡检") + aMap.addOnMyLocationChangeListener { + if (routes.size <= 2) { + routes.add(LocationBean(it.longitude, it.latitude)) + } else { + //计算上一个点和当前点的距离 + val last = routes.size - 1 + val distance = AMapUtils.calculateLineDistance( + LatLng(routes[last].lat, routes[last].lng), + LatLng(it.latitude, it.longitude) + ) + if (distance >= 10) { + routes.add(LocationBean(it.longitude, it.latitude)) + } else { + Log.d(Tag, "距离太近,无需添加路线点--->$last") } } - }) - updateCurrentInspection() + inspectionBean.routes = gson.toJson(routes) + if (inspectionBean.startLng == 0.0) { + inspectionBean.startLng = it.longitude + inspectionBean.startLat = it.latitude + } + //根据定位时间间隔不停的覆盖保存巡检记录 + SaveKeyValues.putValue(Constant.INSPECTION_OBJECT, gson.toJson(inspectionBean)) + } } - private fun updateCurrentInspection() { - SaveKeyValues.putValue(Constant.INSPECTION_OBJECT, gson.toJson(inspectionBean)) + /** + * 结束巡检 + * 不管是手动结束还是页面销毁导致的结束,都保存巡检数据 + * */ + private fun stopInspection() { + LocationHelper.obtainCurrentLocation(this, object : ILocationListener { + override fun onAMapLocationGet(aMapLocation: AMapLocation) { + routes.add( + LocationBean(aMapLocation.longitude, aMapLocation.latitude) + ) + /** + * 提交数据到后台 + * 上传成功之后finish当前页面并提示 + * 提交失败按照保存本地的逻辑走 + * + * 如果保存到本地,则需要设置标识位,然后finish当前页面并提示 + * */ + addInspectionViewModel.addInspection( + id = inspectionId, + name = inspectionName, + startTime = inspectionBean.startTime, + endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + date = inspectionBean.date, + startLng = inspectionBean.startLng, + startLat = inspectionBean.startLat, + endLng = aMapLocation.longitude, + endLat = aMapLocation.latitude, + routes = gson.toJson(routes), + user = userDataModel.name!! + ) + addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "巡检记录保存成功".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) + } else { + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) + } + finish() + }) + addInspectionViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } + }) } /***以下是地图生命周期管理************************************************************************/ @@ -533,7 +652,8 @@ override fun onDestroy() { cancel() super.onDestroy() + stopInspection() + BLEManager.disConnectDevice() mapView.onDestroy() - BroadcastManager.instance.destroy() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt index ff6de36..56382d6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt @@ -56,7 +56,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -98,19 +103,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, 1) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt index c3b1e6b..df5e1df 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt @@ -64,7 +64,7 @@ //根据id查询详情 id = intent.getStringExtra("id")!! val detailViewModel = ViewModelProvider(this).get(InspectDetailViewModel::class.java) - detailViewModel.getInspectDetail(id) + detailViewModel.getInspectDetail(id = id) detailViewModel.resultModel.observe(this, Observer { val detailData = it.data!! inspectNameView.text = detailData.name @@ -96,7 +96,7 @@ addMarker(LatLng(detailData.endLat, detailData.endLng), R.mipmap.zhongdian) // val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(id) + taskViewModel.searchByInspection(inspectionId = id) taskViewModel.resultModel.observe(this@InspectRouteActivity, Observer { taskModel -> taskModel.data!!.list?.forEach { listBean -> addMarker(LatLng(listBean.lat, listBean.lng), R.mipmap.jingshi) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt index 03e0f95..f370c99 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt @@ -50,7 +50,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -93,19 +98,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { if (it.code == 200) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt index dddea49..9ca5f8c 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt @@ -47,7 +47,7 @@ RSAUtils.encryptDataByPublicKey(userPassword.toByteArray(), publicKey!!) //登录并获取Token,POST请求 val loginInViewModel = ViewModelProvider(this).get(LoginInViewModel::class.java) - loginInViewModel.login(account, dataByPublicKey) + loginInViewModel.login(account = account, secretKey = dataByPublicKey) loginInViewModel.resultModel.observe(this, Observer { loginResult -> if (loginResult.code == 200) { AuthenticationHelper.saveToken(loginResult.data!!.token!!) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt index 9e21422..28b7649 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt @@ -4,33 +4,40 @@ import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic +import android.content.Context import android.content.Intent import android.os.Bundle import android.os.Handler import android.os.Message +import android.os.Vibrator import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider +import com.aihook.alertview.library.AlertView +import com.aihook.alertview.library.OnItemClickListener import com.amap.api.location.AMapLocation import com.amap.api.maps.AMap import com.amap.api.maps.AMapOptions +import com.amap.api.maps.AMapUtils +import com.amap.api.maps.model.LatLng import com.amap.api.maps.model.MyLocationStyle import com.casic.birmm.inspect.R import com.casic.birmm.inspect.bean.BlueToothBean import com.casic.birmm.inspect.bean.InspectionBean +import com.casic.birmm.inspect.bean.LocationBean import com.casic.birmm.inspect.extensions.id import com.casic.birmm.inspect.extensions.show import com.casic.birmm.inspect.extensions.toAsciiString import com.casic.birmm.inspect.extensions.toDecStringList -import com.casic.birmm.inspect.model.TaskRecordModel import com.casic.birmm.inspect.model.UserInfoModel import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.utils.callback.ILocationListener import com.casic.birmm.inspect.utils.callback.OnBleConnectListener import com.casic.birmm.inspect.utils.callback.OnDeviceSearchListener -import com.casic.birmm.inspect.vm.TaskRecordViewModel +import com.casic.birmm.inspect.vm.AddInspectionViewModel +import com.casic.birmm.inspect.vm.NewEventViewModel import com.casic.birmm.inspect.widgets.InputDialog import com.casic.birmm.inspect.widgets.SingleChoiceDialog import com.google.gson.Gson @@ -62,15 +69,25 @@ private lateinit var locationStyle: MyLocationStyle private lateinit var inspectionName: String private lateinit var inspectionId: String - private lateinit var inspectionBean: InspectionBean private lateinit var userDataModel: UserInfoModel.DataBean - private var dataBeans: MutableList = ArrayList() - private var routes: MutableList = ArrayList()//路线集和 + private lateinit var inspectionBean: InspectionBean + private lateinit var vibrator: Vibrator + private var routes: MutableList = ArrayList()//路线点集和 private var gson: Gson = Gson() private var isBluetoothOn = true private var blueToothBeans: MutableList = ArrayList()//搜索展示列表 private var currentDevice: BluetoothDevice? = null// 当前蓝牙设备 private var curConnectState = false + private var isInspectionCompleted = + SaveKeyValues.getValue(Constant.INSPECTION_COMPLETED, true) as Boolean + private val isOpenWarning = + SaveKeyValues.getValue(Constant.OPEN_WARNING, false) as Boolean + private val isAutoRecord = + SaveKeyValues.getValue(Constant.AUTO_RECORD, false) as Boolean + private var isGeneratingTask = false + private lateinit var eventViewModel: NewEventViewModel + private var alarmCount = 0 + private lateinit var addInspectionViewModel: AddInspectionViewModel init { weakReferenceHandler = WeakReferenceHandler(this) @@ -158,6 +175,26 @@ mapActivity.currentValueView.text = dataList[0] mapActivity.settingsValueView.text = dataList[2] mapActivity.maxValueView.text = dataList[3] + //判断是否需要报警 + if (mapActivity.isOpenWarning) { + if (dataList[0].toInt() >= dataList[2].toInt()) { + //当前值大于设置值,需要报警 + OtherUtils.playSound(mapActivity, R.raw.alarm) + mapActivity.vibrator.vibrate(1000) + if (mapActivity.isAutoRecord) { + //如果连续超过10个报警,自动生成报警事件 + mapActivity.alarmCount++ + if (mapActivity.alarmCount >= 10) { + mapActivity.generateAlarmTask(dataList[3]) + } + } + } else { + //当测量值小于设置值,报警还未结束,需要停止响铃 + OtherUtils.playSound(mapActivity, 0) + } + } else { + Log.d(Tag, "报警开关未打开,不处理") + } } else { Log.d(Tag, "设备返回值长度异常,无法解析") } @@ -183,6 +220,44 @@ } } + //生成报警事件 + private fun generateAlarmTask(maxValue: String) { + if (isGeneratingTask) return + isGeneratingTask = true + LocationHelper.obtainCurrentLocation(this, object : ILocationListener { + override fun onAMapLocationGet(aMapLocation: AMapLocation) { + eventViewModel.addEventTask( + id = "t".id(), + inspectionId = inspectionId, + name = inspectionName, + createTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + type = "报警事件", + lng = aMapLocation.longitude, + lat = aMapLocation.latitude, + data = maxValue.toDouble(), + images = "", + description = "自动报警记录", + user = userDataModel.name!! + ) + eventViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "保存成功".show(this@MapActivity) + isGeneratingTask = false + alarmCount = 0 + } + }) + eventViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "生成事件中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } + }) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_map) @@ -207,7 +282,9 @@ fun initData() { val userModelJson = SaveKeyValues.getValue(Constant.USER_OBJECT, "") as String userDataModel = gson.fromJson(userModelJson, UserInfoModel::class.java).data!! - + vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator + eventViewModel = ViewModelProvider(this).get(NewEventViewModel::class.java) + addInspectionViewModel = ViewModelProvider(this).get(AddInspectionViewModel::class.java) if (BLEManager.initBle(this)) { if (BLEManager.isEnable()) { BLEManager.openBluetooth(this, false) @@ -222,7 +299,7 @@ if (!curConnectState) { OtherUtils.showLoadingDialog(this, "正在连接[${currentDevice!!.name}]...") BLEManager.connectBleDevice( - this, currentDevice!!, 15000, + this, currentDevice!!, 10000, Constant.SERVICE_UUID, Constant.READ_CHARACTERISTIC_UUID, Constant.WRITE_CHARACTERISTIC_UUID, @@ -232,34 +309,28 @@ } private val onBleConnectListener = object : OnBleConnectListener { - override fun onConnecting( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice? - ) { + override fun onConnecting(bluetoothGatt: BluetoothGatt?) { + } - override fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int - ) { + override fun onConnectSuccess(bluetoothGatt: BluetoothGatt?, status: Int) { + } override fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_FAILURE weakReferenceHandler.sendMessage(message) } - override fun onDisConnecting( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice? - ) { + override fun onDisConnecting(bluetoothGatt: BluetoothGatt?) { } override fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.DISCONNECT_SUCCESS @@ -268,8 +339,7 @@ } override fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_SUCCESS @@ -277,8 +347,7 @@ } override fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_FAILURE @@ -301,8 +370,7 @@ } override fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.SEND_SUCCESS @@ -311,8 +379,7 @@ } override fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.SEND_FAILURE @@ -334,16 +401,14 @@ //显示定位小蓝点 locationStyle = MyLocationStyle() locationStyle.showMyLocation(true)//设置是否显示定位小蓝点 + locationStyle.interval(10 * 1000)////设置连续定位模式下的定位间隔,只在连续定位模式下生效。单位为毫秒 aMap.myLocationStyle = locationStyle aMap.isMyLocationEnabled = true - aMap.addOnMyLocationChangeListener { - // Log.d(Tag, "[" + it.longitude + "," + it.latitude + "]") - } } fun initEvent() { leftBackView.setOnClickListener { this.finish() } - //地图左边五个按钮事件 + //地图左边三个按钮事件 menuButtonEvent() //需要判断是否是上次未完的巡检 selectInspectMode() @@ -383,6 +448,16 @@ } private fun menuButtonEvent() { + //结束巡检 + stopInspectButton.setOnClickListener { + AlertView("温馨提示", "确定结束巡检吗?", "取消", arrayOf("确定"), + null, this, AlertView.Style.Alert, OnItemClickListener { _, position -> + if (position == 0) { + stopInspection() + } + }).show() + } + //蓝牙按钮 if (isBluetoothOn) { bluetoothButton.setImageResource(R.drawable.ic_bluetooth_enable) @@ -432,33 +507,13 @@ message.what = Constant.DISCOVERY_OUT_TIME weakReferenceHandler.sendMessage(message) } - }, 15 * 1000) + }, 5 * 1000) } private fun selectInspectMode() { - val currentInspection = SaveKeyValues.getValue(Constant.INSPECTION_OBJECT, "") as String - if (currentInspection != "") { - inspectionBean = gson.fromJson(currentInspection, InspectionBean::class.java) - /** - * 获取最近的Task - * */ - if (OtherUtils.isNetworkConnected(this)) { - val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(inspectionBean.serverMainId) - taskViewModel.resultModel.observe(this, Observer { - dataBeans = it.data!!.list!! - }) - } else { - dataBeans = - DataBaseManager.instance.queryTaskByInspection(inspectionBean.serverMainId)!! - } - "欢迎回来,继续未完成巡检".show(this) - } else { - InputDialog.Builder().setContext(this) - .setTitle("请输入巡检标签") - .setHintText("如:xxx区间巡检") - .setNegativeButton("放弃") - .setPositiveButton("开始巡检") + if (isInspectionCompleted) { + InputDialog.Builder().setContext(this).setTitle("请输入巡检标签") + .setHintText("如:xxx区间巡检").setNegativeButton("放弃").setPositiveButton("开始巡检") .setOnDialogClickListener(object : InputDialog.OnDialogButtonClickListener { override fun onConfirmClick(input: String) { val currentTimeMillis = System.currentTimeMillis() @@ -466,21 +521,17 @@ inspectionId = currentTimeMillis.id() inspectionName = input - inspectionBean = - InspectionBean( - System.currentTimeMillis(), - inspectionId, - inspectionName, - TimeOrDateUtil.timestampToCompleteDate(currentTimeMillis), - "", - TimeOrDateUtil.timestampToDate(currentTimeMillis), - 0.0, - 0.0, - 0.0, - 0.0, - "", - userDataModel.name - ) + //设置标题和时间 + inspectNameView.text = inspectionName + val completeDate = TimeOrDateUtil.timestampToCompleteDate(currentTimeMillis) + inspectTimeView.text = completeDate + + inspectionBean = InspectionBean( + currentTimeMillis, inspectionId, inspectionName, completeDate, + "", TimeOrDateUtil.timestampToDate(currentTimeMillis), + 0.0, 0.0, 0.0, 0.0, + "", userDataModel.name + ) startInspection() } @@ -489,28 +540,96 @@ } }) .build().show() + } else { + val currentInspection = SaveKeyValues.getValue(Constant.INSPECTION_OBJECT, "") as String + inspectionBean = gson.fromJson(currentInspection, InspectionBean::class.java) + "欢迎回来,继续未完成巡检".show(this) + startInspection() } } /** - * 开始巡检,获取坐标,开定时器,和设备交互 + * 开始巡检 + * 开始巡检,获取坐标,并且一定已经初始化了inspectionBean * */ - fun startInspection() { - LocationHelper.obtainCurrentLocation(this, object : ILocationListener { - override fun onAMapLocationGet(aMapLocation: AMapLocation) { - routes.add(aMapLocation) - inspectionBean.routes = gson.toJson(routes) - if (inspectionBean.startLng == 0.0) { - inspectionBean.startLng = aMapLocation.longitude - inspectionBean.startLat = aMapLocation.latitude + private fun startInspection() { + Log.d(Tag, "开始巡检") + aMap.addOnMyLocationChangeListener { + if (routes.size <= 2) { + routes.add(LocationBean(it.longitude, it.latitude)) + } else { + //计算上一个点和当前点的距离 + val last = routes.size - 1 + val distance = AMapUtils.calculateLineDistance( + LatLng(routes[last].lat, routes[last].lng), + LatLng(it.latitude, it.longitude) + ) + if (distance >= 10) { + routes.add(LocationBean(it.longitude, it.latitude)) + } else { + Log.d(Tag, "距离太近,无需添加路线点--->$last") } } - }) - updateCurrentInspection() + inspectionBean.routes = gson.toJson(routes) + if (inspectionBean.startLng == 0.0) { + inspectionBean.startLng = it.longitude + inspectionBean.startLat = it.latitude + } + //根据定位时间间隔不停的覆盖保存巡检记录 + SaveKeyValues.putValue(Constant.INSPECTION_OBJECT, gson.toJson(inspectionBean)) + } } - private fun updateCurrentInspection() { - SaveKeyValues.putValue(Constant.INSPECTION_OBJECT, gson.toJson(inspectionBean)) + /** + * 结束巡检 + * 不管是手动结束还是页面销毁导致的结束,都保存巡检数据 + * */ + private fun stopInspection() { + LocationHelper.obtainCurrentLocation(this, object : ILocationListener { + override fun onAMapLocationGet(aMapLocation: AMapLocation) { + routes.add( + LocationBean(aMapLocation.longitude, aMapLocation.latitude) + ) + /** + * 提交数据到后台 + * 上传成功之后finish当前页面并提示 + * 提交失败按照保存本地的逻辑走 + * + * 如果保存到本地,则需要设置标识位,然后finish当前页面并提示 + * */ + addInspectionViewModel.addInspection( + id = inspectionId, + name = inspectionName, + startTime = inspectionBean.startTime, + endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + date = inspectionBean.date, + startLng = inspectionBean.startLng, + startLat = inspectionBean.startLat, + endLng = aMapLocation.longitude, + endLat = aMapLocation.latitude, + routes = gson.toJson(routes), + user = userDataModel.name!! + ) + addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "巡检记录保存成功".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) + } else { + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) + } + finish() + }) + addInspectionViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } + }) } /***以下是地图生命周期管理************************************************************************/ @@ -533,7 +652,8 @@ override fun onDestroy() { cancel() super.onDestroy() + stopInspection() + BLEManager.disConnectDevice() mapView.onDestroy() - BroadcastManager.instance.destroy() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt index 8887a06..36b796f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt @@ -180,17 +180,17 @@ //随机生成主键 userModel.data!!.name?.let { user -> viewModel.addEventTask( - "t".id(), - inspectionId, - inspectionName, - eventCreateTimeView.text.toString(), - type, - longitude, - latitude, - data, - imagePaths.reformat(), - eventEditTextView.text.toString().filterString(), - user + id = "t".id(), + inspectionId = inspectionId, + name = inspectionName, + createTime = eventCreateTimeView.text.toString(), + type = type, + lng = longitude, + lat = latitude, + data = data, + images = imagePaths.reformat(), + description = eventEditTextView.text.toString().filterString(), + user = user ) } } @@ -223,13 +223,13 @@ continue } //上传图片 - uploadImageViewModel.uploadImage(file) + uploadImageViewModel.uploadImage(image = file) } } PictureConfig.REQUEST_CAMERA -> { val cameraResult = PictureSelector.obtainMultipleResult(data)[0] //上传图片 - uploadImageViewModel.uploadImage(File(cameraResult.compressPath)) + uploadImageViewModel.uploadImage(image = File(cameraResult.compressPath)) } } } diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt index ff6de36..56382d6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt @@ -56,7 +56,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -98,19 +103,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, 1) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt index c3b1e6b..df5e1df 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt @@ -64,7 +64,7 @@ //根据id查询详情 id = intent.getStringExtra("id")!! val detailViewModel = ViewModelProvider(this).get(InspectDetailViewModel::class.java) - detailViewModel.getInspectDetail(id) + detailViewModel.getInspectDetail(id = id) detailViewModel.resultModel.observe(this, Observer { val detailData = it.data!! inspectNameView.text = detailData.name @@ -96,7 +96,7 @@ addMarker(LatLng(detailData.endLat, detailData.endLng), R.mipmap.zhongdian) // val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(id) + taskViewModel.searchByInspection(inspectionId = id) taskViewModel.resultModel.observe(this@InspectRouteActivity, Observer { taskModel -> taskModel.data!!.list?.forEach { listBean -> addMarker(LatLng(listBean.lat, listBean.lng), R.mipmap.jingshi) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt index 03e0f95..f370c99 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt @@ -50,7 +50,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -93,19 +98,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { if (it.code == 200) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt index dddea49..9ca5f8c 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt @@ -47,7 +47,7 @@ RSAUtils.encryptDataByPublicKey(userPassword.toByteArray(), publicKey!!) //登录并获取Token,POST请求 val loginInViewModel = ViewModelProvider(this).get(LoginInViewModel::class.java) - loginInViewModel.login(account, dataByPublicKey) + loginInViewModel.login(account = account, secretKey = dataByPublicKey) loginInViewModel.resultModel.observe(this, Observer { loginResult -> if (loginResult.code == 200) { AuthenticationHelper.saveToken(loginResult.data!!.token!!) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt index 9e21422..28b7649 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt @@ -4,33 +4,40 @@ import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic +import android.content.Context import android.content.Intent import android.os.Bundle import android.os.Handler import android.os.Message +import android.os.Vibrator import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider +import com.aihook.alertview.library.AlertView +import com.aihook.alertview.library.OnItemClickListener import com.amap.api.location.AMapLocation import com.amap.api.maps.AMap import com.amap.api.maps.AMapOptions +import com.amap.api.maps.AMapUtils +import com.amap.api.maps.model.LatLng import com.amap.api.maps.model.MyLocationStyle import com.casic.birmm.inspect.R import com.casic.birmm.inspect.bean.BlueToothBean import com.casic.birmm.inspect.bean.InspectionBean +import com.casic.birmm.inspect.bean.LocationBean import com.casic.birmm.inspect.extensions.id import com.casic.birmm.inspect.extensions.show import com.casic.birmm.inspect.extensions.toAsciiString import com.casic.birmm.inspect.extensions.toDecStringList -import com.casic.birmm.inspect.model.TaskRecordModel import com.casic.birmm.inspect.model.UserInfoModel import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.utils.callback.ILocationListener import com.casic.birmm.inspect.utils.callback.OnBleConnectListener import com.casic.birmm.inspect.utils.callback.OnDeviceSearchListener -import com.casic.birmm.inspect.vm.TaskRecordViewModel +import com.casic.birmm.inspect.vm.AddInspectionViewModel +import com.casic.birmm.inspect.vm.NewEventViewModel import com.casic.birmm.inspect.widgets.InputDialog import com.casic.birmm.inspect.widgets.SingleChoiceDialog import com.google.gson.Gson @@ -62,15 +69,25 @@ private lateinit var locationStyle: MyLocationStyle private lateinit var inspectionName: String private lateinit var inspectionId: String - private lateinit var inspectionBean: InspectionBean private lateinit var userDataModel: UserInfoModel.DataBean - private var dataBeans: MutableList = ArrayList() - private var routes: MutableList = ArrayList()//路线集和 + private lateinit var inspectionBean: InspectionBean + private lateinit var vibrator: Vibrator + private var routes: MutableList = ArrayList()//路线点集和 private var gson: Gson = Gson() private var isBluetoothOn = true private var blueToothBeans: MutableList = ArrayList()//搜索展示列表 private var currentDevice: BluetoothDevice? = null// 当前蓝牙设备 private var curConnectState = false + private var isInspectionCompleted = + SaveKeyValues.getValue(Constant.INSPECTION_COMPLETED, true) as Boolean + private val isOpenWarning = + SaveKeyValues.getValue(Constant.OPEN_WARNING, false) as Boolean + private val isAutoRecord = + SaveKeyValues.getValue(Constant.AUTO_RECORD, false) as Boolean + private var isGeneratingTask = false + private lateinit var eventViewModel: NewEventViewModel + private var alarmCount = 0 + private lateinit var addInspectionViewModel: AddInspectionViewModel init { weakReferenceHandler = WeakReferenceHandler(this) @@ -158,6 +175,26 @@ mapActivity.currentValueView.text = dataList[0] mapActivity.settingsValueView.text = dataList[2] mapActivity.maxValueView.text = dataList[3] + //判断是否需要报警 + if (mapActivity.isOpenWarning) { + if (dataList[0].toInt() >= dataList[2].toInt()) { + //当前值大于设置值,需要报警 + OtherUtils.playSound(mapActivity, R.raw.alarm) + mapActivity.vibrator.vibrate(1000) + if (mapActivity.isAutoRecord) { + //如果连续超过10个报警,自动生成报警事件 + mapActivity.alarmCount++ + if (mapActivity.alarmCount >= 10) { + mapActivity.generateAlarmTask(dataList[3]) + } + } + } else { + //当测量值小于设置值,报警还未结束,需要停止响铃 + OtherUtils.playSound(mapActivity, 0) + } + } else { + Log.d(Tag, "报警开关未打开,不处理") + } } else { Log.d(Tag, "设备返回值长度异常,无法解析") } @@ -183,6 +220,44 @@ } } + //生成报警事件 + private fun generateAlarmTask(maxValue: String) { + if (isGeneratingTask) return + isGeneratingTask = true + LocationHelper.obtainCurrentLocation(this, object : ILocationListener { + override fun onAMapLocationGet(aMapLocation: AMapLocation) { + eventViewModel.addEventTask( + id = "t".id(), + inspectionId = inspectionId, + name = inspectionName, + createTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + type = "报警事件", + lng = aMapLocation.longitude, + lat = aMapLocation.latitude, + data = maxValue.toDouble(), + images = "", + description = "自动报警记录", + user = userDataModel.name!! + ) + eventViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "保存成功".show(this@MapActivity) + isGeneratingTask = false + alarmCount = 0 + } + }) + eventViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "生成事件中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } + }) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_map) @@ -207,7 +282,9 @@ fun initData() { val userModelJson = SaveKeyValues.getValue(Constant.USER_OBJECT, "") as String userDataModel = gson.fromJson(userModelJson, UserInfoModel::class.java).data!! - + vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator + eventViewModel = ViewModelProvider(this).get(NewEventViewModel::class.java) + addInspectionViewModel = ViewModelProvider(this).get(AddInspectionViewModel::class.java) if (BLEManager.initBle(this)) { if (BLEManager.isEnable()) { BLEManager.openBluetooth(this, false) @@ -222,7 +299,7 @@ if (!curConnectState) { OtherUtils.showLoadingDialog(this, "正在连接[${currentDevice!!.name}]...") BLEManager.connectBleDevice( - this, currentDevice!!, 15000, + this, currentDevice!!, 10000, Constant.SERVICE_UUID, Constant.READ_CHARACTERISTIC_UUID, Constant.WRITE_CHARACTERISTIC_UUID, @@ -232,34 +309,28 @@ } private val onBleConnectListener = object : OnBleConnectListener { - override fun onConnecting( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice? - ) { + override fun onConnecting(bluetoothGatt: BluetoothGatt?) { + } - override fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int - ) { + override fun onConnectSuccess(bluetoothGatt: BluetoothGatt?, status: Int) { + } override fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_FAILURE weakReferenceHandler.sendMessage(message) } - override fun onDisConnecting( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice? - ) { + override fun onDisConnecting(bluetoothGatt: BluetoothGatt?) { } override fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.DISCONNECT_SUCCESS @@ -268,8 +339,7 @@ } override fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_SUCCESS @@ -277,8 +347,7 @@ } override fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_FAILURE @@ -301,8 +370,7 @@ } override fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.SEND_SUCCESS @@ -311,8 +379,7 @@ } override fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.SEND_FAILURE @@ -334,16 +401,14 @@ //显示定位小蓝点 locationStyle = MyLocationStyle() locationStyle.showMyLocation(true)//设置是否显示定位小蓝点 + locationStyle.interval(10 * 1000)////设置连续定位模式下的定位间隔,只在连续定位模式下生效。单位为毫秒 aMap.myLocationStyle = locationStyle aMap.isMyLocationEnabled = true - aMap.addOnMyLocationChangeListener { - // Log.d(Tag, "[" + it.longitude + "," + it.latitude + "]") - } } fun initEvent() { leftBackView.setOnClickListener { this.finish() } - //地图左边五个按钮事件 + //地图左边三个按钮事件 menuButtonEvent() //需要判断是否是上次未完的巡检 selectInspectMode() @@ -383,6 +448,16 @@ } private fun menuButtonEvent() { + //结束巡检 + stopInspectButton.setOnClickListener { + AlertView("温馨提示", "确定结束巡检吗?", "取消", arrayOf("确定"), + null, this, AlertView.Style.Alert, OnItemClickListener { _, position -> + if (position == 0) { + stopInspection() + } + }).show() + } + //蓝牙按钮 if (isBluetoothOn) { bluetoothButton.setImageResource(R.drawable.ic_bluetooth_enable) @@ -432,33 +507,13 @@ message.what = Constant.DISCOVERY_OUT_TIME weakReferenceHandler.sendMessage(message) } - }, 15 * 1000) + }, 5 * 1000) } private fun selectInspectMode() { - val currentInspection = SaveKeyValues.getValue(Constant.INSPECTION_OBJECT, "") as String - if (currentInspection != "") { - inspectionBean = gson.fromJson(currentInspection, InspectionBean::class.java) - /** - * 获取最近的Task - * */ - if (OtherUtils.isNetworkConnected(this)) { - val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(inspectionBean.serverMainId) - taskViewModel.resultModel.observe(this, Observer { - dataBeans = it.data!!.list!! - }) - } else { - dataBeans = - DataBaseManager.instance.queryTaskByInspection(inspectionBean.serverMainId)!! - } - "欢迎回来,继续未完成巡检".show(this) - } else { - InputDialog.Builder().setContext(this) - .setTitle("请输入巡检标签") - .setHintText("如:xxx区间巡检") - .setNegativeButton("放弃") - .setPositiveButton("开始巡检") + if (isInspectionCompleted) { + InputDialog.Builder().setContext(this).setTitle("请输入巡检标签") + .setHintText("如:xxx区间巡检").setNegativeButton("放弃").setPositiveButton("开始巡检") .setOnDialogClickListener(object : InputDialog.OnDialogButtonClickListener { override fun onConfirmClick(input: String) { val currentTimeMillis = System.currentTimeMillis() @@ -466,21 +521,17 @@ inspectionId = currentTimeMillis.id() inspectionName = input - inspectionBean = - InspectionBean( - System.currentTimeMillis(), - inspectionId, - inspectionName, - TimeOrDateUtil.timestampToCompleteDate(currentTimeMillis), - "", - TimeOrDateUtil.timestampToDate(currentTimeMillis), - 0.0, - 0.0, - 0.0, - 0.0, - "", - userDataModel.name - ) + //设置标题和时间 + inspectNameView.text = inspectionName + val completeDate = TimeOrDateUtil.timestampToCompleteDate(currentTimeMillis) + inspectTimeView.text = completeDate + + inspectionBean = InspectionBean( + currentTimeMillis, inspectionId, inspectionName, completeDate, + "", TimeOrDateUtil.timestampToDate(currentTimeMillis), + 0.0, 0.0, 0.0, 0.0, + "", userDataModel.name + ) startInspection() } @@ -489,28 +540,96 @@ } }) .build().show() + } else { + val currentInspection = SaveKeyValues.getValue(Constant.INSPECTION_OBJECT, "") as String + inspectionBean = gson.fromJson(currentInspection, InspectionBean::class.java) + "欢迎回来,继续未完成巡检".show(this) + startInspection() } } /** - * 开始巡检,获取坐标,开定时器,和设备交互 + * 开始巡检 + * 开始巡检,获取坐标,并且一定已经初始化了inspectionBean * */ - fun startInspection() { - LocationHelper.obtainCurrentLocation(this, object : ILocationListener { - override fun onAMapLocationGet(aMapLocation: AMapLocation) { - routes.add(aMapLocation) - inspectionBean.routes = gson.toJson(routes) - if (inspectionBean.startLng == 0.0) { - inspectionBean.startLng = aMapLocation.longitude - inspectionBean.startLat = aMapLocation.latitude + private fun startInspection() { + Log.d(Tag, "开始巡检") + aMap.addOnMyLocationChangeListener { + if (routes.size <= 2) { + routes.add(LocationBean(it.longitude, it.latitude)) + } else { + //计算上一个点和当前点的距离 + val last = routes.size - 1 + val distance = AMapUtils.calculateLineDistance( + LatLng(routes[last].lat, routes[last].lng), + LatLng(it.latitude, it.longitude) + ) + if (distance >= 10) { + routes.add(LocationBean(it.longitude, it.latitude)) + } else { + Log.d(Tag, "距离太近,无需添加路线点--->$last") } } - }) - updateCurrentInspection() + inspectionBean.routes = gson.toJson(routes) + if (inspectionBean.startLng == 0.0) { + inspectionBean.startLng = it.longitude + inspectionBean.startLat = it.latitude + } + //根据定位时间间隔不停的覆盖保存巡检记录 + SaveKeyValues.putValue(Constant.INSPECTION_OBJECT, gson.toJson(inspectionBean)) + } } - private fun updateCurrentInspection() { - SaveKeyValues.putValue(Constant.INSPECTION_OBJECT, gson.toJson(inspectionBean)) + /** + * 结束巡检 + * 不管是手动结束还是页面销毁导致的结束,都保存巡检数据 + * */ + private fun stopInspection() { + LocationHelper.obtainCurrentLocation(this, object : ILocationListener { + override fun onAMapLocationGet(aMapLocation: AMapLocation) { + routes.add( + LocationBean(aMapLocation.longitude, aMapLocation.latitude) + ) + /** + * 提交数据到后台 + * 上传成功之后finish当前页面并提示 + * 提交失败按照保存本地的逻辑走 + * + * 如果保存到本地,则需要设置标识位,然后finish当前页面并提示 + * */ + addInspectionViewModel.addInspection( + id = inspectionId, + name = inspectionName, + startTime = inspectionBean.startTime, + endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + date = inspectionBean.date, + startLng = inspectionBean.startLng, + startLat = inspectionBean.startLat, + endLng = aMapLocation.longitude, + endLat = aMapLocation.latitude, + routes = gson.toJson(routes), + user = userDataModel.name!! + ) + addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "巡检记录保存成功".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) + } else { + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) + } + finish() + }) + addInspectionViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } + }) } /***以下是地图生命周期管理************************************************************************/ @@ -533,7 +652,8 @@ override fun onDestroy() { cancel() super.onDestroy() + stopInspection() + BLEManager.disConnectDevice() mapView.onDestroy() - BroadcastManager.instance.destroy() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt index 8887a06..36b796f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt @@ -180,17 +180,17 @@ //随机生成主键 userModel.data!!.name?.let { user -> viewModel.addEventTask( - "t".id(), - inspectionId, - inspectionName, - eventCreateTimeView.text.toString(), - type, - longitude, - latitude, - data, - imagePaths.reformat(), - eventEditTextView.text.toString().filterString(), - user + id = "t".id(), + inspectionId = inspectionId, + name = inspectionName, + createTime = eventCreateTimeView.text.toString(), + type = type, + lng = longitude, + lat = latitude, + data = data, + images = imagePaths.reformat(), + description = eventEditTextView.text.toString().filterString(), + user = user ) } } @@ -223,13 +223,13 @@ continue } //上传图片 - uploadImageViewModel.uploadImage(file) + uploadImageViewModel.uploadImage(image = file) } } PictureConfig.REQUEST_CAMERA -> { val cameraResult = PictureSelector.obtainMultipleResult(data)[0] //上传图片 - uploadImageViewModel.uploadImage(File(cameraResult.compressPath)) + uploadImageViewModel.uploadImage(image = File(cameraResult.compressPath)) } } } diff --git a/app/src/main/java/com/casic/birmm/inspect/vm/AddInspectionViewModel.kt b/app/src/main/java/com/casic/birmm/inspect/vm/AddInspectionViewModel.kt new file mode 100644 index 0000000..b9c0d26 --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/vm/AddInspectionViewModel.kt @@ -0,0 +1,26 @@ +package com.casic.birmm.inspect.vm + +import androidx.lifecycle.MutableLiveData +import com.casic.birmm.inspect.base.BaseViewModel +import com.casic.birmm.inspect.extensions.launch +import com.casic.birmm.inspect.model.ActionResultModel +import com.casic.birmm.inspect.utils.LoadState +import com.casic.birmm.inspect.utils.retrofit.RetrofitServiceManager + +class AddInspectionViewModel : BaseViewModel() { + val resultModel = MutableLiveData() + + fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ) = launch({ + loadState.value = LoadState.Loading + resultModel.value = RetrofitServiceManager.addInspection( + id, name, startTime, endTime, date, startLng, startLat, endLng, endLat, routes, user + ) + loadState.value = LoadState.Success + }, { + loadState.value = LoadState.Fail + }) +} \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java new file mode 100644 index 0000000..ebcac7c --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/bean/LocationBean.java @@ -0,0 +1,27 @@ +package com.casic.birmm.inspect.bean; + +public class LocationBean { + private double lng; + private double lat; + + public LocationBean(double lng, double lat) { + this.lng = lng; + this.lat = lat; + } + + public double getLng() { + return lng; + } + + public void setLng(double lng) { + this.lng = lng; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } +} diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt index a1fcddc..df9fb17 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/BLEManager.kt @@ -113,8 +113,11 @@ if (device == null) return if (device.name != null) { Log.d(Tag, device.name + "-->" + device.address) - if (onDeviceSearchListener != null) { - onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) //扫描到设备回调 + //去掉非必要的数据,比如mobike的蓝牙的等数据 + if (device.name != "mobike" || device.name != "") { + if (onDeviceSearchListener != null) { + onDeviceSearchListener!!.onDeviceFound(BlueToothBean(device, rssi)) + } } } } @@ -139,10 +142,10 @@ outTime: Long, serviceUUID: String, readUUID: String, writeUUID: String, onBleConnectListener: OnBleConnectListener - ): BluetoothGatt? { + ) { if (isConnecting) { Log.d(Tag, "connectBleDevice()-->isConnecting = true") - return null + return } this.curConnectDevice = bluetoothDevice this.serviceUUID = UUID.fromString(serviceUUID) @@ -153,13 +156,11 @@ try { bluetoothGatt = bluetoothDevice.connectGatt(context, false, bluetoothGattCallback) bluetoothGatt!!.connect() - isConnecting = true } catch (e: Exception) { e.printStackTrace() } //设置连接超时时间10s handler.postDelayed(connectOutTimeRunnable, outTime) - return bluetoothGatt } private val bluetoothGattCallback = object : BluetoothGattCallback() { @@ -174,7 +175,7 @@ BluetoothGatt.STATE_CONNECTING -> { Log.d(Tag, "正在连接...") if (onBleConnectListener != null) { - onBleConnectListener!!.onConnecting(gatt, bluetoothDevice) //正在连接回调 + onBleConnectListener!!.onConnecting(gatt) //正在连接回调 } } BluetoothGatt.STATE_CONNECTED -> { @@ -184,83 +185,55 @@ //设置发现服务超时时间 handler.postDelayed(serviceDiscoverOutTimeRunnable, MAX_CONNECT_TIME) if (onBleConnectListener != null) { - onBleConnectListener!!.onConnectSuccess( - gatt, - bluetoothDevice, - status - ) //连接成功回调 + onBleConnectListener!!.onConnectSuccess(gatt, status) } } BluetoothGatt.STATE_DISCONNECTING -> { Log.d(Tag, "正在断开...") if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnecting(gatt, bluetoothDevice) //正在断开回调 + onBleConnectListener!!.onDisConnecting(gatt) //正在断开回调 } } BluetoothGatt.STATE_DISCONNECTED -> { Log.d(Tag, "断开连接status: $status") gatt.close() + isConnecting = false when (status) { - 133 -> { - //无法连接 + 133 -> {//133连接异常,无法连接 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接异常!", - status - ) //133连接异常 异常断开 + gatt, "连接异常!", status + ) Log.d(Tag, "连接失败status:" + status + " " + bluetoothDevice.address) } } - 62 -> { + 62 -> {//62没有发现服务 异常断开 if (onBleConnectListener != null) { gatt.close() onBleConnectListener!!.onConnectFailure( - gatt, - bluetoothDevice, - "连接成功服务未发现断开!", - status - )//62没有发现服务 异常断开 - Log.d(Tag, "连接成功服务未发现断开status:$status") + gatt, "连接成功服务未发现断开!", status + ) } } - 0 -> { + 0 -> { //0正常断开 回调 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //0正常断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } 8 -> {//因为距离远或者电池无法供电断开连接 - // 已经成功发现服务 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //8断电断开 回调 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } - 34 -> { + 34 -> {//34断开 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //34断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } else -> {//其它断开连接 if (onBleConnectListener != null) { - onBleConnectListener!!.onDisConnectSuccess( - gatt, - bluetoothDevice, - status - ) //其它断开 + onBleConnectListener!!.onDisConnectSuccess(gatt, status) } } } @@ -270,24 +243,16 @@ override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) + isConnecting = false //移除发现服务超时 handler.removeCallbacks(serviceDiscoverOutTimeRunnable) //配置服务信息 - if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoverySucceed( - gatt, - gatt.device, - status - ) //成功发现服务回调 - } - } else { - if (onBleConnectListener != null) { - onBleConnectListener!!.onServiceDiscoveryFailed( - gatt, - gatt.device, - "获取服务特征异常" - ) //发现服务失败回调 + if (onBleConnectListener != null) { + if (setupService(gatt!!, serviceUUID, readUUID, writeUUID)) { + //成功发现服务回调 + onBleConnectListener!!.onServiceDiscoverySucceed(gatt, status) + } else { + onBleConnectListener!!.onServiceDiscoveryFailed(gatt, "获取服务特征异常") } } } @@ -303,8 +268,7 @@ //向蓝牙设备写入数据结果回调 override fun onCharacteristicWrite( gatt: BluetoothGatt?, - characteristic: BluetoothGattCharacteristic?, - status: Int + characteristic: BluetoothGattCharacteristic?, status: Int ) { super.onCharacteristicWrite(gatt, characteristic, status) if (characteristic!!.value == null) { @@ -316,22 +280,13 @@ BluetoothGatt.GATT_SUCCESS -> { //写入成功 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteSuccess( - gatt, - gatt!!.device, - characteristic.value - ) //写入成功回调 + onBleConnectListener!!.onWriteSuccess(gatt, characteristic.value) } } BluetoothGatt.GATT_FAILURE -> { //写入失败 if (onBleConnectListener != null) { - onBleConnectListener!!.onWriteFailure( - gatt, - gatt!!.device, - characteristic.value, - "写入失败" - ) //写入失败回调 + onBleConnectListener!!.onWriteFailure(gatt, characteristic.value, "写入失败") } } BluetoothGatt.GATT_WRITE_NOT_PERMITTED -> { @@ -345,11 +300,11 @@ ) { super.onCharacteristicChanged(gatt, characteristic) //接收数据 - Log.d(Tag, "收到数据:" + characteristic!!.value) + Log.d(Tag, "收到数据:" + characteristic!!.value.toList()) if (onBleConnectListener != null) { onBleConnectListener!!.onReceiveMessage(gatt, characteristic) //接收数据回调 } else { - Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null: ") + Log.d(Tag, "onCharacteristicChanged-->onBleConnectListener == null") } } @@ -395,7 +350,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "连接超时", -1 ) //连接失败回调 @@ -413,7 +367,6 @@ if (onBleConnectListener != null) { onBleConnectListener!!.onConnectFailure( bluetoothGatt, - curConnectDevice, "发现服务超时!", -1 ); //连接失败回调 @@ -480,5 +433,6 @@ return } bluetoothGatt!!.disconnect() + isConnecting = false } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt index 55549ec..ab0ce47 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/Constant.kt @@ -33,6 +33,7 @@ const val OPEN_WARNING = "isOpenWarning" const val AUTO_RECORD = "isRecordLog" const val INSPECTION_OBJECT = "inspectionObject" + const val INSPECTION_COMPLETED = "inspectionCompleted" val ASK_DEV_CODE_COMMAND = byteArrayOf(0x01, 0x0D, 0x0A)// 查询设备编号命令 val OPEN_TRANSFER_COMMAND = byteArrayOf(0x02, 0x0D, 0x0A) // 开启数据发送命令 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt index fa53beb..3eb72a6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/LocationHelper.kt @@ -12,7 +12,7 @@ fun obtainCurrentLocation(context: Context, listener: ILocationListener) { val locationClient = AMapLocationClient(context) val locationOption = AMapLocationClientOption() - locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy; + locationOption.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy locationOption.isNeedAddress = true//设置是否返回地址信息(默认返回地址信息) locationOption.isOnceLocation = true//设置是否只定位一次,默认为false locationOption.isMockEnable = false//设置是否允许模拟位置,默认为false,不允许模拟位置 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt index b0b05a0..751c709 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/callback/OnBleConnectListener.kt @@ -1,36 +1,33 @@ package com.casic.birmm.inspect.utils.callback -import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic interface OnBleConnectListener { - fun onConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在连接 + fun onConnecting(bluetoothGatt: BluetoothGatt?) //正在连接 fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //连接成功 fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) //连接失败 - fun onDisConnecting(bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?) //正在断开 + fun onDisConnecting(bluetoothGatt: BluetoothGatt?) //正在断开 fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) // 断开连接 fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) //发现服务成功 fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) //发现服务失败 fun onReceiveMessage( @@ -40,12 +37,11 @@ fun onReceiveError(errorMsg: String?) //接收数据出错 fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) //写入成功 fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) //写入失败 fun onReadRssi(bluetoothGatt: BluetoothGatt?, rssi: Int, status: Int) //成功读取到连接信号强度 diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt index c5c9a30..6b22ce0 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitService.kt @@ -113,4 +113,24 @@ @Field("description") description: String, @Field("user") user: String ): ActionResultModel + + /** + * 新增巡检记录 + */ + @FormUrlEncoded + @POST("/inspection/add") + suspend fun addInspection( + @Header("token") token: String, + @Field("id") id: String, + @Field("name") name: String, + @Field("startTime") startTime: String, + @Field("endTime") endTime: String, + @Field("date") date: String, + @Field("startLng") startLng: Double, + @Field("startLat") startLat: Double, + @Field("endLng") endLng: Double, + @Field("endLat") endLat: Double, + @Field("routes") routes: String, + @Field("user") user: String + ): ActionResultModel } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt index d24faff..6b0ac3d 100644 --- a/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt +++ b/app/src/main/java/com/casic/birmm/inspect/utils/retrofit/RetrofitServiceManager.kt @@ -45,17 +45,10 @@ * 巡检记录列表 */ suspend fun queryInspect( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): InspectListModel { return api.obtainInspectList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -64,17 +57,10 @@ * 事件记录列表 */ suspend fun queryEvent( - keywords: String, - startTime: String, - endTime: String, - pageIndex: Int + keywords: String, startTime: String, endTime: String, pageIndex: Int ): EventListModel { return api.obtainEventList( - AuthenticationHelper.token!!, - keywords, - startTime, - endTime, - Constant.PAGE_LIMIT, + AuthenticationHelper.token!!, keywords, startTime, endTime, Constant.PAGE_LIMIT, pageIndex ) } @@ -110,8 +96,22 @@ lng: Double, lat: Double, data: Double, images: String, description: String, user: String ): ActionResultModel { return api.addEventTask( - AuthenticationHelper.token!!, id, inspectionId, name, createTime, - type, lng, lat, data, images, description, user + AuthenticationHelper.token!!, id, inspectionId, name, createTime, type, lng, lat, data, + images, description, user + ) + } + + /** + * 新增巡检记录 + */ + suspend fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ): ActionResultModel { + return api.addInspection( + AuthenticationHelper.token!!, id, name, startTime, endTime, date, startLng, startLat, + endLng, endLat, routes, user ) } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt index 7456901..cec8b6f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventListActivity.kt @@ -31,7 +31,7 @@ override fun initData() { val id = intent.getStringExtra("id")!! - viewModel.searchByInspection(id) + viewModel.searchByInspection(inspectionId = id) viewModel.resultModel.observe(this, Observer { if (it.code == 200) { if (it.data!!.list!!.isEmpty()) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt index ff6de36..56382d6 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/EventQueryActivity.kt @@ -56,7 +56,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -98,19 +103,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, 1) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryEvent(keywords, startTime, endTime, pageIndex) + viewModel.queryEvent( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt index c3b1e6b..df5e1df 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectRouteActivity.kt @@ -64,7 +64,7 @@ //根据id查询详情 id = intent.getStringExtra("id")!! val detailViewModel = ViewModelProvider(this).get(InspectDetailViewModel::class.java) - detailViewModel.getInspectDetail(id) + detailViewModel.getInspectDetail(id = id) detailViewModel.resultModel.observe(this, Observer { val detailData = it.data!! inspectNameView.text = detailData.name @@ -96,7 +96,7 @@ addMarker(LatLng(detailData.endLat, detailData.endLng), R.mipmap.zhongdian) // val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(id) + taskViewModel.searchByInspection(inspectionId = id) taskViewModel.resultModel.observe(this@InspectRouteActivity, Observer { taskModel -> taskModel.data!!.list?.forEach { listBean -> addMarker(LatLng(listBean.lat, listBean.lng), R.mipmap.jingshi) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt index 03e0f95..f370c99 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/InspectionQueryActivity.kt @@ -50,7 +50,12 @@ override fun initData() { weakReferenceHandler = WeakReferenceHandler(this) - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } override fun initEvent() { @@ -93,19 +98,34 @@ keywords = inputTipsView.text.toString() //查询都从第一页开始查 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnRefreshListener { isRefresh = true //刷新之后页码重置 pageIndex = 1 - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } refreshLayout.setOnLoadMoreListener { isLoadMore = true pageIndex++ - viewModel.queryInspect(keywords, startTime, endTime, pageIndex) + viewModel.queryInspect( + keywords = keywords, + startTime = startTime, + endTime = endTime, + pageIndex = pageIndex + ) } viewModel.resultModel.observe(this, Observer { if (it.code == 200) { diff --git a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt index dddea49..9ca5f8c 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/LoginActivity.kt @@ -47,7 +47,7 @@ RSAUtils.encryptDataByPublicKey(userPassword.toByteArray(), publicKey!!) //登录并获取Token,POST请求 val loginInViewModel = ViewModelProvider(this).get(LoginInViewModel::class.java) - loginInViewModel.login(account, dataByPublicKey) + loginInViewModel.login(account = account, secretKey = dataByPublicKey) loginInViewModel.resultModel.observe(this, Observer { loginResult -> if (loginResult.code == 200) { AuthenticationHelper.saveToken(loginResult.data!!.token!!) diff --git a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt index 9e21422..28b7649 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/MapActivity.kt @@ -4,33 +4,40 @@ import android.bluetooth.BluetoothDevice import android.bluetooth.BluetoothGatt import android.bluetooth.BluetoothGattCharacteristic +import android.content.Context import android.content.Intent import android.os.Bundle import android.os.Handler import android.os.Message +import android.os.Vibrator import android.util.Log import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider +import com.aihook.alertview.library.AlertView +import com.aihook.alertview.library.OnItemClickListener import com.amap.api.location.AMapLocation import com.amap.api.maps.AMap import com.amap.api.maps.AMapOptions +import com.amap.api.maps.AMapUtils +import com.amap.api.maps.model.LatLng import com.amap.api.maps.model.MyLocationStyle import com.casic.birmm.inspect.R import com.casic.birmm.inspect.bean.BlueToothBean import com.casic.birmm.inspect.bean.InspectionBean +import com.casic.birmm.inspect.bean.LocationBean import com.casic.birmm.inspect.extensions.id import com.casic.birmm.inspect.extensions.show import com.casic.birmm.inspect.extensions.toAsciiString import com.casic.birmm.inspect.extensions.toDecStringList -import com.casic.birmm.inspect.model.TaskRecordModel import com.casic.birmm.inspect.model.UserInfoModel import com.casic.birmm.inspect.utils.* import com.casic.birmm.inspect.utils.callback.ILocationListener import com.casic.birmm.inspect.utils.callback.OnBleConnectListener import com.casic.birmm.inspect.utils.callback.OnDeviceSearchListener -import com.casic.birmm.inspect.vm.TaskRecordViewModel +import com.casic.birmm.inspect.vm.AddInspectionViewModel +import com.casic.birmm.inspect.vm.NewEventViewModel import com.casic.birmm.inspect.widgets.InputDialog import com.casic.birmm.inspect.widgets.SingleChoiceDialog import com.google.gson.Gson @@ -62,15 +69,25 @@ private lateinit var locationStyle: MyLocationStyle private lateinit var inspectionName: String private lateinit var inspectionId: String - private lateinit var inspectionBean: InspectionBean private lateinit var userDataModel: UserInfoModel.DataBean - private var dataBeans: MutableList = ArrayList() - private var routes: MutableList = ArrayList()//路线集和 + private lateinit var inspectionBean: InspectionBean + private lateinit var vibrator: Vibrator + private var routes: MutableList = ArrayList()//路线点集和 private var gson: Gson = Gson() private var isBluetoothOn = true private var blueToothBeans: MutableList = ArrayList()//搜索展示列表 private var currentDevice: BluetoothDevice? = null// 当前蓝牙设备 private var curConnectState = false + private var isInspectionCompleted = + SaveKeyValues.getValue(Constant.INSPECTION_COMPLETED, true) as Boolean + private val isOpenWarning = + SaveKeyValues.getValue(Constant.OPEN_WARNING, false) as Boolean + private val isAutoRecord = + SaveKeyValues.getValue(Constant.AUTO_RECORD, false) as Boolean + private var isGeneratingTask = false + private lateinit var eventViewModel: NewEventViewModel + private var alarmCount = 0 + private lateinit var addInspectionViewModel: AddInspectionViewModel init { weakReferenceHandler = WeakReferenceHandler(this) @@ -158,6 +175,26 @@ mapActivity.currentValueView.text = dataList[0] mapActivity.settingsValueView.text = dataList[2] mapActivity.maxValueView.text = dataList[3] + //判断是否需要报警 + if (mapActivity.isOpenWarning) { + if (dataList[0].toInt() >= dataList[2].toInt()) { + //当前值大于设置值,需要报警 + OtherUtils.playSound(mapActivity, R.raw.alarm) + mapActivity.vibrator.vibrate(1000) + if (mapActivity.isAutoRecord) { + //如果连续超过10个报警,自动生成报警事件 + mapActivity.alarmCount++ + if (mapActivity.alarmCount >= 10) { + mapActivity.generateAlarmTask(dataList[3]) + } + } + } else { + //当测量值小于设置值,报警还未结束,需要停止响铃 + OtherUtils.playSound(mapActivity, 0) + } + } else { + Log.d(Tag, "报警开关未打开,不处理") + } } else { Log.d(Tag, "设备返回值长度异常,无法解析") } @@ -183,6 +220,44 @@ } } + //生成报警事件 + private fun generateAlarmTask(maxValue: String) { + if (isGeneratingTask) return + isGeneratingTask = true + LocationHelper.obtainCurrentLocation(this, object : ILocationListener { + override fun onAMapLocationGet(aMapLocation: AMapLocation) { + eventViewModel.addEventTask( + id = "t".id(), + inspectionId = inspectionId, + name = inspectionName, + createTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + type = "报警事件", + lng = aMapLocation.longitude, + lat = aMapLocation.latitude, + data = maxValue.toDouble(), + images = "", + description = "自动报警记录", + user = userDataModel.name!! + ) + eventViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "保存成功".show(this@MapActivity) + isGeneratingTask = false + alarmCount = 0 + } + }) + eventViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "生成事件中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } + }) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_map) @@ -207,7 +282,9 @@ fun initData() { val userModelJson = SaveKeyValues.getValue(Constant.USER_OBJECT, "") as String userDataModel = gson.fromJson(userModelJson, UserInfoModel::class.java).data!! - + vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator + eventViewModel = ViewModelProvider(this).get(NewEventViewModel::class.java) + addInspectionViewModel = ViewModelProvider(this).get(AddInspectionViewModel::class.java) if (BLEManager.initBle(this)) { if (BLEManager.isEnable()) { BLEManager.openBluetooth(this, false) @@ -222,7 +299,7 @@ if (!curConnectState) { OtherUtils.showLoadingDialog(this, "正在连接[${currentDevice!!.name}]...") BLEManager.connectBleDevice( - this, currentDevice!!, 15000, + this, currentDevice!!, 10000, Constant.SERVICE_UUID, Constant.READ_CHARACTERISTIC_UUID, Constant.WRITE_CHARACTERISTIC_UUID, @@ -232,34 +309,28 @@ } private val onBleConnectListener = object : OnBleConnectListener { - override fun onConnecting( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice? - ) { + override fun onConnecting(bluetoothGatt: BluetoothGatt?) { + } - override fun onConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, status: Int - ) { + override fun onConnectSuccess(bluetoothGatt: BluetoothGatt?, status: Int) { + } override fun onConnectFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - exception: String?, status: Int + bluetoothGatt: BluetoothGatt?, exception: String?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_FAILURE weakReferenceHandler.sendMessage(message) } - override fun onDisConnecting( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice? - ) { + override fun onDisConnecting(bluetoothGatt: BluetoothGatt?) { } override fun onDisConnectSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.DISCONNECT_SUCCESS @@ -268,8 +339,7 @@ } override fun onServiceDiscoverySucceed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - status: Int + bluetoothGatt: BluetoothGatt?, status: Int ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_SUCCESS @@ -277,8 +347,7 @@ } override fun onServiceDiscoveryFailed( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: String? + bluetoothGatt: BluetoothGatt?, msg: String? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.CONNECT_FAILURE @@ -301,8 +370,7 @@ } override fun onWriteSuccess( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray? + bluetoothGatt: BluetoothGatt?, msg: ByteArray? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.SEND_SUCCESS @@ -311,8 +379,7 @@ } override fun onWriteFailure( - bluetoothGatt: BluetoothGatt?, bluetoothDevice: BluetoothDevice?, - msg: ByteArray?, errorMsg: String? + bluetoothGatt: BluetoothGatt?, msg: ByteArray?, errorMsg: String? ) { val message = weakReferenceHandler.obtainMessage() message.what = Constant.SEND_FAILURE @@ -334,16 +401,14 @@ //显示定位小蓝点 locationStyle = MyLocationStyle() locationStyle.showMyLocation(true)//设置是否显示定位小蓝点 + locationStyle.interval(10 * 1000)////设置连续定位模式下的定位间隔,只在连续定位模式下生效。单位为毫秒 aMap.myLocationStyle = locationStyle aMap.isMyLocationEnabled = true - aMap.addOnMyLocationChangeListener { - // Log.d(Tag, "[" + it.longitude + "," + it.latitude + "]") - } } fun initEvent() { leftBackView.setOnClickListener { this.finish() } - //地图左边五个按钮事件 + //地图左边三个按钮事件 menuButtonEvent() //需要判断是否是上次未完的巡检 selectInspectMode() @@ -383,6 +448,16 @@ } private fun menuButtonEvent() { + //结束巡检 + stopInspectButton.setOnClickListener { + AlertView("温馨提示", "确定结束巡检吗?", "取消", arrayOf("确定"), + null, this, AlertView.Style.Alert, OnItemClickListener { _, position -> + if (position == 0) { + stopInspection() + } + }).show() + } + //蓝牙按钮 if (isBluetoothOn) { bluetoothButton.setImageResource(R.drawable.ic_bluetooth_enable) @@ -432,33 +507,13 @@ message.what = Constant.DISCOVERY_OUT_TIME weakReferenceHandler.sendMessage(message) } - }, 15 * 1000) + }, 5 * 1000) } private fun selectInspectMode() { - val currentInspection = SaveKeyValues.getValue(Constant.INSPECTION_OBJECT, "") as String - if (currentInspection != "") { - inspectionBean = gson.fromJson(currentInspection, InspectionBean::class.java) - /** - * 获取最近的Task - * */ - if (OtherUtils.isNetworkConnected(this)) { - val taskViewModel = ViewModelProvider(this).get(TaskRecordViewModel::class.java) - taskViewModel.searchByInspection(inspectionBean.serverMainId) - taskViewModel.resultModel.observe(this, Observer { - dataBeans = it.data!!.list!! - }) - } else { - dataBeans = - DataBaseManager.instance.queryTaskByInspection(inspectionBean.serverMainId)!! - } - "欢迎回来,继续未完成巡检".show(this) - } else { - InputDialog.Builder().setContext(this) - .setTitle("请输入巡检标签") - .setHintText("如:xxx区间巡检") - .setNegativeButton("放弃") - .setPositiveButton("开始巡检") + if (isInspectionCompleted) { + InputDialog.Builder().setContext(this).setTitle("请输入巡检标签") + .setHintText("如:xxx区间巡检").setNegativeButton("放弃").setPositiveButton("开始巡检") .setOnDialogClickListener(object : InputDialog.OnDialogButtonClickListener { override fun onConfirmClick(input: String) { val currentTimeMillis = System.currentTimeMillis() @@ -466,21 +521,17 @@ inspectionId = currentTimeMillis.id() inspectionName = input - inspectionBean = - InspectionBean( - System.currentTimeMillis(), - inspectionId, - inspectionName, - TimeOrDateUtil.timestampToCompleteDate(currentTimeMillis), - "", - TimeOrDateUtil.timestampToDate(currentTimeMillis), - 0.0, - 0.0, - 0.0, - 0.0, - "", - userDataModel.name - ) + //设置标题和时间 + inspectNameView.text = inspectionName + val completeDate = TimeOrDateUtil.timestampToCompleteDate(currentTimeMillis) + inspectTimeView.text = completeDate + + inspectionBean = InspectionBean( + currentTimeMillis, inspectionId, inspectionName, completeDate, + "", TimeOrDateUtil.timestampToDate(currentTimeMillis), + 0.0, 0.0, 0.0, 0.0, + "", userDataModel.name + ) startInspection() } @@ -489,28 +540,96 @@ } }) .build().show() + } else { + val currentInspection = SaveKeyValues.getValue(Constant.INSPECTION_OBJECT, "") as String + inspectionBean = gson.fromJson(currentInspection, InspectionBean::class.java) + "欢迎回来,继续未完成巡检".show(this) + startInspection() } } /** - * 开始巡检,获取坐标,开定时器,和设备交互 + * 开始巡检 + * 开始巡检,获取坐标,并且一定已经初始化了inspectionBean * */ - fun startInspection() { - LocationHelper.obtainCurrentLocation(this, object : ILocationListener { - override fun onAMapLocationGet(aMapLocation: AMapLocation) { - routes.add(aMapLocation) - inspectionBean.routes = gson.toJson(routes) - if (inspectionBean.startLng == 0.0) { - inspectionBean.startLng = aMapLocation.longitude - inspectionBean.startLat = aMapLocation.latitude + private fun startInspection() { + Log.d(Tag, "开始巡检") + aMap.addOnMyLocationChangeListener { + if (routes.size <= 2) { + routes.add(LocationBean(it.longitude, it.latitude)) + } else { + //计算上一个点和当前点的距离 + val last = routes.size - 1 + val distance = AMapUtils.calculateLineDistance( + LatLng(routes[last].lat, routes[last].lng), + LatLng(it.latitude, it.longitude) + ) + if (distance >= 10) { + routes.add(LocationBean(it.longitude, it.latitude)) + } else { + Log.d(Tag, "距离太近,无需添加路线点--->$last") } } - }) - updateCurrentInspection() + inspectionBean.routes = gson.toJson(routes) + if (inspectionBean.startLng == 0.0) { + inspectionBean.startLng = it.longitude + inspectionBean.startLat = it.latitude + } + //根据定位时间间隔不停的覆盖保存巡检记录 + SaveKeyValues.putValue(Constant.INSPECTION_OBJECT, gson.toJson(inspectionBean)) + } } - private fun updateCurrentInspection() { - SaveKeyValues.putValue(Constant.INSPECTION_OBJECT, gson.toJson(inspectionBean)) + /** + * 结束巡检 + * 不管是手动结束还是页面销毁导致的结束,都保存巡检数据 + * */ + private fun stopInspection() { + LocationHelper.obtainCurrentLocation(this, object : ILocationListener { + override fun onAMapLocationGet(aMapLocation: AMapLocation) { + routes.add( + LocationBean(aMapLocation.longitude, aMapLocation.latitude) + ) + /** + * 提交数据到后台 + * 上传成功之后finish当前页面并提示 + * 提交失败按照保存本地的逻辑走 + * + * 如果保存到本地,则需要设置标识位,然后finish当前页面并提示 + * */ + addInspectionViewModel.addInspection( + id = inspectionId, + name = inspectionName, + startTime = inspectionBean.startTime, + endTime = TimeOrDateUtil.timestampToCompleteDate(System.currentTimeMillis()), + date = inspectionBean.date, + startLng = inspectionBean.startLng, + startLat = inspectionBean.startLat, + endLng = aMapLocation.longitude, + endLat = aMapLocation.latitude, + routes = gson.toJson(routes), + user = userDataModel.name!! + ) + addInspectionViewModel.resultModel.observe(this@MapActivity, Observer { model -> + if (model.code == 200) { + "巡检记录保存成功".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, true) + } else { + "巡检记录保存失败,已替您缓存到本地".show(this@MapActivity) + SaveKeyValues.putValue(Constant.INSPECTION_COMPLETED, false) + } + finish() + }) + addInspectionViewModel.loadState.observe(this@MapActivity, Observer { + when (it) { + is LoadState.Loading -> { + OtherUtils.showLoadingDialog(this@MapActivity, "保存中,请稍后...") + } + else -> OtherUtils.dismissLoadingDialog() + } + }) + } + }) } /***以下是地图生命周期管理************************************************************************/ @@ -533,7 +652,8 @@ override fun onDestroy() { cancel() super.onDestroy() + stopInspection() + BLEManager.disConnectDevice() mapView.onDestroy() - BroadcastManager.instance.destroy() } } \ No newline at end of file diff --git a/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt b/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt index 8887a06..36b796f 100644 --- a/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt +++ b/app/src/main/java/com/casic/birmm/inspect/view/NewEventActivity.kt @@ -180,17 +180,17 @@ //随机生成主键 userModel.data!!.name?.let { user -> viewModel.addEventTask( - "t".id(), - inspectionId, - inspectionName, - eventCreateTimeView.text.toString(), - type, - longitude, - latitude, - data, - imagePaths.reformat(), - eventEditTextView.text.toString().filterString(), - user + id = "t".id(), + inspectionId = inspectionId, + name = inspectionName, + createTime = eventCreateTimeView.text.toString(), + type = type, + lng = longitude, + lat = latitude, + data = data, + images = imagePaths.reformat(), + description = eventEditTextView.text.toString().filterString(), + user = user ) } } @@ -223,13 +223,13 @@ continue } //上传图片 - uploadImageViewModel.uploadImage(file) + uploadImageViewModel.uploadImage(image = file) } } PictureConfig.REQUEST_CAMERA -> { val cameraResult = PictureSelector.obtainMultipleResult(data)[0] //上传图片 - uploadImageViewModel.uploadImage(File(cameraResult.compressPath)) + uploadImageViewModel.uploadImage(image = File(cameraResult.compressPath)) } } } diff --git a/app/src/main/java/com/casic/birmm/inspect/vm/AddInspectionViewModel.kt b/app/src/main/java/com/casic/birmm/inspect/vm/AddInspectionViewModel.kt new file mode 100644 index 0000000..b9c0d26 --- /dev/null +++ b/app/src/main/java/com/casic/birmm/inspect/vm/AddInspectionViewModel.kt @@ -0,0 +1,26 @@ +package com.casic.birmm.inspect.vm + +import androidx.lifecycle.MutableLiveData +import com.casic.birmm.inspect.base.BaseViewModel +import com.casic.birmm.inspect.extensions.launch +import com.casic.birmm.inspect.model.ActionResultModel +import com.casic.birmm.inspect.utils.LoadState +import com.casic.birmm.inspect.utils.retrofit.RetrofitServiceManager + +class AddInspectionViewModel : BaseViewModel() { + val resultModel = MutableLiveData() + + fun addInspection( + id: String, name: String, startTime: String, endTime: String, date: String, + startLng: Double, startLat: Double, endLng: Double, endLat: Double, routes: String, + user: String + ) = launch({ + loadState.value = LoadState.Loading + resultModel.value = RetrofitServiceManager.addInspection( + id, name, startTime, endTime, date, startLng, startLat, endLng, endLat, routes, user + ) + loadState.value = LoadState.Success + }, { + loadState.value = LoadState.Fail + }) +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_map.xml b/app/src/main/res/layout/activity_map.xml index 13102d9..80d79f1 100644 --- a/app/src/main/res/layout/activity_map.xml +++ b/app/src/main/res/layout/activity_map.xml @@ -59,7 +59,7 @@ android:layout_marginStart="5dp" android:fontFamily="sans-serif-medium" android:paddingVertical="3dp" - android:text="2021-07-13 11:39:11" + android:text="yyyy-MM-dd HH:mm:ss" android:textColor="@color/black" android:textSize="@dimen/textFontSize" app:layout_constraintEnd_toEndOf="parent" @@ -81,11 +81,6 @@ android:src="@drawable/ic_off" /> - - @@ -94,11 +89,6 @@ android:id="@+id/refreshButton" style="@style/imageButtonStyle" android:src="@drawable/ic_refresh" /> - -