package com.ruoyi.system.core.deviceEntity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.system.core.ClientExecute;
import com.ruoyi.system.core.annotate.DeviceEntity;
import com.ruoyi.system.core.domain.DeviceFunc;
import com.ruoyi.system.core.mqtt.DefProtocol;
import com.ruoyi.system.core.mqtt.MqttFuncTopic;
import com.ruoyi.system.core.mqtt.ReplyPackage;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.domain.SysDeviceProperty;
import com.ruoyi.system.service.ISysDevicePropertyService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@DeviceEntity(product = "SF")
@Component
public class WaterFertilizerMachine implements ProductEntity {
    @Resource
    private ISysDevicePropertyService propertyService;

    @Override
    public void put(DefProtocol defProtocol, ClientExecute clientExecute, SysDevice device) {
        try {
            // 使用getMethod查找公有方法
            Method method = this.getClass().getMethod(defProtocol.getFunctionId(), DefProtocol.class, ClientExecute.class, SysDevice.class);
            method.invoke(this, defProtocol, clientExecute, device); // 执行该方法
        } catch (NoSuchMethodException e) {
            // 找不到对应方法时，执行commonPut方法
            commonPut(defProtocol, clientExecute, device);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException("Failed to invoke the method", e);
        }
    }

    @Override
    public void info(DefProtocol defProtocol, ClientExecute clientExecute, SysDevice device) {
        try {
            // 使用getMethod查找公有方法
            Method method = this.getClass().getMethod(defProtocol.getFunctionId(), DefProtocol.class, ClientExecute.class, SysDevice.class);
            method.invoke(this, defProtocol, clientExecute, device); // 执行该方法
        } catch (NoSuchMethodException e) {
            // 找不到对应方法时，执行commonPut方法
            commonPut(defProtocol, clientExecute, device);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException("Failed to invoke the method", e);
        }
    }

}
