| | |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.fasterxml.jackson.databind.DeserializationFeature; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; |
| | | import com.xiaopeng.api.dto.account.XpOperationsUserPositionDTO; |
| | | import com.xiaopeng.pushMessage.webSocket.service.WebSocketUserConnectionManager; |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.mongodb.core.MongoTemplate; |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.data.mongodb.core.query.Update; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.socket.CloseStatus; |
| | |
| | | import org.springframework.web.socket.WebSocketSession; |
| | | import org.springframework.web.socket.handler.TextWebSocketHandler; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.time.LocalDateTime; |
| | | import java.util.UUID; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | private WebSocketAuthHandler authHandler; |
| | | |
| | | private ObjectMapper om; |
| | | |
| | | @PostConstruct |
| | | void setupObjectMapper() { |
| | | // 允许对象忽略json中不存在的属性 |
| | | om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| | | om.registerModule(new JavaTimeModule()); |
| | | om = new ObjectMapper(); |
| | | } |
| | | |
| | | // ==================== 连接生命周期 ==================== |
| | | |
| | |
| | | log.info("[服务器] 收到消息 -> {}", payload); |
| | | |
| | | try { |
| | | if (payload.startsWith("pos:")) { |
| | | if (log.isDebugEnabled()) |
| | | log.debug("收到运维人员位置上报消息:{}",payload); |
| | | XpOperationsUserPositionDTO xpOperationsUserPositionDTO = om.readValue(payload.substring(4), XpOperationsUserPositionDTO.class); |
| | | saveToMongo(xpOperationsUserPositionDTO); |
| | | } |
| | | JSONObject messageObj = JSON.parseObject(payload); |
| | | String messageType = messageObj.getString("type"); |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | @Autowired |
| | | private MongoTemplate mongoTemplate; |
| | | private void saveToMongo(XpOperationsUserPositionDTO dto) { |
| | | XpOperationsUserPositionDTO vehiclePosition = mongoTemplate.findOne(new Query(Criteria.where("id").is(dto.getId())), XpOperationsUserPositionDTO.class); |
| | | if (null == vehiclePosition) { |
| | | mongoTemplate.save(dto); |
| | | }else{ |
| | | Update update = new Update() |
| | | .set("id",dto.getId()) |
| | | .set("latitude", dto.getLatitude()) |
| | | .set("latitude", dto.getLongitude()); |
| | | mongoTemplate.updateFirst(new Query(Criteria.where("id").is(dto.getId())), update, XpOperationsUserPositionDTO.class); |
| | | } |
| | | } |
| | | |
| | | // ==================== 业务处理 ==================== |
| | | |
| | | /** |