Impl getContacts

This commit is contained in:
Changhua 2023-03-19 11:06:22 +08:00
parent 80d9de2d15
commit 45c029ed79
2 changed files with 42 additions and 12 deletions

View File

@ -3,6 +3,8 @@ package com.iamteer;
import com.iamteer.Wcf.Functions;
import com.iamteer.Wcf.Request;
import com.iamteer.Wcf.Response;
import com.iamteer.Wcf.RpcContact;
import io.sisu.nng.Socket;
import io.sisu.nng.pair.Pair1Socket;
import org.slf4j.Logger;
@ -11,6 +13,7 @@ import org.slf4j.LoggerFactory;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Client {
@ -36,6 +39,19 @@ public class Client {
}
}
private Response sendCmd(Request req) {
try {
ByteBuffer bb = ByteBuffer.wrap(req.toByteArray());
socket.send(bb);
ByteBuffer ret = ByteBuffer.allocate(BUFFER_SIZE);
long size = socket.receive(ret, true);
return Response.parseFrom(Arrays.copyOfRange(ret.array(), 0, (int) size));
} catch (Exception e) {
logger.error("命令调用失败: ", e);
return null;
}
}
public boolean isLogin() {
Request req = new Request.Builder().setFuncValue(Functions.FUNC_IS_LOGIN_VALUE).build();
Response rsp = sendCmd(req);
@ -58,8 +74,6 @@ public class Client {
public Map<Integer, String> getMsgTypes() {
Request req = new Request.Builder().setFuncValue(Functions.FUNC_GET_MSG_TYPES_VALUE).build();
Response rsp = sendCmd(req);
Map<Integer, String> types = new HashMap<>();
if (rsp != null) {
return rsp.getTypes().getTypesMap();
}
@ -67,6 +81,16 @@ public class Client {
return Wcf.MsgTypes.newBuilder().build().getTypesMap();
}
public List<RpcContact> getContacts() {
Request req = new Request.Builder().setFuncValue(Functions.FUNC_GET_CONTACTS_VALUE).build();
Response rsp = sendCmd(req);
if (rsp != null) {
return rsp.getContacts().getContactsList();
}
return Wcf.RpcContacts.newBuilder().build().getContactsList();
}
public void waitMs(int ms) {
try {
Thread.sleep(ms);
@ -75,16 +99,21 @@ public class Client {
}
}
private Response sendCmd(Request req) {
try {
ByteBuffer bb = ByteBuffer.wrap(req.toByteArray());
socket.send(bb);
ByteBuffer ret = ByteBuffer.allocate(BUFFER_SIZE);
long size = socket.receive(ret, true);
return Response.parseFrom(Arrays.copyOfRange(ret.array(), 0, (int) size));
} catch (Exception e) {
logger.error("命令调用失败: ", e);
return null;
public void printContacts(List<RpcContact> contacts) {
for (RpcContact c : contacts) {
// logger.info(c.getWxid());
int value = c.getGender();
String gender;
if (value == 1) {
gender = "";
} else if (value == 2) {
gender = "";
} else {
gender = "未知";
}
logger.info("{}, {}, {}, {}, {}, {}, {}", c.getWxid(), c.getName(), c.getCode(), c.getCountry(),
c.getProvince(), c.getCity(), gender);
}
}
}

View File

@ -13,5 +13,6 @@ public class Main {
logger.info("isLogin: {}", client.isLogin());
logger.info("wxid: {}", client.getSelfWxid());
logger.info("message types: {}", client.getMsgTypes());
client.printContacts(client.getContacts());
}
}