| | |
| | | package com.moral.api.websocket; |
| | | |
| | | import com.moral.api.entity.Device; |
| | | import com.moral.api.entity.MonitorPoint; |
| | | import com.moral.api.entity.Sensor; |
| | | import com.moral.api.entity.UnitConversion; |
| | | import com.moral.constant.RedisConstants; |
| | | import lombok.Data; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.ObjectUtils; |
| | | import org.springframework.web.bind.annotation.PathVariable; |
| | | |
| | | import javax.websocket.*; |
| | | import javax.websocket.server.PathParam; |
| | | import javax.websocket.server.ServerEndpoint; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.concurrent.CopyOnWriteArraySet; |
| | | |
| | | /** |
| | |
| | | **/ |
| | | @ServerEndpoint("/singleDevice/{mac}") |
| | | @Component |
| | | @Data |
| | | public class SingleDeviceServer { |
| | | |
| | | //线程安全集合,用于存放server对象 |
| | | public static CopyOnWriteArraySet<SingleDeviceServer> sockets = new CopyOnWriteArraySet<>(); |
| | | |
| | | public static RedisTemplate redisTemplate; |
| | | |
| | | private Session session; |
| | | |
| | | private String mac; |
| | | |
| | | private Device deviceAlarmInfo; |
| | | |
| | | private List<UnitConversion> unitConversions; |
| | | |
| | | private Map<String, Object> regionAqi; |
| | | |
| | | private Map<String,Object> adjustFormula; |
| | | |
| | | @OnOpen |
| | | public void onOpen(Session session, @PathParam("mac") String mac) throws Exception { |
| | | this.session = session; |
| | | this.mac = mac; |
| | | this.deviceAlarmInfo = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac); |
| | | this.unitConversions = redisTemplate.opsForList().range(RedisConstants.UNIT_CONVERSION, 0, -1); |
| | | this.adjustFormula = redisTemplate.opsForHash().entries("adjust_"+mac); |
| | | //获取设备地区对应的AQI用于补偿使用 |
| | | Map<String, Object> deviceInfo = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac); |
| | | Map<String, Object> monitorPointMap = (Map<String, Object>) deviceInfo.get("monitorPoint"); |
| | | String areaCode = String.valueOf(monitorPointMap.get("areaCode")); |
| | | String cityCode = String.valueOf(monitorPointMap.get("cityCode")); |
| | | try { |
| | | this.regionAqi = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.CITY_AQI, areaCode); |
| | | if (ObjectUtils.isEmpty(this.regionAqi)) |
| | | this.regionAqi = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.AQI_DATA, cityCode); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | sockets.add(this); |
| | | System.out.println(mac); |
| | | } |
| | | |
| | | @OnClose |
| | |
| | | |
| | | public void sendMessage(String message) throws Exception { |
| | | if (this.session.isOpen()) { |
| | | synchronized (session) { |
| | | this.session.getBasicRemote().sendText(message); |
| | | } |
| | | // synchronized (session) { |
| | | this.session.getBasicRemote().sendText(message); |
| | | // } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |