All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.foxtech.persist.common.service.updater.DeviceCommStatusUpdater Maven / Gradle / Ivy

The newest version!
/* ----------------------------------------------------------------------------
 * Copyright (c) Guangzhou Fox-Tech Co., Ltd. 2020-2024. All rights reserved.
 * --------------------------------------------------------------------------- */

package cn.foxtech.persist.common.service.updater;

import cn.foxtech.common.entity.constant.DeviceStatusVOFieldConstant;
import cn.foxtech.common.entity.entity.DeviceStatusEntity;
import cn.foxtech.common.utils.number.NumberUtils;
import cn.foxtech.device.domain.vo.OperateRespondVO;
import cn.foxtech.persist.common.service.PersistManageService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class DeviceCommStatusUpdater {
    private static final Logger logger = Logger.getLogger(DeviceCommStatusUpdater.class);

    /**
     * 实体管理
     */
    @Autowired
    private PersistManageService entityManageService;

    /**
     * 根据通信状态数据到redis
     */
    public void updateStatusEntity(Long deviceId, Map status) {
        try {
            if (status == null) {
                return;
            }

            DeviceStatusEntity statusEntity = new DeviceStatusEntity();
            statusEntity.setId(deviceId);

            long commFailedTime = NumberUtils.makeLong(status.get(OperateRespondVO.data_comm_status_failed_time));
            long commSuccessTime = NumberUtils.makeLong(status.get(OperateRespondVO.data_comm_status_success_time));

            long time = System.currentTimeMillis();

            Map existEntity = this.entityManageService.readHashMap(statusEntity.makeServiceKey(), DeviceStatusEntity.class);
            if (existEntity == null) {
                int commFailedCount = 0;
                if (commFailedTime > 0) {
                    commFailedCount++;
                }

                statusEntity.setCreateTime(time);
                statusEntity.setUpdateTime(time);

                // 插入对象
                statusEntity.setCommFailedCount(commFailedCount);
                statusEntity.setCommFailedTime(commFailedTime);
                statusEntity.setCommSuccessTime(commSuccessTime);
                this.entityManageService.writeEntity(statusEntity);

            } else {
                int commFailedCount = NumberUtils.makeInteger(existEntity.getOrDefault(DeviceStatusVOFieldConstant.field_failed_count, 0));
                if (commFailedTime > 0) {
                    commFailedCount++;
                } else {
                    commFailedCount = 0;
                }

                statusEntity.setUpdateTime(time);

                // 更新对象
                statusEntity.setCommFailedCount(commFailedCount);
                statusEntity.setCommFailedTime(commFailedTime);
                statusEntity.setCommSuccessTime(commSuccessTime);
                this.entityManageService.writeEntity(statusEntity);
            }
        } catch (Exception e) {
            logger.error(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy