| | |
| | | package com.example.agvcontroller; |
| | | |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.os.Bundle; |
| | | import android.util.Log; |
| | | import android.view.View; |
| | | |
| | | import androidx.appcompat.app.AppCompatActivity; |
| | | import androidx.recyclerview.widget.LinearLayoutManager; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | |
| | | import com.example.agvcontroller.socket.SocketManager; |
| | | |
| | | import org.greenrobot.eventbus.EventBus; |
| | | import org.greenrobot.eventbus.Subscribe; |
| | | import org.greenrobot.eventbus.ThreadMode; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | |
| | | SocketManager socketManager; |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | |
| | | super.onCreate(savedInstanceState); |
| | | setContentView(R.layout.activity_start); |
| | | |
| | | socketManager = new SocketManager(); |
| | | socketManager.startServer(8080); |
| | | |
| | | 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); |
| | | intent.putExtra("ip", items.get(position).getIp()); |
| | | intent.putExtra("agvNo", items.get(position).getAgvNo()); |
| | | startActivityForResult(intent,1); |
| | | } |
| | | }); |
| | | socketManager = new SocketManager(); |
| | | socketManager.startServer(8022); |
| | | } |
| | | |
| | | @Subscribe(threadMode = ThreadMode.MAIN) |
| | | public void onDeviceConnected(Item deviceAddress) { |
| | | Log.i("EventBus", "Received device connected: " + deviceAddress); |
| | | |
| | | |
| | | if (items.size() > 0) { |
| | | int sameIp = 0; |
| | | for (Item item : items) { |
| | | if (item.getIp().equals(deviceAddress.getIp())) { |
| | | sameIp++; |
| | | } |
| | | Log.i("Item",item.getIp()); |
| | | } |
| | | if (sameIp == 0) { |
| | | items.add(new Item(deviceAddress.getText(),deviceAddress.getIp(),deviceAddress.getAgvNo())); |
| | | } |
| | | } else { |
| | | items.add(new Item(deviceAddress.getText(),deviceAddress.getIp(),deviceAddress.getAgvNo())); |
| | | } |
| | | 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(); |
| | | } |
| | | } |