package org.well.well.resp;

import net.sf.json.JSONObject;

import org.well.well.base.AbstractResponse;
import org.well.well.manager.ConcentratorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by lenovo on 2018/1/9.
 */
public class ConcentratorResponse extends AbstractResponse {

    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    private static final String[] mType = {"OnlineRequest", "Event"};
    public static final String[] bType = {"ConcentratorOnline", "ConcentratorOffline"};

    @Override
    public void process(String content) {
        ClassPathXmlApplicationContext ac = this.getAc();
        ConcentratorManager concentratorManager = ac.getBean(ConcentratorManager.class);

        JSONObject json = JSONObject.fromObject(content);
        String devCode = json.get("devCode").toString();
        JSONObject jsonObject = (JSONObject) json.get("mBody");
        if (mType[0].equals(json.get("mType"))) {//存储上报数据
            logger.info("--------RECEIVE:集中器：" + devCode + "，上传心跳------");
//            concentratorManager.saveConcen(devCode,jsonObject.get("logTime").toString());

        } else if (mType[1].equals(json.get("mType"))) {//存储报警事件

            if (bType[0].equals(jsonObject.get("bType"))) {
                logger.info("--------RECEIVE:集中器：" + devCode + "，上线 -------");
                concentratorManager.saveConcen(devCode, true, jsonObject.get("logTime").toString());
            } else if (bType[1].equals(jsonObject.get("bType"))) {
                logger.info("--------RECEIVE:集中器：" + devCode + "，下线 -------");
                concentratorManager.saveConcen(devCode, false, jsonObject.get("logTime").toString());
            }
        }
    }
}
