<commit_before>import threading
import time
import logging
from GpsMonitor import GpsMonitor
from ConnectionMonitor import ConnectionMonitor

logger = logging.getLogger(__name__)

class StoppableThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.stop_event = threading.Event()

    def stop(self):
        if self.isAlive() == True:
            # set event to signal thread to terminate
            self.stop_event.set()
            # block calling thread until thread really has terminated
            self.join()


class OnboardControl(StoppableThread):
    HEARTBEAT_LOST_TIME = 3
    CONNECTION_LOST = 'CONNECTION_LOST'
    CONNECTION_OK = 'CONNECTION_OK'

    def __init__(self,drone_handler, drone_handler_signal_queue, gsm_transmit_queue, gsm_command_queue, rate = 1):
        StoppableThread.__init__(self)
        self.name = 'OnboardControl'
        self.rate = float(rate)
        self.drone_handler = drone_handler
        self.gsm_transmit_queue = gsm_transmit_queue
        self.gsm_command_queue = gsm_command_queue
        self.signal_gps_lock = threading.Lock()
        self.signal_drone_heartbeat_lock = threading.Lock()
        self.signal_gsm_lock = threading.Lock()
        self.signal_connection_state_lock = threading.Lock()
        self.gps_state = None
        self.gps_source = None
        self.drone_heartbeat = time.time()
        self.drone_connection_state = None
        self.gsm_connection_state = None
        self.connection_state = None
        self.command_terminate = False
        self.command_land_here = False
        self.command_return_to_launch = False

        self.signal_queue = drone_handler_signal_queue

        self.action_list_terminate = [] # ConnectionMonitor.STATE_CON_SERIAL2_LOST should be here... I don't dare it yet
        self.action_list_land_here = [GpsMonitor.STATE_LOST]
        self.action_list_return_to_launch = [OnboardControl.CONNECTION_LOST,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_OK_GSM_LOST,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_TIMEOUT_GSM_LOST,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_LOST_GSM_OK,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_LOST_GSM_TIMEOUT,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_LOST_GSM_LOST]
        self.action_list_wait_here = [GpsMonitor.STATE_TIMEOUT,
                                      GpsMonitor.STATE_MISMATCH,
                                      ConnectionMonitor.STATE_CON_TELEMETRY_OK_GSM_TIMEOUT,
                                      ConnectionMonitor.STATE_CON_TELEMETRY_TIMEOUT_GSM_OK,
                                      ConnectionMonitor.STATE_CON_TELEMETRY_TIMEOUT_GSM_TIMEOUT]
        self.action_list_no_action = [GpsMonitor.STATE_OK,
                                      GpsMonitor.STATE_NO_FIX_YET,
                                      OnboardControl.CONNECTION_OK,
                                      ConnectionMonitor.STATE_CON_OK]

        self.terminate_activated = False
        self.land_here_activated = False
        self.return_to_launch_activated = False
        self.wait_here_activated = False

        self.timer_wait_here = None

    def signal_gps_state(self, state, source = None):
        with self.signal_gps_lock:
            self.gps_state = state
            self.gps_source = source

    def signal_drone_state(self, state):
        with self.signal_drone_heartbeat_lock:
            self.drone_heartbeat = state

    def signal_gsm_state(self, state):
        with self.signal_gsm_lock:
            self.gsm_connection_state = state

    def signal_connection_state(self, state):
        with self.signal_connection_state_lock:
            self.connection_state = state

    def run(self):
        logger.info('OnboardControl started')
        while self.stop_event.is_set() is False:

            # Update heartbeat
            while not self.signal_queue.empty():
                self.drone_heartbeat = self.signal_queue.get()

            while not self.gsm_command_queue.empty():
                msg = self.gsm_command_queue.get()
                if msg['type'] == 'ACTION_TERMINATE':
                    self.command_terminate = True
                    logger.info('GSM_COMMAND: Terminate')
                elif msg['type'] == 'ACTION_LAND_HERE':
                    self.command_land_here = True
                    logger.info('GSM_COMMAND: Land here')
                elif msg['type'] == 'ACTION_RETURN_TO_LAUNCH':
                    self.command_return_to_launch = True<commit_msg>Gsm commands tested and working<commit_after>import threading
import time
import logging
from GpsMonitor import GpsMonitor
from ConnectionMonitor import ConnectionMonitor

