Пример #1
0
class DB2Connector:
    __connection = None
    __logger = None

    def __init__(self):
        self.__logger = Logger()

    def getConnection(self, db_detail):
        try:
            if self.__connection == None:
                conn = psycopg2.connect(user=db_detail.get_user(),
                                        password=db_detail.get_password(),
                                        host=db_detail.get_host(),
                                        port=db_detail.get_port(),
                                        database=db_detail.get_database())
                self.__logger.log(
                    "Info", "Connection established sucessfully--------")
                self.__connection = conn
            else:
                return self.__connection
        except Exception as e:
            self.__logger.log(
                "Info", "Can't connect to postgres database {}".format(e))
            #sys.exit(1)
        return self.__connection
Пример #2
0
class BinaryKnob(object):
    value = 0
    pins = []
    logger = None

    def __init__(self, pins):
        self.logger = Logger()
        self.pins = pins
        add_event_detection(self.pins[0], self.__handle_change, True)
        add_event_detection(self.pins[1], self.__handle_change, True)
        add_event_detection(self.pins[2], self.__handle_change, True)
        self.__handle_change

    def get_value(self):
        self.__handle_change(1)
        return self.value

    def __handle_change(self, pin):
        a = GPIO.input(self.pins[0])
        b = GPIO.input(self.pins[1])
        c = GPIO.input(self.pins[2])
        self.value = 1
        if a == True:
            self.value += 1
        if b == True:
            self.value += 2
        if c == True:
            self.value += 4
        self.logger.log("Selected %s" % self.value)
