Example #1
0
def fetch_data(val, userdata):
    global current_heading, calibrated_by_values, calibrated_by_log, last_calib_publish_time

    if "X:" not in val:
        logger.info("Non-data: %s", val)
    else:
        try:
            mqtt_client = userdata[PAHO_CLIENT]
            vals = val.split("\t")

            x_val = vals[0]
            heading = round(float(x_val.split(": ")[1]), 1)
            if heading != current_heading:
                logger.debug(val)
                current_heading = heading
                publish_heading(mqtt_client, userdata[HEADING_TOPIC], heading,
                                userdata)

            if userdata[CALIB_ENABLED] and not calibrated_by_values:
                # The arduino sketch includes a "! " prefix to SYS if the data is not calibrated (and thus not reliable)
                if "! " in val:
                    nocalib_str = val[val.index("! "):]
                    logger.info("9-DOF Sensor not calibrated by log: %s",
                                nocalib_str)
                    mqtt_client.publish(userdata[CALIB_TOPIC],
                                        payload=(nocalib_str.encode("utf-8")),
                                        qos=0)
                    calibrated_by_log = False
                else:
                    if not calibrated_by_log:
                        msg = CALIBRATION_BY_LOG
                        logger.info(msg)
                        mqtt_client.publish(userdata[CALIB_TOPIC],
                                            payload=(msg.encode("utf-8")),
                                            qos=0)
                        calibrated_by_log = True

                    calib_str = vals[3]
                    calibs = calib_str.split(" ")
                    sys_calib = int(calibs[0].split(":")[1])
                    gyro_calib = int(calibs[1].split(":")[1])
                    mag_calib = int(calibs[2].split(":")[1])
                    acc_calib = int(calibs[3].split(":")[1])
                    if sys_calib == 3 and gyro_calib == 3 and mag_calib == 3 and acc_calib == 3:
                        msg = CALIBRATION_BY_VALUES
                        logger.info(msg)
                        mqtt_client.publish(userdata[CALIB_TOPIC],
                                            payload=(msg.encode("utf-8")),
                                            qos=0)
                        calibrated_by_values = True
                    elif current_time_millis(
                    ) - last_calib_publish_time > userdata[
                            CALIB_PUBLISH] * 1000:
                        mqtt_client.publish(
                            userdata[CALIB_TOPIC],
                            payload=(calib_str.encode("utf-8")),
                            qos=0)
                        last_calib_publish_time = current_time_millis()
        except IndexError:
            logger.info("Formatting error: %s", val)
Example #2
0
    def __init__(self):
        # Initialization
        pygame.init()
        self.visualizer = Visualizer()
        self.algorithm = Algorithm()

        self.speed_scl = 1

        self.released = {}

        self.last_tick = current_time_millis()
        self.current_tick = current_time_millis()

        print("------------------------")
        print("        Controls:       ")
        print("[WASD]: Move the graph.")
        print("[UP ARROW]: Zoom in.")
        print("[DOWN ARROW]: Zoom out.")
        print("[LEFT ARROW]: Slow down.")
        print("[RIGHT ARROW]: Speed up.")
        print("------------------------")

        # Main loop
        while True:
            if self.current_tick - self.last_tick > 1000 / SPEED:
                self.tick()
                self.last_tick = current_time_millis()
            else:
                self.current_tick = current_time_millis()
Example #3
0
    def get_access_token(self):
        if self.token_object is None:
            self.token_object = self.get_token()
            self.expiration_time = current_time_millis() + int(self.token_object['expires_in']) * 1000
        elif self.expiration_time < current_time_millis():
            self.token_object = self.refresh_token()
            self.expiration_time = current_time_millis() + int(self.token_object['expires_in']) * 1000

        return self.token_object['access_token']
Example #4
0
    def get_access_token(self):
        if self.token_object is None:
            self.token_object = self.get_token()
            self.expiration_time = current_time_millis() + int(
                self.token_object['expires_in']) * 1000
        elif self.expiration_time < current_time_millis():
            self.token_object = self.refresh_token()
            self.expiration_time = current_time_millis() + int(
                self.token_object['expires_in']) * 1000

        return self.token_object['access_token']
Example #5
0
def publish_heading(client, topic, heading, userdata):
    global last_heading_publish_time

    if not userdata[ENABLED]:
        return

    elapsed_time = current_time_millis() - last_heading_publish_time
    if elapsed_time < 75:
        return

    publish_lock = userdata[PUBLISH_LOCK]
    with publish_lock:
        client.publish(topic, payload=(str(heading).encode("utf-8")), qos=0)
        last_heading_publish_time = current_time_millis()
Example #6
0
def background_publisher(userdata, min_publish_secs):
    client = userdata[PAHO_CLIENT]
    heading_topic = userdata[HEADING_TOPIC]
    while not stopped:
        time.sleep(.5)
        elapsed_time = current_time_millis() - last_heading_publish_time
        if elapsed_time > min_publish_secs * 1000 and current_heading != -1:
            publish_heading(client, heading_topic, current_heading, userdata)
Example #7
0
def post_event_flume(url, object):
    data_object = [{
        'headers': {
            'timestamp': current_time_millis()
        },
        'body': json.dumps(object)
    }]

    requests.post(url, json=data_object)

    return data_object
Example #8
0
def check_events(stations_list, s1, s2):
    events_list = []
    stations_ids = [e['id'] for e in stations_list['stations']] 
    for st_id in stations_ids:
        e1 = find_by_id(st_id, s1)
        e2 = find_by_id(st_id, s2)
        if (not e1 is None) and (not e2 is None):
            bikes_diff = e1['availability']['bikes'] - e2['availability']['bikes']
            slots_diff = e1['availability']['slots'] - e2['availability']['slots']
            if bikes_diff != 0 or slots_diff != 0:
                evt = {'station_id': st_id, 'timestamp': current_time_millis(), 'bike_diff':bikes_diff, 'slot_diff': slots_diff}
                events_list.append(evt)

    return events_list
Example #9
0
def check_events(stations_list, s1, s2):
    events_list = []
    stations_ids = [e['id'] for e in stations_list['stations']]
    for st_id in stations_ids:
        e1 = find_by_id(st_id, s1)
        e2 = find_by_id(st_id, s2)
        if (not e1 is None) and (not e2 is None):
            bikes_diff = e1['availability']['bikes'] - e2['availability'][
                'bikes']
            slots_diff = e1['availability']['slots'] - e2['availability'][
                'slots']
            if bikes_diff != 0 or slots_diff != 0:
                evt = {
                    'station_id': st_id,
                    'timestamp': current_time_millis(),
                    'bike_diff': bikes_diff,
                    'slot_diff': slots_diff
                }
                events_list.append(evt)

    return events_list
Example #10
0
 def __init__(self, port=None):
     super(EchoServer, self).__init__(port=port, desc="echo server")
     self._start_time = current_time_millis()
     self.grpc_server = None