| | |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.entity.UnitConversion; |
| | | import com.moral.api.utils.AdjustDataUtils; |
| | | import com.moral.api.utils.UnitConvertUtils; |
| | | import com.moral.api.websocket.SingleDeviceServer; |
| | | import com.moral.util.UnitConvertUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.kafka.clients.consumer.Consumer; |
| | | import org.apache.kafka.clients.consumer.ConsumerRecord; |
| | | import org.apache.kafka.common.TopicPartition; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | * @Version TODO |
| | | **/ |
| | | @Component |
| | | @Slf4j |
| | | public class SecondDataConsumer implements ConsumerSeekAware { |
| | | |
| | | @KafkaListener(topics = "second_data", groupId = "SecondsDataGroup998") |
| | | @KafkaListener(containerFactory = "secondDataListenerFactory", topics = "second_data") |
| | | public void listen(ConsumerRecord<String, String> record, Consumer consumer) throws Exception { |
| | | String messageStr = record.value(); |
| | | Map<String, Object> message = (Map<String, Object>) JSON.parse(messageStr); |
| | | CopyOnWriteArraySet<SingleDeviceServer> sockets = SingleDeviceServer.sockets; |
| | | for (SingleDeviceServer socket : sockets) { |
| | | //判断消息书否数据该socket |
| | | //取出基本信息 |
| | | //判断消息是否数据该socket |
| | | String messageMac = (String) message.get("mac"); |
| | | String mac = socket.getMac(); |
| | | Map<String, Device> devicesInfo = socket.getDevicesInfo(); |
| | | Device device = devicesInfo.get(mac); |
| | | if (!mac.equalsIgnoreCase(messageMac)) |
| | | continue; |
| | | //取出基本信息 |
| | | Device device = socket.getDeviceAlarmInfo(); |
| | | Map<String, Object> adjustFormula = socket.getAdjustFormula(); |
| | | Map<String, Object> regionAqi = socket.getRegionAqi(); |
| | | //补偿数据 |
| | | if(adjustFormula!=null) |
| | | message = AdjustDataUtils.adjust(message,adjustFormula,regionAqi); |
| | | //创建最终消息对象 |
| | | Map<String, Object> resultMessgae = new HashMap<>(); |
| | | //拼接单位 |
| | |
| | | String showUnitKey = sensor.getShowUnitKey(); |
| | | String unitKey = sensor.getUnitKey(); |
| | | String unit = sensor.getUnit(); |
| | | String sourceData = String.valueOf(message.get(code)); |
| | | //数据补偿 |
| | | Object value = message.get(code); |
| | | if(value==null) |
| | | continue; |
| | | Double sourceDataD = Double.valueOf(String.valueOf(value)); |
| | | BigDecimal bg = new BigDecimal(sourceDataD); |
| | | bg = bg.setScale(2, BigDecimal.ROUND_FLOOR); |
| | | String sourceData = bg.toString(); |
| | | //单位转换 |
| | | if (!unitKey.equals(showUnitKey)) {//如果源单位和显示单位不同,则进行单位转换 |
| | | String formula = sensor.getFormula(); |
| | |
| | | //单位转换 |
| | | String resultData = UnitConvertUtils.calculate(sourceData, formula); |
| | | if (resultData != null) { |
| | | resultData+=showUnit; |
| | | }else{//如果转换出的数据为null,则代表缓存中也没有公式,依然使用源单位。 |
| | | resultData = sourceData+unit; |
| | | resultData += showUnit; |
| | | } else {//如果转换出的数据为null,则代表缓存中也没有公式,依然使用源单位。 |
| | | resultData = sourceData + unit; |
| | | } |
| | | resultMessgae.put(sensor.getCode(), resultData); |
| | | } else { |
| | | //拼接单位 |
| | | sourceData += showUnit; |
| | | if(!showUnit.equals("无单位")) |
| | | sourceData = sourceData + " " + showUnit; |
| | | resultMessgae.put(sensor.getCode(), sourceData); |
| | | } |
| | | } |
| | | resultMessgae.put("serverTime",System.currentTimeMillis()); |
| | | socket.sendMessage(JSON.toJSONString(resultMessgae)); |
| | | } |
| | | } |