package cn.easyiot.application.simulation;
import java.io.IOException;
//import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
* NBIotHttpClientTest.java
* @author
* @email
* @date 2017年11月30日 上午11:46:15
* @version 1.0
*/
//@Slf4j
public class NBIotHttpClientTest {
private static String serverID;
private static String accessToken;
public static void main(String[] args) {
if(args==null || args.length<2){
args=new String[]{"XXXX","XXXX"};
}
String result = test(args);
System.out.println(result);
}
public static String test(String[] args){
StringBuffer out = new StringBuffer();
out.delete(0, out.length());
String ret = "";
//鉴权登录
ret = login(args[0],args[1]);
out.append("\r\n\tlogin return:["+ret+"]");
//IOT接入点列表
ret = getIOTServers();
out.append("\r\n\tgetIOTServers return:["+ret+"]");
//单个设备注册
//registerDev();
//批量注册
//registerDevBatch();
//更新设备
//updateDev();
//查询设备
// queryDev();
//删除设备
//deleteDev();
//获取设备类型列表
//getDeviceTypes();
//获取设备列表
ret = getDevices();
out.append("\r\n\tgetDevices return:["+ret+"]");
//注册订阅地址
if(args!=null && args.length>2){
ret = subscribe(args[2]);
out.append("\r\n\tquerySubscribe return:["+ret+"]");
}
//取消订阅
//unsubscribe();
//查询订阅地址
ret = querySubscribe();
out.append("\r\n\tquerySubscribe return:["+ret+"]");
return out.toString();
}
/**
* 登录验证
*/
public static String login(String serverId,String password){
String result="";
serverID = serverId;
String charset="utf-8";
String contentType="application/json";
//请求参数json字符串
String reqBody=null;
//请求地址
String url="https://www.easy-iot.cn/idev/3rdcap/server/login";
// //参数serverId
// String serverId="请填写用户名";
// serverId="txrddev01";
//
// //参数password
// String password="请填写密码";
// password="123456aB";
//1、使用其他JSON工具,如:com.alibaba.fastjson.JSON,version=1.2.7
JSONObject json = new JSONObject();
json.put("serverId", serverId);
json.put("password", password);
reqBody=json.toJSONString();
//2、自己拼装json结构字符串
//reqBody="{\"serverId\":\""+serverId+"\",\"password\":\""+password+"\"}";
//使用HttpClient访问接口
CloseableHttpClient httpClient = HttpClients.createDefault();
StringEntity bodyEntity = new StringEntity(reqBody, charset);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-type", contentType);
httpPost.setEntity(bodyEntity);
try {
CloseableHttpResponse resp = httpClient.execute(httpPost);
HttpEntity entity = resp.getEntity();
if (resp.getStatusLine().getStatusCode()==200) {
String responseData = EntityUtils.toString(entity);
JSONObject respJson = JSONObject.parseObject(responseData);
if ("0".equals(respJson.getString("optResult"))) {
accessToken = respJson.getString("accessToken");
// if(log.isTraceEnabled()){
// log.trace("登录验证成功,accessToken="+respJson.getString("accessToken"));
// }
}else{
// if(log.isTraceEnabled()){
// log.trace("登录验证失败,返回码="+respJson.getString("optResult"));
// }
}
result = respJson.getString("optResult");
}else{
// if(log.isTraceEnabled()){
// log.trace("访问接口失败,状态码="+resp.getStatusLine().getStatusCode());
// }
}
return "" + resp.getStatusLine().getStatusCode() +":"+ result;
} catch (Throwable e) {
// log.error(e.toString(),e);
e.printStackTrace(System.out);
return e.toString();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 查询IoT连接平台列表
*/
public static String getIOTServers(){
String contentType="application/json";
//请求地址
String url="https://www.easy-iot.cn/idev/3rdcap/service-config/get-iotservers";
//消息头参数serverID
// String serverID="请填写用户名";
//消息头参数accessToken
// String accessToken="请填写成功登陆后的accessToken";
//使用HttpClient访问接口
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Content-type", contentType);
httpGet.setHeader("serverID", serverID);
httpGet.setHeader("accessToken", accessToken);
try {
CloseableHttpResponse resp = httpClient.execute(httpGet);
HttpEntity entity = resp.getEntity();
if (resp.getStatusLine().getStatusCode()==200) {
String responseData = EntityUtils.toString(entity);
JSONObject respJson = JSONObject.parseObject(responseData);
if ("0".equals(respJson.getString("optResult"))) {
// if(log.isTraceEnabled()){
// log.trace("IOT连接平台列表如下:\n"+respJson.getJSONArray("iotserverList"));
// }
// net.sf.json.JSONObject obj = new net.sf.json.JSONObject()
// .fromObject(requestJsonString);
//
// setRequestObject(net.sf.json.JSONObject.toBean(obj,
// requestObjectClass));
Object []iotserverList=respJson.getJSONArray("iotserverList").toArray();
StringBuffer resutBuf=new StringBuffer();
resutBuf.delete(0, resutBuf.length());
for(int pos=0;pos<iotserverList.length;pos++){
resutBuf.append("\r\n\t"+iotserverList[pos].toString());
}
return resutBuf.toString();
}else{
// if(log.isTraceEnabled()){
// log.trace("获取IOT连接平台列表失败,返回码="+respJson.getString("optResult"));
// }
}
}else{
// if(log.isTraceEnabled()){
// log.trace("访问接口失败,状态码="+resp.getStatusLine().getStatusCode());
// }
}
return "" +resp.getStatusLine().getStatusCode();
} catch (Throwable e) {
// log.error(e.toString(),e);
e.printStackTrace(System.out);
return e.toString();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 单设备注册
*/
public static void registerDev(){
String charset="utf-8";
String contentType="application/json";
//请求地址
String url="https://www.easy-iot.cn/idev/3rdcap/devices/reg-device";
//消息头参数serverID
// String serverID="请填写用户名";
//消息头参数accessToken
// String accessToken="请填写成功登陆后的accessToken";
//设备序列号
String devSerial="请填写序列号";
//设备名称
String name="请填写名称";
//设备类型
String deviceType="请填写设备类型";
//设备接入点
String connectPointId="请填写IOT平台";
//封装请求消息体
JSONObject reqBody = new JSONObject();
reqBody.put("devSerial", devSerial);
reqBody.put("name", name);
reqBody.put("deviceType", deviceType);
reqBody.put("connectPointId", connectPointId);
//忽略非必选的参数...
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
//设置消息头
htt
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
1、Usage:java -cp lib\RXTXcomm.jar;bin\ cn.easyiot.terminal.simulation.TerminalSimulator port rate [testPlayCount msgType param_1_Type param_1_valueMin param_1_valueMax] 2、运行参数: port rate [testPlayCount msgType param_1_Type param_1_valueMin param_1_valueMax] 如: COM4 9600 3 0A 01 40 80 表示: 通过COM4口用9600的波特率发3次 设备消息代码为10,传感器TLVType为1,值为40-80间的随机数的用户上报消息(上行)。 3、消息发不上去时,请拨插串口线,sim卡,开关。如自行组装消息时请不要忘记 回车换行"\r\n"。 4、机器64位和32位与jdk机器64位和32位要一致,并且rxtxParallel.dll 和 rxtxSerial.dll 要用相对应版本
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JAR.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 24 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
- wutongjiushi2019-04-30您好,这个该怎么用呀
![avatar](https://profile-avatar.csdnimg.cn/9f7295520527401b9e3b4ef10fd59bfe_luokn1995.jpg!1)
菜的不能再菜的程序猿
- 粉丝: 16
- 资源: 13
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)