inter server

This commit is contained in:
ScorpioMiku
2019-09-06 21:22:03 +08:00
parent 13717715de
commit 5184cd4ee6
35 changed files with 2091 additions and 805 deletions

View File

@@ -0,0 +1,70 @@
import com.google.gson.Gson;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import java.io.IOException;
public class Main {
private static String isin;
private static String temporature;
private static String humidity;
private static WebUtils webUtils;
public static void main(String[] args) {
webUtils = WebUtils.getInstance();
System.out.println("开始转发任务");
getAndUpData();
}
private static void getAndUpData() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
webUtils.getRoomData(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String body = response.body().string();
Gson gson = new Gson();
Node[] nodes = gson.fromJson(body, Node[].class);
for (Node node : nodes) {
funcList[] funcLists = node.getFuncList();
for (funcList funcList : funcLists) {
if (funcList.getType().equals("安防")) {
isin = String.valueOf((int) funcList.getData());
} else if (funcList.getType().equals("温度")) {
temporature = String.valueOf(funcList.getData());
} else if (funcList.getType().equals("湿度")) {
humidity = String.valueOf(funcList.getData());
}
}
}
RoomData roomData = new RoomData(isin, temporature, humidity, TimeUtils.getTime(), "1");
webUtils.upRoomState(roomData.getMap(), new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println(e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("上传成功:" + roomData.toString());
getAndUpData();
}
});
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}

View File

@@ -0,0 +1,54 @@
/**
* Created by ScorpioMiku on 2019/9/3.
*/
public class Node {
private String nwkAddr;
private String parAddr;
private String macAddr;
private funcList[] funcList;
public funcList[] getFuncList() {
return funcList;
}
public void setFuncList(funcList[] funcList) {
this.funcList = funcList;
}
@Override
public String toString() {
return "Node{" +
"nwkAddr='" + nwkAddr + '\'' +
", parAddr='" + parAddr + '\'' +
", macAddr='" + macAddr + '\'' +
", funcList=" + funcList +
'}';
}
public String getNwkAddr() {
return nwkAddr;
}
public void setNwkAddr(String nwkAddr) {
this.nwkAddr = nwkAddr;
}
public String getParAddr() {
return parAddr;
}
public void setParAddr(String parAddr) {
this.parAddr = parAddr;
}
public String getMacAddr() {
return macAddr;
}
public void setMacAddr(String macAddr) {
this.macAddr = macAddr;
}
}

View File

@@ -0,0 +1,82 @@
import java.util.HashMap;
/**
* Created by ScorpioMiku on 2019/9/3.
*/
public class RoomData {
private String isin;
private String temporature;
private String humidity;
private String time;
private String roomId;
public RoomData(String isin, String temporature, String humidity, String time, String roomId) {
this.isin = isin;
this.temporature = temporature;
this.humidity = humidity;
this.time = time;
this.roomId = roomId;
}
public HashMap<String, String> getMap() {
HashMap<String, String> mapData = new HashMap<>();
mapData.put("roomId", getRoomId());
mapData.put("time", TimeUtils.getTime());
mapData.put("temperature", getTemporature());
mapData.put("humidity", getHumidity());
mapData.put("isin", getIsin());
return mapData;
}
public String getIsin() {
return isin;
}
public void setIsin(String isin) {
this.isin = isin;
}
public String getTemporature() {
return temporature;
}
public void setTemporature(String temporature) {
this.temporature = temporature;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getRoomId() {
return roomId;
}
public void setRoomId(String roomId) {
this.roomId = roomId;
}
@Override
public String toString() {
return "RoomData{" +
"isin='" + isin + '\'' +
", temporature='" + temporature + '\'' +
", humidity='" + humidity + '\'' +
", time='" + time + '\'' +
", roomId='" + roomId + '\'' +
'}';
}
}

View File

@@ -0,0 +1,18 @@
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
/**
* Created by ScorpioMiku on 2019/9/3.
*/
public class TimeUtils {
public static String getTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH:mm:ss
//获取当前时间
Date date = new Date(System.currentTimeMillis());
return simpleDateFormat.format(date);
}
}

View File

@@ -0,0 +1,61 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Set;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by ScorpioMiku on 2019/9/3.
*/
public class WebUtils {
private static final String TAG = "WebUtils";
private static WebUtils instance = new WebUtils();
private static OkHttpClient mClient = new OkHttpClient();
private static final String webHost = "http://39.105.65.209:8080";
private WebUtils() {
}
public static WebUtils getInstance() {
return instance;
}
public RequestBody getRequestBody(HashMap<String, String> data) {
String body = "";
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
Set<String> keys = data.keySet();
for (String key : keys) {
body += key + "=" + data.get(key) + "&";
}
body = body.substring(0, body.length() - 1);
RequestBody requestBody = RequestBody.create(mediaType, body);
return requestBody;
}
public void upRoomState(HashMap<String, String> hashMap, Callback callback) {
Request request = new Request.Builder().post(getRequestBody(hashMap))
.url(webHost + "/rstate/add/").build();
Call call = mClient.newCall(request);
call.enqueue(callback);
}
public void getRoomData(Callback callback) {
Request request = new Request.Builder()
.url("http://192.168.0.1/cgi-bin/node.cgi")
.build();
Call call = mClient.newCall(request);
call.enqueue(callback);
}
}

View File

@@ -0,0 +1,55 @@
import java.util.HashMap;
/**
* Created by ScorpioMiku on 2019/9/3.
*/
public class funcList {
private String typeCode;
private String type;
private float cycle;
private float data;
@Override
public String toString() {
return "funcList{" +
"typeCode='" + typeCode + '\'' +
", type='" + type + '\'' +
", cycle=" + cycle +
", data=" + data +
'}';
}
public String getTypeCode() {
return typeCode;
}
public void setTypeCode(String typeCode) {
this.typeCode = typeCode;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public float getCycle() {
return cycle;
}
public void setCycle(float cycle) {
this.cycle = cycle;
}
public float getData() {
return data;
}
public void setData(float data) {
this.data = data;
}
}