前言

在第二次的基础上,增加了简单的自动控制,舵机的使用,温度的获取等等

使用C#封装了部分API 部分API接口 部分封装的安卓API会在后续给出



布局文件

新大陆云平台 DAMT配置 新大陆云平台介绍_Text


由于界面不需要多么原生,这里仅做简单使用即可.

我这里给出了跟第二次不同的布局代码

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="阙值"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:text="最小值:"/>
        <EditText
            android:id="@+id/minTemp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="20"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="最大值:"/>
        <EditText
            android:id="@+id/maxTemp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="30"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="自动"/>
        <Button
            android:id="@+id/auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:text="开启"/>
        <Button
            android:id="@+id/noauto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="关闭"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="舵机"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100dp"
            android:text="水平值:"/>
        <TextView
            android:id="@+id/currentX"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="0"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="垂直值:"/>
        <TextView
            android:id="@+id/currentY"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="0"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="水平控制"/>
        <SeekBar
            android:id="@+id/seekBarX"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="180"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="50dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="垂直控制"/>
        <SeekBar
            android:id="@+id/seekBarY"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="180"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="50dp"/>
    </LinearLayout>

可见,都是基本的UI组件堆砌而成



控制界面

这里,针对第二次末尾提到的问题,如下

新大陆云平台 DAMT配置 新大陆云平台介绍_Text_02


这里,我给出了解决办法,在NCallBack类中,添加一个无参的构造方法,如下:

public NCallBack() {

    }

即可解决问题.



舵机的控制

舵机的控制跟灯泡,风扇控制一样,只需要给出对应的参数即可,如

control("10439","steeringengine0",i);
	//10439设备标识符,steeringengine0舵机标识符,i是角度,0-180即可(整型)



自动控制

自动控制,简单的说就是运行一个线程,当温度高于上阙值时,开风扇,关灯;当温度低于下阙值时,开灯,关风扇;其他情况关灯和关风扇



温度的获取

由于温度的获取跟控制不相关,因此,我们必须查找官方给的SDK,查看跟传感器相关的类

新大陆云平台 DAMT配置 新大陆云平台介绍_android_03


参数设备Id,传感器标识符,回调函数,这里我们再看看回调函数.

新大陆云平台 DAMT配置 新大陆云平台介绍_新大陆云平台 DAMT配置_04


有个onResponse和onFailure方法,一个是请求成功的返回,一个是请求失败的返回,现在我们来查看新大陆云平台获取到了格式,如下

新大陆云平台 DAMT配置 新大陆云平台介绍_ide_05


json类型,因此我们必须对请求到的数据进行json解析.




相关代码

