| | |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.xiaopeng.api.vo.automaticPilot.AppUserPosition; |
| | | import com.xiaopeng.common.redis.service.RedisService; |
| | | import com.xiaopeng.pushMessage.webSocket.service.WebSocketUserConnectionManager; |
| | | import io.netty.channel.ChannelHandler; |
| | | 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.geo.GeoJsonPoint; |
| | | 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 javax.annotation.Resource; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | |
| | | |
| | | @Autowired |
| | | private WebSocketAuthHandler authHandler; |
| | | |
| | | @Resource |
| | | private MongoTemplate mongoTemplate; |
| | | |
| | | Map<String, BusinessMsgHandler> businessMsgHandlers; |
| | | |
| | |
| | | log.info("收到用户 {} 的实时位置消息: {}", appUserId, messageObj.toJSONString()); |
| | | Double latitude = messageObj.getDouble("latitude"); |
| | | Double longitude = messageObj.getDouble("longitude"); |
| | | String timestamp = messageObj.getString("timestamp"); |
| | | |
| | | saveAppUserLocation(Long.parseLong(appUserId), latitude, longitude); |
| | | } |
| | | |
| | | // 回复消息 |
| | |
| | | log.error("WebSocket连接异常: {}", cause.getMessage(), cause); |
| | | ctx.channel().close(); |
| | | } |
| | | |
| | | |
| | | |
| | | private void saveAppUserLocation(Long appUserId, Double latitude, Double longitude) { |
| | | AppUserPosition userPosition = mongoTemplate.findOne(Query.query(Criteria.where("appUserId").is(appUserId)), AppUserPosition.class); |
| | | GeoJsonPoint point = new GeoJsonPoint(longitude, latitude); |
| | | if(null != userPosition){ |
| | | mongoTemplate.updateFirst(Query.query(Criteria.where("appUserId").is(appUserId)), |
| | | Update.update("point", point).set("addTime", System.currentTimeMillis()), AppUserPosition.class); |
| | | }else{ |
| | | userPosition = new AppUserPosition(); |
| | | userPosition.setAppUserId(appUserId); |
| | | userPosition.setPoint(point); |
| | | userPosition.setAddTime(System.currentTimeMillis()); |
| | | mongoTemplate.save(userPosition); |
| | | } |
| | | } |
| | | } |