forked from a2633063/SmartControl_Android_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
2,532 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
app/src/main/java/com/zyc/zcontrol/deviceItem/DeviceClass/Devicez863key.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.zyc.zcontrol.deviceItem.DeviceClass; | ||
|
||
import android.preference.PreferenceFragment; | ||
|
||
|
||
import com.zyc.zcontrol.deviceItem.z863key.z863keyFragment; | ||
import com.zyc.zcontrol.deviceItem.z863key.z863keySettingFragment; | ||
|
||
import androidx.fragment.app.Fragment; | ||
|
||
public class Devicez863key extends Device { | ||
|
||
public Devicez863key(String name, String mac) { | ||
super(TYPE_Z863KEY, name, mac); | ||
} | ||
|
||
//region 必须重构的函数 | ||
public String[] getRecvMqttTopic() { | ||
String[] topic = new String[]{ | ||
"device/z86_3key/" + getMac() + "/state", | ||
"device/z86_3key/" + getMac() + "/sensor" | ||
}; | ||
//topic[0] = "device/z86_3key/" + getMac() + "/state"; | ||
//topic[1] = "device/z86_3key/" + getMac() + "/sensor"; | ||
//topic[2] = "device/z86_3key/" + getMac() + "/availability"; | ||
return topic; | ||
} | ||
|
||
public String getSendMqttTopic() { | ||
return "device/z86_3key/" + getMac() + "/set"; | ||
} | ||
|
||
|
||
Fragment fragment; | ||
PreferenceFragment settingFragment; | ||
public Fragment getFragment() { | ||
if (fragment == null) { | ||
fragment = new z863keyFragment(this); | ||
} | ||
return fragment; | ||
} | ||
public PreferenceFragment getSettingFragment(){ | ||
if (settingFragment == null) { | ||
settingFragment = new z863keySettingFragment(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 | ||
|
||
} |
176 changes: 176 additions & 0 deletions
176
app/src/main/java/com/zyc/zcontrol/deviceItem/z863key/TaskItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package com.zyc.zcontrol.deviceItem.z863key; | ||
|
||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
|
||
public class TaskItem { | ||
|
||
public TaskItem() { | ||
} | ||
|
||
public TaskItem(Context context) { | ||
this.context = context; | ||
} | ||
|
||
|
||
public final static int TASK_TYPE_RELAY = 0; | ||
public final static int TASK_TYPE_MQTT = 1; | ||
public final static int TASK_TYPE_MAX = 2; | ||
|
||
public final static int TASK_BUTTON_TYPE_BUTTON = 0; | ||
public final static int TASK_BUTTON_TYPE_TOGGLE = 1; | ||
|
||
|
||
private Context context; | ||
|
||
public String name; | ||
public int button_type; | ||
public int type; | ||
public int[] color = new int[2]; | ||
|
||
//region 自动化类定义 | ||
class MQTT_C { | ||
public String topic = ""; | ||
public String payload = ""; | ||
public int qos; | ||
public int retained; | ||
} | ||
|
||
public class RELAY_C { | ||
//public int dat_length; //满足条件时,是否将接收到的数据发至mqtt | ||
public int index; //将接收到的数据中第reserved_rec个值 | ||
public int on; //填入要发送的第reserved_send个字段 | ||
} | ||
|
||
public class DATA_C { | ||
//public int dat_length; //满足条件时,是否将接收到的数据发至mqtt | ||
public MQTT_C mqtt= new MQTT_C(); | ||
public RELAY_C relay= new RELAY_C(); | ||
} | ||
//endregion | ||
|
||
// public MQTT_C mqtt = new MQTT_C(); | ||
// public RELAY_C relay = new RELAY_C(); | ||
|
||
public DATA_C[] data = | ||
{ | ||
new DATA_C(), | ||
new DATA_C(), | ||
}; | ||
|
||
public void setBase(String name, int button_type, int type) { | ||
this.name = name; | ||
this.button_type = button_type; | ||
this.type = type; | ||
} | ||
|
||
public void setColor(int color0,int color1) { | ||
this.color[0] = color0; | ||
this.color[1] = color1; | ||
} | ||
//region 设置自动化任务 | ||
|
||
//region mqtt任务部分 | ||
public void setMqtt(int task_index, String topic, String payload, int qos, int retained) { | ||
this.data[task_index].mqtt.topic = topic; | ||
this.data[task_index].mqtt.payload = payload; | ||
this.data[task_index].mqtt.qos = qos; | ||
this.data[task_index].mqtt.retained = retained; | ||
} | ||
//endregion | ||
|
||
//region relay | ||
public void setRelay(int task_index, int index, int on) { | ||
this.data[task_index].relay.index = index; | ||
this.data[task_index].relay.on = on; | ||
} | ||
//endregion | ||
//endregion | ||
|
||
public JSONObject getJson() { | ||
JSONObject jsonRoot = new JSONObject(); | ||
try { | ||
JSONObject jsonObject ; | ||
JSONArray jsonArray ; | ||
jsonRoot.put("name", name); | ||
jsonRoot.put("type", type); | ||
jsonRoot.put("button_type", button_type); | ||
jsonArray = new JSONArray(); | ||
jsonArray.put(color[0]); | ||
jsonArray.put(color[1]); | ||
jsonRoot.put("color", jsonArray); | ||
//region 执行部分 | ||
switch (type) { | ||
case TASK_TYPE_MQTT: { | ||
jsonObject = new JSONObject(); | ||
jsonArray = new JSONArray(); | ||
jsonObject.put("topic", this.data[0].mqtt.topic); | ||
jsonObject.put("payload", this.data[0].mqtt.payload); | ||
jsonObject.put("qos", this.data[0].mqtt.qos); | ||
jsonObject.put("retained", this.data[0].mqtt.retained); | ||
jsonArray.put(jsonObject); | ||
if (button_type == TASK_BUTTON_TYPE_TOGGLE) { | ||
jsonObject = new JSONObject(); | ||
jsonObject.put("topic", this.data[1].mqtt.topic); | ||
jsonObject.put("payload", this.data[1].mqtt.payload); | ||
jsonObject.put("qos", this.data[1].mqtt.qos); | ||
jsonObject.put("retained", this.data[1].mqtt.retained); | ||
jsonArray.put(jsonObject); | ||
} | ||
jsonRoot.put("mqtt", jsonArray); | ||
break; | ||
} | ||
|
||
case TASK_TYPE_RELAY: { | ||
jsonObject = new JSONObject(); | ||
jsonArray = new JSONArray(); | ||
jsonObject.put("index", this.data[0].relay.index); | ||
jsonObject.put("on", this.data[0].relay.on); | ||
jsonArray.put(jsonObject); | ||
if (button_type == TASK_BUTTON_TYPE_TOGGLE) { | ||
jsonObject = new JSONObject(); | ||
jsonObject.put("index", this.data[1].relay.index); | ||
jsonObject.put("on", this.data[1].relay.on); | ||
jsonArray.put(jsonObject); | ||
} | ||
jsonRoot.put("relay", jsonArray); | ||
break; | ||
} | ||
} | ||
//endregion | ||
|
||
Log.d("z86_3key task json", jsonRoot.toString()); | ||
return jsonRoot; | ||
} catch (JSONException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
public TaskItem clone() { | ||
TaskItem newTask = new TaskItem(); | ||
newTask.setBase(this.name, this.button_type, this.type); | ||
newTask.setMqtt(0,this.data[0].mqtt.topic, this.data[0].mqtt.payload, this.data[0].mqtt.qos, this.data[0].mqtt.retained); | ||
newTask.setMqtt(1,this.data[1].mqtt.topic, this.data[1].mqtt.payload, this.data[1].mqtt.qos, this.data[1].mqtt.retained); | ||
newTask.setRelay(0,this.data[0].relay.index, this.data[0].relay.on); | ||
newTask.setRelay(1,this.data[1].relay.index, this.data[1].relay.on); | ||
return newTask; | ||
} | ||
|
||
public void Copy(TaskItem oldTask) { | ||
if (oldTask == null) return; | ||
this.setBase(oldTask.name, this.button_type, oldTask.type); | ||
this.setMqtt(0,oldTask.data[0].mqtt.topic, oldTask.data[0].mqtt.payload, oldTask.data[0].mqtt.qos, oldTask.data[0].mqtt.retained); | ||
this.setMqtt(1,oldTask.data[1].mqtt.topic, oldTask.data[1].mqtt.payload, oldTask.data[1].mqtt.qos, oldTask.data[1].mqtt.retained); | ||
this.setRelay(0,oldTask.data[0].relay.index, oldTask.data[0].relay.on); | ||
this.setRelay(1,oldTask.data[1].relay.index, oldTask.data[1].relay.on); | ||
} | ||
|
||
} |
Oops, something went wrong.