Newer
Older
huludao / src / main / java / com / newfiber / api / pc / camera / service / CameraInfoService.java
package com.newfiber.api.pc.camera.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.ImmutableMap;
import com.newfiber.api.core.commons.CustomException;
import com.newfiber.api.pc.camera.dao.CameraInfoDao;
import com.newfiber.api.pc.camera.dto.CameraInfo;
import com.newfiber.api.pc.camera.dto.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * 摄像头表 Service
 *
 * @author xiebinbin
 * @date 2020-12-12 16:49:48
 */
@Service("cameraInfoService")
public class CameraInfoService extends BaseService<CameraInfoDao, CameraInfo> {
    @Autowired
    private CameraInfoDao cameraInfoDao;

    @Autowired
    private CameraService cameraService;

    /**
     * 根据条件获取摄像头表列表(分页)
     */
    public List<CameraInfo> queryList(Map<String, Object> map, Page<CameraInfo> page) {
        return cameraInfoDao.queryList(map, page);
    }

    /**
     * 根据条件获取摄像头表列表(无分页)
     */
    public List<CameraInfo> queryList(Map<String, Object> map) {
        return cameraInfoDao.queryList(map);
    }

    /**
     * 站点和摄像头绑定
     *
     * @param
     * @param siteId
     * @return boolean
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/12 17:06
     * @description: ${description}
     */
    public boolean bindSite(Integer id, Integer siteId) {
//        int count = selectCount(new EntityWrapper<CameraInfo>().eq("site_id", siteId));
//        if (count == 0) {
        CameraInfo cameraInfo = selectOne(new EntityWrapper<CameraInfo>().eq("id", id).setSqlSelect("device_code", "id", "status_online", "site_id", "device_name", "channel_id", "stream_id", "camera_type"));
        cameraInfo.setSiteId(siteId);
        return updateById(cameraInfo);
//        }
//        throw new CustomException(500,"该站点已经绑定摄像头,请先解绑摄像头");
    }

    /**
     * 解绑设备
     *
     * @param
     * @return boolean
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/12 17:08
     * @description: ${description}
     */
    public boolean unBindSite(Integer id) {
        CameraInfo cameraInfo = selectOne(new EntityWrapper<CameraInfo>().eq("id", id).setSqlSelect("device_code", "id", "status_online", "site_id", "device_name", "channel_id", "stream_id", "camera_type"));
        cameraInfo.setSiteId(-1);
        return updateById(cameraInfo);
    }

    /**
     * 根据设备id,播放视频
     *
     * @param id
     * @return java.lang.String
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/14 17:24
     * @description: ${description}
     */
    public String playCameraByCameraId(Integer id) {
        Optional<CameraInfo> op = Optional.ofNullable(selectById(id));
        CameraInfo info = op.orElseThrow(() ->  new CustomException(500,"找不到该摄像头信息"));
        if (StringUtils.isNotBlank(info.getDeviceCode())) {
            return cameraService.checkCameraPlaying(info.getDeviceCode(),info.getChannelId());
        }
        throw  new CustomException(500,"该设备未绑定设备序列号,请绑定设备序列号");
    }

    /**
     * 根据站点id,播放摄像头
     *
     * @param siteId
     * @return java.lang.String
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/14 17:29
     * @description: ${description}
     */
    public String playCameraBySiteId(Integer siteId) {
        Optional<CameraInfo> op = Optional.ofNullable(selectOne(new EntityWrapper<CameraInfo>().eq("site_id", siteId)));
        CameraInfo info = op.orElseThrow(() -> new CustomException(500,"找不到该摄像头信息,请确认是否绑定站点"));
        if (StringUtils.isNotBlank(info.getDeviceCode())) {
            return cameraService.checkCameraPlaying(info.getDeviceCode(),info.getChannelId());
        }
        throw new CustomException(500,"该设备未绑定设备序列号,请绑定设备序列号");
    }

    /**
     * 首页摄像头在线统计
     *
     * @param
     * @return Map
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/15 8:55
     * @description: ${description}
     */
    public Map<String, Object> indexCameraStatistics() {
        int onlineCount = selectCount(new EntityWrapper<CameraInfo>().eq("status_online", 1).ne("device_code", "").isNotNull("device_code"));
        int count = selectCount(new EntityWrapper<CameraInfo>());
        return ImmutableMap.of("onlineCount", onlineCount, "count", count, "offline", count - onlineCount);
    }

    /**
     * 更新摄像头的在线状态
     *
     * @param
     * @return void
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/15 9:51
     * @description: ${description}
     */
    public void rpcCameraStatus() {
        List<CameraInfo> cameraInfoList = selectList(new EntityWrapper<CameraInfo>().ne("device_code", "").isNotNull("device_code").setSqlSelect("device_code", "id", "status_online", "site_id", "device_name", "channel_id", "stream_id", "camera_type"));
        cameraInfoList.forEach(i->{
           if(cameraService.selectCameraStatus(i.getDeviceCode())) {
               i.setStatusOnline(1);
           }else {
               i.setStatusOnline(0);
           }
            updateById(i);
        });
    }
    /**
     * 自动绑定到平台账号
     * @param
     * @return void
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/21 15:35
     * @description: ${description}
     */
    public void autoBuildCameraByDeviceCode() {
        List<CameraInfo> cameraInfoList = selectList(new EntityWrapper<CameraInfo>().ne("device_code", "").isNotNull("device_code").setSqlSelect("device_code", "id", "status_online", "site_id", "device_name", "channel_id", "stream_id", "camera_type"));
        cameraInfoList.forEach(i -> {
            try {
                bind(i.getDeviceCode());
            } catch (Exception e) {
                logger.error("自动绑定摄像头到平台账号错误:{},设备编号:{}", e.getLocalizedMessage(),i.getDeviceCode());
            }
        });
    }
    /**
     * 根据设备code查询,该账号是否被平台账号绑定绑定
     * @param deviceCode
     * @return void
     * @author djt
     * @creed: Talk is cheap,show me the code
     * @date 2020/12/21 15:36
     * @description: ${description}
     */
    private void bind(String deviceCode) {
        Result<JSONObject> result = cameraService.selectDeviceBindByDeviceCode(deviceCode);
        if (StringUtils.equalsIgnoreCase(result.getCode(), "0")) {
//            if (result.getData().getBoolean("isBind") && result.getData().getBoolean("isMine")) {
////                //已经绑定,且是自己账号
////            } else
              if (!result.getData().getBoolean("isBind")) {
                //没有被绑定
                cameraService.bindDevice(deviceCode, "admin123");
            }
            return;
        }
        throw new CustomException(500,result.getMsg());
    }
}