//package com.ruoyi.system.core.scnner;
//
//import com.ruoyi.system.core.ClientExecute;
//import com.ruoyi.system.core.ClientExecuteFactory;
//import com.ruoyi.system.core.DeviceStatus;
//import com.ruoyi.system.core.domain.DeviceFunc;
//import com.ruoyi.system.core.mqtt.MqttFuncTopic;
//import com.ruoyi.system.domain.SysDevice;
//import com.ruoyi.system.service.ISysDeviceService;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import java.util.concurrent.ConcurrentHashMap;
//
//@Component("heartbeat")
//public class Heartbeat {
//    @Resource
//    private ISysDeviceService deviceService;
//    @Resource
//    private ClientExecuteFactory factory;
//
//    private static ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
//
//    public void run(){
//        SysDevice sysDevice = new SysDevice();
//        sysDevice.setOpenHeartbeat(true);
//        sysDevice.setEnable(true);
//        deviceService.selectList(sysDevice).subscribe(devices -> {
//            for (SysDevice x : devices) {
//                ClientExecute clientExecute = factory.getClientExecute(x.getConnectionType());
//                DeviceFunc deviceFunc = new DeviceFunc();
//                deviceFunc.setMessage(x.getHeartbeat());
//                deviceFunc.setFuncTopic(MqttFuncTopic.REPORT);
//                boolean send = clientExecute.send(x, deviceFunc);
//                Integer countError = map.get(x.getDeviceNo());
//                if (!send && !x.getStatus().equals(DeviceStatus.OFFLINE)) {
//                    if (countError == null || countError.equals(0)) {
//                        map.put(x.getDeviceNo(), 1);
//                    } else if (countError >= 3) {
//                        x.setStatus(DeviceStatus.OFFLINE);
//                        deviceService.update(x).subscribe();
//                    } else {
//                        map.put(x.getDeviceNo(), countError + 1);
//                    }
//                }
//                if (send) {
//                    map.put(x.getDeviceNo(), 0);
//                }
//            }
//        });
//    }
//}
