好的 就是接收 锁的信息 判断什么指令 然后执行相应操作
public class HandlerThread implements Runnable {
private Socket socket;
public HandlerThread(Socket client) {
socket = client;
new Thread(this).start();
}
public void run() {
while (true) {
try {
InputStream inputStream = socket.getInputStream();
byte[] buf = new byte[1100];
inputStream.read(buf);
String msg = new String(buf, "utf8");
// Logs.get().debug("msg==="+msg);
if (msg.contains("HTBT")) {
String imei = msg.split(",")[1];
addActionMap(imei, this);
feedBack(imei); // 心跳反馈
}
if (msg.contains("WARNING")) {
System.out.println("========震动报警===");
String imei = msg.split(",")[1];
shakeAlert(imei);
Logs.get().debug("shake");
}
if (msg.contains("125") && !msg.contains("N") && !msg.contains("E") ) {
String[] location = msg.split(",");
if (msg.contains("fbffffff") || msg.contains("ffffffff")) {
String imei = location[1];
String baterry = location[13];
lowbattery(imei, baterry);
}
}
if (msg.contains("LOCK")) {
String imei = msg.split(",")[1];
if (msg.contains("ffffffff")) { // off
// org.nutz.http.Response response = Http.get(LOCK_STATUS + "imei=" + imei + "&onoroff=off");
changeLockStatus(imei, "off");
// Logs.get().debug(Json.toJson(response.getContent()));
}
if (msg.contains("fbffffff")) { // on
changeLockStatus(imei, "on");
}
}
// 接收地理信息
if (msg.contains("A") && msg.contains("N") && msg.contains("E")) {
String[] location = msg.split(",");
String imei = location[1];
String lat = location[5];
String log = location[7];
String baterry = location[13];
changeBikePostion(imei, lat, log, baterry);
}
} catch (Exception e) {
System.out.println("errrr");
System.out.println("服务器 run 异常: " + e.getMessage());
}
}
}
public void sendLock(String imei) {
try {
OutputStream outputStream = socket.getOutputStream();
String lockCommand = "*HQ," + imei + ",LOCK,1,1,"+BurroKit.getTotalSecondsOfDay()+",1"+"#";
Logs.get().debug("unlock===="+lockCommand);
outputStream.write(lockCommand.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendLock2(String imei) {
try {
OutputStream outputStream = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(outputStream, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
PrintWriter pw = new PrintWriter(bw);
String lockCommand = "*HQ," + imei + ",LOCK,1,1,"+BurroKit.getTotalSecondsOfDay()+"#";
pw.write(lockCommand);
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 心跳反馈
*
* @param imei
*/
public void feedBack(String imei) {
// *HQ,imei,HTBT,[时间]#
try {
OutputStream outputStream = socket.getOutputStream();
String lockCommand = "*HQ," + imei + ",HTBT," + BurroKit.getCurrTimeStr() + "#";
outputStream.write(lockCommand.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 重启
* @param imei
*/
public void reboot(String imei) {
try {
OutputStream outputStream = socket.getOutputStream();
String lockCommand = "*HQ," + imei + ",REBOOT," + BurroKit.getCurrTimeStr() + "#";
outputStream.write(lockCommand.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}