Fun Things OneNET platform of MQTT service ⑤ - OneNet Intelligent Light + MVP framework

Give a man a fish than giving the fishing, the purpose is not to teach you to develop specific projects, but to learn the ability to learn. I hope you share with friends or classmates around you need, maybe there is a large growth path God cornerstone Boge. . .

QQ technology group interaction: ESP8266 & 32 things develop group number 622 368 884, do not like do not spray

First, if you want to learn Arduino-based development technology ESP8266

First, the Basics

  1. ESP8266 Development Basics ① trip into the world of ESP8266
  2. ESP8266 Development Basics Tour ② how to install ESP8266 the Arduino development environment
  3. ESP8266 development trip Basics ③ ESP8266 and the Arduino development notes
  4. ESP8266 development trip Basics ④ ESP8266 and EEPROM
  5. ESP8266 development trip Basics ⑤ ESP8266 SPI and I2C communication communication
  6. ESP8266 development trip Basics ⑥ Ticker - ESP8266 regular library

Second, the network articles

  1. ESP8266 network development trip to articles ① Meet Arduino Core For ESP8266
  2. ESP8266 Development Journey articles ② ESP8266 network operation mode and ESP8266WiFi library
  3. ESP8266 network development trip to articles ③ Soft-AP - use ESP8266WiFiAP library
  4. ESP8266 network development trip to articles ④ Station - use ESP8266WiFiSTA library
  5. ESP8266 network development trip to articles ⑤ Scan WiFi - Use ESP8266WiFiScan library
  6. ESP8266 network development trip to the library foundation piece ⑥ ESP8266WiFiGeneric--
  7. ESP8266 network development trip to articles ⑦ TCP Server & TCP Client
  8. ESP8266 network development trip to articles ⑧ SmartConfig-- a key distribution network
  9. ESP8266 network development trip to articles ⑨ HttpClient - use ESP8266HTTPClient library
  10. ESP8266 Development Journey articles ⑩ UDP network services
  11. Tour of network development articles ESP8266 ⑪ WebServer - use ESP8266WebServer library
  12. ESP8266 network development trip ⑫ articles Domain Name Service --ESP8266mDNS library
  13. ESP8266 network development trip to articles ⑬ SPIFFS - ESP8266 Flash File System
  14. ESP8266 network development trip to articles ⑭ web distribution network
  15. ESP8266 network development trip to articles ⑮ true Domain Name Service --DNSServer
  16. ESP8266 development trip ⑯ wireless network articles --OTA update firmware update

Third, the application papers

  1. ESP8266 development trip application piece ① LAN applications - cool RGB lights
  2. ESP8266 development application piece Journey ② OLED display screen weather
  3. ESP8266 development application piece ③ Tour Lite car WiFi

Fourth, advanced piece

  1. ESP8266 development journey Advanced articles ① code optimization - ESP8266 Memory Management
  2. ESP8266 development journey Advanced articles ② chat Arduino IDE For ESP8266 configuration
  3. ESP8266 development journey Advanced articles ③ chat ESP8266 Flash
  4. ESP8266 development journey Advanced articles ④ common problems - to solve problems
  5. ESP8266 development journey Advanced articles ⑤ code standards - like writing articles as beautiful
  6. ESP8266 development journey Advanced articles ⑥ ESP-specific APIs Description

1 Introduction

    In the last post Fun Things ④ OneNET MQTT service platform - The remote control LED (device self-registration) + Android App control , the blogger offers a complete App Mqtt agreement OneNet intelligent lamp. So, in order to facilitate the development and expansion of bloggers here briefly source design.

2. Download the source code

  • Go to blogger technical exchange group group file download:
    Here Insert Picture Description

3. source code analysis

3.1 App Development Tools

  • Android Studio, official certification tools, your own environment and configure the AS Java tools, bloggers currently belong to the latest version.

3.2 overall structure of the code

  • After the download source introduced top down Android Studio, see the following structure:
    Here Insert Picture Description
    bloggers developed herein for convenience, the three separation module:
  • app_common, commonly common module, such as various tools (tools printing log time format conversion tools)
    Here Insert Picture Description
  • app_ormlib, database services (not used here, in order to later expand the reserve)
    Here Insert Picture Description
  • app_widget, the UI used, base assembly, including a package base activity class, recyclerview, mvp and widely used design framework I
    Here Insert Picture Description
  • app: this is our main business code.
    Here Insert Picture Description

