package com.mortals.xhx.feign.device;

import com.mortals.framework.common.Rest;
import com.mortals.xhx.common.pdu.UploadDeviceReq;
import com.mortals.xhx.common.pdu.device.DeviceMsgReqPdu;
import com.mortals.xhx.feign.IFeign;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 设备 消息 Feign接口
 *
 * @author zxfei
 * @date 2023-02-25
 */
@FeignClient(name = "device-manager", path = "/m", fallbackFactory = DeviceFeignFallbackFactory.class)
public interface IDeviceMessageFeign extends IFeign {


    /**
     * 设备消息通知
     *
     * @param devicePdu
     * @return
     */
    @PostMapping(value = "/api/callbackByProduct")
    Rest<String> callbackByProduct(@RequestBody DeviceMsgReqPdu devicePdu);


    /**
     * 设备消息 下发
     *
     * @param req
     * @return
     */
    @PostMapping(value = "/notify/downMsg")
    Rest<String> downMsg(@RequestBody UploadDeviceReq req);


}


@Slf4j
@Component
class DeviceMessageFeignFallbackFactory implements FallbackFactory<IDeviceMessageFeign> {
    @Override
    public IDeviceMessageFeign create(Throwable t) {
        return new IDeviceMessageFeign() {

            @Override
            public Rest<String> callbackByProduct(DeviceMsgReqPdu devicePdu) {
                return Rest.fail("发送消息失败，请稍后再试!");
            }

            @Override
            public Rest<String> downMsg(UploadDeviceReq req) {
                return Rest.fail("发送消息失败，请稍后再试!");
            }
        };
    }
}








