-
Notifications
You must be signed in to change notification settings - Fork 12
/
CoT.py
93 lines (77 loc) · 2.3 KB
/
CoT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import datetime as dt
import uuid
import xml.etree.ElementTree as ET
import socket
import logging
logger = logging.getLogger("django")
ID = {
"pending": "p",
"unknown": "u",
"assumed-friend": "a",
"friend": "f",
"neutral": "n",
"suspect": "s",
"hostile": "h",
"joker": "j",
"faker": "f",
"none": "o",
"other": "x"
}
DIM = {
"space": "P",
"air": "A",
"land-unit": "G",
"land-equipment": "G",
"land-installation": "G",
"sea-surface": "S",
"sea-subsurface": "U",
"subsurface": "U",
"other": "X"
}
DATETIME_FMT = "%Y-%m-%dT%H:%M:%SZ"
class CursorOnTarget:
def atoms(__self, unit):
timer = dt.datetime
now = timer.utcnow()
zulu = now.strftime(DATETIME_FMT)
stale_part = now.minute + 1
if stale_part > 59:
stale_part = stale_part - 60
stale_now = now.replace(minute=stale_part)
stale = stale_now.strftime(DATETIME_FMT)
unit_id = ID[unit["identity"]] or ID["none"]
cot_type = "a-" + unit_id + "-" + DIM[unit["dimension"]]
if "type" in unit:
cot_type = cot_type + "-" + unit["type"]
if "uid" in unit:
cot_id = unit["uid"]
else:
cot_id = uuid.uuid4().get_hex()
evt_attr = {
"version": "2.0",
"uid": cot_id,
"time": zulu,
"start": zulu,
"stale": stale,
"type": cot_type
}
pt_attr = {
"lat": str(unit["lat"]),
"lon": str(unit["lon"]),
"hae": "0", #unit["hae"],
"ce": "10", #unit["ce"],
"le": "10" #unit["le"]1
}
cot = ET.Element('event', attrib=evt_attr)
ET.SubElement(cot, 'detail')
ET.SubElement(cot,'point', attrib=pt_attr)
cot_xml = '<?xml version="1.0" standalone="yes"?>' + ET.tostring(cot)
return cot_xml
def pushUDP(__self, ip_address, port, cot_xml):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sent = sock.sendto(cot_xml, (ip_address, port))
return sent
def pushTCP(__self, ip_address, port, cot_xml):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn = sock.connect((ip_address, port))
return sock.send(cot_xml)