logger = logging.getLogger(__name__)

class StoppableThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.stop_event = threading.Event()

    def stop(self):
        if self.isAlive() == True:
            # set event to signal thread to terminate
            self.stop_event.set()
            # block calling thread until thread really has terminated
            self.join()


class OnboardControl(StoppableThread):
    HEARTBEAT_LOST_TIME = 3
    CONNECTION_LOST = 'CONNECTION_LOST'
    CONNECTION_OK = 'CONNECTION_OK'

    def __init__(self,drone_handler, drone_handler_signal_queue, gsm_transmit_queue, gsm_command_queue, rate = 1):
        StoppableThread.__init__(self)
        self.name = 'OnboardControl'
        self.rate = float(rate)
        self.drone_handler = drone_handler
        self.gsm_transmit_queue = gsm_transmit_queue
        self.gsm_command_queue = gsm_command_queue
        self.signal_gps_lock = threading.Lock()
        self.signal_drone_heartbeat_lock = threading.Lock()
        self.signal_gsm_lock = threading.Lock()
        self.signal_connection_state_lock = threading.Lock()
        self.gps_state = None
        self.gps_source = None
        self.drone_heartbeat = time.time()
        self.drone_connection_state = None
        self.gsm_connection_state = None
        self.connection_state = None
        self.command_terminate = False
        self.command_land_here = False
        self.command_return_to_launch = False

        self.signal_queue = drone_handler_signal_queue

        self.action_list_terminate = [] # ConnectionMonitor.STATE_CON_SERIAL2_LOST should be here... I don't dare it yet
        self.action_list_land_here = [GpsMonitor.STATE_LOST]
        self.action_list_return_to_launch = [OnboardControl.CONNECTION_LOST,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_OK_GSM_LOST,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_TIMEOUT_GSM_LOST,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_LOST_GSM_OK,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_LOST_GSM_TIMEOUT,
                                             ConnectionMonitor.STATE_CON_TELEMETRY_LOST_GSM_LOST]
        self.action_list_wait_here = [GpsMonitor.STATE_TIMEOUT,
                                      GpsMonitor.STATE_MISMATCH,
                                      ConnectionMonitor.STATE_CON_TELEMETRY_OK_GSM_TIMEOUT,
                                      ConnectionMonitor.STATE_CON_TELEMETRY_TIMEOUT_GSM_OK,
                                      ConnectionMonitor.STATE_CON_TELEMETRY_TIMEOUT_GSM_TIMEOUT]
        self.action_list_no_action = [GpsMonitor.STATE_OK,
                                      GpsMonitor.STATE_NO_FIX_YET,
                                      OnboardControl.CONNECTION_OK,
                                      ConnectionMonitor.STATE_CON_OK]

        self.terminate_activated = False
        self.land_here_activated = False
        self.return_to_launch_activated = False
        self.wait_here_activated = False

        self.timer_wait_here = None

    def signal_gps_state(self, state, source = None):
        with self.signal_gps_lock:
            self.gps_state = state
            self.gps_source = source

    def signal_drone_state(self, state):
        with self.signal_drone_heartbeat_lock:
            self.drone_heartbeat = state

    def signal_gsm_state(self, state):
        with self.signal_gsm_lock:
            self.gsm_connection_state = state

    def signal_connection_state(self, state):
        with self.signal_connection_state_lock:
            self.connection_state = state 

    def run(self):
        logger.info('OnboardControl started')
        while self.stop_event.is_set() is False:

            # Update heartbeat
            while not self.signal_queue.empty():
                self.drone_heartbeat = self.signal_queue.get()

            while not self.gsm_command_queue.empty():
                msg = self.gsm_command_queue.get()
                if msg['type'] == 'ACTION_TERMINATE':
                    self.command_terminate = True
                    logger.info('GSM_COMMAND: Terminate')
                elif msg['type'] == 'ACTION_LAND_HERE':
                    self.command_land_here = True
                    logger.info('GSM_COMMAND: Land here')
                elif msg['type'] == 'ACTION_RETURN_TO_LAUNCH':
                    self.command_return_to_launch = True
{'fraction_non_alphanumeric': 0.06353912951392567, 'fraction_numerical': 0.0006353912951392566, 'mean_word_length': 2.0523594053005816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5253499', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2587, 'n_words': 425}