#
whycq
2025-02-06 17bb52d9337328323f5f8d2a806cf4f445673b4a
app/src/main/java/com/example/agvcontroller/StartActivity.java
@@ -27,7 +27,7 @@
public class StartActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private List<Item> items;
    private List<AGVCar> items;
    private ItemAdapter adapter;
    private Button addItem;
    private SharedPreferences sharedPreferences;
@@ -48,16 +48,16 @@
            }
        });
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        sharedPreferences = getSharedPreferences("AGVControllerPrefs", MODE_PRIVATE);
        // Load items from SharedPreferences
        items = loadItemsFromSharedPreferences();
        items = new ArrayList<>();
//        items = new ArrayList<>();
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ItemAdapter(items);
        recyclerView.setAdapter(adapter);
@@ -69,14 +69,16 @@
            public void onItemClick(View view, int position) {
                Context context = view.getContext();
                Intent intent = new Intent(context,MainActivity.class);
                String ip = items.get(position).getIp();
                String clientId = items.get(position).getClientId();
                String agvNo = items.get(position).getAgvNo();
                AGVCar item = items.get(position);
//                String ip = items.get(position).getIp();
//                String clientId = items.get(position).getClientId();
//                String agvNo = items.get(position).getAgvNo();
                intent.putExtra("ip", ip);
                intent.putExtra("agvNo", agvNo);
                intent.putExtra("clientId", clientId);
//                intent.putExtra("ip", ip);
//                intent.putExtra("agvNo", agvNo);
//                intent.putExtra("clientId", clientId);
                intent.putExtra("item", item);
                startActivityForResult(intent,1);
            }
        });
@@ -84,49 +86,43 @@
        socketManager.startServer(8022);
    }
    private List<Item> loadItemsFromSharedPreferences() {
    private List<AGVCar> loadItemsFromSharedPreferences() {
        Gson gson = new Gson();
        String json = sharedPreferences.getString("items", null);
        Log.i("SharedPreferences", "Loading items from shared preferences: " + json);
        if (json != null) {
            Type type = new TypeToken<List<Item>>(){}.getType();
            Type type = new TypeToken<List<AGVCar>>(){}.getType();
            return gson.fromJson(json, type);
        }
        return new ArrayList<>();
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceConnected(Item deviceAddress) {
        Log.i("EventBus", "Received device connected: " + deviceAddress);
    public void onDeviceConnected(AGVCar car) {
        Log.i("EventBus", "Received device connected: " + car);
        if (!items.isEmpty()) {
            int sameIp = 0;
            for (Item item : items) {
                if (item.getIp().equals(deviceAddress.getIp())) {
                    item.setAgvNo(deviceAddress.getAgvNo());
                    item.setPort(deviceAddress.getPort());
                    item.setClientId(deviceAddress.getClientId());
                    item.setStatus(deviceAddress.getStatus());
            for (AGVCar item : items) {
                if (item.getIp().equals(car.getIp())) {
                    item.setAgvNo(car.getAgvNo().isEmpty() ? car.getAgvNo() : item.getAgvNo());
                    item.setPort(car.getPort());
                    item.setClientId(car.getClientId());
                    item.setStatus(car.getStatus());
                    item.setBattery(car.getBattery() != 0  ? car.getBattery() : item.getBattery());
                    adapter.notifyItemChanged(items.indexOf(item));
                    MainActivity.upClient(deviceAddress.getClientId());
                    MainActivity.upClient(car);
                    sameIp++;
                }
                Log.i("Item",item.getIp());
                Log.i("Item",item.getClientId());
            }
            if (sameIp == 0) {
                items.add(new Item(deviceAddress.getClientId()
                        ,deviceAddress.getIp()
                        ,deviceAddress.getPort()
                        ,deviceAddress.getAgvNo()
                        ,deviceAddress.getStatus()));
                items.add(car);
            }
        } else {
            items.add(new Item(deviceAddress.getClientId()
                    ,deviceAddress.getIp()
                    ,deviceAddress.getPort()
                    ,deviceAddress.getAgvNo()
                    ,deviceAddress.getStatus()));
            items.add(car);
        }
        adapter.notifyDataSetChanged();
    }