管灌系统巡查员智能手机App
app/src/main/java/com/dayu/pipirrapp/tool/InspectionUtils.java
@@ -4,10 +4,14 @@
import android.location.Location;
import android.util.Log;
import androidx.room.Transaction;
import com.dayu.pipirrapp.MyApplication;
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.fragment.MapFragment;
import com.dayu.pipirrapp.utils.DateUtils;
import java.util.UUID;
@@ -26,9 +30,10 @@
    private static final String TAG = "InspectionUtils";
    //打点的最小两点最小距离
    private static final int MinMeters = 10;
    //0没有开始,1开始,2暂停,3关闭
    //0没有开始,1开始,2暂停,3关闭,4是点击的开始按钮
    public static final int NO_INSPECTION = 0;
    public static final int STAT_INSPECTION = 1;
    public static final int STAT_INSPECTION_ONCLICK = 4;
    public static final int PAUSE_INSPECTION = 2;
    public static final int STOP_INSPECTION = 3;
@@ -36,13 +41,31 @@
    /**
     * 获取当前巡检记录ID
     *
     * @param context
     * @param fragment
     * @return
     */
    public static String getInspectionId(Context context) {
    public static String getInspectionId(MapFragment fragment) {
        DaoSingleton.getAsynchInstance(fragment.getContext()).inspectionDao().getMostRecentInspectionWithNoStopTime()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io()).subscribe(inspectionBean -> {
                });
        return "";
    }
    public static void aginShowLocation(MapFragment fragment) {
        //查询当前未关闭的巡检记录
        DaoSingleton.getAsynchInstance(fragment.getContext()).inspectionDao().getMostRecentInspectionWithNoStopTime()
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io()).subscribe(inspectionBean -> {
                    fragment.mInspectionBean = inspectionBean;
                    // 查询当前未关闭的巡检记录下所有的坐标
                    DaoSingleton.getAsynchInstance(fragment.getContext()).inspectionLocationDao().findByInspectId(inspectionBean.getmInspectId()).subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread()).subscribe(inspectionLocationBeans -> {
                                fragment.aginShowLocation(inspectionLocationBeans);
                            });
                });
    }
@@ -57,6 +80,7 @@
        InspectionBean inspectionBean = new InspectionBean();
        inspectionBean.setmInspectId(UUID.randomUUID().toString());
        inspectionBean.setStartTime(DateUtils.getNowDateStr());
//        inspectionBean.
        // 异步插入到数据库
        DaoSingleton.getAsynchInstance(context)
                .inspectionDao()
@@ -66,7 +90,7 @@
                .subscribe(() -> {
                    Log.i(TAG, "Inspection started and inserted successfully.");
                }, throwable -> {
                    Log.e(TAG, "Error inserting inspection data: ", throwable);
                    Log.e(TAG, "Error inserting inspection data: "+throwable);
                });
        // 获取Dao并执行插入操作
        return inspectionBean;  // 插入完成后切换到主线程
@@ -80,7 +104,15 @@
     * @param locationBean
     */
    public static void addInspectionLocationData(Context context, InspectionLocationBean locationBean) {
        DaoSingleton.getAsynchInstance(context).inspectionLocationDao().insert(locationBean).subscribeOn(Schedulers.io());
        DaoSingleton.getAsynchInstance(context).inspectionLocationDao().insert(locationBean).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(() -> {
                    // 插入成功的回调
                    Log.d(TAG, "addInspectionLocationData数据插入成功");
                }, throwable -> {
                    // 处理错误
                    Log.e(TAG, "addInspectionLocationData数据插入失败", throwable);
                });
        ;
    }
    /**
@@ -89,8 +121,17 @@
     * @param context
     * @param locationBean
     */
    @Transaction
    public static void updateInspectionLocationData(Context context, InspectionLocationBean locationBean) {
        DaoSingleton.getAsynchInstance(context).inspectionLocationDao().update(locationBean).subscribeOn(Schedulers.io());
        DaoSingleton.getAsynchInstance(context).inspectionLocationDao().update(locationBean).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(() -> {
                    // 插入成功的回调
                    Log.d(TAG, "updateInspectionLocationData数据插入成功");
                }, throwable -> {
                    // 处理错误
                    Log.e(TAG, "updateInspectionLocationData数据插入失败", throwable);
                });
        ;
    }
    /**
@@ -115,4 +156,21 @@
        }
    }
    /**
     * 创建InspectionLocationBean
     */
    public static InspectionLocationBean createInspectionLocation(LatLonBean latLonBean, InspectionBean mInspectionBean) {
        InspectionLocationBean inspectionLocationBean = new InspectionLocationBean();
        inspectionLocationBean.setId(UUID.randomUUID().toString());
        inspectionLocationBean.setInspectId(mInspectionBean.getInspectId());
        inspectionLocationBean.setmInspectId(mInspectionBean.getmInspectId());
        inspectionLocationBean.setLocateTime(DateUtils.getNowDateStr());
        inspectionLocationBean.setPost(false);
        inspectionLocationBean.setInspectorId(MyApplication.myApplication.userId);
        inspectionLocationBean.setLng(String.valueOf(latLonBean.getLongitude()));
        inspectionLocationBean.setLat(String.valueOf(latLonBean.getLatitude()));
        return inspectionLocationBean;
    }
}