界面 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto” xmlns:tools=“http://schemas.android.com/tools” android:layout_width=“match_parent” android:layout_height=“match_parent” tools:context=".MainActivity">
<TextView
android:id="@+id/m_chuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="m_光照传感器"
android:textColor="#2c2c2c"
android:textSize="30dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/z_zigbee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="z_温度传感器"
android:textColor="#2c2c2c"
android:textSize="30dp"
app:layout_constraintStart_toStartOf="@+id/m_chuan"
app:layout_constraintTop_toBottomOf="@+id/m_chuan" />
<TextView
android:id="@+id/m_kong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="m_报警灯"
android:textColor="#2c2c2c"
android:textSize="30dp"
app:layout_constraintStart_toStartOf="@+id/z_zigbee"
app:layout_constraintTop_toBottomOf="@+id/z_zigbee" />
<TextView
android:id="@+id/m_zhi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:textColor="#2c2c2c"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="@+id/m_chuan"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/m_chuan"
app:layout_constraintTop_toTopOf="@+id/m_chuan" />
<TextView
android:id="@+id/z_zhi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:textColor="#2c2c2c"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="@+id/z_zigbee"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/z_zigbee"
app:layout_constraintTop_toTopOf="@+id/z_zigbee" />
<Button
android:id="@+id/m_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:onClick="m_click"
android:textColor="#2c2c2c"
android:textSize="30dp"
android:text="打开"
app:layout_constraintBottom_toBottomOf="@+id/m_kong"
app:layout_constraintStart_toEndOf="@+id/m_kong"
app:layout_constraintTop_toTopOf="@+id/m_kong" />
<Button
android:id="@+id/m_off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:onClick="m_click"
android:textColor="#2c2c2c"
android:textSize="30dp"
android:text="关闭"
app:layout_constraintBottom_toBottomOf="@+id/m_on"
app:layout_constraintStart_toEndOf="@+id/m_on"
app:layout_constraintTop_toTopOf="@+id/m_on" />
</android.support.constraint.ConstraintLayout>
代码 package com.example.newland.yun;
import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast;
import cn.com.newland.nle_sdk.requestEntity.SignIn; 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;
public class MainActivity extends AppCompatActivity { private TextView m_zhi,z_zhi; private String token; private String wz=“http://www.nlecloud.com/”; private String SBBS=“321264”; private Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Sign();
handler.postDelayed(runnable,2000);
}
private void Sign() {
NetWorkBusiness netWorkBusiness=new NetWorkBusiness("",wz);
netWorkBusiness.signIn(new SignIn("18079164182", "20040907ljl"), new NCallBack<BaseResponseEntity<User>>(getApplicationContext()) {
@Override
protected void onResponse(BaseResponseEntity<User> user) {
token=user.getResultObj().getAccessToken();
}
});
}
private void init() {
m_zhi=findViewById(R.id.m_zhi);
z_zhi=findViewById(R.id.z_zhi);
}
public void m_click(View view) {
Toast.makeText(this,"点击成功",Toast.LENGTH_SHORT).show();
switch (view.getId()){
case R.id.m_on:
NetWorkBusiness netWorkBusiness=new NetWorkBusiness(token,wz);
netWorkBusiness.control(SBBS, "bjd", 1, new NCallBack<BaseResponseEntity>(getApplicationContext()) {
@Override
protected void onResponse(BaseResponseEntity base1) {
}
});
break;
case R.id.m_off:
NetWorkBusiness netWorkBusiness1=new NetWorkBusiness(token,wz);
netWorkBusiness1.control(SBBS, "bjd", 0, new NCallBack<BaseResponseEntity>(getApplicationContext()) {
@Override
protected void onResponse(BaseResponseEntity base2) {
}
});
break;
}
}
private Runnable runnable=new Runnable() {
@Override
public void run() {
getchuan();
getzigbee();
handler.postDelayed(runnable,2000);
}
};
private void getchuan(){
NetWorkBusiness netWorkBusiness=new NetWorkBusiness(token,wz);
netWorkBusiness.getSensor(SBBS, "gz", new NCallBack<BaseResponseEntity<SensorInfo>>(getApplicationContext()) {
@Override
protected void onResponse(BaseResponseEntity<SensorInfo> sensor) {
SensorInfo sensorInfo=sensor.getResultObj();
m_zhi.setText(sensorInfo.getValue());
}
});
}
private void getzigbee(){
NetWorkBusiness netWorkBusiness=new NetWorkBusiness(token,wz);
netWorkBusiness.getSensor(SBBS, "z_wd", new NCallBack<BaseResponseEntity<SensorInfo>>(getApplicationContext()) {
@Override
protected void onResponse(BaseResponseEntity<SensorInfo> sensor) {
SensorInfo sensorInfo=sensor.getResultObj();
z_zhi.setText(sensorInfo.getValue());
}
});
}
}
|