package com.dayu.pipirrapp.tool;
|
|
import android.content.Context;
|
import android.location.Location;
|
import android.util.Log;
|
|
import com.dayu.pipirrapp.bean.db.InspectionBean;
|
import com.dayu.pipirrapp.bean.db.InspectionLocationBean;
|
import com.dayu.pipirrapp.bean.db.LatLonBean;
|
import com.dayu.pipirrapp.dao.DaoSingleton;
|
import com.dayu.pipirrapp.utils.DateUtils;
|
|
import java.util.UUID;
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
import io.reactivex.rxjava3.schedulers.Schedulers;
|
|
/**
|
* InspectionUtils -巡检记录相关功能合集
|
*
|
* @author zuoxiao
|
* @version 1.0
|
* @since 2024-11-29
|
*/
|
public class InspectionUtils {
|
private static final String TAG = "InspectionUtils";
|
//打点的最小两点最小距离
|
private static final int MinMeters = 10;
|
//0没有开始,1开始,2暂停,3关闭
|
public static final int NO_INSPECTION = 0;
|
public static final int STAT_INSPECTION = 1;
|
public static final int PAUSE_INSPECTION = 2;
|
public static final int STOP_INSPECTION = 3;
|
|
|
/**
|
* 获取当前巡检记录ID
|
*
|
* @param context
|
* @return
|
*/
|
public static String getInspectionId(Context context) {
|
|
|
return "";
|
}
|
|
|
/**
|
* 开始巡检并插入巡检记录
|
*
|
* @param context 上下文
|
* @return Completable 表示插入完成或失败的流
|
*/
|
public static InspectionBean startInspection(Context context) {
|
// 创建巡检记录
|
InspectionBean inspectionBean = new InspectionBean();
|
inspectionBean.setmInspectId(UUID.randomUUID().toString());
|
inspectionBean.setStartTime(DateUtils.getNowDateStr());
|
// 异步插入到数据库
|
DaoSingleton.getAsynchInstance(context)
|
.inspectionDao()
|
.insert(inspectionBean) // 插入操作
|
.subscribeOn(Schedulers.io())
|
.observeOn(AndroidSchedulers.mainThread())
|
.subscribe(() -> {
|
Log.i(TAG, "Inspection started and inserted successfully.");
|
}, throwable -> {
|
Log.e(TAG, "Error inserting inspection data: ", throwable);
|
});
|
// 获取Dao并执行插入操作
|
return inspectionBean; // 插入完成后切换到主线程
|
}
|
|
|
/**
|
* 添加巡检记录坐标
|
*
|
* @param context
|
* @param locationBean
|
*/
|
public static void addInspectionLocationData(Context context, InspectionLocationBean locationBean) {
|
DaoSingleton.getAsynchInstance(context).inspectionLocationDao().insert(locationBean).subscribeOn(Schedulers.io());
|
}
|
|
/**
|
* 修改巡检记录坐标
|
*
|
* @param context
|
* @param locationBean
|
*/
|
public static void updateInspectionLocationData(Context context, InspectionLocationBean locationBean) {
|
DaoSingleton.getAsynchInstance(context).inspectionLocationDao().update(locationBean).subscribeOn(Schedulers.io());
|
}
|
|
/**
|
* 判断两个地点的距离是否大于设定的最小距离
|
*
|
* @param lastLocation 第一个地点
|
* @param newLocation 第二个地点
|
* @return 如果距离大于10米,返回true,否则返回false
|
*/
|
public static boolean isThanMinMeters(LatLonBean lastLocation, LatLonBean newLocation) {
|
//当lastLocation为null默认认为返回true
|
if (lastLocation != null) {
|
// 使用 Location.distanceBetween 计算两个点之间的距离,单位为米
|
float[] results = new float[1];
|
Location.distanceBetween(lastLocation.getLatitude(), lastLocation.getLongitude(), newLocation.getLatitude(), newLocation.getLongitude(), results);
|
float distanceInMeters = results[0];
|
|
// 判断距离是否大于10米
|
return distanceInMeters > MinMeters;
|
} else {
|
return true;
|
}
|
|
}
|
}
|