|  |  |  | 
|---|
|  |  |  | import com.moral.entity.Account; | 
|---|
|  |  |  | import com.moral.entity.Device; | 
|---|
|  |  |  | import com.moral.entity.Sensor; | 
|---|
|  |  |  | import com.moral.entity.SensorUnit; | 
|---|
|  |  |  | import com.moral.service.AccountService; | 
|---|
|  |  |  | import com.moral.service.DeviceService; | 
|---|
|  |  |  | import com.moral.service.SensorService; | 
|---|
|  |  |  | import com.moral.service.SensorUnitService; | 
|---|
|  |  |  | import com.moral.util.RabbitMQUtils; | 
|---|
|  |  |  | import com.rabbitmq.client.*; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.jexl3.JexlBuilder; | 
|---|
|  |  |  | import org.apache.commons.jexl3.JexlEngine; | 
|---|
|  |  |  | import org.apache.commons.jexl3.JexlExpression; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.CrossOrigin; | 
|---|
|  |  |  | 
|---|
|  |  |  | import javax.websocket.server.PathParam; | 
|---|
|  |  |  | import javax.websocket.server.ServerEndpoint; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.concurrent.ConcurrentHashMap; | 
|---|
|  |  |  | import java.util.concurrent.CopyOnWriteArraySet; | 
|---|
|  |  |  | 
|---|
|  |  |  | public static AccountService accountService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static SensorService sensorService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static SensorUnitService sensorUnitService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * concurrent包的线程安全Set,用来存放每个客户端对应的WebSocket对象。 | 
|---|
|  |  |  | 
|---|
|  |  |  | String value = resultMap.get(Arrays.asList(resultMap.keySet().toArray(new String[resultMap.keySet().size()])).get(i)).toString(); | 
|---|
|  |  |  | /* String e = Arrays.asList(resultMap.keySet().toArray(new String[resultMap.keySet().size()])).get(i); | 
|---|
|  |  |  | resultMap.remove(e);*/ | 
|---|
|  |  |  | if (sensor.getUnit().equals("ppm")){ | 
|---|
|  |  |  | List<SensorUnit> sensorUnits = sensorUnitService.queryListBySensorId(sensor.getId()); | 
|---|
|  |  |  | if (sensorUnits.size()>1){ | 
|---|
|  |  |  | String s = sensorUnits.get(1).getRules(); | 
|---|
|  |  |  | s = s.replace("{0}",value); | 
|---|
|  |  |  | JexlEngine jexlEngine = new JexlBuilder().create(); | 
|---|
|  |  |  | JexlExpression jexlExpression = jexlEngine.createExpression(s); | 
|---|
|  |  |  | Object evaluate = jexlExpression.evaluate(null); | 
|---|
|  |  |  | BigDecimal bg = new BigDecimal(evaluate.toString()); | 
|---|
|  |  |  | value = bg.setScale(3, BigDecimal.ROUND_HALF_UP).toString(); | 
|---|
|  |  |  | reMap.put(sensor.getName(), value+sensorUnits.get(0).getName()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | String s = sensorUnits.get(0).getRules(); | 
|---|
|  |  |  | s = s.replace("{0}",value); | 
|---|
|  |  |  | JexlEngine jexlEngine = new JexlBuilder().create(); | 
|---|
|  |  |  | JexlExpression jexlExpression = jexlEngine.createExpression(s); | 
|---|
|  |  |  | Object evaluate = jexlExpression.evaluate(null); | 
|---|
|  |  |  | BigDecimal bg = new BigDecimal(evaluate.toString()); | 
|---|
|  |  |  | value = bg.setScale(3, BigDecimal.ROUND_HALF_UP).toString(); | 
|---|
|  |  |  | reMap.put(sensor.getName(), value+sensorUnits.get(0).getName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | reMap.put(sensor.getName(), value+sensor.getUnit()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | continue; | 
|---|