Revize 1526cb8b
Přidáno uživatelem Jakub Šilhavý před více než 2 roky(ů)
client/src/usb_detector/event_listener.py | ||
---|---|---|
7 | 7 |
|
8 | 8 |
|
9 | 9 |
def _get_metadata() -> dict: |
10 |
"""Returns metadata associated with the computer. |
|
11 |
|
|
12 |
This metadata is sent to the server as a part |
|
13 |
of each payload. It includes the username, hostname, |
|
14 |
and timestamp. |
|
15 |
|
|
16 |
:return: metadata associated with the PC |
|
17 |
""" |
|
10 | 18 |
logging.debug("getting computer metadata") |
11 | 19 |
return { |
12 |
"username": getpass.getuser(), |
|
13 |
"hostname": platform.uname().node, |
|
14 |
"timestamp": str(datetime.now()).split('.')[0] |
|
20 |
"username": getpass.getuser(), # username
|
|
21 |
"hostname": platform.uname().node, # hostname
|
|
22 |
"timestamp": str(datetime.now()).split('.')[0] # timestamp (format: 2022-04-07 12:11:02)
|
|
15 | 23 |
} |
16 | 24 |
|
17 | 25 |
|
18 | 26 |
def _send_payload_to_server(device: dict, status: str): |
27 |
""" Creates a payload and calls the send_data function to send it to the server. |
|
28 |
|
|
29 |
Each payload consists of metadata, status (connected/disconnected), and device, |
|
30 |
which contains a vendor id, product id, and serial number. |
|
31 |
|
|
32 |
:param device: USB device that has been detected |
|
33 |
:param status: status of the USB device (connected/disconnected) |
|
34 |
""" |
|
19 | 35 |
logging.debug("payload send preparation") |
36 |
|
|
37 |
# Get metadata that will be put into the payload. |
|
20 | 38 |
payload = _get_metadata() |
39 |
|
|
40 |
# Add information about the USB device. |
|
21 | 41 |
payload["device"] = device |
42 |
|
|
43 |
# Add the status of the USB device (connected/disconnected). |
|
22 | 44 |
payload["status"] = status |
45 |
|
|
46 |
# Send the payload off to the server. |
|
23 | 47 |
send_data(payload) |
24 | 48 |
|
25 | 49 |
|
26 | 50 |
def usb_connected_callback(device: dict): |
51 |
"""Callback function for a connected USB device. |
|
52 |
|
|
53 |
This function gets called whenever a USB device is |
|
54 |
plugged into the computer (it is registered as a listener in |
|
55 |
the USB detector). The device consists of a vendor id, product id, |
|
56 |
and serial number. |
|
57 |
|
|
58 |
:param device: USB device that was just plugged into the PC. |
|
59 |
""" |
|
27 | 60 |
logging.info(f"Device {device} has been connected") |
61 |
|
|
62 |
# Create a payload and send it to the API (server). |
|
28 | 63 |
_send_payload_to_server(device, "connected") |
29 | 64 |
|
30 | 65 |
|
31 | 66 |
def usb_disconnected_callback(device: dict): |
67 |
"""Callback function for a disconnected USB device. |
|
68 |
|
|
69 |
This function gets called whenever a USB device is |
|
70 |
disconnected from the computer (it is registered as a listener in |
|
71 |
the USB detector). The device consists of a vendor id, product id, |
|
72 |
and serial number. |
|
73 |
|
|
74 |
:param device: USB device that was just disconnected from the PC. |
|
75 |
""" |
|
32 | 76 |
logging.info(f"Device {device} has been disconnected") |
77 |
|
|
78 |
# Create a payload and send it to the API (server). |
|
33 | 79 |
_send_payload_to_server(device, "disconnected") |
Také k dispozici: Unified diff
re #9422 Commented detector.py, event_listener.py, and usb_reader.py