package com.huimv.datacollection.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.huimv.common.exception.RRException;
import com.huimv.datacollection.constant.XtAppConstant;
import com.huimv.datacollection.dao.ConsumeElectricDao;
import com.huimv.datacollection.dao.ConsumeWarterDao;
import com.huimv.datacollection.dao.DeviceDataDao;
import com.huimv.datacollection.entity.*;
import com.huimv.datacollection.enums.CmdEnum;
import com.huimv.datacollection.feign.ManagementFeignService;
import com.huimv.datacollection.service.HandleDataPackageService;
import com.huimv.datacollection.service.IClientDataPacketService;
import com.huimv.datacollection.utils.DateUtil;
import com.huimv.datacollection.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author yinhao
 * @version 1.0
 * @date 2021/07/07 15:07:00
 **/
@Service
@Slf4j
public class ClientDataPacketServiceImpl implements IClientDataPacketService {

    @Autowired
    private DeviceDataDao deviceDataDao;

    @Autowired
    private ManagementFeignService managementFeignService;

    @Autowired
    private HandleDataPackageService handleDataPackageService;

    @Autowired
    private ConsumeWarterDao consumeWarterDao;

    @Autowired
    private ConsumeElectricDao consumeElectricDao;

    @Override
    public String handleClientSendPacket(String clientMessageTxt) {

        if (clientMessageTxt == null || clientMessageTxt.isEmpty()) {
            log.error("本地提交数据包为空!");
            return null;
        }

        String matcher = "[+]";
        String[] result = clientMessageTxt.split(matcher);
        String cmd = result[2];
        String jqid = result[1];
        log.info("本次执行命令>>" + cmd);
        String rs ="";

        if (jqid.equals("0")) {

        } else {
            //同步时间
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_1)) {
                rs = getNow(result);
            }
            //上传料槽数据
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_2)) {
                rs = insertSscssj(result);
            }
            //短信发送
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_3)) {
                rs = sendSMS(result);
            }
            //声光报警短信发送
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_4)) {
                rs = sendSgbjSMS(result);
            }
            //报警短信发送
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_5)) {
                rs = sendBjSMS(result);
            }
            //温度上传
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_6)) {
                rs = insertBjqWd(result);
            }
            /** 第二版 start **/
            //温度上传
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_7)) {
                rs = insertHkSj(result);
            }
            //湿度上传
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_8)) {
                rs = insertHkSj(result);
            }
            //氨气上传
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_9)) {
                rs = insertHkSj(result);
            }
            //氨气上传
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_23)) {
                rs = insertHkSj(result);
            }
            //风机配置同步
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_10)) {
                rs = insertHkFjtb(result);
            }
            //设备上传配置
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_11)) {
                rs = insertHkYxcs(result);
            }
            //平台下发配置
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_12)) {
                rs = selectHkYxcs(result);
            }
            //报警使能
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_14)) {
                rs = selectHkBjsn(result);
            }
            //报警参数
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_18)) {
                rs = selectHkBjcs(result);
            }
            //短信发送
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_15)) {
                rs = sendSMS2(result);
            }
            //声光报警短信发送
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_16)) {
                rs = sendSgbjSMS2(result);
            }
            //报警短信发送
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_17)) {
                rs = sendBjSMS2(result);
            }
            //报警短信发送
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_19)) {
                rs = sendHkXxts(result);
            }
            //电压
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_20)) {
                rs = insertHkSj(result);
            }
            /** 第二版 end **/
            //地暖
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_21)) {
                rs = insertHkSj(result);
            }
            //远程JQID获取
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_22)) {
                rs = selectJqid(result);
            }
            //水压
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_25)) {
                rs = insertHkSj(result);
            }
            //PH
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_24)) {
                rs = insertHkSj(result);
            }
            //氧气
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_26)) {
                rs = insertHkSj(result);
            }
            //二氧化碳
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_27)) {
                rs = insertHkSj(result);
            }
            //硫化氢
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_28)) {
                rs = insertHkSj(result);
            }
            //加热器同步
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_30)) {
                //rs = insertHkJrq(result);
                rs = "";
            }
            //采食计量上传记录
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_40)) {
                rs = insertHkSj(result);
            }
            //同步校准砝码重量及允许校准误差
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_41)) {
                rs = selectJywc(result);
            }
            //获取运行参数
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_42)) {
                rs = selectCsjlYxcs(result);
            }
            //获取运行参数
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_44)) {
                rs = insertSbyxzt(result);
            }
            //下发下料重量
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_45)) {
                rs = selectXlzl(result);
            }

            //2017-9-19
            //室外温度
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_46)) {
                rs = insertHkSj(result);
            }
            //室外湿度
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_47)) {
                rs = insertHkSj(result);
            }
            //饲料
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_60)) {
                rs = insertHkSl(result);
            }
            //耗水
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_61)) {
                rs = insertHkSj(result);
            }
            //耗电
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_62)) {
                rs = insertHkSj(result);
            }
            //耗电
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_64)) {
                rs = "";
            }

            //液位数据上传
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_70)) {
                rs = insertHkSj(result);
            }
            //同步液位阈值
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_71)) {
                rs = selectQkywyz(result);
            }
            //上传驱动板状态
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_72)) {
                rs = insetLogQdbzt(result);
            }
            //喷雾除臭
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_80)) {
                rs = insertPwcc(result);
            }
            //同步关闭延时参数
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_81)) {
                rs = updateYscs(result);
            }
            //查询料控时间
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_82)) {
                System.out.println("--> 开始下载数据");
                rs = selectLksj(result);
                System.out.println("-->数据: "+result);
                System.out.println("--> 完成下载数据");
            }
            //上报"打料失败"事件
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_83)) {
//										rs = insertDlsb(result);
            }
            //上报电推杆设备状态
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_200)) {
                rs = insertTlgSbzt(result);
            }
            //电推杆同步配置
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_201)) {
                rs = updatTlgPz(result);
            }
            //同步料线控制定时配置
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_90)) {
                rs = updatLxdskz(result);
            }
            //料线远程控制
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_91)) {
                rs = updatLxyckz(result);
            }
            //同步灯光控制定时配置
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_92)) {
                rs = updatDgdskz(result);
            }
            //灯光远程控制
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_93)) {
                rs = updatDgyckz(result);
            }
            //获取设备模块信息
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_94)) {
                rs = insetLogSbmkxx(result);
            }

        }



        return rs;

