app/build.gradle | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/src/main/java/com/example/agvcontroller/StartActivity.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
app/build.gradle
@@ -36,6 +36,7 @@ testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' implementation 'org.greenrobot:eventbus:3.2.0' implementation 'androidx.recyclerview:recyclerview:1.3.0' implementation 'io.netty:netty-all:4.1.68.Final' app/src/main/java/com/example/agvcontroller/StartActivity.java
@@ -1,12 +1,17 @@ package com.example.agvcontroller; import android.os.Bundle; import android.util.Log; 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; @@ -22,19 +27,41 @@ 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); socketManager = new SocketManager(); socketManager.startServer(8080); } @Subscribe(threadMode = ThreadMode.MAIN) public void onDeviceConnected(Item deviceAddress) { Log.i("EventBus", "Received device connected: " + deviceAddress); items.add(new Item("1",deviceAddress.getIp(),"3")); if (items.size() > 0) { for (Item item : items) { Log.i("Item",item.getIp()); } } 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(); } } app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
@@ -8,6 +8,14 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import android.util.Log; import com.example.agvcontroller.Item; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import java.util.concurrent.ConcurrentHashMap; public class NettyServerHandler extends ChannelInboundHandlerAdapter { @@ -19,6 +27,7 @@ public void channelActive(ChannelHandlerContext ctx) throws Exception { String clientId = ctx.channel().remoteAddress().toString(); channelMap.put(clientId, ctx.channel()); EventBus.getDefault().post(new Item("1",clientId,"3")); Log.d(TAG, "Client connected: " + clientId); } @@ -26,6 +35,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { String clientId = ctx.channel().remoteAddress().toString(); channelMap.remove(clientId); EventBus.getDefault().post(clientId); Log.d(TAG, "Client disconnected: " + clientId); }