package org.ts.ddcs.worker;


import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.log.LogHelper;

import java.util.concurrent.Callable;


public class LzLogSpliteWorker implements Callable<Integer> {

    private String log;

    LzLogSpliteWorker(String log) {
        this.log = log;
    }

    @Override
    public Integer call() throws Exception {
        try {
            JSONObject jsonLog = new JSONObject();
            int index = log.indexOf("***Receive String Hex:") + "***Receive String Hex:".length();
            String hexStr = log.substring(index);
            if (hexStr.substring(0,2).equals("68")) {
                jsonLog.put("agreement", "szy2012");
                jsonLog.put("rtu", hexStr.substring("682468B3".length(), "682468B3".length() + 10));
                jsonLog.put("datagram", hexStr);
                String tm = log.substring(0, "yyyymmdd hh:MM:ss".length());
                String rePackTm = tm.substring(0,4)+"-"+tm.substring(4,6)+"-"+tm.substring(6,8)+" "+tm.substring("yyyymmdd ".length());
                jsonLog.put("logTime", rePackTm);
                DataProcessHolder.newInstance().dataProcess.submitMapTask(jsonLog);
            }else{
                String tm = log.substring(0, "yyyy-mm-dd hh:MM:ss.mmm ".length());
                jsonLog.put("logTime", tm);
                jsonLog.put("agreement", "sw2014");
                jsonLog.put("rtu", hexStr.substring("7E7E01".length(), "7E7E01".length() + 10));
                jsonLog.put("datagram", hexStr);
                DataProcessHolder.newInstance().dataProcess.submitMapTask(jsonLog);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
