| | |
| | | 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.AllArgsConstructor; |
| | | 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.beans.factory.annotation.Value; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.kafka.annotation.KafkaListener; |
| | | import org.springframework.kafka.listener.ConsumerSeekAware; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | @Slf4j |
| | | public class SecondDataConsumer implements ConsumerSeekAware { |
| | | |
| | | @KafkaListener(containerFactory = "secondDataListenerFactory",topics = "second_data") |
| | | @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); |
| | | //long serverStartTime = System.currentTimeMillis(); |
| | | CopyOnWriteArraySet<SingleDeviceServer> sockets = SingleDeviceServer.sockets; |
| | | for (SingleDeviceServer socket : sockets) { |
| | | //判断消息是否数据该socket |
| | |
| | | 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(); |
| | | //对数据保留两位小数,并且向下取整 |
| | | Double sourceDataD = Double.valueOf(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); |
| | | bg = bg.setScale(2, BigDecimal.ROUND_FLOOR); |
| | | String sourceData = bg.toString(); |
| | | //数据补偿 |
| | | //单位转换 |
| | | if (!unitKey.equals(showUnitKey)) {//如果源单位和显示单位不同,则进行单位转换 |
| | | String formula = sensor.getFormula(); |
| | |
| | | resultMessgae.put(sensor.getCode(), resultData); |
| | | } else { |
| | | //拼接单位 |
| | | sourceData = sourceData + " " + showUnit; |
| | | if(!showUnit.equals("无单位")) |
| | | sourceData = sourceData + " " + showUnit; |
| | | resultMessgae.put(sensor.getCode(), sourceData); |
| | | } |
| | | } |
| | | //测试时间延迟使用的属性 |
| | | //resultMessgae.put("DataTime", message.get("DataTime")); |
| | | //resultMessgae.put("time", message.get("time")); |
| | | //resultMessgae.put("time1", message.get("time1")); |
| | | //resultMessgae.put("serverTime", System.currentTimeMillis()); |
| | | //resultMessgae.put("serverStartTime", serverStartTime); |
| | | socket.sendMessage(JSON.toJSONString(resultMessgae)); |
| | | } |
| | | } |