Projekt

Obecné

Profil

Stáhnout (2.52 KB) Statistiky
| Větev: | Tag: | Revize:
1
import platform
2
import logging
3
import getpass
4
from datetime import datetime
5

    
6
from .api_client import send_data
7

    
8

    
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
    """
18
    logging.debug("getting computer metadata")
19
    return {
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)
23
    }
24

    
25

    
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
    """
35
    logging.debug("payload send preparation")
36

    
37
    # Get metadata that will be put into the payload.
38
    payload = _get_metadata()
39

    
40
    # Add information about the USB device.
41
    payload["device"] = device
42

    
43
    # Add the status of the USB device (connected/disconnected).
44
    payload["status"] = status
45

    
46
    # Send the payload off to the server.
47
    send_data(payload)
48

    
49

    
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
    """
60
    logging.info(f"Device {device} has been connected")
61

    
62
    # Create a payload and send it to the API (server).
63
    _send_payload_to_server(device, "connected")
64

    
65

    
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
    """
76
    logging.info(f"Device {device} has been disconnected")
77

    
78
    # Create a payload and send it to the API (server).
79
    _send_payload_to_server(device, "disconnected")
(4-4/5)