package com.mortals.xhx.module.capture.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.mortals.framework.common.Rest;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.capture.model.File;
import com.mortals.xhx.module.capture.service.CaptureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 高拍仪实现类
 *
 * @author: zxfei
 * @date: 2024/1/2 10:00
 */
@Service
@Slf4j
@ConditionalOnProperty(prefix = "config.capture", name = "impl", havingValue = "default")
public class CaptureServiceImpl implements CaptureService {

    @Value("${config.capture.url:http://127.0.0.1:9000/GetHardWareInfo?GetType=3}")
    private String url;

    private Integer timeout = 180;

    @Override
    public Rest<List<File>> getCapture() {
        url = url + "&watiTime=" + timeout;
        String resp = HttpUtil.get(url, timeout * 1000);
        log.info("获取拍照！ http resp:{}", resp);
        if (!ObjectUtils.isEmpty(resp)) {
            Rest<List<File>> rest = JSON.parseObject(resp, new TypeReference<Rest<List<File>>>() {
            });
            if (rest.getCode() == YesNoEnum.YES.getValue()) {
                log.info("获取拍照成功！fileData:{}", rest.getData().size());
                return rest;
            } else {
                return Rest.fail();
            }
        } else {
            log.info("获取拍照失败！resp:{}", resp);
            return Rest.fail();
        }

    }
}
