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

cn.foxtech.persist.common.service.DeviceObjectMapper Maven / Gradle / Ivy

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

package cn.foxtech.persist.common.service;

import cn.foxtech.common.entity.entity.BaseEntity;
import cn.foxtech.common.entity.entity.DeviceMapperEntity;
import cn.foxtech.common.utils.MapUtils;
import cn.foxtech.common.utils.pair.Pair;
import cn.foxtech.device.protocol.v1.utils.MethodUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DeviceObjectMapper {
    @Autowired
    private PersistManageService entityManageService;

    /**
     * 映射表
     */
    private Map mapperEntityMap;

    public Pair getValue(String manufacturer, String deviceType, String objectName) {
        return (Pair) MapUtils.getValue(this.mapperEntityMap, manufacturer, deviceType, objectName);
    }

    public Map getValue(String manufacturer, String deviceType) {
        return (Map) MapUtils.getValue(this.mapperEntityMap, manufacturer, deviceType);
    }

    /**
     * 从Redis读取来自管理服务的DeviceMapperEntity的配置信息,缓存到本地内存,
     * 方便后续数据加工的时候使用
     */
    public void syncEntity() {
        // 检查:是否有重新状态的配置到达
        Long updateTime = this.entityManageService.removeReloadedFlag(DeviceMapperEntity.class.getSimpleName());
        if (updateTime == null && this.mapperEntityMap != null) {
            return;
        }

        // 取出重新状态的配置
        List entityList = this.entityManageService.getEntityList(DeviceMapperEntity.class);
        Map map = new HashMap<>();
        for (BaseEntity entity : entityList) {
            DeviceMapperEntity mapperEntity = (DeviceMapperEntity) entity;

            // 检查数据
            if (MethodUtils.hasEmpty(mapperEntity.getManufacturer(), mapperEntity.getDeviceType(), mapperEntity.getObjectName(), mapperEntity.getMapperName(), mapperEntity.getMapperMode())) {
                continue;
            }

            // 构造数据
            Pair pair = new Pair(mapperEntity.getMapperName(), mapperEntity.getMapperMode());
            MapUtils.setValue(map, mapperEntity.getManufacturer(), mapperEntity.getDeviceType(), mapperEntity.getObjectName(), pair);
        }

        this.mapperEntityMap = map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy