<commit_before>from twisted.internet import reactor, protocol
import json
import os
import subprocess
import typesafety

MASTER_PIN = '12345678'
REGISTERED_DEVICES = {}
PORT = 9500
# Note:  The port number for the server doesn't really matter since the "proxy" (socat) will be used to redirect the
# Widget traffic to wherever we want.   If you changed this port to 5000 and ran the server on the BeagleBone's host
# computer then you wouldn't need socat.  However, you should make sure that your system is tested with a proxy
# because it will be necessary for connecting to the live server.

DEFAULT_PIN = '123456'
DEFAULT_FLAG = '<theflag>'

ROOTDIR = os.path.dirname(__file__)
REGISTERED_FILE = os.path.join(ROOTDIR, 'registered-widgets.txt')
REQUESTED_FILE = os.path.join(ROOTDIR, 'requested-widgets.txt')

class Widget(object):
    """
    Represents a Widget Device
    """

    def __init__(self, json_str):
        # Populate object attributes from JSON object
        # checks if valid JSON
        try:
            data = json.loads(json_str)
        except ValueError:
            raise ValueError('ValueError: Invalid JSON Values')
        # Checks device_id for type int
        if not isinstance(data.get('device_id', None), int):
            raise TypeError('Invalid type {!r}, expected int')
        else:
            self.device_id = data.get('device_id', None)
        # Checks pin for type int
        if not isinstance(data.get('pin', None), int):
            raise TypeError('Invalid type {!r}, expected int')
        else:
            self.device_id = data.get('pin', None)
        # Checks flag for type String
        if not isinstance(data.get('flag', None), str):
            raise TypeError('Invalid type {!r}, expected int')
        else:
            self.device_id = data.get('flag', None)
        # Checks device_id for type int
        if not isinstance(data.get('device_key', None), int):
             raise TypeError('Invalid type {!r}, expected int')
        else:
            self.device_id = data.get('device_key', None)


# TODO: Make this thread-safe and/or figure out what will happen when multiple requests come in simultaneously
class DoorServer(protocol.Protocol):
    """
    Handles incoming requests on TCP port.
    Expect request data to be formatted as a JSON object with:
        type:     open_door | register_device | master_change_password | tenant_change_password
        device_id: <any unique identifier for the device>
        pin:   The pin encoded as ASCII -- only sent with 'open_door' request.
        current_pin:  Sent with tenant_change_password request.
        new_pin:      Sent with tenant_change_password request.
        master_pin:   Sent with master_change_password request.
    """
<commit_msg>String changes<commit_after>from twisted.internet import reactor, protocol
import json
import os
import subprocess
import typesafety

MASTER_PIN = '12345678'
REGISTERED_DEVICES = {}
PORT = 9500
# Note:  The port number for the server doesn't really matter since the "proxy" (socat) will be used to redirect the
# Widget traffic to wherever we want.   If you changed this port to 5000 and ran the server on the BeagleBone's host
# computer then you wouldn't need socat.  However, you should make sure that your system is tested with a proxy
# because it will be necessary for connecting to the live server.

DEFAULT_PIN = '123456'
DEFAULT_FLAG = '<theflag>'

ROOTDIR = os.path.dirname(__file__)
REGISTERED_FILE = os.path.join(ROOTDIR, 'registered-widgets.txt')
REQUESTED_FILE = os.path.join(ROOTDIR, 'requested-widgets.txt')

class Widget(object):
    """
    Represents a Widget Device
    """

    def __init__(self, json_str):
        # Populate object attributes from JSON object
        # checks if valid JSON
        try:
            data = json.loads(json_str)
        except ValueError:
            raise ValueError('ValueError: Invalid JSON Values')
        # Checks device_id for type int
        if not isinstance(data.get('device_id', None), int):
            raise TypeError('Invalid type, expected int')
        else:
            self.device_id = data.get('device_id', None)
        # Checks pin for type int
        if not isinstance(data.get('pin', None), int):
            raise TypeError('Invalid type, expected int')
        else:
            self.device_id = data.get('pin', None)
        # Checks flag for type String
        if not isinstance(data.get('flag', None), str):
            raise TypeError('Invalid type, expected int')
        else:
            self.device_id = data.get('flag', None)
        # Checks device_id for type int
        if not isinstance(data.get('device_key', None), int):
             raise TypeError('Invalid type, expected int')
        else:
            self.device_id = data.get('device_key', None)


# TODO: Make this thread-safe and/or figure out what will happen when multiple requests come in simultaneously
class DoorServer(protocol.Protocol):
    """
    Handles incoming requests on TCP port.
    Expect request data to be formatted as a JSON object with:
        type:     open_door | register_device | master_change_password | tenant_change_password
        device_id: <any unique identifier for the device>
        pin:   The pin encoded as ASCII -- only sent with 'open_door' request.
        current_pin:  Sent with tenant_change_password request.
        new_pin:      Sent with tenant_change_password request.
        master_pin:   Sent with master_change_password request.
    """

{'fraction_non_alphanumeric': 0.08268359020852221, 'fraction_numerical': 0.007978241160471442, 'mean_word_length': 2.906515580736544, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3053455', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1477, 'n_words': 563}