/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.yfd.platform.modules.basedata.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yfd.platform.config.HttpServerConfig;
import com.yfd.platform.modules.basedata.service.ISubstationPatroldeviceService;
import com.yfd.platform.utils.HttpRESTfulUtils;
import com.yfd.platform.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;

import java.net.URLEncoder;


/**
 * @date 2023-05-23
 * 动态执行摄像头静默监视任务
 */

@Slf4j
public class DailyMonitorJob implements Runnable {
    private String deviceinfo;
    private ISubstationPatroldeviceService deviceService;
    private HttpRESTfulUtils httpUtil;
    private HttpServerConfig serverConfig;

    public DailyMonitorJob(String deviceinfo) {
        this.deviceinfo = deviceinfo;
        this.deviceService = SpringContextHolder.getBean(ISubstationPatroldeviceService.class);
        this.httpUtil = SpringContextHolder.getBean(HttpRESTfulUtils.class);
        this.serverConfig = SpringContextHolder.getBean(HttpServerConfig.class);
    }

    @Override
    public void run() {
        log.info(String.format("静默任务开始执行时间：%s", DateUtil.now()));
        JSONObject device = JSONUtil.parseObj(deviceinfo);
        String isWorking = deviceService.isWorkingofPatroldevice(device.getStr("station_code"), device.getStr(
                "patroldevice_code"));
        if ("0".equals(isWorking)) {//摄像头不处于工作状态
            //路径命名格式为：变电站编码/年/月/日/巡视任务编码/CCD
            String filepath = String.format("%s/%s/",
                    device.getStr("station_code"),
                    DateUtil.format(DateUtil.date(), "yyyyMMdd")
            );
            //文件命名格式为：时间_区域_摄像头_类型.jpg
            String filename = String.format("%s_%s_%s_%s.jpg",
                    DateUtil.format(DateUtil.date(),"HHmmss"),
                    device.getStr("area_name"),
                    device.getStr("place"),
                    "原图"
            );
            try {
                String fullfilename = filepath + filename;
                String rtslurl = String.format("rtsp://%s:%s/rtp/%s_%s", serverConfig.getMediaIp(),
                        serverConfig.getMonitorRtspPort(), device.getStr(
                        "patroldevice_code"), device.getStr("patrolchannel_code"));
                if (ObjUtil.isNotEmpty(device.getStr("rtspurl"))) {
                    rtslurl = device.getStr("rtspurl");
                }
                httpUtil.getMonitorVideoSnap(serverConfig.getffmpegPath(), rtslurl, fullfilename);
                log.info("完成视频截图并保存到对应位置下！,文件名称：" + fullfilename);                // 3、 调用图片识别算法，进行图形识别
                JSONArray typelist = new JSONArray();
                typelist.add(device.getStr("sliencetype"));
                fullfilename = URLEncoder.encode(fullfilename, "utf-8");//转码存储
                log.info(String.format("静默任务执行完成时间：%s", DateUtil.now()));
                httpUtil.callPicAnalyse(device.getStr("patroldevice_id"), device.getStr("patroldevice_id"),
                        JSONUtil.toJsonStr(typelist), JSONUtil.toJsonStr(device.getJSONObject("customParams")),
                        fullfilename);
            } catch (Exception e) {
                log.error(e.getMessage());;
            }
         }
   }
}
