Demo entry 6868803

xongdodo

   

Submitted by anonymous on Aug 20, 2019 at 04:49
Language: Java. Code size: 12.0 kB.

import com.alibaba.fastjson.JSON;
import com.wz.hcnet.HCNetService;
import com.wzzc.Wz_Link.WZ_Link_Inter;
import com.wzzc.commpg.Host_Pkt;
import com.wzzc.web.mapper.*;
import com.wzzc.web.model.*;
import com.wzzc.web.service.WZGwService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class WZGwServiceImpl implements WZGwService {
    @Autowired
    private WzEquipmentMapper wzEquipmentMapper;
    @Autowired
    private WzProjectEquipmentMapper wzProjectEquipmentMapper;
    @Autowired
    private WzProjectEquipmentSensorbaseMapper wzProjectEquipmentSensorbaseMapper;
    @Autowired
    private WzEquipmentSensorbaseMapper wzEquipmentSensorbaseMapper;
    @Autowired
    private WzProjectMapper wzProjectMapper;
    @Autowired
    private WzSensorbaseMapper wzSensorbaseMapper;

    /**
     * 启用socket检测设备是否存在
     *
     * @param equipmenttype 设备类型 0--网关 1--摄像头 2--门禁 3--报警
     * @param ip
     * @param sn
     * @param pw
     * @return
     */
    public static String isDevExist(Integer equipmenttype, String ip, String sn, String pw) {
        switch (equipmenttype) {
            case 0:
                try {
                    WZ_Link_Inter wz_link_inter = new WZ_Link_Inter("check");
                    return wz_link_inter.check(new Host_Pkt(ip, Integer.parseInt(sn), Integer.parseInt(pw)));
                } catch (NumberFormatException e) {
                    return "200002";
                }
            case 1:
            case 2:
                HCNetService hcNetService = new HCNetService();
                hcNetService.init();
                hcNetService.login(ip, (short) 8000, "admin", pw);
                int ccode = hcNetService.getlasterr();
                if (ccode == 1) {
                    hcNetService.unit();
                    return "200007";
                } else if (ccode == 17) {
                    hcNetService.unit();
                    return "200008";
                } else if (ccode != 0) {
                    hcNetService.unit();
                    return "200005";
                } else {
                    hcNetService.logout();
                    hcNetService.unit();
                    return "200000";
                }
            default:
                return "200009";
        }
    }


    /**
     * 向学生的项目设备中间表中添加对应的项目设备绑定
     * step : 首先通过ip和sn设备是否已注册,
     * 如果没有,则启用socket连接检测设备是否存在且在线,如果是,则注册设备,并添加项目设备绑定,如果否,则返回错误码
     * 如果有,则添加设备项目绑定
     *
     * @param equipmenttype
     * @param ip
     * @param sn
     * @param pw
     * @param wzProjectEquipment
     * @return
     */
    @Override
    public String addEquipment(Integer equipmenttype, String ip, String sn, String pw, WzProjectEquipment wzProjectEquipment) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        WzEquipment wzEquipment = wzEquipmentMapper.getEquipmentByIPSN(ip, sn);
        if (wzEquipment == null) {
            String code = isDevExist(equipmenttype, ip, sn, pw);
            if ("200000".equals(code)) {
                wzEquipment = new WzEquipment();
                wzEquipment.setEquipmentname(ip + sn);
                wzEquipment.setEquipmenttype(equipmenttype);
                wzEquipment.setIp(ip);
                if (equipmenttype == 0) {
                    wzEquipment.setProt1(1314);
                    wzEquipment.setProt2(520);
                } else if (equipmenttype == 2){
                    wzEquipment.setProt1(8000);
                } else if (equipmenttype == 3) {
                    wzEquipment.setProt1(8000);
                }
                wzEquipment.setSn(sn);
                wzEquipment.setPassword(pw);
                wzEquipment.setCreatetime(format.format(new Date()));
                wzEquipment.setModifytime(format.format(new Date()));
                wzEquipment.setIsonline(1);
                wzEquipmentMapper.insertEquipment(wzEquipment);

                wzEquipment = wzEquipmentMapper.getEquipmentByIPSN(ip, sn);
                wzProjectEquipment.setEquipmentid(wzEquipment.getEquipmentid());
                wzProjectEquipment.setCreatetime(format.format(new Date()));
                wzProjectEquipmentMapper.insert(wzProjectEquipment);
            }
            return code;
        } else {
            wzProjectEquipment.setEquipmentid(wzEquipment.getEquipmentid());
            wzProjectEquipment.setCreatetime(format.format(new Date()));
            int resultcode = 0;
            if (wzProjectEquipmentMapper.selectByPidEid(wzProjectEquipment.getProjectid(), wzEquipment.getEquipmentid()) == null) {
                resultcode = wzProjectEquipmentMapper.insert(wzProjectEquipment);
            }
            if (resultcode == 0) {
                return "200006";
            } else {
                return "200000";
            }
        }
    }

    /**
     * 通过项目id获得与项目绑定的所有的设备的信息
     *
     * @param projectid
     * @return
     */
    @Override
    public List<WzEquipmentSup> getEquipmentsByPid(String projectid, String equipmentAlias) {
        return wzEquipmentMapper.getEquipmentsByPid(projectid, equipmentAlias);
    }

    /**
     * 通过项目id和设备id获得对应设备下的传感器信息
     * <p>
     * step : 首先在通过项目id和设备id查询项目设备传感器中间表,获得对应的传感器集合
     * 如果传感器集合为空或长度小于1,则说明用户是第一次打开传感器页面,
     * 则从通过设备id从传感器类型查出传感器列表,并在项目设备传感器中间表中注册
     *
     * @param projectid
     * @param equipmentid
     * @param type
     * @return
     */
    @Override
    public List<WzSensorbaseSup> getSensors(String projectid, Integer equipmentid, Integer type) {
        /*List<WzSensorbaseSup> wzEquipmentSupList = wzProjectEquipmentSensorbaseMapper.getSensorsByPIDEID(projectid, equipmentid);
        if (wzEquipmentSupList == null || wzEquipmentSupList.size() < 1) {
            wzEquipmentSupList = wzEquipmentSensorbaseMapper.getSensorsByEID(equipmentid);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (WzSensorbaseSup wzSensorSup : wzEquipmentSupList) {
                wzSensorSup.setProjectid(projectid);
                wzSensorSup.setCreatetime(format.format(new Date()));
                wzSensorSup.setModifytime(format.format(new Date()));
                wzProjectEquipmentSensorbaseMapper.saveSensor(wzSensorSup);
            }
            if (type == null)
                wzEquipmentSupList = wzProjectEquipmentSensorbaseMapper.getSensorsByPIDEID(projectid, equipmentid);
        }
        if (type != null)*/
        return wzProjectEquipmentSensorbaseMapper.getSensorsByPIDEIDTYPE(projectid, equipmentid, type);
    }

    /**
     * 删除学生设备
     *
     * @param projectid
     * @param equipmentid
     * @return
     */
    @Override
    public Integer deleteEquipment(String projectid, Integer equipmentid) {
        WzProject project = wzProjectMapper.selectByPrimaryKey(projectid);
        if (project == null || project.getIscreate() == 1)
            return 0;
        wzProjectEquipmentSensorbaseMapper.deleteSensors(projectid, equipmentid);
        return wzProjectEquipmentMapper.deleteEquipmentByPidEid(projectid, equipmentid);
    }

    /**
     * 修改学生设备
     *
     * @param wzProjectEquipment
     * @return
     */
    @Override
    public Integer updateEquipment(WzProjectEquipment wzProjectEquipment) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        wzProjectEquipment.setModifytime(simpleDateFormat.format(new Date()));
        return wzProjectEquipmentMapper.updateProjectEquipmentByPidEid(wzProjectEquipment);
    }

    /**
     * 添加学生传感器
     *
     * @param wzSensorbaseSup
     * @return
     */
    @Override
    public Integer addSensor(WzSensorbaseSup wzSensorbaseSup) {
        Integer Sensorid = wzSensorbaseMapper.getPrimaryKey(wzSensorbaseSup.getUiid(), wzSensorbaseSup.getPassagewaynum());
        wzSensorbaseSup.setSensorid(Sensorid);
        if (wzProjectEquipmentSensorbaseMapper.getSensorByID(wzSensorbaseSup.getProjectid(), wzSensorbaseSup.getEquipmentid(), wzSensorbaseSup.getSensorid()) == null) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            wzSensorbaseSup.setCreatetime(format.format(new Date()));
            return wzProjectEquipmentSensorbaseMapper.saveSensor(wzSensorbaseSup);
        } else {
            return 0;
        }
    }

    /**
     * 删除学生传感器
     *
     * @param projectid
     * @param equipmentid
     * @param sensorid
     * @return
     */
    @Override
    public Integer delSensorByPidEidSid(String projectid, Integer equipmentid, Integer sensorid) {
        return wzProjectEquipmentSensorbaseMapper.deleteSensor(projectid, equipmentid, sensorid);
    }

    /**
     * 更新学生传感器
     *
     * @param wzSensorbaseSup
     * @return
     */
    @Override
    public Integer updateSensor(WzSensorbaseSup wzSensorbaseSup) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        wzSensorbaseSup.setModifytime(format.format(new Date()));
        return wzProjectEquipmentSensorbaseMapper.updateSensor(wzSensorbaseSup);
    }

    @Override
    public WzProjectEquipment getEquipmentsByPidEid(String projectid, Integer equipmentid) {
        return wzProjectEquipmentMapper.selectByPidEid(projectid, equipmentid);
    }

    @Override
    public WzSensorbaseSup getSensorByPidEidSid(Integer equipmentid, String projectid, Integer sensorid) {
        return wzProjectEquipmentSensorbaseMapper.getSensorByID(projectid, equipmentid, sensorid);
    }

    @Override
    public List getSensorByDevId4API(String projectid, Integer equipmentid) {
        List<WzSensorbaseSup> wzEquipmentSupList = wzProjectEquipmentSensorbaseMapper.getSensorsByPIDEID4API(projectid, equipmentid);
        if (wzEquipmentSupList == null || wzEquipmentSupList.size() < 1) {
            wzEquipmentSupList = wzEquipmentSensorbaseMapper.getSensorsByEID(equipmentid);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (WzSensorbaseSup wzSensorSup : wzEquipmentSupList) {
                wzSensorSup.setProjectid(projectid);
                wzSensorSup.setCreatetime(format.format(new Date()));
                wzSensorSup.setModifytime(format.format(new Date()));
                wzProjectEquipmentSensorbaseMapper.saveSensor(wzSensorSup);
            }
            wzEquipmentSupList = wzProjectEquipmentSensorbaseMapper.getSensorsByPIDEID4API(projectid, equipmentid);
        }

        return wzEquipmentSupList;
    }

    @Override
    public List<WzSensorbase> getSensorsByEquipment(String projectid, Integer equipmentid) {
        return wzEquipmentSensorbaseMapper.getSensorsByEID_other(projectid, equipmentid);
    }

    @Transactional
    @Override
    public Integer addSensor_batch(String projectid, Integer equipmentid, String data) {
        System.err.println(data);
        List<WzSensorbaseSup> wzSensorbaseSups = JSON.parseArray(data, WzSensorbaseSup.class);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int res = 0;
        System.err.println(wzSensorbaseSups.size());
        for (WzSensorbaseSup wzSensorbaseSup : wzSensorbaseSups) { ;
            wzSensorbaseSup.setProjectid(projectid);
            wzSensorbaseSup.setEquipmentid(equipmentid);
            wzSensorbaseSup.setCreatetime(format.format(new Date()));
            res += wzProjectEquipmentSensorbaseMapper.saveSensor(wzSensorbaseSup);
        }
        return res;
    }
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).