Пример #3
0
 def put_text(self, img):
     """
     Apply a put text filter
     :param img: the image to apply the filter
     :return: return the filtered image
     """
     logger = Logger()
     logger.log(" Applying FilterZeTeam filter...")
     return cv2.putText(img, 'Moise, Lucas & Kevin', (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (50, 0, 255), 2, cv2.LINE_AA)
 def grayscale(self, img):
     """
     Apply a grayscale filter
     :param img: the image to apply the filter
     :return: return the filtered image
     """
     logger = Logger()
     logger.log(" Applying grayscale filter...")
     return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Пример #5
0
 def dilate_file(self, img, intensity):
     """
     Apply a dilation filter to the image
     :param img: The image path
     :param intensity: The intensity of the filter
     :return:
     """
     logger = Logger()
     kernel = np.ones((20, 20), np.uint8)
     logger.log(" Applying dilation filter...")
     return cv2.dilate(img, kernel, iterations=intensity)
Пример #6
0
 def blur_image(self, img, intensity):
     """
     Apply a blur filter to the image
     :param intensity: The intensity of the filter
     :param img: The image path
     :return:
     """
     if intensity % 2 == False:
         intensity += 1
     log = Logger()
     log.log(" Applying a blur filter...")
     # Blur the image with gaussian filter
     return cv2.GaussianBlur(img, (intensity, intensity), 0)
Пример #7
0
def _check(parsed:List[str]) -> bool:
    """ [Async] Final type cast check before inserting data to DB. """
    try:
        str(parsed[0])
        int(parsed[1])
        str(parsed[2])
        return True
    except ValueError:
        Logger.log("Unable to parse sent_idx in {}".format(parsed), mode=Modes.es_insert)
        return False
    except Exception:
        Logger.log("General parse error in {}".format(parsed), mode=Modes.es_insert)
        return False
Пример #8
0
class ConfigLoader:
    """
    This class contains a simple constructor in order to keep track of the
    configurations across multiple instances which use them.
    """

    def __init__(self):
        """
        A simple constructor that keeps track of the configurations.
        """
        self.conf = None
        self.__logger = Logger(name='Config Loader')

    def get_configurations(self):
        """
        Retrieves the credentials from the config.ini file
        """

        # Retrieve configurations from config.ini file
        dirname = os.path.dirname(__file__)
        filename = os.path.join(dirname, '../../config.ini')

        cfg = configparser.ConfigParser()
        cfg.read(filename)

        # TODO: Load all different configs here
        if "logging" in cfg:
            self.conf = dict(cfg.items('logging'))
            self.__logger.log(message="Successfully loaded settings from config.ini", mtype="INFO")
        else:
            self.__logger.log(message="Wrong configurations in file: config.ini", mtype="CRITICAL")
            exit(100)

    def reset_config_file(self):
        try:
            with open("../config.ini", 'w') as file:
                file.write(";This file includes all the logging parameters of the project. \n;"
                           "Please modify as preferred.\n[logging]\nlogger=true\nlevel=DEBUG")
                print("Successfully reset config file.")
                file.close()
        except Exception as e:
            print("Problem resetting config file. Error Message: " + str(e))
            exit(104)
Пример #9
0
class Button(object):
    value = 0
    pin = None
    logger = None

    def __init__(self, pin, callback):
        self.logger = Logger()
        self.pin = pin
        if callback is None:
            add_event_detection(self.pin, self.__default_callback, True)
        else:
            add_event_detection(self.pin, callback, True)
        self.__handle_change

    def __default_callback(self):
        self.logger.log("Button: at pin %s callback with default callback")

    def get_value(self):
        self.__handle_change(1)
        return self.value
Пример #10
0
class Button(object):
    value = 0
    pin = None
    logger = None

    def __init__(self, pin, callback):
        self.logger = Logger()
        self.pin = pin
        if callback is None:
            add_event_detection(self.pin, self.__default_callback, True)
        else:
            add_event_detection(self.pin, callback, True)
        self.__handle_change

    def __default_callback(self):
        self.logger.log("Button: at pin %s callback with default callback")

    def get_value(self):
        self.__handle_change(1)
        return self.value
Пример #11
0
    def make_call(self,
                  method,
                  request,
                  params,
                  headers=config.DEFAULT_REQUEST_HEADERS):
        http_method = getattr(self, method)
        headers.update(headers if headers else None)
        response = http_method(url=request, headers=headers, params=params)

        try:
            response.raise_for_status()
        except requests.RequestException as e:
            logger = Logger()
            logger.log('\n'.join([
                "Details:", "Request: " + curlify.to_curl(response.request),
                "Response status: " + str(response.status_code),
                "Response: " + response.text,
                "Python Exception: " + str(e) + '\n'
            ]))

        return response.json()
class EmojiLoader:
    FILE_URL = "http://unicode.org/Public/emoji/11.0/"

    # TODO: Implement mode
    def __init__(self, mode):
        """

        :param mode: Either load from file or from web address
        """
        self.__logger = Logger(name='emoji_logger')
        self.data = self.load()

    def load(self):

        try:
            http = urllib3.PoolManager()
            r = http.request('GET', self.FILE_URL)

            if r.status != 200:
                self.__logger.log(message="Problem loading data using HTTP. Error code: " + r.status, mtype="CRITICAL")
                exit(102)
            self.__logger.log(message="Successfully loaded source dataset from URL", mtype="INFO")
            return r.data
        except Exception as e:
            self.__logger.log(message="Problem performing HTTP request. Error message: " + str(e), mtype="CRITICAL")
            exit(101)
Пример #13
0
class Parser:
    LANG_FILE_LINE = "$MESS[\"{code}\"] = \"{mess}\";\n"
    PATH_FILE = "{file_name}.php"
    FULL_PATH_FILE = "{path}/{file}"
    PATH_LANG_DIR_ROOT = '{root}/lang/'
    PATH_LANG_DIR = '{lang_id}/'
    _map_char = maketrans('йцукенгшщзхъфывапролджэячсмитьбюё ',
                          'ICUKENGSSZHBFYWAPROLDZEYCSMITPBUE_')
    _lang_id = 'ru'
    _regexp_list = {}
    _map = []
    _replace_rule = Dict[str, str]
    _path = ''
    __lang_dir_path = ''
    __logger = Logger

    def __init__(self, **kwargs):
        self.__logger = Logger()
        if 'lang_id':
            self._lang_id = kwargs['lang_id']
        else:
            raise KeyError('lang_id')
        if 'path' in kwargs:
            if kwargs['path'][-1] == '/':
                self._path = kwargs['path'][:-1]
            else:
                self._path = kwargs['path']
            self.__lang_dir_path = self.__make_lang_dir(self._path)
        if 'lang_id' in kwargs:
            self._lang_id = kwargs['lang_id']

    def _log(self, mess: str):
        self.__logger.log(__name__, mess)

    def __make_lang_dir(self, path: str):
        lang_dir_root = self.PATH_LANG_DIR_ROOT.format(root=path)
        if not os.path.isdir(lang_dir_root):
            os.mkdir(lang_dir_root)
        lang_dir_path = lang_dir_root + self.PATH_LANG_DIR.format(
            lang_id=self._lang_id)
        if not os.path.isdir(lang_dir_path):
            os.mkdir(lang_dir_path)
        return lang_dir_path

    def _make_lang_file(self, file_path: str, langs: Iterator[Tuple]):
        file = open(self.__lang_dir_path + file_path, 'w+')
        lang_file_code = "<?php\n"
        for code, mess in langs:
            lang_file_code += self.LANG_FILE_LINE.format(code=code, mess=mess)
        file.write(lang_file_code)
        file.close()

    @abstractmethod
    def _collect_messages(self, php_code: str):
        """
        :param php_code: string
        :return: list
        """
        return []

    @abstractmethod
    def _replace_messages(self, php_code: str, langs: Iterator[Tuple]):
        """
        :param php_code: 
        :param langs: 
        :return: 
        """
        return []

    @staticmethod
    def get_additional_map():
        return []

    def _make_code(self, message: str):
        code = message.lower().translate(self._map_char)
        reg_filer = re.compile("[^a-zA-Z_]+")
        code = reg_filer.sub('', code)
        return code[:30], message

    def run(self):
        for file_name in self._map + self.get_additional_map():
            try:
                file_path = self.PATH_FILE.format(file_name=file_name)
                full_path_file = self.FULL_PATH_FILE.format(path=self._path,
                                                            file=file_path)
                file = open(full_path_file)
                php_code = file.read()
                file.close()

                messages = self._collect_messages(php_code)
                if len(messages) <= 0:
                    self._log("~ {file_name}".format(file_name=file_name))
                    continue

                map(self._log, messages)

                langs = map(self._make_code, messages)
                self._make_lang_file(file_path, langs)

                file = open(full_path_file, 'w')
                langs = map(self._make_code, messages)
                php_code = self._replace_messages(php_code, langs)
                print(php_code)
                file.write(php_code)
                file.close()

            except FileNotFoundError:
                self._log("~ {file_name}".format(file_name=file_name))
                continue
            self._log("+ {file_name} {messages}".format(
                file_name=file_name, messages=messages.__str__()))
Пример #14
0
class Training:
    def __init__(self, training_db_path):
        self._training_db_path = training_db_path
        self._time_created = datetime.now()
        self._logger = Logger(
            f'training_logs_{self._time_created.date()}_{self._time_created.strftime("%H%M%S")}.log'
        )
        date = self._time_created.date()
        time = self._time_created.strftime("%H%M%S")
        # self._db_handler = SqliteDbHandler(self._logger,self._training_db_path,f'training_db_{date}_{time}')
        self._db_handler = SqliteDbHandler(self._logger,
                                           self._training_db_path,
                                           f'training_db')
        self._cluster_controller = None
        self._path_to_artifacts = os.path.join('.', 'artifacts')
        fileutils = FileUtils(self._logger, self._path_to_artifacts)
        self.model_repo_path = fileutils.create('models',
                                                delete_before_creation=True)

    def begin_training(self):

        self._logger.log('Training: Begin....')
        self._logger.log(
            f'Training: Attempting to get training data from {self._training_db_path}.'
        )
        self._db_handler.create_db_connection(self._training_db_path)

        with st.spinner('Collecting training data from DB....'):
            df = self._db_handler.get_data_from_db('thyroid_training')
            st.info('Training data loaded successfully')
            self._logger.log('Training: data loaded successfully.')

        with st.spinner('Begin Clustering of data..'):
            X = df.drop('Class', axis=1)
            y = df['Class']
            clusters = self._get_clusters_from_data(X)
            st.info(f'Found {clusters} clusters in training data')

            self._logger.log(
                f'Training: Found {clusters} clusters in training data')

        with st.spinner('Assigning Clusters to Training Data:'):
            df = self._assign_clusters_to_data(X)
            df['label'] = y
            st.write({
                'Clustering Model':
                self._cluster_controller.cluster_model.__class__.__name__,
                'Model Save path':
                self._cluster_model_save_path
            })
            # st.write(df.head())
            # df.to_csv('./data/test_pycaret_classification.csv')
        with st.spinner('Getting best model for each cluster'):
            self._logger.log(
                f'Training: Starting Process for getting best model for clusters '
            )
            st.info('Saved Models Overview:')
            st.write(self._train_models_for_clusters(df))

    def _get_clusters_from_data(self, data):

        self._cluster_controller = KmeansClustering(self._logger, data)
        return self._cluster_controller.find_clusters_using_elbow_plot()

    def _assign_clusters_to_data(self, data):

        data = self._cluster_controller.assign_clusters_to_data(data)

        model_name = self._cluster_controller.cluster_model.__class__.__name__
        file_utils = FileUtils(self._logger, self.model_repo_path)
        model_path = file_utils.create(model_name)
        self._cluster_model_save_path = os.path.join(model_path,
                                                     f'{model_name}.joblib')
        try:
            dump(self._cluster_controller.cluster_model,
                 self._cluster_model_save_path)
        except Exception as e:
            self._logger.log(
                f'Training: Exception occured while saving {self._cluster_model_save_path}, Exception : {str(e)}'
            )

        return data

    def _train_models_for_clusters(self, data):

        cluster_models = {}
        # Loop at each Cluster and find best model/HP combination
        for group_name, df_group in data.groupby('Cluster'):
            self._logger.log(
                f'Training: Finding best model for Cluster: {group_name}')
            model_trainer = Train_Models(self._logger,
                                         df_group.drop('Cluster', axis=1))
            cluster_models[
                group_name] = model_trainer.find_best_classifier_for_data()

        # fileutils = FileUtils(self._logger,self._path_to_artifacts)
        # model_repo_path = fileutils.create('models',delete_before_creation=True)
        # Save Models as per the cluster
        #
        saved_models = []
        for key, value in cluster_models.items():

            saved_model = {}

            model = jsonpickle.decode(value)
            model_name = model.__class__.__name__ + str(key)

            model_utils = FileUtils(self._logger, self.model_repo_path)
            model_path = model_utils.create(model_name)

            try:
                model_save_path = os.path.join(model_path,
                                               f'{model_name}.joblib')
                dump(model, model_save_path)

                saved_model.update(Path=model_save_path)
                saved_model.update(Cluster=key)
                saved_model.update(model=model.__class__.__name__)
                saved_models.append(saved_model)

                self._logger.log(f'Saving Model {model_name} in {model_path}')

            except Exception as e:
                self._logger.log(
                    f'Exception occured while saving Model {model_name} in {self.model_repo_path}: {str(e)}'
                )

        return saved_models
class BinaryBoxController(object):
    binary_output_pins = [3, 5, 7, 11]
    mux_enable_output_pins = [13, 15]

    current_binary_output = [0, 0, 0, 0]
    lower_mux_disabled = 1
    higher_mux_disabled = 1

    currently_open = False

    logger = None

    def __init__(self):
        GPIO.setup(self.binary_output_pins, GPIO.OUT)
        GPIO.setup(self.mux_enable_output_pins, GPIO.OUT)

        self.logger = Logger()
        self.__set_latch(self.binary_output_pins[0], 0)
        self.__set_latch(self.binary_output_pins[1], 0)
        self.__set_latch(self.binary_output_pins[2], 0)
        self.__set_latch(self.binary_output_pins[3], 0)
        self.close_boxes()

    def set_box(self, number):
        #Change the number to counting at 0 instead of 1
        number = number - 1
        binary_num = '{0:05b}'.format(number)

        if binary_num[0] == '1':
            self.higher_mux_disabled = 0
            self.lower_mux_disabled = 1
        else:
            self.higher_mux_disabled = 1
            self.lower_mux_disabled = 0

        self.current_binary_output = [
            int(binary_num[4]),
            int(binary_num[3]),
            int(binary_num[2]),
            int(binary_num[1])
        ]

    def open_current_box(self):
        if self.currently_open:
            self.close_boxes()

        #Set the box number
        self.__set_latch(self.binary_output_pins[0],
                         self.current_binary_output[0])
        self.__set_latch(self.binary_output_pins[1],
                         self.current_binary_output[1])
        self.__set_latch(self.binary_output_pins[2],
                         self.current_binary_output[2])
        self.__set_latch(self.binary_output_pins[3],
                         self.current_binary_output[3])

        #Enable the muxes
        self.__set_latch(self.mux_enable_output_pins[0],
                         self.lower_mux_disabled)
        self.__set_latch(self.mux_enable_output_pins[1],
                         self.higher_mux_disabled)

    def close_boxes(self):
        self.__set_latch(self.mux_enable_output_pins[0], 1)
        self.__set_latch(self.mux_enable_output_pins[1], 1)

    def __set_latch(self, pin, value):
        self.logger.log("Setting Latch %s to %s" % (pin, value))
        try:
            GPIO.output(pin, value)
        except RuntimeError:
            self.logger.log("Error setting Latch")
Пример #16
0
class Printer(object):
    conn = cups.Connection()
    printers = conn.getPrinters()
    printer_name = printers.keys()[0]
    tmpfilePath = "/home/pi/tmpadventure.pdf"
    ready_to_print = True

    logger = None

    def __init__(self):
        self.logger = Logger()

    def __print(self):
        self.logger.log("Printer: printing using %s" % self.printer_name)
        self.conn.cancelAllJobs(self.printer_name)
        self.conn.printFile(self.printer_name, self.tmpfilePath, "adventure", {})

    def __create_file(self, adventure):
        self.logger.log("Printer: creating pdf")
        try:
            os.remove(self.tmpfilePath)
            self.logger.log("  Success")
        except OSError:
            self.logger.log("  Failure")
            pass
        title = adventure["title"].replace("\\n", "\n")
        desc = adventure["desc"].replace("\\n", "\n")

        pdf = Adventure()
        pdf.set_margins(left=18, top=0, right=0)
        pdf.set_auto_page_break(False)

        pdf.add_page(orientation='P', format=(90,115))
        pdf.set_font('Arial', 'B', 16)
        pdf.multi_cell(0, 6, title, align='C')
        pdf.ln()
        pdf.set_font('Arial', '', 12)
        pdf.multi_cell(0, 6, desc, align='C')
        pdf.output(self.tmpfilePath, 'F')

    def __ready_to_print(self):
        self.logger.log("Printer: setting ready to print from %s to True" % self.ready_to_print)
        self.ready_to_print = True

    def printAdventure(self, adventure):
        self.logger.log("Printer: trying to print adventure with id %s and ready status %s" % (adventure['id'], self.ready_to_print))
        if self.ready_to_print:
            self.__create_file(adventure)
            self.__print()
            self.ready_to_print = False
            t = threading.Timer(1.0, self.__ready_to_print)
            t.start()
class BinaryBoxController(object):
    binary_output_pins = [3,5,7,11]
    mux_enable_output_pins = [13,15]

    current_binary_output = [0,0,0,0]
    lower_mux_disabled = 1
    higher_mux_disabled = 1

    currently_open = False

    logger = None

    def __init__(self):
        GPIO.setup(self.binary_output_pins, GPIO.OUT)
        GPIO.setup(self.mux_enable_output_pins, GPIO.OUT)

        self.logger = Logger()
        self.__set_latch(self.binary_output_pins[0], 0)
        self.__set_latch(self.binary_output_pins[1], 0)
        self.__set_latch(self.binary_output_pins[2], 0)
        self.__set_latch(self.binary_output_pins[3], 0)
        self.close_boxes()

    def set_box(self, number):
        #Change the number to counting at 0 instead of 1
        number = number - 1
        binary_num = '{0:05b}'.format(number);

        if binary_num[0] == '1':
            self.higher_mux_disabled = 0
            self.lower_mux_disabled = 1
        else:
            self.higher_mux_disabled = 1
            self.lower_mux_disabled = 0

        self.current_binary_output = [
            int(binary_num[4]),
            int(binary_num[3]),
            int(binary_num[2]),
            int(binary_num[1])]

    def open_current_box(self):
        if self.currently_open:
            self.close_boxes()

        #Set the box number
        self.__set_latch(self.binary_output_pins[0], self.current_binary_output[0])
        self.__set_latch(self.binary_output_pins[1], self.current_binary_output[1])
        self.__set_latch(self.binary_output_pins[2], self.current_binary_output[2])
        self.__set_latch(self.binary_output_pins[3], self.current_binary_output[3])

        #Enable the muxes
        self.__set_latch(self.mux_enable_output_pins[0], self.lower_mux_disabled)
        self.__set_latch(self.mux_enable_output_pins[1], self.higher_mux_disabled)


    def close_boxes(self):
        self.__set_latch(self.mux_enable_output_pins[0], 1)
        self.__set_latch(self.mux_enable_output_pins[1], 1)


    def __set_latch(self, pin, value):
        self.logger.log("Setting Latch %s to %s" % (pin, value))
        try:
            GPIO.output(pin, value)
        except RuntimeError:
            self.logger.log("Error setting Latch")
class Inference_Controller:
    def __init__(self,inference_db_path,preprocessor):
        self._inference_db_path = inference_db_path
        self._time_created = datetime.now()
        self._logger = Logger(f'inferencing_logs_{self._time_created.date()}_{self._time_created.strftime("%H%M%S")}.log')
        self._db_handler = SqliteDbHandler(self._logger,self._inference_db_path,'inferencing_db')
        self._model_utils = ModelUtils(self._logger)
        self._preprocessor = preprocessor

    def _load_data(self):
        self._logger.log('Inference: Started Inferencing')
        self._logger.log('Inference: Loading data for Inferencing')
        try:    
            self._db_handler.create_db_connection()
            df = self._db_handler.get_data_from_db('thyroid_inferencing')
            return df
        except Exception as e:
            self._logger.log(f'Inference: Exception occured while Loading data for Inferencing, {str(e)}')

    def _cluster_data(self,df):

        clustering_model_name_with_extension = self._all_models[0]
        model_name_only = clustering_model_name_with_extension.split('.')[0]
        clustering_model = self._model_utils.load_model(model_name_only)
        
        clusters = clustering_model.predict(df)
        df['clusters']=clusters
        self._clusters=df['clusters'].unique()

        return df
    
    def _get_model_for_clusters(self):
        
        model_repository = {}
        for cluster in self._clusters:
            model_repository[cluster]=self._model_utils.find_model_for_cluster(cluster)
        
        return model_repository

    def _get_label_encoder(self):
        return self._preprocessor.label_encoder

    def _make_predictions_for_clusters(self,df):

        label_encoder = self._get_label_encoder()
        all_models = self._get_model_for_clusters()

        final_predictions = pd.DataFrame()
        for cluster in self._clusters:
            current_cluster_data = df[df['clusters']==cluster]
            current_cluster_data = current_cluster_data.drop(['clusters'],axis=1)
            model = all_models.get(cluster)
            predicted_labels = model.predict(current_cluster_data)
            predicted_labels = predicted_labels.astype(int)
            predicted_labels = label_encoder.inverse_transform(predicted_labels)
            current_cluster_data['predictions'] = predicted_labels
            final_predictions = pd.concat([final_predictions,current_cluster_data])
        
        return final_predictions



    def run_inferencing(self):
        with st.spinner("Loading validated data from DB..."):
            df = self._load_data()

        with st.spinner("Loading models from repository..."):
            # self._all_models = list(more_itertools.flatten(self._model_utils.get_all_models_info()))
            self._all_models = self._model_utils.get_all_models_info()

        with st.spinner('Clustering data'):
            df = self._cluster_data(df)

        with st.spinner('Getting Predictions..'):
            st.info('Predictions:')
            final_predictions = self._make_predictions_for_clusters(df)
            st.write(final_predictions)

        with st.spinner('Writing Predictions to file..'):
            # file_utils = FileUtils(self._logger,os.path.join('.','data'))
            # predictions_save_path = file_utils.create('final_predictions',delete_before_creation=True)
            # predictions_save_path= os.path.join(predictions_save_path,'predictions.csv')
            final_predictions.to_csv('./data/final_predictions/predictions.csv',index=False)
Пример #19
0
class VendingMachine(object):
    adventure_button_pin = 18
    gift_button_pin = 16
    adventure_type_pin = 22
    box_select_pins_a = [24,26,32]
    box_select_pins_b = [36,38,40]
    out_of_service_pin = 23
    price_pins = [33,35,37]
    #Change this to False to use in the real vending machine
    #Leave as True to use the demo box with three buttons
    demo_mode = False
    random_box_mode = False

    print_adventures = True

    box_controller = None
    printer = None
    #deprecated by ligting system
    lighting = None
    server = None
    adventure_knob_a = None
    adventure_knob_b = None
    coin_machine = None
    logger = None
    api = None

    adventure_count = 0
    gift_count = 0
    adv_types = [["bmapi", 40], ["fun", 10], ["coin", 60]]

    def __init__(self):
        GPIO.cleanup()
        GPIO.setmode(GPIO.BOARD)
        self.logger = Logger()
        self.__init_pins()
        self.box_controller = BinaryBoxController()
        self.printer = Printer()
        LightSystemManager.setup()
        #depracted by lighting system manager
        self.lighting = LightingController()
        self.adventure_knob_a = BinaryKnob(self.box_select_pins_a)
        self.adventure_knob_b = BinaryKnob(self.box_select_pins_b)
        self.coin_machine = CoinMachine(self.lighting, self.demo_mode)
        self.server = api.run.ServerController()
        self.api = api.run

    # Private -------------------------------------------

    def __init_pins(self):
        self.logger.log("Machine: initializing pins")
        GPIO.setup(self.out_of_service_pin, GPIO.OUT)
        GPIO.setup(self.price_pins, GPIO.OUT)
        GPIO.setup(self.adventure_type_pin, GPIO.IN)
        GPIO.output(self.out_of_service_pin, True)
        GPIO.output(self.price_pins[0], True)
        GPIO.output(self.price_pins[1], True)
        GPIO.output(self.price_pins[2], True)

    def __adventure_button_cb(self, pin):
        self.logger.log("Machine: adventure button pressed with waiting status: %s" % self.waiting_to_give_adventure)
        if self.waiting_to_give_adventure == True:
            self.logger.log("  Dispensing Adventure")
            self.dispense_adventure()
            self.waiting_to_give_adventure = False
            t = threading.Timer(1.0, self.__allow_dispensing_adventures)
            t.start()

    def __allow_dispensing_adventures(self):
        self.waiting_to_give_adventure = True

    def __start_waiting_for_user(self):
        self.logger.log("Machine: waiting for user at pin %s" % self.adventure_button_pin)
        add_event_detection(self.adventure_button_pin, callback=self.__adventure_button_cb)
        add_event_detection(self.gift_button_pin, callback=self.__gift_button_pressed)

        self.__allow_dispensing_adventures()

    def __reset_box(self):
        self.box_controller.close_boxes()

    def __start_waiting_for_boxes(self):
        self.logger.log("Machine: waiting for boxes with demo mode: %s" % self.demo_mode)
        if self.demo_mode == True:
            add_event_detection(self.box_select_pins_a[0], callback=self.__box_a_pressed)
            add_event_detection(self.box_select_pins_a[1], callback=self.__box_b_pressed)
            add_event_detection(self.box_select_pins_a[2], callback=self.__box_c_pressed)


    def __box_a_pressed(self, channel):
        self.logger.log("Machine: box button a pressed")
        self.open_prize_box(1)
        self.lighting.box_selected(1)

    def __box_b_pressed(self, channel):
        self.logger.log("Machine: box button b pressed")
        self.open_prize_box(2)
        self.lighting.box_selected(2)

    def __box_c_pressed(self, channel):
        self.logger.log("Machine: box button b pressed")
        self.open_prize_box(3)
        self.lighting.box_selected(3)

    def __gift_button_pressed(self, pin):
        self.logger.log("Machine: gift Button Pressed")
        box_number = self.adventure_knob_a.get_value()
        wall_number = self.adventure_knob_b.get_value()
        if box_number > 4 or self.random_box_mode == True:
            #random box number if nothing is selected
            box_number = random.randint(1,4)
        if wall_number > 7 or self.random_box_mode == True:
            #random wall number if nothing is selected
            wall_number = random.randint(1,4)
        latch_number = ((wall_number - 1) * 4) + box_number
        self.logger.log("  selected box %s" % latch_number)
        self.open_prize_box(latch_number)

    def __weighted_choice(self, choices):
            total = sum(w for c, w in choices)
            r = random.uniform(0, total)
            upto = 0
            for c,w in choices:
                if upto + w >= r:
                    return c
                upto += w
            assert False, "Shouldn't get here"  

    # Public --------------------------------------------

    def open_prize_box(self, box_number):
        self.logger.log("Machine: selected box %s with credits: %s" % (box_number, self.coin_machine.current_value))
        #For now, all boxes cost one. TODO: Hook this up with prices
        box_cost = 1
        if (self.coin_machine.current_value >= box_cost):
        #if (True):
            self.logger.log("  Signalling to open box %s" % box_number)
            self.box_controller.set_box(box_number)
            #play this LED routine for opening a box
            LightSystemManager.open_box(box_number)
            self.box_controller.open_current_box()
            self.lighting.dispense_prize(box_number)
            self.gift_count = self.gift_count + 1
            self.coin_machine.subtract_coins(box_cost)
            if self.demo_mode == True:
                self.coin_machine.clear_coins()
            t = threading.Timer(5.0, self.__reset_box)
            t.start()

    def get_adventure(self):
        #adventures = api.run.av_data
        #enabled_adventures = []            
        adv_type = self.__weighted_choice(self.adv_types)
        time_format = '%Y-%m-%dT%X+00:00'
        #now = datetime.datetime.now()
        now = datetime.datetime.strptime("2016-09-02T18:00:00+00:00", time_format)
        Adventure = Query()

        def adventure_query(val, now, adv_type):
            return val and val["label"] and val["label"] == "coin"

        def time_query(val, now):
            match = True
            if not val:
                #If something doesn't have a time, then just show it anyways
                return True
            for event_time in val:
                start_time = datetime.datetime.strptime(event_time["start_time"], time_format)
                start_time_minus_hour = start_time - datetime.timedelta(hours=1)
                end_time = datetime.datetime.strptime(event_time["end_time"], time_format)
                if now < (start_time - datetime.timedelta(hours=1)) or now > (end_time - datetime.timedelta(hours=1)):
                    match = False
            return match

        if adv_type == "coin":
            enabled_adventures = self.api.db.search((Adventure["event_type"]["label"] == "coin") & (Adventure.enabled == True))
        elif adv_type == "fun":
            enabled_adventures = self.api.db.search((Adventure["event_type"]["label"] == "Fun") & ~(Adventure.desc == ""))
        else:
            enabled_adventures = self.api.db.search((Adventure["occurrence_set"].test(time_query, now)) & (Adventure["event_source"] == adv_type))
        
        
        return random.choice(enabled_adventures)

    def dispense_adventure(self):
        self.logger.log("Machine: preparing to dispense adventure with printing set to: %s" % self.print_adventures)
        adventure_type = GPIO.input(self.adventure_type_pin)
        #TODO: Use the adventure type to pick an adventure
        adventure = self.get_adventure()
        self.adventure_count = self.adventure_count + 1
        if self.print_adventures == True:
            self.logger.log("  Printing adventure: %s" % adventure['id'])
            self.printer.printAdventure(adventure)
        self.lighting.dispense_adventure()

    def open_all_boxes(self):
        for i in range(1, 32):
            self.box_controller.set_box(i)
            self.box_controller.open_current_box()
            time.sleep(1)
        self.box_controller.close_all_boxes()
            
    
    def start(self):
        self.logger.log("Machine: starting")
        self.__start_waiting_for_boxes()
        self.__start_waiting_for_user()
        self.server.start()

    def stop(self):
        self.logger.log("Machine: stopping")
        self.server.stop()
Пример #20
0
class CNN_skipCo_trainer(object):
    def __init__(self):

        self.image_type = 'OA'

        self.dataset = ProcessData(train_ratio=0.9,
                                   process_raw_data=False,
                                   do_augment=False,
                                   add_augment=True,
                                   do_flip=True,
                                   do_blur=True,
                                   do_deform=True,
                                   do_crop=True,
                                   image_type=self.image_type,
                                   get_scale_center=False,
                                   single_sample=True)

        self.model = cnn_toy_model.cnn_skipC_model(criterion=nn.MSELoss(),
                                                   optimizer=torch.optim.Adam,
                                                   learning_rate=0.001,
                                                   weight_decay=0)

        if torch.cuda.is_available():
            torch.cuda.current_device()
            self.model.cuda()

        self.logger = Logger(model=self.model,
                             project_root_dir=self.dataset.project_root_dir,
                             image_type=self.image_type)
        self.epochs = 2

    def fit(self):
        # get scale and center parameters
        scale_params_low, scale_params_high = self.dataset.load_params(
            param_type="scale_params")
        mean_image_low, mean_image_high = self.dataset.load_params(
            param_type="mean_images")

        # currently for one image:
        '''
        self.dataset.batch_names(batch_size=5)
        X, Y = self.dataset.create_train_batches(self.dataset.train_batch_chunks[1])
        print(X.shape)
        X = X[0,:,:]
        Y = Y[0,:,:]
        '''
        # load validation set, normalize and parse into tensor

        input_tensor_val, target_tensor_val = self.dataset.scale_and_parse_to_tensor(
            batch_files=self.dataset.val_file_names,
            scale_params_low=scale_params_low,
            scale_params_high=scale_params_high,
            mean_image_low=mean_image_low,
            mean_image_high=mean_image_high)

        if torch.cuda.is_available():
            input_tensor_val = input_tensor_val.cuda()
            target_tensor_val = target_tensor_val.cuda()

        for e in range(0, self.epochs):
            # separate names into random batches and shuffle every epoch
            self.dataset.batch_names(batch_size=32)
            # in self.batch_number is the number of batches in the training set
            for i in range(self.dataset.batch_number):
                input_tensor, target_tensor = self.dataset.scale_and_parse_to_tensor(
                    batch_files=self.dataset.train_batch_chunks[i],
                    scale_params_low=scale_params_low,
                    scale_params_high=scale_params_high,
                    mean_image_low=mean_image_low,
                    mean_image_high=mean_image_high)

                if torch.cuda.is_available():

                    input_tensor = input_tensor.cuda()
                    target_tensor = target_tensor.cuda()

                self.model.train_model(input_tensor,
                                       target_tensor,
                                       current_epoch=e)

            # calculate the validation loss and add to validation history
            self.logger.get_val_loss(val_in=input_tensor_val,
                                     val_target=target_tensor_val)
            # save model every x epochs
            if e % 25 == 0 or e == self.epochs - 1:
                self.logger.log(
                    save_appendix='_epoch_' + str(e),
                    current_epoch=e,
                    epochs=self.epochs,
                    mean_images=[mean_image_low, mean_image_high],
                    scale_params=[scale_params_low, scale_params_high])

                # how to undo the scaling:
                # unscaled_X = utils.scale_and_center_reverse(scale_center_X,
                #  scale_params_low, mean_image_low, image_type = self.dataset.image_type)
                # unscaled_Y = utils.scale_and_center_reverse(scale_center_Y, scale_params_high,
                #  mean_image_high, image_type=self.dataset.image_type)
    def predict(self):
        # self.model.predict()

        # see self.dataset.X_val and self.dataset.Y_val
        pass
Пример #21
0
class CoinMachine(object):
    coin_input_pin = 21
    coin_counter_input_pin = 12
    coin_counter_pins = [29, 31]

    lighting = None

    waiting_for_coin = False
    accepted_a_coin = False

    coin_detected = False
    coin_counted = False
    coin_pending = False
    demo_mode = False

    current_value = 0

    logger = None

    def __init__(self, lighting, demo_mode=False):
        self.logger = Logger()
        #self.demo_mode = demo_mode
        #For now, lets leave it as only counting one coin.
        self.demo_mode = True
        self.lighting = lighting
        GPIO.setup(self.coin_counter_pins, GPIO.OUT)
        GPIO.setup(self.coin_input_pin, GPIO.IN)
        GPIO.setup(self.coin_counter_input_pin, GPIO.IN)
        self.__set_coin_count(0)
        self.start_waiting_for_coin()

    # Public --------------------------------------------

    def start_waiting_for_coin(self):
        self.logger.log("Coin: waiting for coin at pin %s" %
                        self.coin_input_pin)
        add_event_detection(self.coin_input_pin, callback=self.__coin_cb)
        add_event_detection(self.coin_counter_input_pin,
                            callback=self.__coin_counter_cb)
        self.waiting_for_coin = True

    def clear_coins(self):
        self.logger.log("Coin: clearing count")
        self.__set_coin_count(0)

    def subtract_coins(self, num):
        self.logger.log("Coin: new coin count = %s - %s" %
                        (self.current_value, num))
        self.__set_coin_count(self.current_value - num)

    # Private -------------------------------------------

    def __coin_cb(self, channel):
        self.logger.log("Coin: coin cb with waiting status: %s" %
                        self.waiting_for_coin)
        if self.waiting_for_coin is True:
            self.__coin_detected()
            self.coin_detected = True

    def __coin_counter_cb(self, channel):
        self.logger.log("Coin: coin counter cb with waiting status: %s" %
                        self.waiting_for_coin)
        if self.waiting_for_coin is True:
            self.__coin_detected()
            self.coin_counted = True

    def __coin_detected(self):
        self.logger.log("Coin: coin detected with pending status: %s" %
                        self.coin_pending)
        if self.coin_pending is False:
            self.coin_pending = True
            self.coin_detected = False
            self.coin_counted = False
            t = threading.Timer(1.0, self.__done_waiting_for_coin)
            t.start()

    def __done_waiting_for_coin(self):
        self.logger.log("Coin: done waiting with pending status")
        self.coin_detected = True
        #Fake the coin detection for now, it's broken
        if self.coin_detected is True and self.coin_counted is True:
            self.logger.log("  Got a coin, pick a box")
            self.__set_accepted_coin(True)
        else:
            self.logger.log("  Not accepted")
        self.coin_detected = False
        self.coin_counted = False
        self.coin_pending = False

    def __wait_for_coin(self):
        self.logger.log("Coin: waiting_for_coin set to true, was: %s" %
                        self.waiting_for_coin)
        self.waiting_for_coin = True

    def __set_accepted_coin(self, value):
        self.logger.log("Coin: accepting coin with value %s" % value)
        self.accepted_a_coin = value
        if value is True:
            self.lighting.coin_received()
            try:
                if self.demo_mode is True:
                    #If it's demo mode, then we should only allow 1 credit
                    self.__set_coin_count(1)
                else:
                    self.__set_coin_count(self.current_value + 1)
            except RuntimeError:
                self.logger.log("  error set_accepted_coin")

    def __set_coin_count(self, count):
        self.logger.log("Coin: attempting to set count to %s from %s" %
                        (count, self.current_value))
        #Don't allow more than three credits
        if count < 0:
            self.logger.log("  count below min, setting to min")
            count = 0
        if count > 3:
            self.logger.log("  count exceeds max, setting to max")
            count = 3
        self.logger.log("  setting count to %s" % count)
        self.current_value = count
        if count == 3:
            GPIO.output(self.coin_counter_pins[0], True)
            GPIO.output(self.coin_counter_pins[1], True)
        elif count == 2:
            GPIO.output(self.coin_counter_pins[0], False)
            GPIO.output(self.coin_counter_pins[1], True)
        elif count == 1:
            GPIO.output(self.coin_counter_pins[0], True)
            GPIO.output(self.coin_counter_pins[1], False)
        else:
            GPIO.output(self.coin_counter_pins[0], False)
            GPIO.output(self.coin_counter_pins[1], False)
Пример #22
0
class CoinMachine(object):
    coin_input_pin = 21
    coin_counter_input_pin = 12
    coin_counter_pins = [29, 31]

    lighting = None

    waiting_for_coin = False
    accepted_a_coin = False

    coin_detected = False
    coin_counted = False
    coin_pending = False
    demo_mode = False

    current_value = 0

    logger = None

    def __init__(self, lighting, demo_mode=False):
        self.logger = Logger()
        self.demo_mode = demo_mode
        self.lighting = lighting
        GPIO.setup(self.coin_counter_pins, GPIO.OUT)
        GPIO.setup(self.coin_input_pin, GPIO.IN)
        GPIO.setup(self.coin_counter_input_pin, GPIO.IN)
        self.__set_coin_count(0)
        self.start_waiting_for_coin()

    # Public --------------------------------------------

    def start_waiting_for_coin(self):
        self.logger.log("Coin: waiting for coin at pin %s" % self.coin_input_pin)
        add_event_detection(self.coin_input_pin, callback=self.__coin_cb)
        add_event_detection(self.coin_counter_input_pin, callback=self.__coin_counter_cb)
        self.waiting_for_coin = True

    def clear_coins(self):
        self.logger.log("Coin: clearing count")
        self.__set_coin_count(0)

    def subtract_coins(self, num):
        self.logger.log("Coin: new coin count = %s - %s" % (self.current_value, num))
        self.__set_coin_count(self.current_value - num)

    # Private -------------------------------------------

    def __coin_cb(self, channel):
        self.logger.log("Coin: coin cb with waiting status: %s" % self.waiting_for_coin)
        if self.waiting_for_coin is True:
            self.__coin_detected()
            self.coin_detected = True

    def __coin_counter_cb(self, channel):
        self.logger.log("Coin: coin counter cb with waiting status: %s" % self.waiting_for_coin)
        if self.waiting_for_coin is True:
            self.__coin_detected()
            self.coin_counted = True

    def __coin_detected(self):
        self.logger.log("Coin: coin detected with pending status: %s" % self.coin_pending)
        if self.coin_pending is False:
            self.coin_pending = True
            self.coin_detected = False
            self.coin_counted = False
            t = threading.Timer(1.0, self.__done_waiting_for_coin)
            t.start()

    def __done_waiting_for_coin(self):
        self.logger.log("Coin: done waiting with pending status")
        self.coin_detected = True
        #Fake the coin detection for now, it's broken
        if self.coin_detected is True and self.coin_counted is True:
            self.logger.log("  Got a coin, pick a box")
            self.__set_accepted_coin(True)
        else:
            self.logger.log("  Not accepted")
        self.coin_detected = False
        self.coin_counted = False
        self.coin_pending = False

    def __wait_for_coin(self):
        self.logger.log("Coin: waiting_for_coin set to true, was: %s" % self.waiting_for_coin)
        self.waiting_for_coin = True


    def __set_accepted_coin(self, value):
        self.logger.log("Coin: accepting coin with value %s" % value)
        self.accepted_a_coin = value
        if value is True:
            self.lighting.coin_received()
            try:
                if self.demo_mode is True:
                    #If it's demo mode, then we should only allow 1 credit
                    self.__set_coin_count(1)
                else:
                    self.__set_coin_count(self.current_value + 1)
            except RuntimeError:
                self.logger.log("  error set_accepted_coin")

    def __set_coin_count(self, count):
        self.logger.log("Coin: attempting to set count to %s from %s" % (count, self.current_value))
        #Don't allow more than three credits
        if count < 0:
            self.logger.log("  count below min, setting to min")
            count = 0
        if count > 3:
            self.logger.log("  count exceeds max, setting to max")
            count = 3
        self.logger.log("  setting count to %s" % count)
        self.current_value = count
        if count == 3:
            GPIO.output(self.coin_counter_pins[0], True)
            GPIO.output(self.coin_counter_pins[1], True)
        elif count == 2:
            GPIO.output(self.coin_counter_pins[0], False)
            GPIO.output(self.coin_counter_pins[1], True)
        elif count == 1:
            GPIO.output(self.coin_counter_pins[0], True)
            GPIO.output(self.coin_counter_pins[1], False)
        else:
            GPIO.output(self.coin_counter_pins[0], False)
            GPIO.output(self.coin_counter_pins[1], False)
Пример #23
0
class VendingMachine(object):
    adventure_button_pin = 18
    gift_button_pin = 16
    adventure_type_pin = 22
    box_select_pins_a = [24, 26, 32]
    box_select_pins_b = [36, 38, 40]
    out_of_service_pin = 23
    price_pins = [33, 35, 37]
    #Change this to False to use in the real vending machine
    #Leave as True to use the demo box with three buttons
    demo_mode = False
    random_box_mode = False

    print_adventures = True

    box_controller = None
    printer = None
    #deprecated by ligting system
    lighting = None
    server = None
    adventure_knob_a = None
    adventure_knob_b = None
    coin_machine = None
    logger = None
    api = None

    adventure_count = 0
    gift_count = 0
    adv_types = [["bmapi", 40], ["fun", 10], ["coin", 60]]

    def __init__(self):
        GPIO.cleanup()
        GPIO.setmode(GPIO.BOARD)
        self.logger = Logger()
        self.__init_pins()
        self.box_controller = BinaryBoxController()
        self.printer = Printer()
        LightSystemManager.setup()
        #depracted by lighting system manager
        self.lighting = LightingController()
        self.adventure_knob_a = BinaryKnob(self.box_select_pins_a)
        self.adventure_knob_b = BinaryKnob(self.box_select_pins_b)
        self.coin_machine = CoinMachine(self.lighting, self.demo_mode)
        self.server = api.run.ServerController()
        self.api = api.run

    # Private -------------------------------------------

    def __init_pins(self):
        self.logger.log("Machine: initializing pins")
        GPIO.setup(self.out_of_service_pin, GPIO.OUT)
        GPIO.setup(self.price_pins, GPIO.OUT)
        GPIO.setup(self.adventure_type_pin, GPIO.IN)
        GPIO.output(self.out_of_service_pin, True)
        GPIO.output(self.price_pins[0], True)
        GPIO.output(self.price_pins[1], True)
        GPIO.output(self.price_pins[2], True)

    def __adventure_button_cb(self, pin):
        self.logger.log(
            "Machine: adventure button pressed with waiting status: %s" %
            self.waiting_to_give_adventure)
        if self.waiting_to_give_adventure == True:
            self.logger.log("  Dispensing Adventure")
            self.dispense_adventure()
            self.waiting_to_give_adventure = False
            t = threading.Timer(1.0, self.__allow_dispensing_adventures)
            t.start()

    def __allow_dispensing_adventures(self):
        self.waiting_to_give_adventure = True

    def __start_waiting_for_user(self):
        self.logger.log("Machine: waiting for user at pin %s" %
                        self.adventure_button_pin)
        add_event_detection(self.adventure_button_pin,
                            callback=self.__adventure_button_cb)
        add_event_detection(self.gift_button_pin,
                            callback=self.__gift_button_pressed)

        self.__allow_dispensing_adventures()

    def __reset_box(self):
        self.box_controller.close_boxes()

    def __start_waiting_for_boxes(self):
        self.logger.log("Machine: waiting for boxes with demo mode: %s" %
                        self.demo_mode)
        if self.demo_mode == True:
            add_event_detection(self.box_select_pins_a[0],
                                callback=self.__box_a_pressed)
            add_event_detection(self.box_select_pins_a[1],
                                callback=self.__box_b_pressed)
            add_event_detection(self.box_select_pins_a[2],
                                callback=self.__box_c_pressed)

    def __box_a_pressed(self, channel):
        self.logger.log("Machine: box button a pressed")
        self.open_prize_box(1)
        self.lighting.box_selected(1)

    def __box_b_pressed(self, channel):
        self.logger.log("Machine: box button b pressed")
        self.open_prize_box(2)
        self.lighting.box_selected(2)

    def __box_c_pressed(self, channel):
        self.logger.log("Machine: box button b pressed")
        self.open_prize_box(3)
        self.lighting.box_selected(3)

    def __gift_button_pressed(self, pin):
        self.logger.log("Machine: gift Button Pressed")
        box_number = self.adventure_knob_a.get_value()
        wall_number = self.adventure_knob_b.get_value()
        if box_number > 4 or self.random_box_mode == True:
            #random box number if nothing is selected
            box_number = random.randint(1, 4)
        if wall_number > 7 or self.random_box_mode == True:
            #random wall number if nothing is selected
            wall_number = random.randint(1, 4)
        latch_number = ((wall_number - 1) * 4) + box_number
        self.logger.log("  selected box %s" % latch_number)
        self.open_prize_box(latch_number)

    def __weighted_choice(self, choices):
        total = sum(w for c, w in choices)
        r = random.uniform(0, total)
        upto = 0
        for c, w in choices:
            if upto + w >= r:
                return c
            upto += w
        assert False, "Shouldn't get here"

    # Public --------------------------------------------

    def open_prize_box(self, box_number):
        self.logger.log("Machine: selected box %s with credits: %s" %
                        (box_number, self.coin_machine.current_value))
        #For now, all boxes cost one. TODO: Hook this up with prices
        box_cost = 1
        if (self.coin_machine.current_value >= box_cost):
            #if (True):
            self.logger.log("  Signalling to open box %s" % box_number)
            self.box_controller.set_box(box_number)
            #play this LED routine for opening a box
            LightSystemManager.open_box(box_number)
            self.box_controller.open_current_box()
            self.lighting.dispense_prize(box_number)
            self.gift_count = self.gift_count + 1
            self.coin_machine.subtract_coins(box_cost)
            if self.demo_mode == True:
                self.coin_machine.clear_coins()
            t = threading.Timer(5.0, self.__reset_box)
            t.start()

    def get_adventure(self):
        #adventures = api.run.av_data
        #enabled_adventures = []
        adv_type = self.__weighted_choice(self.adv_types)
        time_format = '%Y-%m-%dT%X+00:00'
        #now = datetime.datetime.now()
        now = datetime.datetime.strptime("2016-09-02T18:00:00+00:00",
                                         time_format)
        Adventure = Query()

        def adventure_query(val, now, adv_type):
            return val and val["label"] and val["label"] == "coin"

        def time_query(val, now):
            match = True
            if not val:
                #If something doesn't have a time, then just show it anyways
                return True
            for event_time in val:
                start_time = datetime.datetime.strptime(
                    event_time["start_time"], time_format)
                start_time_minus_hour = start_time - datetime.timedelta(
                    hours=1)
                end_time = datetime.datetime.strptime(event_time["end_time"],
                                                      time_format)
                if now < (start_time - datetime.timedelta(hours=1)) or now > (
                        end_time - datetime.timedelta(hours=1)):
                    match = False
            return match

        if adv_type == "coin":
            enabled_adventures = self.api.db.search(
                (Adventure["event_type"]["label"] == "coin")
                & (Adventure.enabled == True))
        elif adv_type == "fun":
            enabled_adventures = self.api.db.search(
                (Adventure["event_type"]["label"] == "Fun")
                & ~(Adventure.desc == ""))
        else:
            enabled_adventures = self.api.db.search(
                (Adventure["occurrence_set"].test(time_query, now))
                & (Adventure["event_source"] == adv_type))

        return random.choice(enabled_adventures)

    def dispense_adventure(self):
        self.logger.log(
            "Machine: preparing to dispense adventure with printing set to: %s"
            % self.print_adventures)
        adventure_type = GPIO.input(self.adventure_type_pin)
        #TODO: Use the adventure type to pick an adventure
        adventure = self.get_adventure()
        self.adventure_count = self.adventure_count + 1
        if self.print_adventures == True:
            self.logger.log("  Printing adventure: %s" % adventure['id'])
            self.printer.printAdventure(adventure)
        self.lighting.dispense_adventure()

    def open_all_boxes(self):
        for i in range(1, 32):
            self.box_controller.set_box(i)
            self.box_controller.open_current_box()
            time.sleep(1)
        self.box_controller.close_all_boxes()

    def start(self):
        self.logger.log("Machine: starting")
        self.__start_waiting_for_boxes()
        self.__start_waiting_for_user()
        self.server.start()

    def stop(self):
        self.logger.log("Machine: stopping")
        self.server.stop()
Пример #24
0
class Printer(object):
    conn = cups.Connection()
    printers = conn.getPrinters()
    printer_name = printers.keys()[0]
    tmpfilePath = "/home/pi/tmpadventure.pdf"
    ready_to_print = True

    logger = None

    def __init__(self):
        self.logger = Logger()

    def __print(self):
        self.logger.log("Printer: printing using %s" % self.printer_name)
        self.conn.cancelAllJobs(self.printer_name)
        self.conn.printFile(self.printer_name, self.tmpfilePath, "adventure",
                            {})

    def __create_file(self, adventure):
        self.logger.log("Printer: creating pdf")
        try:
            os.remove(self.tmpfilePath)
            self.logger.log("  Success")
        except OSError:
            self.logger.log("  Failure")
            pass
        title = adventure["title"].replace("\\n", "\n")
        desc = adventure["desc"].replace("\\n", "\n")

        pdf = Adventure()
        pdf.set_margins(left=18, top=0, right=0)
        pdf.set_auto_page_break(False)

        pdf.add_page(orientation='P', format=(90, 115))
        pdf.set_font('Arial', 'B', 16)
        pdf.multi_cell(0, 6, title, align='C')
        pdf.ln()
        pdf.set_font('Arial', '', 12)
        pdf.multi_cell(0, 6, desc, align='C')
        pdf.output(self.tmpfilePath, 'F')

    def __ready_to_print(self):
        self.logger.log("Printer: setting ready to print from %s to True" %
                        self.ready_to_print)
        self.ready_to_print = True

    def printAdventure(self, adventure):
        self.logger.log(
            "Printer: trying to print adventure with id %s and ready status %s"
            % (adventure['id'], self.ready_to_print))
        if self.ready_to_print:
            self.__create_file(adventure)
            self.__print()
            self.ready_to_print = False
            t = threading.Timer(1.0, self.__ready_to_print)
            t.start()
class ValidationController:
    def __init__(self, file_path, schema_path, process):
        self._file_path = file_path
        self._schema_path = schema_path
        self._time_created = datetime.now()
        self._logger = Logger(
            f'validation_{process}_logs_{self._time_created.date()}_{self._time_created.strftime("%H%M%S")}.log'
        )
        self._path_to_good_files = None
        self._path_to_bad_files = None
        self.process = process

    def _run_validation(self):

        self._logger.log('Starting files validation')
        try:
            files_validation = ValidateData(self._logger, self._file_path,
                                            self._schema_path)
            validation_result, path_to_good_files, path_to_bad_files = files_validation.validate_files(
            )
        except:
            self._logger.log(f'No Valid Files Found in {self._file_path}')
            raise Exception('No Valid files found')
        self._logger.log('Files Validaiton Completed.')  #
        st.info('Validation Results:')
        st.write(validation_result)
        st.subheader('Files moved to:')

        self._path_to_good_files = path_to_good_files
        self._path_to_bad_files = path_to_bad_files
        st.info(f'Path to Good Files: "{self._path_to_good_files}"')
        st.info(f'Path to Bad Files: "{self._path_to_bad_files}"')

        return validation_result

    def get_preprocessor(self):

        unnecessary_columns = [
            'TSH_measured', 'T3_measured', 'TT4_measured', 'T4U_measured',
            'FTI_measured', 'TBG_measured', 'TBG', 'TSH'
        ]
        preprocessor = Preprocessor(self._logger, unnecessary_columns)
        return preprocessor

    def _preprocess_data(self):
        data_reader = GlobDataReader(self._logger, self._path_to_good_files)
        df = data_reader.read()
        preprocessor = self.get_preprocessor()
        cleaned_data = preprocessor.transform(df)
        date = self._time_created.date()
        time = self._time_created.strftime("%H%M%S")
        self._save_preprocessor(preprocessor)
        # preprocessor_save_path = f'./artifacts/preprocessing/preprocessor_{date}_{time}.joblib'
        # dump(preprocessor,preprocessor_save_path )
        # st.info(f'Preprocessor saved as preprocessor_{date}_{time}.joblib')
        # self._logger.log(f'Preprocessor: Preprocessor saved as {preprocessor_save_path}')

        return cleaned_data

    def _save_preprocessor(self, preprocessor):
        try:
            self._logger.log(f'Preprocessor: Attempting to save preprocessor')
            self._path_to_artifacts = os.path.join('.', 'artifacts')
            fileutils = FileUtils(self._logger, self._path_to_artifacts)
            preprocessor_save_path = os.path.join(
                fileutils.create('preprocessing', delete_before_creation=True),
                'preprocessor.joblib')
            dump(preprocessor, preprocessor_save_path)
            st.info(f'Preprocessor saved as {preprocessor_save_path}')
            self._logger.log(
                f'Preprocessor: Preprocessor saved as {preprocessor_save_path}'
            )
        except Exception as e:
            self._logger.log(
                f'Preprocessor: Exception occured while saving preprocessor - {str(e)}'
            )

    def _get_parent_folder(self, full_path):
        return str(Path(full_path).parents[0])  # "path/to"

    def _save_preprocessed_data(self, data):
        date = self._time_created.date()
        time = self._time_created.strftime("%H%M%S")
        # db_path = os.path.join(self._training_file_path,'training_db')

        fileutils = FileUtils(self._logger,
                              self._get_parent_folder(self._file_path))
        db_path = fileutils.create(f'{self.process}_DB')
        # db = SqliteDbHandler(self._logger,db_path,f'training_db_{date}_{time}')
        db = SqliteDbHandler(self._logger, db_path, f'{self.process}_db')
        db.create_db_connection()
        db.save_table_in_db(f'thyroid_{self.process}', data)

    def start_validation(self):

        # bar = st.progress(0)
        with st.spinner('Validating files now, please wait.....'):
            # Validation
            validation_result = self._run_validation()
            st.info('Validation Completed')

        # Read Data from all files in Good Folder and run preprocessing
        with st.spinner('Preprocessing Data now, please wait....'):
            self._preprocessed_data = self._preprocess_data()
            st.write(self._preprocessed_data)
            st.info('Preprocessing Data Completed')