STANAANDREY

netrom telemetry mije

Mar 24th, 2026
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5.  
  6. abstract class TelemetryEvent {
  7.     private int id, timestamp;
  8.  
  9.     public TelemetryEvent(int id, int timestamp) {
  10.         this.id = id;
  11.         this.timestamp = timestamp;
  12.     }
  13.  
  14.     protected String getCommon() {
  15.         return "id: " +  id + ", timestamp: " + timestamp;
  16.     }
  17.  
  18.     public abstract boolean shouldAlert();
  19.     public abstract String getAlertMessage();
  20.     public abstract String getSummary();
  21. }
  22.  
  23. class LocationEvent extends TelemetryEvent {
  24.     private final float lat, lon;
  25.  
  26.     public LocationEvent(int id, int timestamp, float lat, float lon) {
  27.         super(id, timestamp);
  28.         this.lat = lat;
  29.         this.lon = lon;
  30.     }
  31.  
  32.     @Override
  33.     public boolean shouldAlert() {
  34.         return lat < -90 || lat > 90 || lon < -180 || lon > 180;
  35.     }
  36.  
  37.     @Override
  38.     public String getAlertMessage() {
  39.         return " !!CORRUPTED COORDINATES!! ";
  40.     }
  41.  
  42.     @Override
  43.     public String getSummary() {
  44.         return "LocationEvent: " + getCommon() + ", lat: " + lat + ", lon: " + lon;
  45.     }
  46. }
  47.  
  48. class SpeedEvent extends TelemetryEvent {
  49.     private final float speed;
  50.  
  51.     public SpeedEvent(int id, int timestamp, float speed) {
  52.         super(id, timestamp);
  53.         this.speed = speed;
  54.     }
  55.  
  56.     @Override
  57.     public boolean shouldAlert() {
  58.         return speed > 110;
  59.     }
  60.  
  61.     @Override
  62.     public String getAlertMessage() {
  63.         return " !!SPEED TOO BIG!! ";
  64.     }
  65.  
  66.     @Override
  67.     public String getSummary() {
  68.         return "SpeedEvent: " + getCommon() + ", speed: " + speed;
  69.     }
  70. }
  71.  
  72.  
  73. interface NotifChannel {
  74.     void notify(TelemetryEvent event);
  75. }
  76.  
  77. class SMSNotifChannel implements NotifChannel {
  78.     private final String nr;
  79.  
  80.     public SMSNotifChannel(String nr) {
  81.         this.nr = nr;
  82.     }
  83.  
  84.     @Override
  85.     public void notify(TelemetryEvent event) {
  86.         IO.println("sending sms to " + nr + ".... " + event.getAlertMessage());
  87.     }
  88. }
  89.  
  90. class EmailNotifChannel implements NotifChannel {
  91.     private final String email;
  92.  
  93.     public EmailNotifChannel(String email) {
  94.         this.email = email;
  95.     }
  96.  
  97.     @Override
  98.     public void notify(TelemetryEvent event) {
  99.         IO.println("sending email to " + email + "... " + event.getAlertMessage());
  100.     }
  101. }
  102.  
  103. class EventProcessor {
  104.     private final TelemetryEvent[] events;
  105.     private final List<NotifChannel> channels;
  106.  
  107.     public EventProcessor(final TelemetryEvent[] events) {
  108.         this.events = events;
  109.         this.channels = new ArrayList<>();
  110.     }
  111.  
  112.     public void addChannel(final NotifChannel channel) {
  113.         this.channels.add(channel);
  114.     }
  115.  
  116.     public void showEvents() {
  117.         for (final var event : events) {
  118.             if (event.shouldAlert()) {
  119.                 for (final var channel : channels) {
  120.                     channel.notify(event);
  121.                 }
  122.             }
  123.             IO.println(event.getSummary());
  124.         }
  125.     }
  126. }
  127.  
  128. public class Main {
  129.     static void main(final String[] args) {
  130.         final var e1 = new SpeedEvent(1, 1231, 33);
  131.         final var e2 = new LocationEvent(2, 32, 2, 23);
  132.         final var e3 = new SpeedEvent(3, 322, 212);
  133.  
  134.         final TelemetryEvent[] events = {e1, e2, e3};
  135.         final var ep = new EventProcessor(events);
  136.  
  137.         final var smsChannel = new SMSNotifChannel("123456789");
  138.         ep.addChannel(smsChannel);
  139.  
  140.         ep.showEvents();
  141.         IO.println("----------------------------------------");
  142.  
  143.         final var emailNotifChannel = new EmailNotifChannel("[email protected]");
  144.         ep.addChannel(emailNotifChannel);
  145.         ep.showEvents();
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment