Skip to content

Commit

Permalink
Merge pull request #85 from usdot-jpo-ode/socket-fixes
Browse files Browse the repository at this point in the history
Socket fixes
  • Loading branch information
John-Wiens authored May 16, 2024
2 parents 5f13db4 + 31678a3 commit 0985b78
Showing 1 changed file with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package us.dot.its.jpo.ode.api.controllers;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer;

import us.dot.its.jpo.conflictmonitor.monitor.models.bsm.BsmIntersectionIdKey;
import us.dot.its.jpo.geojsonconverter.pojos.geojson.LineString;
import us.dot.its.jpo.geojsonconverter.pojos.geojson.map.ProcessedMap;
import us.dot.its.jpo.geojsonconverter.pojos.spat.ProcessedSpat;
import us.dot.its.jpo.ode.api.models.LiveFeedSessionIndex;
import us.dot.its.jpo.ode.model.OdeBsmData;

@Controller
Expand All @@ -20,6 +26,24 @@ public class StompController {
@Autowired
private SimpMessagingTemplate brokerMessagingTemplate;

private ObjectMapper mapper;

StompController(){
mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ISO_INSTANT;
ZonedDateTimeSerializer zonedDateTimeSerializer = new ZonedDateTimeSerializer(dateTimeFormatter);

SimpleModule module = new SimpleModule();
module.addSerializer(ZonedDateTime.class, zonedDateTimeSerializer);
mapper.registerModule(module);

mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
}



// @Scheduled(fixedRate = 10000) // Broadcast a message every second
public void broadcastMessage(String topic, String message) {
brokerMessagingTemplate.convertAndSend(topic, message);
Expand All @@ -41,7 +65,13 @@ public void broadcastSpat(ProcessedSpat spat) {
}

if (intersectionID != -1) {
broadcastMessage(buildTopicName(-1, intersectionID, "spat"), spat.toString());
try {
broadcastMessage(buildTopicName(-1, intersectionID, "spat"), mapper.writeValueAsString(spat));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

Expand All @@ -57,13 +87,23 @@ public void broadcastMap(ProcessedMap<LineString> map) {
}

if (intersectionID != -1) {
broadcastMessage(buildTopicName(-1, intersectionID, "map"), map.toString());
try {
broadcastMessage(buildTopicName(-1, intersectionID, "map"), mapper.writeValueAsString(map));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public void broadcastBSM(BsmIntersectionIdKey key, OdeBsmData bsm) {
if (key.getIntersectionId() != -1) {
broadcastMessage(buildTopicName(-1, key.getIntersectionId(), "bsm"), bsm.toString());
try {
broadcastMessage(buildTopicName(-1, key.getIntersectionId(), "bsm"), mapper.writeValueAsString(bsm));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Expand Down

0 comments on commit 0985b78

Please sign in to comment.