STANAANDREY

netrom telemetry modern

Mar 24th, 2026
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. // Using an interface with default behavior or a sealed hierarchy is cleaner in modern Java
  4. sealed interface TelemetryEvent permits LocationEvent, SpeedEvent {
  5.     int id();
  6.     int timestamp();
  7.     String getSummary();
  8.  
  9.     // Combined Alert Management
  10.     default Optional<String> getAlert() {
  11.         return Optional.empty();
  12.     }
  13. }
  14.  
  15. record LocationEvent(int id, int timestamp, float lat, float lon) implements TelemetryEvent {
  16.     @Override
  17.     public Optional<String> getAlert() {
  18.         if (lat < -90 || lat > 90 || lon < -180 || lon > 180) {
  19.             return Optional.of("!!CORRUPTED COORDINATES!!");
  20.         }
  21.         return Optional.empty();
  22.     }
  23.  
  24.     @Override
  25.     public String getSummary() {
  26.         return String.format("LocationEvent [id=%d]: lat=%.2f, lon=%.2f", id, lat, lon);
  27.     }
  28. }
  29.  
  30. record SpeedEvent(int id, int timestamp, float speed) implements TelemetryEvent {
  31.     @Override
  32.     public Optional<String> getAlert() {
  33.         return speed > 110 ? Optional.of("!!SPEED TOO HIGH!!") : Optional.empty();
  34.     }
  35.  
  36.     @Override
  37.     public String getSummary() {
  38.         return String.format("SpeedEvent [id=%d]: speed=%.2f", id, speed);
  39.     }
  40. }
  41.  
  42. interface NotifChannel {
  43.     void notify(String message);
  44. }
  45.  
  46. // Concrete Observers
  47. record SMSNotifChannel(String nr) implements NotifChannel {
  48.     public void notify(String msg) { System.out.println("SMS to " + nr + ": " + msg); }
  49. }
  50.  
  51. record EmailNotifChannel(String email) implements NotifChannel {
  52.     public void notify(String msg) { System.out.println("Email to " + email + ": " + msg); }
  53. }
  54.  
  55. class EventProcessor {
  56.     private final List<NotifChannel> channels = new ArrayList<>();
  57.  
  58.     public void addChannel(NotifChannel channel) { channels.add(channel); }
  59.  
  60.     public void process(TelemetryEvent event) {
  61.         // Handle Alerts
  62.         event.getAlert().ifPresent(msg -> {
  63.             channels.forEach(channel -> channel.notify(msg));
  64.         });
  65.  
  66.         // Log Summary
  67.         System.out.println(event.getSummary());
  68.     }
  69. }
  70.  
  71. public class Main {
  72.     static void main(String[] args) {
  73.         var processor = new EventProcessor();
  74.         processor.addChannel(new SMSNotifChannel("123-456"));
  75.  
  76.         List<TelemetryEvent> events = List.of(
  77.                 new SpeedEvent(1, 1000, 30),
  78.                 new SpeedEvent(2, 1001, 150), // Should Alert
  79.                 new LocationEvent(3, 1002, 200, 45) // Should Alert
  80.         );
  81.  
  82.         events.forEach(processor::process);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment