|
| 1 | +use arma_rs::{FromArma, FromArmaError}; |
| 2 | +use chrono::{Utc, Duration, SecondsFormat}; |
| 3 | +use uuid::Uuid; |
| 4 | + |
| 5 | +pub struct MessagePayload { |
| 6 | + pub sender_callsign: String, |
| 7 | + pub chatroom: String, |
| 8 | + pub message_text: String, |
| 9 | + pub point_lat: f64, |
| 10 | + pub point_lon: f64, |
| 11 | + pub point_hae: f32, |
| 12 | + pub sender_uid: String, |
| 13 | +} |
| 14 | + |
| 15 | +impl FromArma for MessagePayload { |
| 16 | + fn from_arma(data: String) -> Result<Self, FromArmaError> { |
| 17 | + let (sender_callsign, chatroom, message_text, |
| 18 | + point_lat, point_lon, point_hae, sender_uid) = |
| 19 | + <(String, String, String, f64, f64, f32, String)>::from_arma(data)?; |
| 20 | + |
| 21 | + Ok(Self { |
| 22 | + sender_callsign, |
| 23 | + chatroom, |
| 24 | + message_text, |
| 25 | + point_lat, |
| 26 | + point_lon, |
| 27 | + point_hae, |
| 28 | + sender_uid, |
| 29 | + }) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub struct MessageCot { |
| 34 | + pub sender_callsign: String, |
| 35 | + pub chatroom: String, |
| 36 | + pub message_text: String, |
| 37 | + pub point_lat: f64, |
| 38 | + pub point_lon: f64, |
| 39 | + pub point_hae: f32, |
| 40 | + pub sender_uid: String, |
| 41 | +} |
| 42 | + |
| 43 | +impl MessageCot { |
| 44 | + pub fn from_payload(p: MessagePayload) -> Self { |
| 45 | + Self { |
| 46 | + sender_callsign: p.sender_callsign, |
| 47 | + chatroom: p.chatroom, |
| 48 | + message_text: p.message_text, |
| 49 | + point_lat: p.point_lat, |
| 50 | + point_lon: p.point_lon, |
| 51 | + point_hae: p.point_hae, |
| 52 | + sender_uid: p.sender_uid, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn to_xml(&self) -> String { |
| 57 | + let created_time = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true); |
| 58 | + let stale_time = (Utc::now() + Duration::days(1)) |
| 59 | + .to_rfc3339_opts(SecondsFormat::Millis, true); |
| 60 | + |
| 61 | + // MESSAGE ID (random UUID) |
| 62 | + let message_uuid = Uuid::new_v4().to_string(); |
| 63 | + |
| 64 | + // FULL EVENT UID |
| 65 | + // format: GeoChat.{sender}.{chatroom}.{uuid} |
| 66 | + let event_uid = format!( |
| 67 | + "GeoChat.{}.{}.{}", |
| 68 | + self.sender_uid, |
| 69 | + self.chatroom.replace(" ", "_"), |
| 70 | + message_uuid, |
| 71 | + ); |
| 72 | + |
| 73 | + let mut xml = String::new(); |
| 74 | + |
| 75 | + xml.push_str("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 76 | + |
| 77 | + xml.push_str( |
| 78 | + format!( |
| 79 | + "<event version=\"2.0\" uid=\"{}\" type=\"b-t-f\" time=\"{}\" start=\"{}\" stale=\"{}\" how=\"h-g-i-g-o\" access=\"Undefined\">", |
| 80 | + event_uid, created_time, created_time, stale_time |
| 81 | + ) |
| 82 | + .as_str(), |
| 83 | + ); |
| 84 | + |
| 85 | + xml.push_str( |
| 86 | + format!( |
| 87 | + "<point lat=\"{}\" lon=\"{}\" hae=\"{}\" ce=\"10.3\" le=\"9999999.0\"/>", |
| 88 | + self.point_lat, self.point_lon, self.point_hae |
| 89 | + ) |
| 90 | + .as_str(), |
| 91 | + ); |
| 92 | + |
| 93 | + xml.push_str("<detail>"); |
| 94 | + |
| 95 | + // ========== CHAT OBJECT ========== |
| 96 | + |
| 97 | + xml.push_str( |
| 98 | + format!( |
| 99 | + "<__chat parent=\"RootContactGroup\" groupOwner=\"false\" \ |
| 100 | + messageId=\"{}\" chatroom=\"{}\" id=\"{}\" senderCallsign=\"{}\">", |
| 101 | + message_uuid, |
| 102 | + self.chatroom, |
| 103 | + self.chatroom, |
| 104 | + self.sender_callsign, |
| 105 | + ) |
| 106 | + .as_str(), |
| 107 | + ); |
| 108 | + |
| 109 | + xml.push_str( |
| 110 | + format!( |
| 111 | + "<chatgrp uid0=\"{}\" uid1=\"{}\" id=\"{}\" />", |
| 112 | + self.sender_uid, |
| 113 | + self.chatroom, |
| 114 | + self.chatroom |
| 115 | + ) |
| 116 | + .as_str(), |
| 117 | + ); |
| 118 | + |
| 119 | + xml.push_str("</__chat>"); |
| 120 | + |
| 121 | + // ========== LINK ELEMENT ========== |
| 122 | + xml.push_str( |
| 123 | + format!( |
| 124 | + "<link uid=\"{}\" type=\"a-f-G-U-C\" relation=\"p-p\" />", |
| 125 | + self.sender_uid |
| 126 | + ) |
| 127 | + .as_str(), |
| 128 | + ); |
| 129 | + |
| 130 | + // ========== SERVER DEST ========== |
| 131 | + // This is optional — you may remove or customize it |
| 132 | + xml.push_str( |
| 133 | + format!( |
| 134 | + "<__serverdestination destinations=\"0.0.0.0:0:tcp:{}\" />", |
| 135 | + self.sender_uid |
| 136 | + ) |
| 137 | + .as_str(), |
| 138 | + ); |
| 139 | + |
| 140 | + // ========== MESSAGE REMARKS ========== |
| 141 | + xml.push_str( |
| 142 | + format!( |
| 143 | + "<remarks source=\"ARMATAK.{}\" to=\"{}\" time=\"{}\">{}</remarks>", |
| 144 | + self.sender_uid, self.chatroom, created_time, self.message_text |
| 145 | + ) |
| 146 | + .as_str(), |
| 147 | + ); |
| 148 | + |
| 149 | + xml.push_str("</detail></event>"); |
| 150 | + |
| 151 | + xml |
| 152 | + } |
| 153 | +} |
0 commit comments