panlili2024
2024-09-19 071a8328823a2861f93ce556a4da3e4119cab1a3
HDL_TTLSDK485/src/main/java/com/hdl/sdk/ttl/HDLDeviceManger/Core/HDLCommand.java
@@ -24,10 +24,13 @@
import com.hdl.sdk.ttl.HDLAppliances.HDLLight.LightCtrlBackInfo;
import com.hdl.sdk.ttl.HDLAppliances.HDLLight.Parser.LightCtrlParser;
import com.hdl.sdk.ttl.HDLAppliances.HDLLogic.LogicCtrlBackInfo;
import com.hdl.sdk.ttl.HDLAppliances.HDLLogic.SceneCtrlBackInfo;
import com.hdl.sdk.ttl.HDLAppliances.HDLSecurity.Parser.SecurityParser;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.AppliancesInfo;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.DeviceStateBean;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.MCUDataBean;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.ScenesData;
import com.hdl.sdk.ttl.HDLDeviceManger.Bean.SendDatas;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.AirFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.ColourLightFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.CommonSwitchCtrlBackEvent;
@@ -41,6 +44,7 @@
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.LightFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.LogicFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.MCUFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.SceneFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.SecurityArmingFeedBackEvent;
import com.hdl.sdk.ttl.HDLDeviceManger.EventBusEvent.UpdateRemarkFeedBackEvent;
import com.hdl.sdk.ttl.Utils.HDLUtlis.HDLUtlis;
@@ -61,6 +65,7 @@
public class HDLCommand {
    public static int mRequestTimeout = 5000;//默认请求超时时间
    public static int mRequestCurtainTimeout = 8000;//默认请求超时时间
    private static Timer getStateFailTimer = null;//获取设备状态失败Timer
    private static Timer lightCtrlFailTimer = null;//控制灯光失败Timer
    private static Timer curtainCtrlFailTimer = null;//控制窗帘失败Timer
@@ -101,13 +106,27 @@
//        HDLDeviceManager.release();
//    }
    /**
     * 搜索网关
     */
    public static void seachGateway() {
        HandleSearch.seachGateway();
    }
    /**
     * 获取家居设备
     */
    public static void getHomeDevices() {
        HandleSearch.getHomeDevices();
    public static void getHomeDevices(int subnetID, int deviceID) {
        HandleSearch.getHomeDevices(subnetID, deviceID);
    }
    /**
     * 获取家居场景
     */
    public static void getHomeScenes(int subnetID, int deviceID) {
        HandleSearch.seachHomeScenes(subnetID, deviceID);
    }
    /**
     * 获取家居设备备注
@@ -119,8 +138,8 @@
    /**
     * 发现新设备搜索,不会清空原设备列表数据
     */
    public static void getNewHomeDevices() {
        HandleSearch.getNewHomeDevices();
    public static void getNewHomeDevices(int subnetID, int deviceID) {
        HandleSearch.getNewHomeDevices(subnetID, deviceID);
    }
    /**
@@ -301,8 +320,7 @@
     * @param info
     * @param state
     */
    public static void curtainCtrl(final AppliancesInfo info, int state) {
//        HDLDeviceManager.isCurtainCtrlSuccess = false;
    public static void curtainCtrl(final AppliancesInfo info, int state, int type) {
        HDLDeviceManager.setDeviceCtrlSuccessStateWithInfo(info, false);
        if (curtainCtrlFailTimer != null) {
            curtainCtrlFailTimer.cancel();
@@ -310,19 +328,23 @@
        }
        byte[] bytes;
        if (info.getBigType() == Configuration.CURTAIN_BIG_TYPE) {
            if (info.getLittleType() == 1 || info.getLittleType() == 0) {
                //这里是判断卷帘电机和开合帘电机
            if (info.getLittleType() == 1 || info.getLittleType() == 0 || info.getLittleType() == 3) {//判断卷帘电机/开合帘电机/香格里拉帘电机
                int newChannelNum;
                if (state == CurtainCtrlParser.curtainOff
                        || state == CurtainCtrlParser.curtainOn
                        || state == CurtainCtrlParser.curtainPause) {
                    newChannelNum = info.getChannelNum();
                if (type == 0) {
                    if (state == CurtainCtrlParser.curtainOff
                            || state == CurtainCtrlParser.curtainOn
                            || state == CurtainCtrlParser.curtainPause) {
                        newChannelNum = info.getChannelNum();
                    } else {
                        newChannelNum = info.getChannelNum() + 16;
                    }
                } else {
                    newChannelNum = info.getChannelNum() + 16;
                    newChannelNum = info.getChannelNum();
                }
                bytes = CurtainCtrlParser.getCurtainAddByte(newChannelNum, state);
                bytes = CurtainCtrlParser.getCurtainAddByte(newChannelNum, state, type);
            } else {
                bytes = CurtainCtrlParser.getCurtainAddByte(info.getChannelNum(), state);
                bytes = CurtainCtrlParser.getCurtainAddByte(info.getChannelNum(), state, type);
            }
            addSendData(info, bytes, Configuration.CONTROL);
@@ -336,11 +358,11 @@
                        CurtainCtrlBackInfo curtainCtrlBackInfo = new CurtainCtrlBackInfo();
                        curtainCtrlBackInfo.setAppliancesInfo(info);
                        curtainCtrlBackInfo.setNum(info.getChannelNum());
                        EventBus.getDefault().post(new CurtainFeedBackEvent(curtainCtrlBackInfo, false));
                    }
                }
            }, mRequestTimeout);
            }, mRequestCurtainTimeout);
        } else {
            HDLLog.I("djlCtrl;  不是窗帘设备"
@@ -369,7 +391,7 @@
        if (info.getBigType() == Configuration.AIR_BIG_TYPE) {
            if (info.getDeviceType() == HDLApConfig.TYPE_AC_PANEL) {
                addSendData(info, AirCtrlParser.getAirPanelAddByte(type, state), Configuration.CONTROL);
            } else if (info.getDeviceType() == HDLApConfig.TYPE_AC_TECHSYS) {
            } else if (info.getDeviceType() == HDLApConfig.TYPE_AC_TECHSYS || info.getDeviceType() == HDLApConfig.TYPE_AC_KNXTECHSYS) {
                byte[] airbytes = AirCtrlParser.getAirTechAddByte(info, type, state);
                if (airbytes != null) {
                    addSendData(info, airbytes, Configuration.CONTROL);
@@ -553,56 +575,42 @@
                }
            }, mRequestTimeout);
        } else {
            HDLLog.I("djlCtrl: 逻辑模块设备控制不在范围内"
            HDLLog.I("逻辑模块设备控制不在范围内"
                    + " LittleType = " + info.getLittleType()
                    + " BigType = " + info.getBigType()
            );
        }
    }
    //  获取单一回路设备状态
    //  读取设备状态
//    public static void getDeviceState(final AppliancesInfo I) {
//        HDLDeviceManager.isGetDeviceStateSuccess = false;
//        switch (I.getBigType()) {
//            case Configuration.LIGTH_BIG_TYPE:
//                addSendData(I, new byte[]{}, Configuration.STATE);
//                break;
//            case Configuration.CURTAIN_BIG_TYPE:
//                if (I.getDeviceType() == HDLApConfig.TYPE_CURTAIN_MODULE) {
//                    addSendData(I, new byte[]{(byte) I.getChannelNum()}, Configuration.STATE);
//                } else {
//                    addSendData(I, new byte[]{(byte) (I.getChannelNum() + 16)}, Configuration.STATE);
//                }
//                break;
//            case Configuration.AIR_BIG_TYPE:
//                addSendData(I, new byte[]{AirCtrlParser.airSwich}, Configuration.STATE);
////                addSendData(I,new byte[]{AirCtrlParser.refTem},Configuration.STATE);
//                addSendData(I, new byte[]{AirCtrlParser.airSpeed}, Configuration.STATE);
//                addSendData(I, new byte[]{AirCtrlParser.airMode}, Configuration.STATE);
////                addSendData(I,new byte[]{AirCtrlParser.heatTem},Configuration.STATE);
////                addSendData(I,new byte[]{AirCtrlParser.autoTem},Configuration.STATE);
//                break;
//        }
//
//        if (I.getBigType() == Configuration.AIR_BIG_TYPE) {
//            return;
//        }
//        if (getStateFailTimer != null) {
//            getStateFailTimer.cancel();
//            getStateFailTimer = null;
//        }
//        getStateFailTimer = new Timer();
//        getStateFailTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                if (!HDLDeviceManager.isGetDeviceStateSuccess) {
//                    EventBus.getDefault().post(new DeviceStateEvent(I, false));
//                }
//            }
//        }, mRequestTimeout);
//
//    }
    /**
     * 场景控制
     *
     * @param scenesData
     */
    public static void sceneCtrl(final ScenesData scenesData) {
        cusSendCommand(Configuration.LOGIC_CTRL_COMMAND, scenesData.getSourceSubnetID(), scenesData.getSourceDeviceID(), new byte[]{
                (byte) (scenesData.getAreaCodeID()),
                (byte) (scenesData.getSceneID())
        });
        if (sceneCtrlFailTimer != null) {
            sceneCtrlFailTimer.cancel();
            sceneCtrlFailTimer = null;
        }
        sceneCtrlFailTimer = new Timer();
        sceneCtrlFailTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (!HDLDeviceManager.isSceneCtrlSuccess()) {
                    HDLLog.I("场景控制失败");
                    SceneCtrlBackInfo sceneCtrlBackInfo = new SceneCtrlBackInfo();
                    sceneCtrlBackInfo.setAreaCodeID(scenesData.getAreaCodeID());
                    sceneCtrlBackInfo.setSceneID(scenesData.getSceneID());
                    EventBus.getDefault().post(new SceneFeedBackEvent(sceneCtrlBackInfo, false));
                }
            }
        }, mRequestTimeout);
    }
    /**
     * 控制窗帘
@@ -690,10 +698,14 @@
        }
        HDLDeviceManager.isGetDeviceStateSuccess = false;
        switch (info.getDeviceType()) {
            case HDLApConfig.TYPE_CURTAIN_MODULE:
                addSendData(info, new byte[]{(byte) info.getChannelNum(), 0, 1}, Configuration.STATE);
                break;
            case HDLApConfig.TYPE_CURTAIN_GLYSTRO:
            case HDLApConfig.TYPE_CURTAIN_ROLLER:
            case HDLApConfig.TYPE_CURTAIN_MODULE:
                addSendData(info, new byte[]{(byte) info.getChannelNum()}, Configuration.STATE);
            case HDLApConfig.TYPE_CURTAIN_SHANGRILA:
                //只读取百分比,不读取开关停
                addSendData(info, new byte[]{(byte) info.getChannelNum(), 0, 2}, Configuration.STATE);
                break;
            default:
                HDLLog.I("不是窗帘设备");
@@ -717,7 +729,8 @@
            case HDLApConfig.TYPE_LIGHT_MIX_DIMMER:
            case HDLApConfig.TYPE_LIGHT_MIX_RELAY:
                //发送获取灯光状态数据
                addSendData(info, new byte[0], Configuration.STATE);
                byte[] sendbytes = new byte[]{(byte) info.getChannelNum()};
                addSendData(info, sendbytes, Configuration.STATE);
                break;
            default:
                HDLLog.I("不是灯光设备");
@@ -805,34 +818,11 @@
        }
        HDLDeviceManager.isGetDeviceStateSuccess = false;
        switch (info.getDeviceType()) {
            case HDLApConfig.TYPE_SENSOR_DRY_CONTACT:
            case HDLApConfig.TYPE_SENSOR_MOVEMENT_DETECTOR:
            case HDLApConfig.TYPE_SENSOR_TEMP:
            case HDLApConfig.TYPE_SENSOR_HUMIDITY:
            case HDLApConfig.TYPE_SENSOR_ILLUMINACE:
            case HDLApConfig.TYPE_SENSOR_VOC:
            case HDLApConfig.TYPE_SENSOR_PM_2_POINT_5:
            case HDLApConfig.TYPE_SENSOR_C02:
            case HDLApConfig.TYPE_SENSOR_LPG:
            case HDLApConfig.TYPE_SENSOR_CO_H2:
            case HDLApConfig.TYPE_SENSOR_CH4:
            case HDLApConfig.TYPE_SENSOR_SMOG:
            case HDLApConfig.TYPE_SENSOR_WIND_SPEED:
            case HDLApConfig.TYPE_SENSOR_WIND_PRESSURE:
            case HDLApConfig.TYPE_SENSOR_LIQUID_FLOW:
            case HDLApConfig.TYPE_SENSOR_LIQUID_PRESSURE:
            case HDLApConfig.TYPE_SENSOR_LIQUID_DEPTH:
            case HDLApConfig.TYPE_SENSOR_RAIN_FALL:
            case HDLApConfig.TYPE_SENSOR_WEIGHT:
            case HDLApConfig.TYPE_SENSOR_HEIGHT_LENGTH:
            case HDLApConfig.TYPE_SENSOR_OBJECT_SPEED:
            case HDLApConfig.TYPE_SENSOR_SHAKE:
            case HDLApConfig.TYPE_SENSOR_VOLTAGE:
            case HDLApConfig.TYPE_SENSOR_ELECTRICITY:
            case HDLApConfig.TYPE_SENSOR_POWER:
            case HDLApConfig.TYPE_SENSOR_FLOODING:
            case HDLApConfig.TYPE_SENSOR_DOOR_MAGNET:
            case HDLApConfig.TYPE_SENSOR_EMERGENCY_BUTTON:
            case HDLApConfig.TYPE_SENSOR_PM_10:
                //发送获传感器模块状态数据
                byte[] sendDatabyte = new byte[]{
                        (byte) info.getBigType(),
@@ -887,6 +877,7 @@
            case HDLApConfig.TYPE_SENSOR_FLOODING:
            case HDLApConfig.TYPE_SENSOR_DOOR_MAGNET:
            case HDLApConfig.TYPE_SENSOR_EMERGENCY_BUTTON:
            case HDLApConfig.TYPE_SENSOR_PM_10:
                //发送获传感器模块状态数据
                byte[] sendDatabyte = new byte[]{
                        (byte) 1,
@@ -936,8 +927,35 @@
        HDLDeviceManager.isGetDeviceStateSuccess = false;
        switch (info.getDeviceType()) {
            case HDLApConfig.TYPE_AC_TECHSYS:
            case HDLApConfig.TYPE_AC_KNXTECHSYS:
                //发送科技系统状态数据
                addSendData(info, new byte[]{(byte) info.getChannelNum()}, Configuration.STATE);
                break;
            default:
                HDLLog.I("不是科技系统设备");
                break;
        }
    }
    /**
     * 获取科技系统室内湿度状态
     *
     * @param info
     */
    public static void getTechSysDeviceHumidityFromNetwork(final AppliancesInfo info) {
        if (info == null) {
            return;
        }
        info.setGetDeviceStateSuccess(false);
        switch (info.getDeviceType()) {
            case HDLApConfig.TYPE_AC_KNXTECHSYS:
                //发送科技系统状态数据
                byte[] sendDatabyte = new byte[]{
                        (byte) info.getBigType(),
                        (byte) info.getLittleType(),
                        (byte) info.getChannelNum()
                };
                addCommandSendData(info, sendDatabyte, Configuration.SENSOR_STATE_COMMAND);
                break;
            default:
                HDLLog.I("不是科技系统设备");
@@ -970,6 +988,7 @@
            case HDLApConfig.TYPE_CURTAIN_GLYSTRO:
            case HDLApConfig.TYPE_CURTAIN_ROLLER:
            case HDLApConfig.TYPE_CURTAIN_MODULE:
            case HDLApConfig.TYPE_CURTAIN_SHANGRILA:
                int curtainState = getCurtainState(info);
                info.setCurState(curtainState);
                EventBus.getDefault().post(new DeviceStateEvent(info, true));
@@ -980,6 +999,7 @@
            case HDLApConfig.TYPE_AC_INFRARED:
            case HDLApConfig.TYPE_AC_PANEL:
            case HDLApConfig.TYPE_AC_TECHSYS:
            case HDLApConfig.TYPE_AC_KNXTECHSYS:
                //2019-8-21 增加判空
                byte[] acState = getACState(info);
                if (acState != null && acState.length >= 4) {
@@ -1205,6 +1225,7 @@
                            case HDLApConfig.TYPE_AC_HVAC:
                            case HDLApConfig.TYPE_AC_PANEL:
                            case HDLApConfig.TYPE_AC_TECHSYS:
                            case HDLApConfig.TYPE_AC_KNXTECHSYS:
                                curState = infos.get(j).getArrCurState();
                                break;
                            default:
@@ -1780,7 +1801,7 @@
            @Override
            public void run() {
                //2019-07 控制发送次数
                if (sendDatas.count >= 4) {
                if (sendDatas.count >= 3) {
                    sendCycleTimer.cancel();
                } else {
                    sendDatas.count++;
@@ -1807,10 +1828,44 @@
            }
        }, 1, 500);
    }
    /**
     * 单设备发送命令
     * 目前控制发送次数为3次
     * 发一条,补发3条,间隔500ms
     *
     * @param info
     * @param addBytes
     */
    private static void addCommandSendData(final AppliancesInfo info, byte[] addBytes, final int command) {
        final Crc sendDatas = new Crc(info.getStateCommand(), info.getDeviceSubnetID(), info.getDeviceDeviceID(), addBytes);
        sendDatas.count = 0;
        final Timer sendCycleTimer = new Timer();
        sendCycleTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                //2019-07 控制发送次数
                if (sendDatas.count >= 3) {
                    sendCycleTimer.cancel();
                } else {
                    sendDatas.count++;
                    sendDatas.command = command;
                    HDLLog.I("---getDevice: " + info.getDeviceKey() + " ---command: " + Integer.toHexString(command));//2019-8-2
                    sendDatas.isCtrlSuccess = info.isGetDeviceStateSuccess();
                    if (!sendDatas.isCtrlSuccess) {
                        HDLLog.I("读取发送第" + sendDatas.count + "次");
                        cusSendCommand(sendDatas.command, info.getDeviceSubnetID(), info.getDeviceDeviceID(), sendDatas.addBytes);
                    } else {
                        sendCycleTimer.cancel();
                    }
                }
            }
        }, 1, 500);
    }
    /**
     * 发送广播或点对点命令
@@ -1821,9 +1876,12 @@
     * @param addBytes 附加数据
     */
    public static void cusSendCommand(int command, int subnetID, int deviceID, byte[] addBytes) {
        Crc sendDatas = new Crc(command, subnetID, deviceID, addBytes);
        HDLSerialPortCore.sendData(sendDatas);
        if (HDLSerialPortCore.getIsModule()) {
            Crc sendDatas = new Crc(command, subnetID, deviceID, addBytes);
            HDLSerialPortCore.sendData(sendDatas);
        } else {
            SendDatas.AddSendData(command, subnetID, deviceID, addBytes);
        }
    }
    /**