| | |
| | | package com.example.agvcontroller; |
| | | |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.SharedPreferences; |
| | | import android.os.Bundle; |
| | | import android.util.Log; |
| | | import android.view.View; |
| | | import android.widget.Button; |
| | | |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | import androidx.recyclerview.widget.LinearLayoutManager; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | |
| | | import com.example.agvcontroller.socket.SocketManager; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | | |
| | | import org.greenrobot.eventbus.EventBus; |
| | | import org.greenrobot.eventbus.Subscribe; |
| | | import org.greenrobot.eventbus.ThreadMode; |
| | | |
| | | import java.lang.reflect.Type; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | 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; |
| | | SocketManager socketManager; |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | |
| | | super.onCreate(savedInstanceState); |
| | | setContentView(R.layout.activity_start); |
| | | |
| | | socketManager = new SocketManager(); |
| | | socketManager.startServer(8080); |
| | | addItem = findViewById(R.id.addbtn); |
| | | addItem.setOnClickListener(new View.OnClickListener() { |
| | | @Override |
| | | public void onClick(View v) { |
| | | Intent intent = new Intent(StartActivity.this, EditeActivity.class); |
| | | startActivity(intent); |
| | | } |
| | | }); |
| | | |
| | | |
| | | sharedPreferences = getSharedPreferences("AGVControllerPrefs", MODE_PRIVATE); |
| | | |
| | | // Load items from SharedPreferences |
| | | items = loadItemsFromSharedPreferences(); |
| | | |
| | | |
| | | // items = new ArrayList<>(); |
| | | recyclerView = findViewById(R.id.recyclerView); |
| | | recyclerView.setLayoutManager(new LinearLayoutManager(this)); |
| | | |
| | | items = new ArrayList<>(); |
| | | //items.add(new Item("192.168.4.188", "Item 1", "Description of Item 1")); |
| | | //items.add(new Item("192.168.4.61", "Item 2", "Description of Item 2")); |
| | | //items.add(new Item("192.168.4.233", "Item 3", "Description of Item 3")); |
| | | |
| | | adapter = new ItemAdapter(items); |
| | | recyclerView.setAdapter(adapter); |
| | | adapter.addItem(new Item("AGV-3948", "192.168.4.188:56487", "Description of Item 1")); |
| | | |
| | | EventBus.getDefault().register(this); |
| | | |
| | | adapter.setmOnItemClickListener(new ItemAdapter.OnItemClickListener() { |
| | | |
| | | @Override |
| | | public void onItemClick(View view, int position) { |
| | | Context context = view.getContext(); |
| | | Intent intent = new Intent(context,MainActivity.class); |
| | | 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("item", item); |
| | | startActivityForResult(intent,1); |
| | | } |
| | | }); |
| | | socketManager = new SocketManager(); |
| | | socketManager.startServer(8022); |
| | | } |
| | | |
| | | 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<AGVCar>>(){}.getType(); |
| | | return gson.fromJson(json, type); |
| | | } |
| | | return new ArrayList<>(); |
| | | } |
| | | |
| | | @Subscribe(threadMode = ThreadMode.MAIN) |
| | | public void onDeviceConnected(AGVCar car) { |
| | | Log.i("EventBus", "Received device connected: " + car); |
| | | |
| | | |
| | | if (!items.isEmpty()) { |
| | | int sameIp = 0; |
| | | 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(car); |
| | | sameIp++; |
| | | } |
| | | Log.i("Item",item.getIp()); |
| | | Log.i("Item",item.getClientId()); |
| | | } |
| | | if (sameIp == 0) { |
| | | items.add(car); |
| | | } |
| | | } else { |
| | | items.add(car); |
| | | } |
| | | adapter.notifyDataSetChanged(); |
| | | } |
| | | |
| | | @Subscribe(threadMode = ThreadMode.MAIN) |
| | | public void onDeviceDisconnected(String deviceIp) { |
| | | Log.i("EventBus", "Received device disconnected: " + deviceIp); |
| | | for (int i = 0; i < items.size(); i++) { |
| | | if (items.get(i).getIp().equals(deviceIp)) { |
| | | items.remove(i); |
| | | break; |
| | | } |
| | | } |
| | | adapter.notifyDataSetChanged(); |
| | | } |
| | | } |