package com.example.newland;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import cn.com.newland.nle_sdk.responseEntity.SensorInfo;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MenuActivity extends AppCompatActivity {
    private EditText EquimentID;    //设备id
    private EditText MinTemp,MaxTemp;       //最小,最大温度
    private TextView CurrentTemp;   //当前温度
    private Button OpenLight,CloseLight;    //开关灯
    private Button OpenFan,CloseFan;    //开关风扇
    private Button GetTemp; //获取温度
    private Button Auto,Noauto;     //是否自动控制...
    private SeekBar SeekBarX,SeekBarY;  //拖动条
    private TextView CurrentSeekBarX,CurrentSeekBarY; //当前拖动条x,y
    private boolean isAuto = false; //标志位.
    private double tem;
    private NetWorkBusiness netWorkBusiness;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        init();
        OpenLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                control("10439","ctrl",1);  //灯.
            }
        });
        CloseLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                control("10439","ctrl",0);  //灯.
            }
        });
        OpenFan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                control("10439","defense",1);   //fan
            }
        });
        CloseFan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                control("10439","defense",0);
            }
        });
        GetTemp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getTemperature();
            }
        });
        Auto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //自动控制
                isAuto = true;  //运行线程...
                //线程.
                Thread1 th = new Thread1();
                new Thread(th).start();
            }
        });
        Noauto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isAuto = false;
            }
        });
        SeekBarX.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //获取值..
                String a = Integer.toString(i);
                CurrentSeekBarX.setText(a);
                control("10439","steeringengine1",i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        SeekBarY.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                String a = Integer.toString(i);
                CurrentSeekBarY.setText(a);
                control("10439","steeringengine0",i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
    void init(){
        EquimentID = (EditText)findViewById(R.id.equimentID);   //设备id
        CurrentTemp = (TextView) findViewById(R.id.currentTemp);    //当前温度
        OpenLight = (Button) findViewById(R.id.openLight);  //开灯
        CloseLight = (Button) findViewById(R.id.closeLight);    //关灯
        OpenFan = (Button) findViewById(R.id.openFan);  //开风扇
        CloseFan = (Button) findViewById(R.id.closeFan);    //关风扇
        GetTemp = (Button)findViewById(R.id.getTemp);   //获取温度
        Auto = (Button)findViewById(R.id.auto);     //自动控制..
        Noauto = (Button)findViewById(R.id.noauto); //关闭自动控制.
        MinTemp = (EditText)findViewById(R.id.minTemp); //最小温度...
        MaxTemp = (EditText)findViewById(R.id.maxTemp); //最大温度...
        SeekBarX = (SeekBar)findViewById(R.id.seekBarX);    //水平拖动条
        SeekBarY = (SeekBar)findViewById(R.id.seekBarY);
        CurrentSeekBarX = (TextView)findViewById(R.id.currentX);
        CurrentSeekBarY = (TextView)findViewById(R.id.currentY);
        Bundle bundle = getIntent().getExtras();
        String accessToken = bundle.getString("accessToken");   //获得传输秘钥
        netWorkBusiness = new NetWorkBusiness(accessToken,"http://api.nlecloud.com:80/");   //进行登录连接
    }
    public void control(String id,String apiTag,Object value){
        //设备id,标识符,值.
        netWorkBusiness.control(id, apiTag, value, new Callback<BaseResponseEntity>() {
            @Override
            public void onResponse(@NonNull Call<BaseResponseEntity> call,@NonNull Response<BaseResponseEntity> response) {
                BaseResponseEntity<User> baseResponseEntity = response.body();  //获得返回体
                if (baseResponseEntity==null){
                    Toast.makeText(MenuActivity.this,"请求内容为空",Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<BaseResponseEntity> call, Throwable t) {
                Toast.makeText(MenuActivity.this,"请求出错 " + t.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
    }
    public void getTemperature(){
        //获取温度...
        //id和设备标识符写死
        netWorkBusiness.getSensor("10439", "temperature", new NCallBack<BaseResponseEntity<SensorInfo>>() {
            @Override
            public void onResponse(final Call<BaseResponseEntity<SensorInfo>> call, final Response<BaseResponseEntity<SensorInfo>> response) {
                BaseResponseEntity baseResponseEntity = response.body();
                if (baseResponseEntity!=null){
                    //获取到了内容,使用json解析.
                    //JSON 是一种文本形式的数据交换格式,它比XML更轻量、比二进制容易阅读和编写,调式也更加方便;解析和生成的方式很多,Java中最常用的类库有:JSON-Java、Gson、Jackson、FastJson等
                    final Gson gson=new Gson();
                    JSONObject jsonObject=null;
                    String msg=gson.toJson(baseResponseEntity);
                    try {
                        jsonObject = new JSONObject(msg);   //解析数据.
                        JSONObject resultObj = (JSONObject) jsonObject.get("ResultObj");
                        String aaa=resultObj.getString("Value");
                        tem=Double.valueOf(aaa).intValue();
                        CurrentTemp.setText(tem+"℃");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            protected void onResponse(BaseResponseEntity<SensorInfo> response) {

            }

            public void onFailure(final Call<BaseResponseEntity<SensorInfo>> call, final Throwable t) {
                Toast.makeText(MenuActivity.this,"温度获取失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
    class Thread1 implements Runnable{
        @Override
        public void run() {
            while(true){
                //获取温度...
                getTemperature();   //并显示出来
                int currentTemp = (int)tem;
                int min  = Double.valueOf(MinTemp.getText().toString()).intValue();
                int max  = Double.valueOf(MaxTemp.getText().toString()).intValue();
                if (currentTemp>max && isAuto){
                    //开风扇.
                    control("10439","defense",1);
                    control("10439","ctrl",0);  //灯.
                }else if (currentTemp<min && isAuto){
                    //小于报警开灯.,关风扇.
                    control("10439","ctrl",1);  //灯.
                    control("10439","defense",0);
                }else{
                    control("10439","ctrl",0);  //灯.
                    control("10439","defense",0);
                }
                try{
                    Thread.sleep(1000); //希望不用使用Handler处理,使用handler加快处理速度吗?
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}





结果

下面展示安卓获取到的新大陆示意图(跟上文不是一个项目)。

新大陆云平台 DAMT配置 新大陆云平台介绍_新大陆云平台 DAMT配置_06


新大陆云平台 DAMT配置 新大陆云平台介绍_ide_07





问题

1.获取到的温度有延迟,自动控制不能实时控制.
2.代码冗杂度过高,需要优化(本人安卓新手,只是调用API接口获取数据)

期待下次更新,如有问题,请下方留言评论。