Skip to content

Commit

Permalink
add: 以zKey51为模板增加zC1/z485toMqtt设备(仅增加文件)
Browse files Browse the repository at this point in the history
add: mqtt连接失败时log中显示错误信息/错误码
add: 新增设备时,若点击mdns发现的设备,则使用udp往设备ip地址也发送device report信息
  • Loading branch information
a2633063 committed Oct 22, 2021
1 parent 728a7c4 commit cd87e6b
Show file tree
Hide file tree
Showing 31 changed files with 5,149 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions app/src/main/java/com/zyc/zcontrol/ConnectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class ConnectService extends Service {
public final static String ACTION_DATA_AVAILABLE = "com.zyc.zcontrol.mqtt.ACTION_DATA_AVAILABLE";
public final static String EXTRA_DATA_TOPIC = "com.zyc.zcontrol.mqtt.EXTRA_DATA_TOPIC";
public final static String EXTRA_DATA_MESSAGE = "com.zyc.zcontrol.mqtt.EXTRA_DATA_MESSAGE";
public final static String EXTRA_ERROR_CODE = "com.zyc.zcontrol.mqtt.EXTRA_ERROR_CODE";
public final static String EXTRA_ERROR_MESSAGE = "com.zyc.zcontrol.mqtt.EXTRA_ERROR_MESSAGE";
//endregion

//region UDP相关
Expand Down Expand Up @@ -167,6 +169,14 @@ void broadcastUpdate(String action) {
localBroadcastManager.sendBroadcast(intent);
}

void broadcastUpdate(String action,int error_code, String message) {
final Intent intent = new Intent(action);
intent.putExtra(EXTRA_ERROR_CODE, error_code);
if (message != null && !message.isEmpty())
intent.putExtra(EXTRA_ERROR_MESSAGE, message);
localBroadcastManager.sendBroadcast(intent);
}

void broadcastUpdate(String action, String ip, int port, String message) {
final Intent intent = new Intent(action);

Expand Down Expand Up @@ -222,7 +232,15 @@ public void connect(String mqtt_uri, String mqtt_id, String mqtt_user, String mq
@Override
public void connectionLost(Throwable cause) {
Log.d("MQTTThread", "connectionLost");
broadcastUpdate(ACTION_MQTT_DISCONNECTED);
String message = null;
int error_code = -1;
if (cause != null && cause instanceof MqttException) {
error_code=((MqttException)cause).getReasonCode();
}
if (cause != null && cause.getCause() != null)
message = cause.getCause().getMessage();
else message = cause.getMessage();
broadcastUpdate(ACTION_MQTT_DISCONNECTED,error_code, message);
}

@Override
Expand Down Expand Up @@ -260,7 +278,18 @@ public void onSuccess(IMqttToken asyncActionToken) {
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.e("ConnectService", "onFailure:" + exception.getMessage());
broadcastUpdate(ACTION_MQTT_DISCONNECTED); //连接失败

String message = null;
int error_code = -1;
if (exception != null && exception instanceof MqttException) {
error_code=((MqttException)exception).getReasonCode();
}

if (exception != null && exception.getCause() != null)
message = exception.getCause().getMessage();
else message = exception.getMessage();

broadcastUpdate(ACTION_MQTT_DISCONNECTED,error_code, message); //连接失败
}
});

Expand All @@ -277,7 +306,7 @@ public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.e("ConnectService", "cause " + e.getCause());
Log.e("ConnectService", "excep " + e);
e.printStackTrace();
broadcastUpdate(ACTION_MQTT_DISCONNECTED); //连接失败
broadcastUpdate(ACTION_MQTT_DISCONNECTED,e.getReasonCode(), e.getMessage()); //连接失败
}
}

