Prechádzať zdrojové kódy

1.重写物联控制

JaycePC 4 dní pred
rodič
commit
461018bcd0

+ 1 - 1
app/build.gradle

@@ -19,7 +19,7 @@ android {
         minSdk 31
         targetSdk 35
         versionCode 2
-        versionName "2.3"
+        versionName "2.4"
 
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
         room {

+ 6 - 3
app/src/main/AndroidManifest.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:tools="http://schemas.android.com/tools" >
+    xmlns:tools="http://schemas.android.com/tools">
 
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
@@ -33,7 +33,10 @@
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/Theme.西农电子信息牌"
-        android:usesCleartextTraffic="true" >
+        android:usesCleartextTraffic="true">
+        <activity
+            android:name=".main.things.ThingsListActivity"
+            android:exported="false" />
         <activity
             android:name=".home.setting.FaceListActivity"
             android:exported="false" />
@@ -118,7 +121,7 @@
             android:name=".StartActivity"
             android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
             android:exported="true"
-            android:launchMode="singleTop" >
+            android:launchMode="singleTop">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
 

+ 2 - 1
app/src/main/java/xn/xxp/main/MainActivity.kt

@@ -51,6 +51,7 @@ import xn.xxp.main.person.LaboratoryPersonActivity
 import xn.xxp.main.risk.RiskListActivity
 import xn.xxp.main.rule.RuleActivity
 import xn.xxp.main.things.ThingsActivity
+import xn.xxp.main.things.ThingsListActivity
 import xn.xxp.mqtt.event.BannerEvent
 import xn.xxp.mqtt.event.BulletinBoardEvent
 import xn.xxp.mqtt.event.CheckMachineEvent
@@ -368,7 +369,7 @@ class MainActivity :
         })
         // 物联控制
         binding.things.setOnClickListener(FastClickDelegate {
-            val intent = Intent(this, ThingsActivity::class.java)
+            val intent = Intent(this, ThingsListActivity::class.java)
             startActivity(intent)
         })
         // 签到

+ 3 - 4
app/src/main/java/xn/xxp/main/things/ThingsActivity.kt

@@ -3,7 +3,6 @@ package xn.xxp.main.things
 import android.os.Bundle
 import android.view.View
 import androidx.recyclerview.widget.GridLayoutManager
-import com.blankj.utilcode.util.LogUtils
 import com.chad.library.adapter.base.BaseQuickAdapter
 import com.chad.library.adapter.base.viewholder.BaseViewHolder
 import core.ui.activity.BaseCountDownActivity
@@ -21,7 +20,7 @@ import xn.xxp.room.RoomTool
 import xn.xxp.room.bean.LabConfig
 import xn.xxp.utils.Tool
 import xn.xxp.widget.ITitleBar
