package com.xydl.cac.thermal;

import com.netsdk.lib.NetSDKLib;
import com.netsdk.module.LoginModule;
import com.netsdk.module.ThermalCameraModule;
import com.xydl.cac.entity.NSensor;
import com.xydl.cac.entity.ThermalConfig;
import com.xydl.cac.model.StaticVariable;
import com.xydl.cac.service.DataService;
import com.xydl.cac.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ThermalCollectService {

    @Resource
    DataService dataService;

    public void collectAndSave(List<NSensor> list, ThermalConfig config) {
        try {
            LoginModule.init(null, null);
            NSensor sensor = list.get(0);
            boolean r = LoginModule.login(sensor.getIp(), sensor.getPort(), sensor.getUsername(), sensor.getPasswd());
            if (r) {
                log.info("登入成功" + sensor.getIp() + ":" + sensor.getPort() + ", 用户名:" + sensor.getUsername());
            } else {
                log.error("登入失败" + sensor.getIp() + ":" + sensor.getPort() + ", 用户名:" + sensor.getUsername());
                return;
            }
            String time = DateUtil.format(new Date());
            for (NSensor item : list) {
                NetSDKLib.NET_RADIOMETRYINFO data = ThermalCameraModule.queryItemTemper(0, 0, sensor.getTmId(), sensor.getTmType());
                if (data == null) {
                    log.warn("没有采集到数据, 序号" + item.getTmId() + "测量项目" + sensor.getTmType());
                } else {
                    log.info("采集到序号" + item.getTmId() + "测量项目" + sensor.getTmType() + "的max="
                            + data.fTemperMax + ", min=" + data.fTemperMin + ", aver=" + data.fTemperAver);
                    if (StringUtils.isNotBlank(config.getMaxtemp())) {
                        String value = String.valueOf(data.fTemperMax);
                        dataService.insertData(item.getTableName(), item.getDevId(), time, config.getMaxtemp(), value);
                        // 更新最新数据缓存
                        StaticVariable.updateLastData(item.getDevId(), config.getMaxtemp(), value, time);
                    }
                    if (StringUtils.isNotBlank(config.getMintemp())) {
                        String value = String.valueOf(data.fTemperMin);
                        dataService.insertData(item.getTableName(), item.getDevId(), time, config.getMintemp(), value);
                        // 更新最新数据缓存
                        StaticVariable.updateLastData(item.getDevId(), config.getMintemp(), value, time);
                    }
                    if (StringUtils.isNotBlank(config.getAvertemp())) {
                        String value = String.valueOf(data.fTemperAver);
                        dataService.insertData(item.getTableName(), item.getDevId(), time, config.getAvertemp(), value);
                        // 更新最新数据缓存
                        StaticVariable.updateLastData(item.getDevId(), config.getAvertemp(), value, time);
                    }
                }
            }

            LoginModule.logout();
        } catch (Exception ex) {
            log.error("热成像测温采集异常, " + ex.getMessage(), ex);
        } finally {
            LoginModule.cleanup();
        }
    }
}
