简介
主要功能:温湿度传感器,灯,风扇与网关ZIGBBE自组网,然后网关将数据上传至云平台,上位机程序(android)通过云平台实时获取数据
1:获取温湿度数据
2:控制风扇,灯
3:如果实时获取的温度大于了一定数值自动开启风扇,如果小于一定数组自动开启灯
4:输入的温度大于了一定数值自动开启风扇,如果小于一定数值自动开启灯
代码分析
首先连接云平台,进行登陆。获取访问令牌
ConnectCloud.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { netWorkBusiness.signIn( new SignIn( UserName, PassWord ), new NCallBack<BaseResponseEntity<User>>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity<User> response) { } @Override public void onResponse(Call<BaseResponseEntity<User>> call, Response<BaseResponseEntity<User>> response) { BaseResponseEntity<User> body = response.body(); if (body != null){ if (body.getStatus() == 0){ AccessToken = body.getResultObj().getAccessToken(); if (AccessToken == null){ Toast.makeText( MainActivity.this,"访问令牌为空",Toast.LENGTH_SHORT ).show(); return; } netWorkBusiness = new NetWorkBusiness( AccessToken, Url); } } } } ); } } );
开启子线程,实时获取温度数据,
并通过runOnUiThread
从子线程跳转到主线程,更新UI,下面采用的是lamda写法。
thread = new Thread( ()->{ while (true){ /**while start*/ try{ Thread.sleep( 2000 ); }catch (InterruptedException e){ e.printStackTrace(); } /**获取温度数据开始*/ netWorkBusiness.getSensors( DeviceID, TmpTag, new NCallBack<BaseResponseEntity<List<SensorInfo>>>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity<List<SensorInfo>> response) { } @Override public void onResponse(Call<BaseResponseEntity<List<SensorInfo>>> call, Response<BaseResponseEntity<List<SensorInfo>>> response) { BaseResponseEntity<List<SensorInfo>> body = response.body(); if (body != null){ if (body.getStatus() == 0){ TmpValue = Double.parseDouble( body.getResultObj().get( 0 ).getValue() ); } } } } ); /**获取温度数据结束*/ /**改变温度的值*/ runOnUiThread( ()->{ Tmp.setText( TmpValue+" ℃" ); } ); /**改变温度的值*/
打开风扇和灯通过新大陆JAR包中的control方法
/** * 发送命令/控制设备 * * @param deviceId 设备ID(必填) * @param apiTag 传感标识名(必须但值可为空) * @param data 开关类:开=1,关=0,暂停=2 * 家居类:调光灯亮度=0~254,RGB灯色度=2~239,窗帘、卷闸门、幕布打开百分比=3%~100%,红外指令=1(on)2(off) * 其它:integer/float/Json/String类型值 */
OpenFan.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Fan.startAnimation( Rotate ); netWorkBusiness.control( DeviceID, FanTag, 1, new NCallBack<BaseResponseEntity>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity response) { Toast.makeText( MainActivity.this,"打开风扇",Toast.LENGTH_SHORT ).show(); } } ); } } );
当硬件风扇选择时,对应的上位机程序风扇动画
private void InitAnimation(){ Rotate = AnimationUtils.loadAnimation( MainActivity.this,R.anim.rotate ); Rotate.setDuration(1000); //animation.setRepeatCount(-1); //无限旋转 Rotate.setRepeatMode(1); Rotate.setFillAfter(true); CycleInterpolator interpolator = new CycleInterpolator(1); Rotate.setInterpolator(interpolator); }
全部代码
protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); InitView(); GetData(); InitAnimation(); InputTmpOk.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { String GetEditTextTmpValue = InputTmp.getText().toString(); int IntTmpValue = Integer.parseInt( GetEditTextTmpValue ); if (IntTmpValue < 0){ Toast.makeText( MainActivity.this,"温度不能小于0",Toast.LENGTH_SHORT ).show(); return; } if (IntTmpValue > 40){ Toast.makeText( MainActivity.this,"温度不能大于40",Toast.LENGTH_SHORT ).show(); return; } ControlFan(IntTmpValue); } } ); ButtonOpenLamp.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { CloseLamp.setVisibility( View.INVISIBLE ); OpenLamp.setVisibility( View.VISIBLE ); netWorkBusiness.control( DeviceID, LightTag, 1, new NCallBack<BaseResponseEntity>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity response) { Toast.makeText( MainActivity.this,"开灯",Toast.LENGTH_SHORT ).show(); } } ); } } ); ButtonCloseLamp.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { CloseLamp.setVisibility( View.VISIBLE ); OpenLamp.setVisibility( View.INVISIBLE ); netWorkBusiness.control( DeviceID, LightTag, 0, new NCallBack<BaseResponseEntity>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity response) { Toast.makeText( MainActivity.this,"关灯",Toast.LENGTH_SHORT ).show(); } } ); } } ); OpenFan.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Fan.startAnimation( Rotate ); netWorkBusiness.control( DeviceID, FanTag, 1, new NCallBack<BaseResponseEntity>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity response) { Toast.makeText( MainActivity.this,"打开风扇",Toast.LENGTH_SHORT ).show(); } } ); } } ); CloseFan.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Fan.clearAnimation(); netWorkBusiness.control( DeviceID, FanTag, 0, new NCallBack<BaseResponseEntity>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity response) { Toast.makeText( MainActivity.this,"关闭风扇",Toast.LENGTH_SHORT ).show(); } } ); } } ); } private void InitView(){ Tmp = findViewById( R.id.Tmp ); Hum = findViewById( R.id.Hum ); InputTmp = findViewById( R.id.InputTmp ); InputTmpOk = findViewById( R.id.InputOk ); ConnectCloud = findViewById( R.id.ConnectCloud ); CloseLamp = findViewById( R.id.LampClose ); OpenLamp = findViewById( R.id.LampOpen ); ButtonOpenLamp = findViewById( R.id.ButtonOpenLamp ); ButtonCloseLamp = findViewById( R.id.ButtonCloseLamp ); Fan = findViewById( R.id.Fan ); OpenFan = findViewById( R.id.OpenFan ); CloseFan= findViewById( R.id.CloseFan ); } private void InitAnimation(){ Rotate = AnimationUtils.loadAnimation( MainActivity.this,R.anim.rotate ); Rotate.setDuration(1000); //animation.setRepeatCount(-1); //无限旋转 Rotate.setRepeatMode(1); Rotate.setFillAfter(true); CycleInterpolator interpolator = new CycleInterpolator(1); Rotate.setInterpolator(interpolator); } private void GetData(){ /**登陆开始*/ ConnectCloud.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { netWorkBusiness.signIn( new SignIn( UserName, PassWord ), new NCallBack<BaseResponseEntity<User>>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity<User> response) { } @Override public void onResponse(Call<BaseResponseEntity<User>> call, Response<BaseResponseEntity<User>> response) { BaseResponseEntity<User> body = response.body(); if (body != null){ if (body.getStatus() == 0){ AccessToken = body.getResultObj().getAccessToken(); if (AccessToken == null){ Toast.makeText( MainActivity.this,"访问令牌为空",Toast.LENGTH_SHORT ).show(); return; } netWorkBusiness = new NetWorkBusiness( AccessToken, Url); } } } } ); } } ); /**登陆结束*/ thread = new Thread( ()->{ while (true){ /**while start*/ try{ Thread.sleep( 2000 ); }catch (InterruptedException e){ e.printStackTrace(); } /**获取温度数据开始*/ netWorkBusiness.getSensors( DeviceID, TmpTag, new NCallBack<BaseResponseEntity<List<SensorInfo>>>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity<List<SensorInfo>> response) { } @Override public void onResponse(Call<BaseResponseEntity<List<SensorInfo>>> call, Response<BaseResponseEntity<List<SensorInfo>>> response) { BaseResponseEntity<List<SensorInfo>> body = response.body(); if (body != null){ if (body.getStatus() == 0){ TmpValue = Double.parseDouble( body.getResultObj().get( 0 ).getValue() ); } } } } ); /**获取温度数据结束*/ /**改变温度的值*/ runOnUiThread( ()->{ Tmp.setText( TmpValue+" ℃" ); } ); /**改变温度的值*/ /**获取湿度数据开始*/ netWorkBusiness.getSensors( DeviceID, HumTag, new NCallBack<BaseResponseEntity<List<SensorInfo>>>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity<List<SensorInfo>> response) { } @Override public void onResponse(Call<BaseResponseEntity<List<SensorInfo>>> call, Response<BaseResponseEntity<List<SensorInfo>>> response) { BaseResponseEntity<List<SensorInfo>> entity = response.body(); if (entity != null){ if (entity.getStatus() == 0){ HumValue = Double.parseDouble( entity.getResultObj().get( 0 ).getValue() ); } } } } ); /**获取湿度数据结束*/ /**改变湿度的值*/ runOnUiThread( ()->{ Hum.setText( HumValue+" RH" ); } ); /**改变湿度的值*/ /**while end*/ } } ); thread.start(); } /**如果大于规定温度,开启风扇*/ private void ControlFan(int GetTmpValue){ if (GetTmpValue > MaxTmpValue){ netWorkBusiness.control( DeviceID, LightTag, 1, new NCallBack<BaseResponseEntity>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity response) { } @Override public void onResponse(Call<BaseResponseEntity> call, Response<BaseResponseEntity> response) { Toast.makeText( MainActivity.this,"风扇已经开启",Toast.LENGTH_SHORT ).show(); } } ); }else if (GetTmpValue< MInTmpValue){ netWorkBusiness.control( DeviceID, LightTag, 0, new NCallBack<BaseResponseEntity>(getApplicationContext()) { @Override protected void onResponse(BaseResponseEntity response) { } @Override public void onResponse(Call<BaseResponseEntity> call, Response<BaseResponseEntity> response) { Toast.makeText( MainActivity.this,"风扇已经关闭",Toast.LENGTH_SHORT ).show(); } } ); } }