//        //获取设备信息并校验
//        String deviceJson = JSONObject.toJSONString(managementFeignService.deviceInfo(jqId).get("device"));
//        DeviceEntity deviceEntity = JSONObject.parseObject(deviceJson, DeviceEntity.class);
//        if (deviceEntity == null) {
//            throw new RRException("没有获取到设备信息,请联系管理员!");
//        }
//        //保存室内环境数据
//        IndoorEnvironmentEntity indoorEnvironmentEntity = new IndoorEnvironmentEntity();
//        Integer farmId = deviceEntity.getFarmId();
//        indoorEnvironmentEntity.setFarmId(farmId);
//        Integer pigstyId = deviceEntity.getPigstyId();
//        indoorEnvironmentEntity.setPigstyId(pigstyId);
//        indoorEnvironmentEntity.setValue(value);
//        indoorEnvironmentEntity.setCollectTime(new Date());
//
//        Map<String, Double> map = managementFeignService.selectByType((short) 1);
//        switch (deviceDataEntity.getCmdType()) {
//            case 7:
//                indoorEnvironmentEntity.setType(1);
//                handleDataPackageService.handleTemperature(indoorEnvironmentEntity, value, farmId, pigstyId, map);
//                return "hm+7+0+7+end";
//            case 8:
//                indoorEnvironmentEntity.setType(2);
//                handleDataPackageService.handleHumidity(indoorEnvironmentEntity, value, farmId, pigstyId, map);
//                return "hm+8+0+8+end";
//            case 9:
//                indoorEnvironmentEntity.setType(3);
//                handleDataPackageService.handleGas(indoorEnvironmentEntity, value, farmId, pigstyId, map);
//                return "hm+9+0+8+end";
//            default:
//                break;
//        }
//        return "";
    }

    /**
     * 获取当天时间
     */
    private String getNow(String[] result) {
        String jqid = result[1];
        String cmd = result[2];
        String jy = result[3];
        if (!org.springframework.util.StringUtils.hasText(jqid) || !org.springframework.util.StringUtils.hasText(cmd) || !org.springframework.util.StringUtils.hasText(jy)) {
            return "";
        }
        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        String date = DateUtil.parserDateToPatternString(new Date(), XtAppConstant.TIME_FORMAT11);
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += date + "+" + gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 上传采食数据
     * @param result
     * @return
     */
    private String insertSscssj(String[] result) {
        if (result.length < 8) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String ts = result[3];
        String zl = result[4];
        String wz = result[5];
        String date = result[6];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(zl.substring(zl.length() - 1)) +
                Integer.parseInt(wz.substring(wz.length() - 1)) + Integer.parseInt(date.substring(date.length() - 1)) + Integer.parseInt(ts.substring(ts.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //插入数据

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }


    /**
     * 发送短信
     * @param result
     * @return
     */
    private String sendSMS(String[] result) {
        if (result.length < 10) {
            return "";
        }
        String jqid =StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String gs = result[3];
        String hm = result[4];
        String gzbh = result[5];
        String xh = result[6];
        String dz = result[7];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(gs.substring(gs.length() - 1))
                + Integer.parseInt(hm.substring(hm.length() - 1)) + Integer.parseInt(gzbh.substring(gzbh.length() - 1)) + Integer.parseInt(xh.substring(xh.length() - 1)) + Integer.parseInt(dz.substring(dz.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //发送短信

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }
    /**
     * 发送声光报警短信
     * @param result
     * @return
     */
    private String sendSgbjSMS(String[] result) {
        if (result.length < 10) {
            return "";
        }
        String jqid =StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String gs = result[3];
        String hm = result[4];
        String gzbh = result[5];
        String xh = result[6];
        String dz = result[7];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(gs.substring(gs.length() - 1))
                + Integer.parseInt(hm.substring(hm.length() - 1)) + Integer.parseInt(gzbh.substring(gzbh.length() - 1)) + Integer.parseInt(xh.substring(xh.length() - 1)) + Integer.parseInt(dz.substring(dz.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //发送短信

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 发送报警短信
     * @param result
     * @return
     */
    private String sendBjSMS(String[] result) {
        if (result.length < 11) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String gs = result[3];
        String hm = result[4];
        String gs1 = result[5];
        String hm1 = result[6];
        String gzbh = result[7];
        String xh = result[8];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(gs.substring(gs.length() - 1))
                + Integer.parseInt(hm.substring(hm.length() - 1)) + Integer.parseInt(gzbh.substring(gzbh.length() - 1)) + Integer.parseInt(xh.substring(xh.length() - 1)) + Integer.parseInt(gs1.substring(gs1.length() - 1))
                + Integer.parseInt(hm1.substring(hm1.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }



    /**
     * 上传温度数据
     * @param result
     * @return
     */
    private String insertBjqWd(String[] result) {
        if (result.length < 7) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String wd = result[3];
        String date = result[4];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid) % 10 + Integer.parseInt(cmd) % 10 + Integer.parseInt(wd) % 10 + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //插入数据
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }


    /**
     * 上传环控数据
     * @param result
     * @return
     */
    private String insertHkSj(String[] result) {
        if (result.length < 8) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String cgqid = result[3];
        String sj = result[4];
        String date = result[5];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid) % 10 + Integer.parseInt(cmd) % 10 + Integer.parseInt(sj.substring(sj.length() - 1)) + Long.parseLong(date) % 10 + Long.parseLong(cgqid) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy) || cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_62)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //温度
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_7)) {
                DeviceDataEntity deviceDataEntity = new DeviceDataEntity();
                deviceDataEntity.setCmdHeader(result[0]);
                String jqId = result[1];
                deviceDataEntity.setJqId(jqId);
                deviceDataEntity.setCmdType(Integer.parseInt(result[2]));
                deviceDataEntity.setSequence(Integer.parseInt(result[3]));
                Double value = null;
                if (CmdEnum.TEMPERATURE.getTypeNum().equals(cmd) || CmdEnum.HUMIDITY.getTypeNum().equals(cmd)) {
                    value = new BigDecimal(result[4]).divide(new BigDecimal("10")).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                } else if (CmdEnum.GAS.getTypeNum().equals(cmd)) {
                    value = Double.parseDouble(result[4]);
                }
                if (value == null) {
                    throw new RRException("传入数据包的数据字段为空,请检查!");
                }
                deviceDataEntity.setValue(value);
                deviceDataEntity.setTime(result[5]);
                deviceDataEntity.setVerify(Integer.parseInt(result[6]));
                deviceDataEntity.setCmdEnd(result[7]);
                deviceDataDao.insert(deviceDataEntity);

                //获取设备信息并校验
                String deviceJson = JSONObject.toJSONString(managementFeignService.deviceInfo(jqId).get("device"));
                DeviceEntity deviceEntity = JSONObject.parseObject(deviceJson, DeviceEntity.class);
                if (deviceEntity == null) {
                    throw new RRException("没有获取到设备信息,请联系管理员!");
                }
                //保存室内环境数据
                IndoorEnvironmentEntity indoorEnvironmentEntity = new IndoorEnvironmentEntity();
                Integer farmId = deviceEntity.getFarmId();
                indoorEnvironmentEntity.setFarmId(farmId);
                Integer pigstyId = deviceEntity.getPigstyId();
                indoorEnvironmentEntity.setPigstyId(pigstyId);
                indoorEnvironmentEntity.setValue(value);
                indoorEnvironmentEntity.setCollectTime(new Date());

                Map<String, Double> map = managementFeignService.selectByType((short) 1);
                indoorEnvironmentEntity.setType(1);
                handleDataPackageService.handleTemperature(indoorEnvironmentEntity, value, farmId, pigstyId, map);
            }
            //湿度
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_8)) {

                DeviceDataEntity deviceDataEntity = new DeviceDataEntity();
                deviceDataEntity.setCmdHeader(result[0]);
                String jqId = result[1];
                deviceDataEntity.setJqId(jqId);
                deviceDataEntity.setCmdType(Integer.parseInt(result[2]));
                deviceDataEntity.setSequence(Integer.parseInt(result[3]));
                Double value = null;
                if (CmdEnum.TEMPERATURE.getTypeNum().equals(cmd) || CmdEnum.HUMIDITY.getTypeNum().equals(cmd)) {
                    value = new BigDecimal(result[4]).divide(new BigDecimal("10")).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                } else if (CmdEnum.GAS.getTypeNum().equals(cmd)) {
                    value = Double.parseDouble(result[4]);
                }
                if (value == null) {
                    throw new RRException("传入数据包的数据字段为空,请检查!");
                }
                deviceDataEntity.setValue(value);
                deviceDataEntity.setTime(result[5]);
                deviceDataEntity.setVerify(Integer.parseInt(result[6]));
                deviceDataEntity.setCmdEnd(result[7]);
                deviceDataDao.insert(deviceDataEntity);

                //获取设备信息并校验
                String deviceJson = JSONObject.toJSONString(managementFeignService.deviceInfo(jqId).get("device"));
                DeviceEntity deviceEntity = JSONObject.parseObject(deviceJson, DeviceEntity.class);
                if (deviceEntity == null) {
                    throw new RRException("没有获取到设备信息,请联系管理员!");
                }
                //保存室内环境数据
                IndoorEnvironmentEntity indoorEnvironmentEntity = new IndoorEnvironmentEntity();
                Integer farmId = deviceEntity.getFarmId();
                indoorEnvironmentEntity.setFarmId(farmId);
                Integer pigstyId = deviceEntity.getPigstyId();
                indoorEnvironmentEntity.setPigstyId(pigstyId);
                indoorEnvironmentEntity.setValue(value);
                indoorEnvironmentEntity.setCollectTime(new Date());

                Map<String, Double> map = managementFeignService.selectByType((short) 1);

                indoorEnvironmentEntity.setType(2);
                handleDataPackageService.handleHumidity(indoorEnvironmentEntity, value, farmId, pigstyId, map);
            }
            //氨气
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_9)) {
                DeviceDataEntity deviceDataEntity = new DeviceDataEntity();
                deviceDataEntity.setCmdHeader(result[0]);
                String jqId = result[1];
                deviceDataEntity.setJqId(jqId);
                deviceDataEntity.setCmdType(Integer.parseInt(result[2]));
                deviceDataEntity.setSequence(Integer.parseInt(result[3]));
                Double value = null;
                if (CmdEnum.TEMPERATURE.getTypeNum().equals(cmd) || CmdEnum.HUMIDITY.getTypeNum().equals(cmd)) {
                    value = new BigDecimal(result[4]).divide(new BigDecimal("10")).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                } else if (CmdEnum.GAS.getTypeNum().equals(cmd)) {
                    value = Double.parseDouble(result[4]);
                }
                if (value == null) {
                    throw new RRException("传入数据包的数据字段为空,请检查!");
                }
                deviceDataEntity.setValue(value);
                deviceDataEntity.setTime(result[5]);
                deviceDataEntity.setVerify(Integer.parseInt(result[6]));
                deviceDataEntity.setCmdEnd(result[7]);
                deviceDataDao.insert(deviceDataEntity);

                //获取设备信息并校验
                String deviceJson = JSONObject.toJSONString(managementFeignService.deviceInfo(jqId).get("device"));
                DeviceEntity deviceEntity = JSONObject.parseObject(deviceJson, DeviceEntity.class);
                if (deviceEntity == null) {
                    throw new RRException("没有获取到设备信息,请联系管理员!");
                }
                //保存室内环境数据
                IndoorEnvironmentEntity indoorEnvironmentEntity = new IndoorEnvironmentEntity();
                Integer farmId = deviceEntity.getFarmId();
                indoorEnvironmentEntity.setFarmId(farmId);
                Integer pigstyId = deviceEntity.getPigstyId();
                indoorEnvironmentEntity.setPigstyId(pigstyId);
                indoorEnvironmentEntity.setValue(value);
                indoorEnvironmentEntity.setCollectTime(new Date());

                Map<String, Double> map = managementFeignService.selectByType((short) 1);
                indoorEnvironmentEntity.setType(3);
                handleDataPackageService.handleGas(indoorEnvironmentEntity, value, farmId, pigstyId, map);

            }
            //氨气
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_23)) {
            }
            //电压
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_20)) {
            }
            //地暖
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_21)) {
            }
            //PH
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_24)) {
            }
            //水压
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_25)) {
            }
            //上料
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_40)) {
            }

            //2017-9-19
            //室外温度
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_46)) {
            }
            //室外湿度
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_47)) {
            }
            //氧气
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_26)) {
            }
            //二氧化碳
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_27)) {
            }
            //硫化氢
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_28)) {
            }
            //耗水
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_61)) {
                ConsumeWarterEntity consumeWarterEntity= new ConsumeWarterEntity();
                consumeWarterEntity.setCollectTime(new Date());
                consumeWarterEntity.setDeviceId(Integer.parseInt(jqid));
                consumeWarterEntity.setConsumeWarter(Double.parseDouble(sj));
                consumeWarterDao.insert(consumeWarterEntity);

            }
            //耗电
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_62)) {

                ConsumeElectricEntity consumeElectricEntity= new ConsumeElectricEntity();
                consumeElectricEntity.setCollectTime(new Date());
                consumeElectricEntity.setDeviceId(Integer.parseInt(jqid));
                consumeElectricEntity.setConsumeElectric(Double.parseDouble(sj));
                consumeElectricDao.insert(consumeElectricEntity);

            }
            //液位数据上传
            if (cmd.equals(XtAppConstant.SOCKET_SJSC_CMD_70)) {
            }
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }


    /**
     * 风机配置同步
     * @param result
     * @return
     */
    private String insertHkFjtb(String[] result) {
        if (result.length < 6) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String date = result[3];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        String dbjg = "0";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += dbjg + "+" + gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 设备上传配置
     * @param result
     * @return
     */
    private String insertHkYxcs(String[] result) {
        if (result.length < 10) {
            return "";
        }
        String jqid =StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String dwgs = result[3];
        String dkgs = result[4];
        String pznr = result[5];
        String mrdw = result[6];
        String date = result[7];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1))
                + Integer.parseInt(dwgs.substring(dwgs.length() - 1)) + Integer.parseInt(dkgs.substring(dkgs.length() - 1))
                + Integer.parseInt(pznr.substring(pznr.length() - 1)) + Integer.parseInt(mrdw.substring(mrdw.length() - 1))
                + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 平台下发配置
     * @param result
     * @return
     */
    private String selectHkYxcs(String[] result) {
        if (result.length < 5) {
            return "";
        }
        String dwnr = "0",mrdw = "6",date = "0";
        String jqid =StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += dwnr + "+" + mrdw + "+" + date + "+" + gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 下发报警使能
     * @param result
     * @return
     */
    private String selectHkBjsn(String[] result) {
        if (result.length < 5) {
            return "";
        }
        String jqid =StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];
        String snnr = "1";

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //报警使能

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += snnr + "+" + gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 下发报警参数
     * @param result
     * @return
     */
    private String selectHkBjcs(String[] result) {
        if (result.length < 5) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];
        String gwbjz = "999";
        String dwbjz = "0";

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //报警使能

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gwbjz + "+" + dwbjz + "+" + gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 发送短信（新）
     * @param result
     * @return
     */
    private String sendSMS2(String[] result) {
        if (result.length < 8) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String gzbh = result[3];
        String xh = result[4];
        String dz = result[5];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1))
                + Integer.parseInt(gzbh.substring(gzbh.length() - 1)) + Integer.parseInt(xh.substring(xh.length() - 1)) + Integer.parseInt(dz.substring(dz.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {

            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //发送短信

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 发送声光报警短信(新)
     * @param result
     * @return
     */
    private String sendSgbjSMS2(String[] result) {
        if (result.length < 8) {
            return "";
        }
        String jqid =StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String gzbh = result[3];
        String xh = result[4];
        String dz = result[5];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1))
                + Integer.parseInt(gzbh.substring(gzbh.length() - 1)) + Integer.parseInt(xh.substring(xh.length() - 1)) + Integer.parseInt(dz.substring(dz.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //发送短信
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 发送报警短信(新)
     * @param result
     * @return
     */
    private String sendBjSMS2(String[] result) {
        if (result.length < 7) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String gzbh = result[3];
        String xh = result[4];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1))
                + Integer.parseInt(gzbh.substring(gzbh.length() - 1)) + Integer.parseInt(xh.substring(xh.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //发送短信

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 环控故障消息推送
     * @param result
     * @return
     */
    private String sendHkXxts(String[] result) {
        if (result.length < 9) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String gzbh = result[3];
        String xh = result[4];
        String dz = result[5];
        String scsj = result[6];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(scsj.substring(scsj.length() - 1))
                + Integer.parseInt(gzbh.substring(gzbh.length() - 1)) + Integer.parseInt(xh.substring(xh.length() - 1)) + Integer.parseInt(dz.substring(dz.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //业务处理

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }


    /**
     * 下发报警参数
     * @param result
     * @return
     */
    private String selectJqid(String[] result) {
        if (result.length < 5) {
            return "";
        }
        String jq = "0";
        String zdh = "0";
        String xlh = result[1];
        String cmd = result[2];
        String jy = result[result.length - 2];

        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";

        String gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;


        rs += gz + "+" + Integer.parseInt(jq) + "+" + zdh + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 同步校准砝码重量及允许校准误差
     * @param result
     * @return
     */
    private String selectJywc(String[] result) {
        if (result.length < 5) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];

        String wc = "0+0";
        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //获取

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += wc + "+" + gz + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;;
        return rs;
    }
    /**
     * 获取采食计量运行参数
     * @param result
     * @return
     */
    private String selectCsjlYxcs(String[] result) {
        if (result.length < 19) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String kqys = result[3];
        String xlsj = result[4];
        String mldd = result[5];
        String flsj = result[6];
        String kcdd = result[7];
        String cqzl = result[8];
        String czzl = result[9];
        String slgcsj = result[10];
        String dqddz = result[11];
        String ddz = result[12];
        String dqxdz = result[13];
        String xdz = result[14];
        String dmcs = result[15];
        String xmcs = result[16];
        String jy = result[result.length - 2];

        String wc = "0";
        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1))
                + Integer.parseInt(kqys.substring(kqys.length() - 1)) + Integer.parseInt(xlsj.substring(xlsj.length() - 1))
                + Integer.parseInt(mldd.substring(mldd.length() - 1)) + Integer.parseInt(flsj.substring(flsj.length() - 1))
                + Integer.parseInt(kcdd.substring(kcdd.length() - 1)) + Integer.parseInt(cqzl.substring(cqzl.length() - 1))
                + Integer.parseInt(czzl.substring(czzl.length() - 1)) + Integer.parseInt(slgcsj.substring(slgcsj.length() - 1))
                + Integer.parseInt(dqddz.substring(dqddz.length() - 1)) + Integer.parseInt(ddz.substring(ddz.length() - 1))
                + Integer.parseInt(dqxdz.substring(dqxdz.length() - 1)) + Integer.parseInt(xdz.substring(xdz.length() - 1))
                + Integer.parseInt(dmcs.substring(dmcs.length() - 1)) + Integer.parseInt(xmcs.substring(xmcs.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //获取
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += wc + "+" + gz + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;;
        return rs;
    }

    /**
     * 设备上传配置
     * @param result
     * @return
     */
    private String insertSbyxzt(String[] result) {
        if (result.length < 8) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String sbgs = result[3];
        String sbnr = result[4];
        String date = result[5];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1))
                + Integer.parseInt(sbgs.substring(sbgs.length() - 1)) + Integer.parseInt(sbnr.substring(sbnr.length() - 1))
                + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            // 保存状态
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 获取下料量
     */
    private String selectXlzl(String[] result) {
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[3];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        String sl = "0";

        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += sl + "+" + gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 上传饲料数据
     * @param result
     * @return
     */
    private String insertHkSl(String[] result) {
        if (result.length < 9) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String cgqid = result[3];
        String qt = result[4];
        String sj = result[5];
        String date = result[6];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid) % 10 + Integer.parseInt(cmd) % 10 + Integer.parseInt(qt.substring(qt.length() - 1)) + Integer.parseInt(sj.substring(sj.length() - 1)) + Long.parseLong(date) % 10 + Long.parseLong(cgqid) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //饲料
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 同步液位阈值
     * @param result
     * @return
     */
    private String selectQkywyz(String[] result) {
        if (result.length < 5) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];
        String date = result[result.length - 3];
        String sj = result[3];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(sj.substring(sj.length() - 1)) + Integer.parseInt(date.substring(date.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            Date time = DateUtil.parserDateToPatternString(date, XtAppConstant.TIME_FORMAT11);
            //获取本地的最新记录
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 同步液位阈值
     * @param result
     * @return
     */
    private String insetLogQdbzt(String[] result) {
        if (result.length < 7) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];
        String date = result[result.length - 3];
        String cgqid = result[3];
        String data = result[4];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(data.substring(data.length() - 1))
                + Integer.parseInt(date.substring(date.length() - 1)) + Integer.parseInt(cgqid.substring(cgqid.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 同步液位阈值
     * @param result
     * @return
     */
    private String insertPwcc(String[] result) {
        if (result.length < 7) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];
        String date = result[result.length - 3];
        String dcfzt = result[3];
        String pwdjzt = result[4];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(pwdjzt.substring(pwdjzt.length() - 1))
                + Integer.parseInt(date.substring(date.length() - 1)) + Integer.parseInt(dcfzt.substring(dcfzt.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 同步液位阈值
     * @param result
     * @return
     */
    private String updateYscs(String[] result) {
        if (result.length < 4) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0 + "+5+7";

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 获取打料时间
     * @param result
     * @return
     */
    public String selectLksj(String[] result) {
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[3];

        //读取料机时间


        //料机1时间
        StringBuilder lj1_sj =new StringBuilder();
        StringBuilder lj2_sj=new StringBuilder();
        StringBuilder lj3_sj=new StringBuilder();
        StringBuilder lj4_sj=new StringBuilder();

        lj1_sj.append("FFFF").append("FFFF").append("FFFF").append("FFFF");
        lj2_sj.append("FFFF").append("FFFF").append("FFFF").append("FFFF");
        lj3_sj.append("FFFF").append("FFFF").append("FFFF").append("FFFF");
        lj4_sj.append("FFFF").append("FFFF").append("FFFF").append("FFFF");

        StringBuilder rsSb = new StringBuilder();
        rsSb.append(XtAppConstant.SOCKET_SJSC_START);
        rsSb.append("+");
        rsSb.append(result[1] );
        rsSb.append("+");
        rsSb.append(cmd);
        rsSb.append("+");
        rsSb.append("1");
        rsSb.append("+");
        rsSb.append(lj1_sj);
        rsSb.append("+");
        rsSb.append("2");
        rsSb.append("+");
        rsSb.append(lj2_sj);
        rsSb.append("+");
        rsSb.append("3");
        rsSb.append("+");
        rsSb.append(lj3_sj);
        rsSb.append("+");
        rsSb.append("4");
        rsSb.append("+");
        rsSb.append(lj4_sj);
        rsSb.append("+");
        rsSb.append(jy);
        rsSb.append("+");
        rsSb.append(XtAppConstant.SOCKET_SJSC_END);
        return rsSb.toString();
    }


    /**
     * 插入设备状态
     * @param result
     * @return
     */
    private String insertTlgSbzt(String[] result) {
        if (result.length < 8) {
            return "";
        }
        String jqid =StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String sta = result[3];
        String err = result[4];
        String locate = result[5];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(sta.substring(sta.length() - 1))
                + Integer.parseInt(err.substring(err.length() - 1)) + Integer.parseInt(locate.substring(locate.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            //是否远程
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 推拉杆配置同步
     * @param result
     * @return
     */
    private String updatTlgPz(String[] result) {
        if (result.length < 9) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String xlsj = result[3];
        String lstime = result[4];
        String outtime = result[5];
        String date = result[6];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1))
                + Integer.parseInt(xlsj.substring(xlsj.length() - 1))  + Integer.parseInt(lstime.substring(lstime.length() - 1))  + Integer.parseInt(outtime.substring(outtime.length() - 1))
                + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        String dbjg = "0";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            if (jqid.equals("00000000")) {
                dbjg = XtAppConstant.SOCKET_SJSC_DBJG_1;
            } else {
                dbjg = XtAppConstant.SOCKET_SJSC_TLGTB_0;
            }
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + dbjg + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 料线定时控制
     * @param result
     * @return
     */
    private String updatLxdskz(String[] result) {
        if (result.length < 7) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String pznr = result[3];
        String date = result[4];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1))
                + Integer.parseInt(pznr.substring(pznr.length() - 1))
                + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";


        String dbjg = "0";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            if (jqid.equals("00000000")) {
                dbjg = XtAppConstant.SOCKET_SJSC_TLGTB_0;	//不需要同步
            } else {
               dbjg = XtAppConstant.SOCKET_SJSC_TLGTB_1;

            }
        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + dbjg + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 料线远程控制
     * @param result
     * @return
     */
    private String updatLxyckz(String[] result) {
        if (result.length < 7) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String lxzt = result[3];
        String date = result[4];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1))
                + Integer.parseInt(lxzt.substring(lxzt.length() - 1))
                + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        String dbjg = "0";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            dbjg = XtAppConstant.SOCKET_SJSC_TLGTB_0;

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + dbjg + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 灯光定时控制
     * @param result
     * @return
     */
    private String updatDgdskz(String[] result) {
        if (result.length < 7) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String pznr = result[3];
        String date = result[4];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1))
                + Integer.parseInt(pznr.substring(pznr.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        String dbjg = "0";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;
            dbjg = XtAppConstant.SOCKET_SJSC_TLGTB_0;

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + dbjg + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 推拉杆配置同步
     * @param result
     * @return
     */
    private String updatDgyckz(String[] result) {
        if (result.length < 6) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String date = result[3];
        String jy = result[result.length - 2];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1))
                + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Long.parseLong(date) % 10);
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        String dbjg = "0";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;

        dbjg = XtAppConstant.SOCKET_SJSC_TLGTB_0;

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + dbjg + "+";
        rs += jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }

    /**
     * 获取设备模块信息
     * @param result
     * @return
     */
    private String insetLogSbmkxx(String[] result) {
        if (result.length < 8) {
            return "";
        }
        String jqid = StringUtils.formatStringAdd0(result[1], 8, 1);
        String cmd = result[2];
        String jy = result[result.length - 2];
        String nameImei = result[3];
        String imsi = result[4];
        String ccid = result[5];

        String gz = String.valueOf(Integer.parseInt(jqid.substring(jqid.length() - 1)) + Integer.parseInt(cmd.substring(cmd.length() - 1)) + Integer.parseInt(nameImei.substring(nameImei.length() - 1))
                + Integer.parseInt(imsi.substring(imsi.length() - 1)) + Integer.parseInt(ccid.substring(ccid.length() - 1)));
        String rs = XtAppConstant.SOCKET_SJSC_START + "+" + result[1] + "+" + cmd + "+";
        if ((gz.substring(gz.length() - 1)).equals(jy)) {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_0;

        } else {
            gz = XtAppConstant.SOCKET_SJSC_GZBZ_1;
        }
        rs += gz + "+" + jy + "+" + XtAppConstant.SOCKET_SJSC_END;
        return rs;
    }


    @Override
    public void interfaceTest() {
        Map<String, Double> map = managementFeignService.selectByType((short) 1);
        System.out.println(map);
    }


}