-import xn.xxp.widget.LotControllerView
+import xn.xxp.widget.IotControllerView
 import xn.xxp.widget.NavViewCompat
 
 /**
@@ -142,10 +141,10 @@ open class ThingsActivity :
 }
 
 private class LotThingsAdapter(private val switchListener: OnItemSwitchListener? = null) :
-    BaseQuickAdapter<LotDeviceVo, BaseViewHolder>(R.layout.item_lot_things) {
+    BaseQuickAdapter<LotDeviceVo, BaseViewHolder>(R.layout.item_iot_things) {
 
     override fun convert(holder: BaseViewHolder, item: LotDeviceVo) {
-        val itemView = holder.itemView as LotControllerView
+        val itemView = holder.itemView as IotControllerView
         val powerLot = isPowerLot(item.hardwareName)
         val resId = if (powerLot) R.mipmap.icon_dykz else R.mipmap.icon_dspf_fs
         itemView.setLotInfo(item.hardwareName, resId)

+ 144 - 0
app/src/main/java/xn/xxp/main/things/ThingsListActivity.java

@@ -0,0 +1,144 @@
+package xn.xxp.main.things;
+
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.AdapterView;
+
+import androidx.activity.EdgeToEdge;
+import androidx.annotation.NonNull;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.core.graphics.Insets;
+import androidx.core.view.ViewCompat;
+import androidx.core.view.WindowInsetsCompat;
+
+import com.blankj.utilcode.util.LogUtils;
+import com.blankj.utilcode.util.ThreadUtils;
+
+import org.greenrobot.eventbus.EventBus;
+import org.greenrobot.eventbus.Subscribe;
+import org.greenrobot.eventbus.ThreadMode;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import core.ui.activity.BaseActivity;
+import core.ui.activity.BaseCountDownActivity;
+import http.client.ApiRepository;
+import http.vo.request.ControllerCMD;
+import http.vo.response.LotDeviceVo;
+import io.reactivex.rxjava3.functions.Consumer;
+import xn.xxp.R;
+import xn.xxp.databinding.ActivityThingsListBinding;
+import xn.xxp.mqtt.event.ThingsEvent;
+import xn.xxp.room.RoomTool;
+import xn.xxp.room.bean.LabConfig;
+import xn.xxp.widget.ITitleBar;
+import xn.xxp.widget.NavViewCompat;
+
+public class ThingsListActivity extends BaseCountDownActivity<ActivityThingsListBinding> {
+    private ActivityThingsListBinding binding;
+    private LabConfig labConfig;
+    private List<LotDeviceVo> lotDeviceVoList = new ArrayList<>();
+    private ThingsListAdapter thingsListAdapter;
+
+    @Override
+    protected ActivityThingsListBinding createViewBinding() {
+        binding = ActivityThingsListBinding.inflate(getLayoutInflater());
+        return binding;
+    }
+
+    @Override
+    protected void initViews(Bundle savedInstanceState) {
+        super.initViews(savedInstanceState);
+        EventBus.getDefault().register(this);
+        labConfig = RoomTool.getInstance().labConfigDao().getLabConfig();
+        thingsListAdapter = new ThingsListAdapter(lotDeviceVoList);
+        binding.iotThings.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+            @Override
+            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+                LotDeviceVo lotDeviceVo = lotDeviceVoList.get(position);
+                if (!lotDeviceVo.isOnline()) {
+                    showToast("当前设备不在线");
+                    return;
+                }
+                showLoading("加载中...");
+                addDisposable(ApiRepository.INSTANCE.sendControllerCMD(generateCMD(lotDeviceVo)).subscribe(new Consumer<Boolean>() {
+                    @Override
+                    public void accept(Boolean aBoolean) throws Throwable {
+                        ThreadUtils.runOnUiThreadDelayed(new Runnable() {
+                            @Override
+                            public void run() {
+                                if (!isDestroyed()) {
+                                    queryControllerList();
+                                }
+                            }
+                        }, 2000);
+                    }
+                }, new Consumer<Throwable>() {
+                    @Override
+                    public void accept(Throwable throwable) throws Throwable {
+                        dismissLoading();
+                        LogUtils.e(Log.getStackTraceString(throwable));
+                        showNetError(throwable);
+                    }
+                }));
+            }
+        });
+        queryControllerList();
+    }
+
+    private ControllerCMD generateCMD(LotDeviceVo lotDeviceVo) {
+        ControllerCMD controllerCMD = new ControllerCMD();
+        controllerCMD.id = lotDeviceVo.hardwareNo;
+        controllerCMD.command = lotDeviceVo.isSwitchOn() ? "0" : "1";
+        return controllerCMD;
+    }
+
+    private void queryControllerList() {
+        showLoading("加载中...");
+        addDisposable(ApiRepository.INSTANCE.controllerList(String.valueOf(labConfig.getLabId())).subscribe(new Consumer<List<LotDeviceVo>>() {
+            @Override
+            public void accept(List<LotDeviceVo> lotDeviceVos) throws Throwable {
+                dismissLoading();
+                if (null != lotDeviceVos) {
+                    if (!isDestroyed()) {
+                        lotDeviceVoList.clear();
+                        lotDeviceVoList.addAll(lotDeviceVos);
+                        thingsListAdapter.notifyDataSetChanged();
+                    }
+                }
+            }
+        }, new Consumer<Throwable>() {
+            @Override
+            public void accept(Throwable throwable) throws Throwable {
+                dismissLoading();
+                LogUtils.e(Log.getStackTraceString(throwable));
+                showNetError(throwable);
+            }
+        }));
+    }
+
+    @Subscribe(threadMode = ThreadMode.MAIN)
+    public void onThingsEvent(ThingsEvent thingsEvent) {
+        if (!isDestroyed()) {
+            queryControllerList();
+        }
+    }
+
+    @Override
+    public ITitleBar getMTitleBar() {
+        return binding.titleBar;
+    }
+
+    @Override
+    public NavViewCompat getMNavView() {
+        return binding.navView;
+    }
+
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        EventBus.getDefault().unregister(this);
+    }
+}

+ 67 - 0
app/src/main/java/xn/xxp/main/things/ThingsListAdapter.java

@@ -0,0 +1,67 @@
+package xn.xxp.main.things;
+
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+
+import java.util.List;
+
+import http.vo.response.LotDeviceVo;
+import kotlin.Unit;
+import kotlin.jvm.functions.Function2;
+import xn.xxp.R;
+import xn.xxp.databinding.ItemIotThingsBinding;
+
+public class ThingsListAdapter extends BaseAdapter {
+    private final List<LotDeviceVo> lotDeviceVoList;
+
+    public ThingsListAdapter(List<LotDeviceVo> lotDeviceVoList) {
+        this.lotDeviceVoList = lotDeviceVoList;
+    }
+
+    @Override
+    public int getCount() {
+        return null != lotDeviceVoList && !lotDeviceVoList.isEmpty() ? lotDeviceVoList.size() : 0;
+    }
+
+    @Override
+    public LotDeviceVo getItem(int position) {
+        return null != lotDeviceVoList && !lotDeviceVoList.isEmpty() ? lotDeviceVoList.get(position) : null;
+    }
+
+    @Override
+    public long getItemId(int position) {
+        return 0;
+    }
+
+    @Override
+    public View getView(int position, View convertView, ViewGroup parent) {
+        ViewHolder viewHolder;
+        if (convertView == null) {
+            ItemIotThingsBinding binding = ItemIotThingsBinding.inflate(LayoutInflater.from(parent.getContext()));
+            convertView = binding.getRoot();
+            viewHolder = new ViewHolder(binding);
+            convertView.setTag(viewHolder);
+        } else {
+            viewHolder = (ViewHolder) convertView.getTag();
+        }
+        LotDeviceVo lotDeviceVo = getItem(position);
+        if (null != lotDeviceVo) {
+            boolean isPowerLot = lotDeviceVo.hardwareName.contains("电源");
+            int resId = isPowerLot ? R.mipmap.icon_dykz : R.mipmap.icon_dspf_fs;
+            viewHolder.binding.lotController.setLotInfo(lotDeviceVo.hardwareName, resId);
+            viewHolder.binding.lotController.controller(lotDeviceVo.isSwitchOn(), false, null);
+        }
+
+        return convertView;
+    }
+
+    static class ViewHolder {
+        private final ItemIotThingsBinding binding;
+
+        public ViewHolder(ItemIotThingsBinding binding) {
+            this.binding = binding;
+        }
+    }
+}

+ 1 - 1
app/src/main/java/xn/xxp/widget/LotControllerView.kt

@@ -13,7 +13,7 @@ import xn.xxp.databinding.WidgetLotControllerBinding
  *
  * @author ReiChin_
  */
