StremProxyService1078Impl.java 3.85 KB
package com.genersoft.iot.vmp.service.impl;

import com.genersoft.iot.vmp.VManageBootstrap;
import com.genersoft.iot.vmp.service.IStreamProxyService;
import com.genersoft.iot.vmp.service.StremProxyService1078;
import com.genersoft.iot.vmp.vmanager.jt1078.platform.ben.HttpClientPostEntity;
import com.genersoft.iot.vmp.vmanager.jt1078.platform.config.Jt1078ConfigBean;
import com.genersoft.iot.vmp.vmanager.jt1078.platform.handler.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class StremProxyService1078Impl implements StremProxyService1078 {

    @Autowired
    private Jt1078ConfigBean jt1078ConfigBean;

    @Autowired
    private HttpClientUtil httpClientUtil;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    private static final Logger log = LoggerFactory.getLogger(StremProxyService1078Impl.class);

    @Override
    public Map<String, Object> sendIORequestStop(String sim, String channel, String stream, Integer port, Integer httpPort) {
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isBlank(sim)) {
            resultMap.put("code", "-100");
            resultMap.put("message", "sim 不能为空");

            return resultMap;
        }

        if (StringUtils.isBlank(channel)) {
            resultMap.put("code", "-100");
            resultMap.put("message", "channel 不能为空");

            return resultMap;
        }

        String msg = jt1078ConfigBean.formatMessageStop(sim, channel);


        String url = null;
        try {
            url = StringUtils.replace(this.jt1078ConfigBean.getJt1078Url(), "{0}", this.jt1078ConfigBean.getStopSendPort());
            HttpClientPostEntity entity = httpClientUtil.doPost(url, msg, null);

            IStreamProxyService streamProxyService = VManageBootstrap.getBean(IStreamProxyService.class);
            if(Objects.nonNull(streamProxyService)){
                streamProxyService.stop1("schedule",stream);
            }
            log.info("port:[{}]",port);
            if(Objects.nonNull(port)){
//                VideoServerApp.stopServer(port,httpPort);
            }
            if (Objects.isNull(entity)) {
                log.info("HttpClientPostEntity is null");
            } else {
                log.info(entity.getResultStr());
            }

            redisTemplate.opsForValue().set("jt1078:count:"+stream,20000,300,TimeUnit.SECONDS);

            resultMap.put("code", "1");
            resultMap.put("message", "OK");

            return resultMap;
        } catch (Exception e) {
            log.error("发送停止推流指令异常;[{}],[{}]", url, msg, e);

            resultMap.put("code", "-20");
            resultMap.put("message", "发送停止推流指令异常");
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> sendIORequestStop(String stream) {
        String sim = StringUtils.substringBefore(stream, "-");
        String channel = StringUtils.substringAfterLast(stream, "-");

        Object port = this.redisTemplate.opsForValue().get("tag:history:port:" + stream);
        Object httpPort = this.redisTemplate.opsForValue().get("tag:history:httpPort:" + stream);

        return sendIORequestStop(sim, channel, stream, (Integer) port, (Integer) httpPort);
    }

    @Override
    public Integer stopCount(String stream) {
        Object countVal = redisTemplate.opsForValue().get("jt1078:count:" + stream);
        return Objects.nonNull(countVal)?(Integer) countVal:null;
    }
}