def fetch_and_send():
    """ Modify feed of speed changes - or at least keep it warm """
    global feed_state, prev_speed, speed

    if feed_state == "warm":
        # A warm state means that previous speed exists and is not stale
        assert prev_speed is not None
        speed = bronx_speed()
        if speed is None:
            feed_state = "cold"
            alert()
            mw.touch(name=NAME)
        else:
            speed = float(speed)
            speed_change = float(speed) - float(prev_speed)
            if abs(speed_change) < 1e-5:
                feed_state = "cold"  # Feed is stale, don't judge
                print("****  Feed unchanged at " +
                      str(datetime.datetime.now()),
                      flush=True)
            else:
                with Timer() as t_put:
                    mw.set(name=NAME, value=speed_change)
                    print("Sending speed change " + str(speed_change) +
                          " at " + str(datetime.datetime.now()),
                          flush=True)
                print(str(t_put.elapsed) + 's putting data.', flush=True)
                prev_speed = speed

    elif feed_state == "cold":
        # Wait until feed is back up and speeds start changing
        prev_prev = prev_speed if prev_speed else None
        prev_speed = bronx_speed()
        if (prev_speed is not None) and (prev_prev is not None) and abs(
                float(prev_prev) - float(prev_speed)) > 1e-5:
            feed_state = "warm"
            print("**** Feed resumed at " + str(datetime.datetime.now()),
                  flush=True)
        else:
            mw.touch(name=NAME)
            print("Touch at " + str(datetime.datetime.now()) + " - speed is " +
                  str(prev_speed),
                  flush=True)
Beispiel #2
0
def poll_and_send_if_changed():
    """ Modify feed of speed changes - or at least keep it warm """
    global speed, prev_speed

    if prev_speed is None:
        prev_speed = bronx_speed()
        speed      = None
    else:
        speed = bronx_speed()
        try:
            fspeed = float(speed)
            fprev  = float(prev_speed)
            if abs(fspeed-fprev)>1e-5:
                print( mw.set(name=NAME,value=fspeed-fprev) )
                prev_speed = speed
            else:
                mw.touch(name=NAME)
        except:
            mw.touch(name=NAME)
        pprint({"time":str(datetime.datetime.now()),"prev_speed":prev_speed,"speed":speed})
Beispiel #3
0
def poll_and_send():
    """ Create stream of traffic speed """
    value = bronx_speed()
    res = mw.set(name=NAME, value=float(value))
    pprint({'speed': value, "res": res})
    print('', flush=True)
def test_live():
    bs = bronx_speed()
Beispiel #5
0
#!/usr/bin/python3.8
from microprediction import MicroWriter
from microprediction.live import bronx_speed
from apscheduler.schedulers.blocking import BlockingScheduler
from pprint import pprint
import datetime

#  Example of sending live data to www.microprediction.com
#  Data server is really slow

NAME = 'bronx_traffic_absolute_speed.json'

# Let's fail fast
initial_value = float(bronx_speed())
print("Initial value is " + str(initial_value), flush=True)

try:
    from microprediction.config_private import TRAFFIC_WRITE_KEY
    mw = MicroWriter(write_key=TRAFFIC_WRITE_KEY)
except:
    raise Exception("You need to set the write key for this example to work")


def poll_and_send():
    """ Create stream of traffic speed """
    value = bronx_speed()
    res = mw.set(name=NAME, value=float(value))
    pprint({'speed': value, "res": res})
    print('', flush=True)