package org.nl.acs.common;

import com.alibaba.fastjson.JSONObject;
import org.nl.acs.device.domain.Device;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author geng by
 * @param <T>
 * 抽象类
 */
public class AbstractDriverService<T> implements IDriverService{
    @Override
    public JSONObject getDeviceInfo(Device device) {
        return null;
    }

    @Override
    public Integer getDbValue(Device device,String dbName) {
        return null;
    }


    public Integer getDbValue(T t,String dbName){
        Integer dbValue = 0;
        try {
            Class<?> tClass = t.getClass();
            Method methodDb = tClass.getMethod("get"+dbName);
            dbValue = (Integer) methodDb.invoke(t);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return dbValue;
    }


    public JSONObject getCommonDeviceInfo(T t){
        JSONObject map = new JSONObject();
        try {
            Class<?> tClass = t.getClass();
            Method methodMode = tClass.getMethod("getMode");
            Method methodMove = tClass.getMethod("getMove");
            Integer mode = (Integer) methodMode.invoke(t);
            Integer move = (Integer) methodMove.invoke(t);
           /* map.put("move",StageActorUtil.getMove(move));
            map.put("hasGoods",StageActorUtil.getHasGoods(move));
            map.put("mode",StageActorUtil.getMode(mode));
            map.put("isOnline",StageActorUtil.getIsOnline(mode));*/
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }

    public JSONObject getCommonDeviceInfo(T t,int i){
        JSONObject map = new JSONObject();
        try {
            Class<?> tClass = t.getClass();
            Method methodMode = tClass.getMethod("getMode");
            Integer mode = (Integer) methodMode.invoke(t);
     /*       map.put("mode",StageActorUtil.getMode(mode));
            map.put("isOnline",StageActorUtil.getIsOnline(mode));*/
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }

    public JSONObject getCommonDeviceInfo(T t,int i,int j){
        JSONObject map = new JSONObject();
        try {
            Class<?> tClass = t.getClass();
            Method methodMode = tClass.getMethod("getMode");
            Method methodMove = tClass.getMethod("getMove");
            Method methodGetHasGoods = tClass.getMethod("getHasGoods");
            Method methodGetIsonline = tClass.getMethod("getIsonline");
            Integer mode = (Integer) methodMode.invoke(t);
            Integer move = (Integer) methodMove.invoke(t);
            Integer hasGoods = (Integer) methodGetHasGoods.invoke(t);
            Boolean isOnline = (Boolean) methodGetIsonline.invoke(t);
     /*       map.put("mode",StageActorUtil.getMode(mode,0));
            map.put("move",StageActorUtil.getMove(move));*/
            map.put("hasGoods",hasGoods);
            map.put("isOnline",isOnline);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }
}
