安卓控制新大陆云平台(三)

前言

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

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



布局文件

在这里插入图片描述
由于界面不需要多么原生,这里仅做简单使用即可.
我这里给出了跟第二次不同的布局代码

<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组件堆砌而成,如果对UI组件有什么问题,请参考:
https://blog.csdn.net/qq_40318498/article/details/89609248




控制界面

这里,针对第二次末尾提到的问题,如下
在这里插入图片描述
这里,我给出了解决办法,在NCallBack类中,添加一个无参的构造方法,如下:

public NCallBack() {

    }

即可解决问题.


舵机的控制

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

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

自动控制

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


温度的获取

由于温度的获取跟控制不相关,因此,我们必须查找官方给的SDK,查看跟传感器相关的类
在这里插入图片描述
参数设备Id,传感器标识符,回调函数,这里我们再看看回调函数.
在这里插入图片描述
有个onResponse和onFailure方法,一个是请求成功的返回,一个是请求失败的返回,

现在我们来查看新大陆云平台获取到了格式,如下
在这里插入图片描述
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();
                }
            }
        }
    }
}




结果

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

在这里插入图片描述
在这里插入图片描述




问题

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

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

  • 11
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
Android获取新大陆云平台数据可以通过以下步骤进行: 1. 下载并安装新大陆云平台SDK:首先需要在Android开发环境中下载并安装新大陆云平台SDKSDK(软件开发工具包)是一套用于开发新大陆云平台应用程序的软件工具集合。 2. 导入SDK到Android项目:在Android项目中,可以将新大陆云平台SDK导入到项目中。这可以通过在项目的build.gradle文件中添加相应的依赖项来实现。 3. 连接到云平台:使用新大陆云平台提供的API和连接方法,将Android设备连接到云平台。这可以通过提供相应的身份验证凭据(如用户名和密码)来实现。 4. 请求数据:一旦与云平台成功连接,可以使用提供的API方法向云平台发送请求来获取所需的数据。这可以是传感器数据、设备状态等。 5. 处理数据:收到来自云平台的响应后,可以使用Android开发环境中的相应类和方法来处理数据。这可以包括解析数据、将其显示在应用程序界面上或执行其他需要的操作。 6. 断开连接:当不再需要从云平台获取数据时,可以使用云平台提供的方法断开与云平台的连接。 总结起来,Android获取新大陆云平台数据需要下载安装云平台SDK,导入SDK到Android项目中,连接到云平台,发送请求获取数据,处理数据并最后断开连接。这样,Android应用程序就可以与新大陆云平台进行数据交互了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值