XingheDeviceService.java 4.39 KB
package com.viontech.integration.jiaoguansuo.service;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.integration.jiaoguansuo.entity.VideoEntity;
import com.viontech.integration.jiaoguansuo.model.DeviceDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * msl
 * 星河平台:设备服务
 */
@Service
@Slf4j
public class XingheDeviceService {
    @Value("${xinghe.device.syncSwitch:0}")
    private String deviceSyncSwitch;
    @Value("${xinghe.device.addUrl:}")
    private String deviceAddUrl;
    @Value("${xinghe.device.delUrl:}")
    private String deviceDelUrl;
    @Value("${xinghe.device.queryUrl:}")
    private String deviceQueryUrl;
    @Resource
    private RestTemplate restTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 添加设备
     *
     * @param addVideo
     */
    public DeviceDO addDevice(VideoEntity addVideo) {
        if (!"1".equals(deviceSyncSwitch)) {
            return null;
        }
        if (StringUtils.isBlank(deviceAddUrl)) {
            log.warn("deviceAddUrl is null");
            return null;
        }
        DeviceDO deviceDO = new DeviceDO();
        try {
            deviceDO.setSbcj("02");
            deviceDO.setJkdId(0);
            deviceDO.setSbmc("无锡所ADD");
            deviceDO.setSbip("0.0.0.0");
            deviceDO.setSbbh(addVideo.getSxjbh());
            deviceDO.setSbldz(addVideo.getJkdz());
            log.info("addDevice.data={}", JSON.toJSONString(deviceDO));
            //保存设备信息
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
            headers.setContentType(type);
            HttpEntity<DeviceDO> httpEntity = new HttpEntity<>(deviceDO, headers);
            ResponseEntity<Map> resEntity = restTemplate.exchange(deviceAddUrl, HttpMethod.PUT, httpEntity, Map.class);
            log.info("addDevice.result={}", JSON.toJSONString(resEntity.getBody()));
        } catch (Exception e) {
            log.error("addDevice.Exception", e);
        }
        return deviceDO;
    }

    /**
     * 删除设备
     *
     * @param delVideo
     */
    public void deleteDevice(VideoEntity delVideo) {
        if (!"1".equals(deviceSyncSwitch)) {
            return;
        }
        if (StringUtils.isBlank(delVideo.getSxjbh())) {
            log.warn("sxjbh is null");
            return;
        }
        if (StringUtils.isBlank(deviceQueryUrl) || StringUtils.isBlank(deviceDelUrl)) {
            log.warn("deviceQueryUrl,deviceDelUrl is null");
            return;
        }
        //查询出设备
        Integer deviceId = null;
        try {
            String requestUrl = deviceQueryUrl + "?pageSize=50&pageNum=1&sbbh=" + delVideo.getSxjbh();
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(URLDecoder.decode(requestUrl, "UTF-8"), String.class);
            Map resultMap = objectMapper.readValue(responseEntity.getBody(), Map.class);
            if (resultMap.containsKey("data") && resultMap.get("data") != null) {
                Map map = (Map) resultMap.get("data");
                if (map.get("list") != null) {
                    List<Map> mapList = (List<Map>) map.get("list");
                    for (Map deviceMap : mapList) {
                        if (delVideo.getSxjbh().equals(deviceMap.get("sbbh"))) {
                            deviceId = (Integer) deviceMap.get("id");
                            break;
                        }
                    }
                }
            }
            log.info("deleteDevice.id={}", deviceId);
            if (deviceId == null || deviceId <= 0) {
                return;
            }
            //删除设备信息
            String delUrl = String.format(deviceDelUrl, deviceId + "");
            restTemplate.delete(delUrl);
            log.info("deleteDevice.id={},success", deviceId);
        } catch (Exception e) {
            log.error("deleteDevice.Exception", e);
        }
        return;
    }
}