package com.huatek.busi.task;

import com.alibaba.fastjson.JSONObject;
import com.huatek.busi.mapper.IecPdFileHeaderMapper;
import com.huatek.busi.task.base.BusiTaskExecutor;
import com.mathworks.toolbox.javabuilder.MWException;
import lombok.extern.slf4j.Slf4j;
import matlabplot.MathPlot;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * nn Matlab谱图定时器
 * 
 * @author edward
 * @date 2019/11/09
 */
@Slf4j
@Component("matlabTask")
public class MatlabTask implements BusiTaskExecutor {

	@Value("${base.username}")
	private String baseUserName;

	@Value("${base.password}")
	private String basePassword;

	@Value("${base.url}")
	private String baseUrl;

	@Value("${base.matlabImage.path}")
	private String matlabImagePath;

	@Autowired
	private IecPdFileHeaderMapper iecPdFileHeaderMapper;

	@Override
	public void execute() {
		Long counts = iecPdFileHeaderMapper.selectCount();
		if (null == counts || counts == 0) {
			log.info("未发现图谱数据.......");
			return;
		}
		/*ExecutorService executor = Executors.newSingleThreadExecutor();
		try{
			Future<?> result = executor.submit(new Callable<Object>() {
				@Override
				public Object[] call()   {
					Runtime runtime = Runtime.getRuntime();
					long startMemory = runtime.totalMemory()/1024/1024;
					log.info("---->> 调用Matlab开始,当前内存[{}]M",startMemory );
					long startTime = System.currentTimeMillis();
					Object[] obj = callMatlab();
					long endTime=System.currentTimeMillis();
					float excTime=(float)(endTime-startTime)/1000;
					long endMemory = runtime.totalMemory()/1024/1024;
					log.info("---->> 调用Matlab结束,当前内存[{}]M",endMemory);
					log.info("---->> 调用Matlab结束,用时[{}]秒", excTime);
					return obj ;
				}
			});
			if(null != result.get() ){
				log.info("---->> 调用Matlab返回结果[{}]", JSONObject.toJSON(result.get() ));
			}
//			executor.shutdown();
		}catch(Exception e){


		}	*/
	
		MathPlot func  =null ;
		try {
			 func = new MathPlot();
			String params[] = baseUrl.split(":");
			String dbUrl = params[2].replace("//", "");
			String portArr[] = params[3].split("/");
			String dbPort = portArr[0];
			String dbNameArr[] = portArr[1].split("\\?");
			String dbName = dbNameArr[0];
			Runtime runtime = Runtime.getRuntime();
			long startMemory = runtime.totalMemory()/1024/1024;
			log.info("---->> 调用Matlab开始,当前内存[{}]M",startMemory );
			long startTime = System.currentTimeMillis();
			Object[] result =  func.soectrogram(1, dbName, dbUrl, baseUserName, basePassword, dbPort, matlabImagePath, 0, -59);
			long endMemory = runtime.totalMemory()/1024/1024;
			long endTime=System.currentTimeMillis();
			float excTime=(float)(endTime-startTime)/1000;
			log.info("---->> 调用Matlab结束,当前内存[{}]M",endMemory);
			log.info("---->> 调用Matlab结束,用时[{}]秒", excTime);
			 if (ObjectUtils.isNotEmpty(result)) {
				 log.info("---->> 调用Matlab返回结果",result[0].toString().equals("") ? 9 : result[0]);
			 }
		} catch (MWException e) {
				e.printStackTrace();
				log.error("---->> 调用Matlab异常{[]}",e.getMessage());
			}finally{
				if(null != func){
					log.info("---->> 调用Matlab开始执行关闭操作");
					func.dispose();
					log.info("---->> 调用Matlab关闭完成");
				}
			}
		}

	private Object[] callMatlab()  {
		MathPlot func = null;
		Object[] result= null ;
		try {
			func = new MathPlot();
			String params[] = baseUrl.split(":");
			String dbUrl = params[2].replace("//", "");
			String portArr[] = params[3].split("/");
			String dbPort = portArr[0];
			String dbNameArr[] = portArr[1].split("\\?");
			String dbName = dbNameArr[0];
			result =  func.soectrogram(1, dbName, dbUrl, baseUserName, basePassword, dbPort, matlabImagePath, 0, -59);
			log.info("---->> 调用Matlab返回结果",result);
		} catch (MWException e) {
			e.printStackTrace();
			log.error("---->> 调用Matlab异常{[]}",e.getMessage());
		}finally{
			if(null != func){
				log.info("---->> 调用Matlab开始执行关闭操作");
				func.dispose();
				log.info("---->> 调用Matlab关闭完成");
			}
		}
		return  result;
	}

	@Override
	public void execute(String params) {

	}
	
	public static void main(String [] args ){
		Object [] a = new Object []{1};
		System.out.print(JSONObject.toJSON(a ));
	}
}