3.3 MVP analytical framework

  • As for what is called the MVP, bloggers do not speak here, please Baidu own. The following code constitutes the main framework for MVP:
    Here Insert Picture Description
    Model: focus on data processing
    View: focus on the page display
    Presenter: call to coordinate the relationship between the Model and the View
  • Next, look at the code-based business MVP architecture:
    Here Insert Picture Description
    by name, you can quickly know exactly corresponding to the service:
  • MQTT configuration page: OneNetConfigXXXX
  • A list of the main page: MainXXXXX
  • On the application page: AboutMeXXXX
  • Expand the page: XXXXXXX

Next, we pick a page to explain, to facilitate understanding MVP work.

3.4 Main control the main page parsing

3.4.1 MVP Agreement

/**
 * mvp 主页面协议 presenter层
 */
public interface IMainContract {
    // model层
    interface IMainModel extends IBaseModel {
        void loadData(Context context, ICallBack<List<OneNetDeviceModel>> callBack);
        void updateDeviceDetail(Context context,OneNetDeviceModel model, ICallBack<String> callBack);
    }
    //view层
    interface IMainView extends IBaseView {
        void showLoading(String loadmsg);
        void dismissLoading(OnDismissCallbackListener callback);
        void refreshList(List<OneNetDeviceModel> list);
    }
}

3.4.2 Model layer - data processing

  • We are concerned about Main relevant code:
public class MainModel implements IMainContract.IMainModel {

    SimpleTask task;
    //获取设备列表
    @Override
    public void loadData(Context context, final ICallBack<List<OneNetDeviceModel>> 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<OneNetDeviceModel> 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<String> 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();
    }
}

3.4.3 View layer processing -UI

/**
 * @author wty
 * @Description 控制页面
 **/
public class MainActivity extends BaseActivity<MainPresenter> implements IMainContract.IMainView {

    @Bind(R.id.listview)
    XRecyclerView listview;

    DeviceListAdapter adapter;


    public static void startActivity(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public MainPresenter getPresenter() {
        return new MainPresenter();
    }

    @Override
    public void onInitView(Bundle savedInstanceState) {
        /**
         * 第一个参数是服务器地址
         * 第二个参数是用户名
         * 第三个参数是密码
         * 第四个参数是客户端ID
         * 第五个参数是主题
         **/
        MqttManager
                .getInstance()
                .creatConnect(
                        AppConstant.RequestUrl.ONENET_MQTT,
                        PreferenceUtil.getInstance().getProductId(),
                        PreferenceUtil.getInstance().getApiKey(),
                        PreferenceUtil.getInstance().getDeviceId(),
                        "esp8266");

        getDefaultNavigation().setTitle("OneNet智能灯");
        getDefaultNavigation().getLeftButton().setButton("配置", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OneNetMqttConfigActivity.startActivity(MainActivity.this);
                finish();
            }
        });
        getDefaultNavigation().getRightButton().setButton("关于应用", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AboutActivity.startActivity(MainActivity.this);
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        listview.setLayoutManager(layoutManager);
        listview.setPullRefreshEnabled(false);
        listview.setLoadingMoreEnabled(false);
        List<OneNetDeviceModel> data = new ArrayList<>();
        adapter = new DeviceListAdapter(this,data,mPresenter);
        listview.setAdapter(adapter);

        mPresenter.refreshList(MainActivity.this);
    }

    @Override
    protected boolean isEnableStatusBar() {
        return true;
    }

    @Override
    protected List<String> validate() {
        List<String> list = super.validate();
        return list;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MqttManager.release();
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_main;
    }

    @Override
    public void refreshList(List<OneNetDeviceModel> list) {
        adapter.retsetData(list);
    }
}

3.4.4 Presenter layer - intermediate layer Coordination

/**
 * 主页面 presenter
 */
public class MainPresenter extends BasePresenter<IMainContract.IMainView> {

    private IMainContract.IMainModel mMainModel;

    public MainPresenter(){
        mMainModel = new MainModel();
    }

    public void refreshList(Context context){
        mView.showLoading("获取设备列表中...");

        mMainModel.loadData(context, new ICallBack<List<OneNetDeviceModel>>() {
            @Override
            public void onSuccess(final List<OneNetDeviceModel> 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<String>() {
            @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));
            }
        });
    }
}

Basically, each page are in accordance with the MVP design ideas to analyze it, very simple.

4. Summary

In short, experts do not spray.

Guess you like

Origin www.cnblogs.com/danpianjicainiao/p/11756791.html