Expand Down
25 changes: 21 additions & 4 deletions app/src/main/java/com/zyc/zcontrol/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.zyc.zcontrol.deviceItem.DeviceClass.Device;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceA1;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceButtonMate;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceC1;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceClock;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceClockMatrix;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceDC1;
Expand All @@ -62,6 +63,7 @@
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceRGBW;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceS7;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceTC1;
import com.zyc.zcontrol.deviceItem.DeviceClass.DeviceUartToMqtt;
import com.zyc.zcontrol.deviceItem.SettingActivity;
import com.zyc.zcontrol.deviceAdd.DeviceAddChoiceActivity;
import com.zyc.zcontrol.mainActivity.MainDeviceFragmentAdapter;
Expand Down Expand Up @@ -160,9 +162,15 @@ public void handleMessage(Message msg) {
//endregion
//region 当获取局域网设备时,每隔2秒发送{"cmd":"device report"}
case 3:
String ip=(String)msg.obj;
if(ip!=null) mConnectService.UDPsend(ip, "{\"cmd\":\"device report\"}");
mConnectService.UDPsend("255.255.255.255", "{\"cmd\":\"device report\"}");
if (mainDeviceLanUdpScanListAdapter != null) {
handler.sendEmptyMessageDelayed(3, 1000);
Message new_msg=new Message();
new_msg.what=3;
new_msg.obj=ip;
handler.sendMessageDelayed(new_msg,2500);
//handler.sendEmptyMessageDelayed(3, 1000);
}
break;
//endregion
Expand Down Expand Up @@ -518,7 +526,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent intent)
String ip = intent.getExtras().getString("ip");
String mac = intent.getExtras().getString("mac");
Log.e(Tag, "get device result:" + ip + "," + mac + "," + type);
popupwindowLanUdpScan();
popupwindowLanUdpScan(ip);
// if (ip != null && ip.equals("255.255.255.255")) {
// popupwindowLanUdpScan();
// } else {
Expand Down Expand Up @@ -864,7 +872,7 @@ public boolean onTouch(View v, MotionEvent event) {

}

