package org.well.well.resp;

import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.well.well.base.AbstractResponse;
import org.well.well.manager.ImeiManager;
import org.well.well.manager.LocatorManager;

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

    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    private static final String[] mType = {"Data", "Event", "StartupRequest"};

    @Override
    public void process(String content) {
        ClassPathXmlApplicationContext ac = this.getAc();
        LocatorManager locatorManager = ac.getBean(LocatorManager.class);
        ImeiManager imeiManager = ac.getBean(ImeiManager.class);
        JSONObject json = JSONObject.fromObject(content);
        String devCode = json.get("devCode").toString();
        logger.info("--------RECEIVE:设备编号：" + devCode + "，上传数据：" + json.toString() + "-------");
        JSONObject jsonObject = (JSONObject) json.get("mBody");
        if (mType[0].equals(json.get("mType"))) {//存储上报数据
            locatorManager.processNormalData(jsonObject,devCode);
        } else if (mType[2].equals(json.get("mType"))) {//三码存储
            try {
                String imei = jsonObject.get("imei").toString();
                String iccid = jsonObject.get("iccid").toString();
                imeiManager.saveData(devCode, imei, iccid);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("--------ERROR:设备编号：" + devCode + "，更新参数下发状态失败-------");
            }
        }
    }
}