-class LotControllerView @JvmOverloads constructor(
+class IotControllerView @JvmOverloads constructor(
     context: Context,
     attrs: AttributeSet? = null,
     defStyleAttr: Int = 0

+ 53 - 0
app/src/main/res/layout/activity_things_list.xml

@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:background="@mipmap/img_bg"
+    android:orientation="vertical">
+
+    <xn.xxp.widget.TitleBar
+        android:id="@+id/titleBar"
+        android:layout_width="match_parent"
+        android:layout_height="56dp"
+        android:background="@mipmap/img_navigation_bg"
+        app:showNotice="true"
+        app:signed="true" />
+
+    <xn.xxp.widget.NavViewCompat
+        android:id="@+id/nav_view"
+        android:layout_width="match_parent"
+        android:layout_height="42dp"
+        app:location="物联控制"
+        app:showBack="true"
+        app:showHome="true" />
+
+    <GridView
+        android:id="@+id/iotThings"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:layout_marginStart="33dp"
+        android:layout_marginTop="8dp"
+        android:layout_marginEnd="33dp"
+        android:layout_marginBottom="16dp"
+        android:background="@drawable/shape_rect_round_11_solid_163277_50p"
+        android:numColumns="3"
+        android:padding="12dp"
+        android:stretchMode="columnWidth"
+        android:verticalSpacing="8dp"
+        tools:listitem="@layout/item_iot_things" />
+
+    <TextView
+        android:id="@+id/countDown"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="end"
+        android:layout_marginEnd="17dp"
+        android:layout_marginBottom="12dp"
+        android:textColor="#333333"
+        android:textSize="17sp"
+        android:visibility="gone"
+        tools:text="张同学-登录失效:180秒" />
+
+</LinearLayout>

+ 1 - 1
app/src/main/res/layout/item_lot_things.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<xn.xxp.widget.LotControllerView xmlns:android="http://schemas.android.com/apk/res/android"
+<xn.xxp.widget.IotControllerView xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/lotController"
     android:layout_width="@dimen/lot_things_size"