package com.yfyy.iotx.util;

import com.aliyun.iot.as.bridge.core.handler.tsl.TslUplinkHandler;
import com.aliyun.iot.as.bridge.core.model.Session;
import com.yfyy.iotx.bean.DevSubBean;
import com.yfyy.iotx.bean.ExtBean;

import java.util.Random;

/**
 * 数据处理工具类
 */
public class DataUtil {

    private final static Random random = new Random();

    // 解析数据，然后上报
    public static void handleAndReport(DevSubBean devSubBean){
        ExtBean extBean = devSubBean.getExt();
        String originalIdentity = extBean.getImei();

        try{
            onlineAndReportProperty(originalIdentity, extBean);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // 上线+上报属性
    private static void onlineAndReportProperty(String originalIdentity, ExtBean extBean){
        // device Session of your specific protocol, If none, you can just use empty object
        Object originalChannel = new Object();
        Session session = Session.newInstance(originalIdentity, originalChannel);
        // 设备上线
        TslUplinkHandler tslUplinkHandler = new TslUplinkHandler();
        tslUplinkHandler.doOnline(session, originalIdentity);
        // 上报属性
        String requestId = String.valueOf(random.nextInt(1000));

        tslUplinkHandler.reportProperty(requestId, originalIdentity, "gps", extBean.getGps());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "ds", extBean.getDs());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "tt", extBean.getTt());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "batStatus", extBean.getBatStatus());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "hst", extBean.getHst());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "htim", extBean.getHtim());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "turnon", extBean.getTurnon());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "stamp", extBean.getStamp());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "hrt", extBean.getHrt());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "current", extBean.getCurrent());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "collt", extBean.getCollt());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "rps", extBean.getRps());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "lps", extBean.getLps());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "tps", extBean.getTps());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "vbat", extBean.getVbat());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "dver", extBean.getDver());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "dtype", extBean.getDtype());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "iccid", extBean.getIccid());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "imei", extBean.getImei());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "shake_sec", extBean.getShake_sec());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "upds", extBean.getUpds());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "dnds", extBean.getDnds());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "lamp", extBean.getLamp());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "fuse_voltage", extBean.getFuse_voltage());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "proj", extBean.getProj());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "vs", extBean.getVs());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "dat_f", extBean.getDat_f());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "shake", extBean.getShake());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "ts", extBean.getTs());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "gs", extBean.getGs());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "hs", extBean.getHs());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "tph", extBean.getTph());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "tpl", extBean.getTpl());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "ah", extBean.getAh());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "at", extBean.getAt());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "et", extBean.getEt());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "st", extBean.getSt());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "ws", extBean.getWs());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "csq", extBean.getCsq());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "lat", extBean.getLat());
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "lng", extBean.getLng());
    }

    // 上线+上报图片
    public static void onlineAndReportPic(String originalIdentity, String image, String result_image, String result){
        // device Session of your specific protocol, If none, you can just use empty object
        Object originalChannel = new Object();
        Session session = Session.newInstance(originalIdentity, originalChannel);
        // 设备上线
        TslUplinkHandler tslUplinkHandler = new TslUplinkHandler();
        tslUplinkHandler.doOnline(session, originalIdentity);
        // 上报属性
        String requestId = String.valueOf(random.nextInt(1000));
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "Image", image);
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "Result_image", result_image);
        tslUplinkHandler.reportProperty(requestId, originalIdentity, "Result", result);
    }

    // 离线
    public static boolean offline(String originalIdentity){
        // 设备离线
        TslUplinkHandler tslUplinkHandler = new TslUplinkHandler();
        return tslUplinkHandler.doOffline(originalIdentity);
    }
}
