Exemple #1
0
def get_time(rtc, logger):

    if gps_lock.locked():
        logger.debug("Waiting for other gps thread to finish")
    with gps_lock:
        logger.info("Getting UTC datetime via GPS")

        serial, chrono, indicator_led = gps_init(logger)  # initialize serial and timer
        com_counter = int(chrono.read())  # counter for checking whether gps is connected
        message = False  # no message while terminal is disabled (by default)

        while True:
            # data_in = '$GPRMC,085258.000,A,5056.1384,N,00123.1522,W,0.00,159.12,200819,,,A*7E\r\n'
            data_in = (str(serial.readline()))[1:]

            if (int(chrono.read()) - com_counter) >= 10:
                gps_deinit(serial, logger, message, indicator_led)
                logger.error("GPS enabled, but not connected")
                return False

            if data_in[1:4] != "$GP":
                time.sleep(1)
            else:
                for char in data_in:
                    sentence = gps.update(char)
                    if sentence == "GPRMC":
                        com_counter = int(chrono.read())
                        if gps.valid:

                            # Set current time on pycom - convert seconds (timestamp[2]) from float to int
                            datetime = (int('20' + str(gps.date[2])), gps.date[1], gps.date[0], gps.timestamp[0],
                                        gps.timestamp[1], int(gps.timestamp[2]), 0, 0)
                            rtc.init(datetime)

                            # Set current time on RTC module if connected - convert seconds (timestamp[2]) from float to int
                            h_day, h_mnth, h_yr = int(str(gps.date[0]), 16), int(str(gps.date[1]), 16), int(str(gps.date[2]),
                                                                                                            16)
                            h_hr, h_min, h_sec = int(str(gps.timestamp[0]), 16), int(str(gps.timestamp[1]), 16), int(
                                str(int(gps.timestamp[2])), 16)
                            try:
                                clock.set_time(h_yr, h_mnth, h_day, h_hr, h_min, h_sec)
                                message = """GPS UTC datetime successfully updated on pycom board 
                                            GPS UTC datetime successfully updated on RTC module"""
                            except Exception:
                                message = """GPS UTC datetime successfully updated on pycom board 
                                            Failed to set GPS UTC datetime on the RTC module"""

                            gps_deinit(serial, logger, message, indicator_led)
                            return True

            # If timeout elapsed exit function or thread
            if chrono.read() >= int(config.get_config("GPS_timeout")):
                gps_deinit(serial, logger, message, indicator_led)
                logger.error("""GPS timeout
                Check if GPS module is connected
                Place device under clear sky
                Increase GPS timeout in configurations""")
                return False
Exemple #2
0
def process_data(received_data, logger):
    """
    Processes form sent by the user as a json string and saves new configurations. Also updates time on the RTC module.
    :param received_data: json string received from the web socket
    :type received_data: str
    :param logger: status logger
    :type logger: LoggerFactory
    :return: True or False
    :rtype: bool
    """
    #  find json string in received message
    first_index = received_data.rfind("time_begin")
    last_index = received_data.rfind("time_end")

    if first_index != -1 and last_index != -1:
        config_time_str = received_data[(first_index +
                                         len("time_begin")):last_index]
        config_time_lst = config_time_str.split(":")
        config_time_lst[0] = config_time_lst[0][2:]
        h_yr, h_mnth, h_day, h_hr, h_min, h_sec = (
            int(config_time_lst[0], 16),
            int(config_time_lst[1], 16),
            int(config_time_lst[2], 16),
            int(config_time_lst[3], 16),
            int(config_time_lst[4], 16),
            int(config_time_lst[5], 16),
        )
        try:
            clock.set_time(h_yr, h_mnth, h_day, h_hr, h_min, h_sec)
            logger.info("RTC module calibrated via WiFi")
        except Exception:
            logger.warning(
                "RTC module is not available for calibration via WiFi")

    #  find json string in received message
    first_index = received_data.rfind("json_str_begin")
    last_index = received_data.rfind("json_str_end")

    if first_index != -1 and last_index != -1:
        config_json_str = received_data[(first_index +
                                         len("json_str_begin")):last_index]
        new_config_dict = {
            "LORA": "OFF"
        }  # checkbox default value is false - gets overwritten if its true
        new_config_dict.update(ujson.loads(config_json_str))

        if len(config_json_str) >= 1000:
            logger.error("Received configurations are too long")
            logger.info("Enter configurations with valid length")
            return False  # keep looping - wait for new message from client

        logger.info("Configuration data received from user")
        config.save_config(new_config_dict)
        return True

    return False  # keep looping - wait for new message from client
Exemple #3
0
def process_data(received_data, logger):

    #  find json string in received message
    first_index = received_data.rfind('time_begin')
    last_index = received_data.rfind('time_end')

    if first_index != -1 and last_index != -1:
        config_time_str = received_data[(first_index +
                                         len('time_begin')):last_index]
        config_time_lst = config_time_str.split(':')
        config_time_lst[0] = config_time_lst[0][2:]
        h_yr, h_mnth, h_day, h_hr, h_min, h_sec = int(config_time_lst[0], 16), int(config_time_lst[1], 16), \
                                                  int(config_time_lst[2], 16), int(config_time_lst[3], 16), \
                                                  int(config_time_lst[4], 16), int(config_time_lst[5], 16)
        try:
            clock.set_time(h_yr, h_mnth, h_day, h_hr, h_min, h_sec)
            logger.info('RTC module calibrated via WiFi')
        except Exception:
            logger.warning(
                'RTC module is not available for calibration via WiFi')

    #  find json string in received message
    first_index = received_data.rfind('json_str_begin')
    last_index = received_data.rfind('json_str_end')

    if first_index != -1 and last_index != -1:
        config_json_str = received_data[(first_index +
                                         len('json_str_begin')):last_index]
        new_config_dict = ujson.loads(config_json_str)

        if len(config_json_str) >= 1000:
            logger.error('Received configurations are too long')
            logger.info('Enter configurations with valid length')
            return False  # keep looping - wait for new message from client

        logger.info('Configuration data received from user')
        config.save_configuration(new_config_dict)
        return True

    return False  # keep looping - wait for new message from client