private void popupwindowLanUdpScan() {
private void popupwindowLanUdpScan(String ip) {

final View popupView = getLayoutInflater().inflate(R.layout.app_popupwindow_main_lan_udp_scan, null);
final PopupWindow window = new PopupWindow(popupView, MATCH_PARENT, MATCH_PARENT, true);//wrap_content,wrap_content
Expand Down Expand Up @@ -925,7 +933,12 @@ public void onDismiss() {
//endregion
window.update();
window.showAtLocation(popupView, Gravity.CENTER, 0, 0);
handler.sendEmptyMessageDelayed(3, 0);

Message msg=new Message();
msg.what=3;
msg.obj=ip;
handler.sendMessageDelayed(msg,0);
//handler.sendEmptyMessageDelayed(3, 0);

}

Expand Down Expand Up @@ -1142,6 +1155,10 @@ private Device returnDeviceClass(String name, String mac, int type) {
return new DeviceClockMatrix(name, mac);
case Device.TYPE_KEY51:
return new DeviceKey51(name, mac);
case Device.TYPE_C1:
return new DeviceC1(name, mac);
case Device.TYPE_UARTTOMQTT:
return new DeviceUartToMqtt(name, mac);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public class Device {
public final static int TYPE_RGBW = 8;
public final static int TYPE_CLOCK_MATRIX = 9;
public final static int TYPE_KEY51 = 10;
public final static int TYPE_COUNT = 11;
public final static int TYPE_C1 = 11;
public final static int TYPE_UARTTOMQTT = 12;
public final static int TYPE_COUNT = 13;

//设备名称
public final static String[] TypeName = new String[]{
Expand All @@ -47,6 +49,8 @@ public class Device {
"zRGBW灯", //8
"zClock点阵时钟", //9
"zKey51按键", //10
"zC1门窗传感器", //11
"z485toMqtt", //12
};
//设备链接地址
public final static String[] TypeUri = new String[]{
Expand All @@ -61,6 +65,8 @@ public class Device {
"https://github.com/a2633063/zRGBW", //8
"https://github.com/a2633063/zClock_Matrix", //9
"https://github.com/a2633063/zKey51", //10
"https://github.com/a2633063/zC1", //11
"https://github.com/a2633063/z485toMqtt", //12
};
//设备图标
public final static @DrawableRes
Expand All @@ -76,21 +82,26 @@ public class Device {
R.drawable.device_icon_zrgbw,//8
R.drawable.device_icon_zclock_matrix,//9
R.drawable.device_icon_diy,//10
R.drawable.device_icon_diy,//11
R.drawable.device_icon_diy,//12
};

//设备对应配对页面
public final static Class LinkActivity[] =
{
ESPtouchActivity.class,
TC1LinkActivity.class,
DC1LinkActivity.class,
A1LinkActivity.class,
M1LinkActivity.class,
S7LinkActivity.class,
ClockLinkActivity.class,
MOPSLinkActivity.class,
null,
null,
ESPtouchActivity.class,//0
TC1LinkActivity.class,//1
DC1LinkActivity.class,//2
A1LinkActivity.class,//3
M1LinkActivity.class,//4
S7LinkActivity.class,//5
ClockLinkActivity.class,//6
MOPSLinkActivity.class,//7
null,//8
null,//9
null,//10
null,//11
null,//12
};
//endregion

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.zyc.zcontrol.deviceItem.DeviceClass;

import android.preference.PreferenceFragment;

import androidx.fragment.app.Fragment;

import com.zyc.zcontrol.deviceItem.c1.C1Fragment;
import com.zyc.zcontrol.deviceItem.c1.C1SettingFragment;

public class DeviceC1 extends Device {

public DeviceC1(String name, String mac) {
super(TYPE_C1, name, mac);
}

//region 必须重构的函数
public String[] getRecvMqttTopic() {
String[] topic = new String[3];
topic[0] = "device/zc1/" + getMac() + "/state";
topic[1] = "device/zc1/" + getMac() + "/sensor";
topic[2] = "device/zc1/" + getMac() + "/availability";
return topic;
}

public String getSendMqttTopic() {
return "device/zc1/" + getMac() + "/set";
}


Fragment fragment;
PreferenceFragment settingFragment;
public Fragment getFragment() {
if (fragment == null) {
fragment = new C1Fragment(this);
}
return fragment;
}
public PreferenceFragment getSettingFragment(){
if (settingFragment == null) {
settingFragment = new C1SettingFragment(this);
}
return settingFragment;
}
//endregion

// //region 参数
// boolean lock;
// double power;
// int total_time;
// boolean[] plug = {false,false,false,false};
// String[] plug_name = {"总开关","插口1","插口2","插口3"};
//
//
// public boolean isLock() {
// return lock;
// }
//
// public void setLock(boolean lock) {
// this.lock = lock;
// }
//
// public double getPower() {
// return power;
// }
//
// public void setPower(double power) {
// this.power = power;
// }
//
// public int getTotal_time() {
// return total_time;
// }
//
// public void setTotal_time(int total_time) {
// this.total_time = total_time;
// }
//
// public boolean isPlug(int index) {
// return plug[index];
// }
//
// public void setPlug(int index, boolean plug) {
// this.plug[index] = plug;
// }
//
// public String getPlug_name(int index) {
// return plug_name[index];
// }
//
// public void setPlug_name(int index, String plug_name) {
// this.plug_name[index] = plug_name;
// }
// //endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ public void onReceive(Context context, Intent intent) {
MqttConnected();
} else if (ConnectService.ACTION_MQTT_DISCONNECTED.equals(action)) { //连接失败/断开
Log.w(Tag, "ACTION_MQTT_DISCONNECTED");
Log("app已断开mqtt服务器");
String message = intent.getStringExtra(ConnectService.EXTRA_ERROR_MESSAGE);
int code=intent.getIntExtra(ConnectService.EXTRA_ERROR_CODE,-1);
Log("app已断开mqtt服务器["+code+"]:"+message);
MqttDisconnected();
} else if (action.equals(device_mac)) {//接收到设备独立数据
String ip = intent.getStringExtra(ConnectService.EXTRA_UDP_DATA_IP);
Expand Down
Loading

0 comments on commit cd87e6b

Please sign in to comment.