| | |
| | | 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; |
| | |
| | | } |
| | | }); |
| | | |
| | | 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); |
| | | |
| | |
| | | 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) { |
| | | public void onDeviceConnected(AGVCar deviceAddress) { |
| | | Log.i("EventBus", "Received device connected: " + deviceAddress); |
| | | |
| | | |
| | | if (!items.isEmpty()) { |
| | | int sameIp = 0; |
| | | for (Item item : items) { |
| | | for (AGVCar item : items) { |
| | | if (item.getIp().equals(deviceAddress.getIp())) { |
| | | item.setAgvNo(deviceAddress.getAgvNo()); |
| | | item.setPort(deviceAddress.getPort()); |
| | |
| | | Log.i("Item",item.getClientId()); |
| | | } |
| | | if (sameIp == 0) { |
| | | items.add(new Item(deviceAddress.getClientId() |
| | | items.add(new AGVCar(deviceAddress.getClientId() |
| | | ,deviceAddress.getIp() |
| | | ,deviceAddress.getPort() |
| | | ,deviceAddress.getAgvNo() |
| | | ,deviceAddress.getStatus())); |
| | | } |
| | | } else { |
| | | items.add(new Item(deviceAddress.getClientId() |
| | | items.add(new AGVCar(deviceAddress.getClientId() |
| | | ,deviceAddress.getIp() |
| | | ,deviceAddress.getPort() |
| | | ,deviceAddress.getAgvNo() |