1.前言

在前面的博文 玩转OneNET物联网平台之MQTT服务④ —— 远程控制LED(数量无限制)+ Android App控制 中,这个版本的App控制消息的发送是通过Mqtt客户端去操作,这就意味着一个长连接。博主仔细翻阅了一下OneNet的官方文档,发现可以通过HTTP协议的请求去完成MQTT相同的操作,对于一些适用短连接的场合我们可以使用这个方式去实现。

具体可以参考 OneNet发布消息,读者重点关注红色框框。

35338c4bd17477ba975f3931665802c7.png

2.ESP8266端代码

完全不需要改造,沿用之前博文的代码即可

3. App端代码

重点改造几个地方,读者直接看注释地方:

3.1 MVP三剑客

public interface IMainContract {

interface IMainModel extends IBaseModel {

void loadData(Context context, ICallBack> callBack);

void updateDeviceDetail(Context context,OneNetDeviceModel model, ICallBack callBack);

//加入http方式

void publishMsgToOneNet(String topic,int qos,String payload);

}

interface IMainView extends IBaseView {

void showLoading(String loadmsg);

void dismissLoading(OnDismissCallbackListener callback);

void refreshList(List list);

}

}

3.1.1 Model层

model层实现上面多加入的方法  publishMsgToOneNet

public class MainModel implements IMainContract.IMainModel {

SimpleTask task;

@Override

public void loadData(Context context, final ICallBack> callBack) {

if(task != null && task.getStatus()== AsyncTask.Status.RUNNING){

task.cancel(true);

}

task = new SimpleTask() {

GetOneNetDeviceListEntity entity;

@Override

protected void onPreExecute() {

entity = new GetOneNetDeviceListEntity();

}

@Override

protected Object doInBackground(String... strings) {

return entity.request();

}

@Override

protected void onPostExecute(Object o) {

String result = (String) o;

if("200".equals(result)){

if(entity.data != null && entity.data.devices.size()!=0) {

List list = new ArrayList<>();

for(OneNetDeviceModel model:entity.data.devices){

if(!model.getId().equals(PreferenceUtil.getInstance().getDeviceId())){

list.add(model);

}

}

callBack.onSuccess(list);

}

}else {

callBack.onFaild(result);

}

}

};

task.startTask();

}

@Override

public void updateDeviceDetail(Context context, final OneNetDeviceModel model, final ICallBack callBack) {

if(task != null && task.getStatus()== AsyncTask.Status.RUNNING){

task.cancel(true);

}

task = new SimpleTask() {

UpdateOneNetDeviceDetailEntity entity;

@Override

protected void onPreExecute() {

entity = new UpdateOneNetDeviceDetailEntity(model.getId());

}

@Override

protected Object doInBackground(String... strings) {

return entity.request(model);

}

@Override

protected void onPostExecute(Object o) {

String result = (String) o;

if("200".equals(result)){

callBack.onSuccess("更新成功");

}else {

callBack.onFaild(result);

}

}

};

task.startTask();

}

@Override

//实现http具体逻辑 也就是访问onenet的发布消息接口

public void publishMsgToOneNet(final String topic, int qos, final String payload) {

if(task != null && task.getStatus()== AsyncTask.Status.RUNNING){

task.cancel(true);

}

task = new SimpleTask() {

PublishMsgToOneNetlEntity entity;

@Override

protected void onPreExecute() {

entity = new PublishMsgToOneNetlEntity(topic);

}

@Override

protected Object doInBackground(String... strings) {

return entity.request(payload);

}

@Override

protected void onPostExecute(Object o) {

String result = (String) o;

if("200".equals(result)){

}else {

}

}

};

task.startTask();

}

}

3.1.2 View层

V层直接调用P层的方法,两种方式二选一

open.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

JSONObject obj = new JSONObject();

try {

obj.put("Did",item.getId());

obj.put("sta",1);

//http方式

presenter.httpPublishMsgToOneNet(AppConstant.Topic.Default_Topic,1,obj.toString());

//mqtt方式

//presenter.mqttPublishMsgToOneNet(AppConstant.Topic.Default_Topic,1,obj.toString());

} catch (JSONException e) {

e.printStackTrace();

}

}

});

close.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

JSONObject obj = new JSONObject();

try {

obj.put("Did",item.getId());

obj.put("sta",0);

//http方式

presenter.httpPublishMsgToOneNet(AppConstant.Topic.Default_Topic,1,obj.toString());

//mqtt方式

// presenter.mqttPublishMsgToOneNet(AppConstant.Topic.Default_Topic,1,obj.toString());

} catch (JSONException e) {

e.printStackTrace();

}

}

});

3.1.3 Presenter层

P层同时预留了Http和mqtt的请求方式

public class MainPresenter extends BasePresenter {

private IMainContract.IMainModel mMainModel;

public MainPresenter(){

mMainModel = new MainModel();

}

public void refreshList(Context context){

mView.showLoading("获取设备列表中...");

mMainModel.loadData(context, new ICallBack>() {

@Override

public void onSuccess(final List list) {

mView.refreshList(list);

mView.dismissLoading();

}

@Override

public void onFaild(String msg) {

mView.dismissLoading(new OnDismissCallbackListener(msg, SweetAlertDialog.ERROR_TYPE));

}

});

}

public void updateDeviceDetail(final Context context, OneNetDeviceModel model){

mView.showLoading("更新设备信息中...");

mMainModel.updateDeviceDetail(context,model, new ICallBack() {

@Override

public void onSuccess(final String result) {

mView.dismissLoading();

refreshList(context);

}

@Override

public void onFaild(String msg) {

mView.dismissLoading(new OnDismissCallbackListener(msg, SweetAlertDialog.ERROR_TYPE));

}

});

}

/**

* 以应用层mqtt协议发布消息

**/

public void mqttPublishMsgToOneNet(String topic,int qos,String payload ){

MqttManager.getInstance().publish(AppConstant.Topic.Default_Topic,qos,payload);

}

/**

* 以应用层http协议发布消息

**/

public void httpPublishMsgToOneNet(String topic,int qos,String payload ){

mMainModel.publishMsgToOneNet(topic,qos,payload);

}

}

4.测试结果

0ae0fca815aa042280cedde1c88fcff6.png

5.总结

本篇干货不多,只是以另一种方式去实现mqtt的操作,可供参考。

更多推荐