예제 #1
0
def update_time(network, utc_offset = 0):
    time_url = "https://io.adafruit.com/api/v2/time/seconds"
    time_data = network.fetch_data(time_url)
    # print("time data: " + time_data)
    time_int = int(time_data) + utc_offset * 60 * 60
    time_struct = time.localtime(time_int)
    RTC().datetime = time_struct
    return time_struct
예제 #2
0
def update_time(timezone=None):
    """ Update system date/time from WorldTimeAPI public server;
        no account required. Pass in time zone string
        (http://worldtimeapi.org/api/timezone for list)
        or None to use IP geolocation. Returns current local time as a
        time.struct_time and UTC offset as string. This may throw an
        exception on fetch_data() - it is NOT CAUGHT HERE, should be
        handled in the calling code because different behaviors may be
        needed in different situations (e.g. reschedule for later).
    """
    if timezone: # Use timezone api
        time_url = 'http://worldtimeapi.org/api/timezone/' + timezone
    else: # Use IP geolocation
        time_url = 'http://worldtimeapi.org/api/ip'

    time_data = NETWORK.fetch_data(time_url,
                                   json_path=[['datetime'], ['dst'],
                                              ['utc_offset']])
    time_struct = parse_time(time_data[0], time_data[1])
    RTC().datetime = time_struct
    return time_struct, time_data[2]
예제 #3
0
    color = "red"
    hcolor = 0xFF0000
elif time_data["wday"] == 3 and time_data["hour"] >= 8 : # Wednesday after 9am
    weekday = "WED"
    garbage = "done"
    color = "green"
    hcolor = 0x33CC33
elif time_data["wday"] == 4: # Thursday
    weekday = "THU"
    garbage = "6 days"
    color = "green"
    hcolor = 0x33CC33
elif time_data["wday"] == 5: # Friday
    weekday = "FRI"
    garbage = "5 days"
    color = "green"
    hcolor = 0x33CC33
elif time_data["wday"] == 6: # Saturday
    weekday = "SAT"
    garbage = "4 days"
    color = "green"
    hcolor = 0x33CC33

RTC().datetime = time_struct
print("update_time complete")
print("time_struct: ", time_struct)
print("weekday: ", weekday)
print("garbage: ", garbage)
print("RTC().datetime: ", RTC().datetime)
print("")
예제 #4
0
def update_time(timezone=None, demo_num=0, demo_hour="7"):
    """ Update system date/time from AdafruitIO. Returns current local time
        as a time.struct_time. This may throw an exception on fetch_data() -
        it is NOT CAUGHT HERE, should be handled in the calling code because
        different behaviors may be needed in different situations (e.g.
        reschedule for later).
    """
    print("Start update_time function")
    aio_username = secrets["aio_username"]
    aio_key = secrets["aio_key"]

    if timezone:  # Use timezone api
        #time_url = 'http://worldtimeapi.org/api/timezone/' + timezone
        time_url = 'https://io.adafruit.com/api/v2/' + aio_username + '/integrations/time/struct/?x-aio-key=' + aio_key + '&tz=' + timezone
    else:  # Use IP geolocation
        time_url = 'https://io.adafruit.com/api/v2/' + aio_username + '/integrations/time/struct/?x-aio-key=' + aio_key

    print("time_url:" + time_url)
    print("")
    print("Start update_time function NETWORK.fetch_data")

    if DEMO == False:
        time_data_str = NETWORK.fetch_data(time_url, timeout=10)

        print("time_data_str: ", time_data_str)
        print("time_data_str type: ", type(time_data_str))
        time_data = json.loads(time_data_str)
        print("time_data type: ", type(time_data))
        print("time_data values: ", time_data.values())
        print("time_data keys: ", time_data.keys())
        print("AdafruitIO time_data", time_data["year"], time_data["mon"],
              time_data["mday"], time_data["hour"], time_data["min"],
              time_data["sec"], time_data["wday"], time_data["yday"],
              time_data["isdst"])
    else:
        year = str(random.randint(2021, 2024))
        month = str(random.randint(1, 12))
        day = str(random.randint(1, 28))
        if demo_hour:
            hour = demo_hour
        else:
            hour = str(random.randint(6, 20))
            # hour = str(random.randint(0,23)) # will occasionally show night mode
        minute = str(random.randint(10, 59))
        weekday = str(random.randint(0, 6))
        demoDateTime = '{"year":' + year + ',"mon":' + month + ',"mday":' + day + ',"hour":' + hour + ', "min":' + minute + ',"sec":25,"wday":' + weekday + ',"yday":2,"isdst":0}'
        # AdafruitIO JSON example: {"year":2021,"mon":1,"mday":2,"hour":17,"min":22,"sec":25,"wday":6,"yday":2,"isdst":0}
        print("DEMO demoDateTime: ", demoDateTime)
        time_data = json.loads(demoDateTime)
        print("DEMO time_data type: ", type(time_data))
        print("DEMO time_data values: ", time_data.values())
        print("DEMO time_data keys: ", time_data.keys())
        print("DEMO time_data", time_data["year"], time_data["mon"],
              time_data["mday"], time_data["hour"], time_data["min"],
              time_data["sec"], time_data["wday"], time_data["yday"],
              time_data["isdst"])

        print("DEMO time_data: ", time_data)

    #time_struct = parse_time(time_data[0], time_data[1])
    time_struct = time.struct_time(time_data["year"], time_data["mon"],
                                   time_data["mday"], time_data["hour"],
                                   time_data["min"], time_data["sec"],
                                   time_data["wday"], time_data["yday"],
                                   time_data["isdst"])
    if time_data["wday"] == 0:  # Sunday
        weekday = "SUN"
        garbage = "3 days"
        color = "green"
        hcolor = 0x33CC33
    elif time_data["wday"] == 1:  # Monday
        weekday = "MON"
        garbage = "2 days"
        color = "green"
        hcolor = 0x33CC33
    elif time_data[
            "wday"] == 2 and time_data["hour"] < 19:  # Tuesday before 7pm
        weekday = "TUE"
        garbage = "2nite"
        color = "yellow"
        hcolor = 0xFFFF00
    elif time_data[
            "wday"] == 2 and time_data["hour"] >= 19:  # Tuesday after 7pm
        weekday = "TUE"
        garbage = "NOW"
        color = "red"
        hcolor = 0xFF0000
    elif time_data["wday"] == 3 and time_data["hour"] <= 7 and time_data[
            "min"] <= 59:  # Wednesday 5am - 7:59am
        weekday = "WED"
        garbage = "NOW"
        color = "red"
        hcolor = 0xFF0000
    elif time_data[
            "wday"] == 3 and time_data["hour"] >= 8:  # Wednesday after 9am
        weekday = "WED"
        garbage = "done"
        color = "green"
        hcolor = 0x33CC33
    elif time_data["wday"] == 4:  # Thursday
        weekday = "THU"
        garbage = "6 days"
        color = "green"
        hcolor = 0x33CC33
    elif time_data["wday"] == 5:  # Friday
        weekday = "FRI"
        garbage = "5 days"
        color = "green"
        hcolor = 0x33CC33
    elif time_data["wday"] == 6:  # Saturday
        weekday = "SAT"
        garbage = "4 days"
        color = "green"
        hcolor = 0x33CC33

    RTC().datetime = time_struct
    print("update_time complete")
    print("time_struct: ", time_struct)
    print("weekday: ", weekday)
    print("garbage: ", garbage)
    print("")
    return time_struct, weekday, garbage, color, hcolor
def update_time(timezone=None, demo_num=0, demo_hour="7"):
    """ Update system date/time from WorldTimeAPI public server;
        no account required. Pass in time zone string
        (http://worldtimeapi.org/api/timezone for list)
        or None to use IP geolocation. Returns current local time as a
        time.struct_time and UTC offset as string. This may throw an
        exception on fetch_data() - it is NOT CAUGHT HERE, should be
        handled in the calling code because different behaviors may be
        needed in different situations (e.g. reschedule for later).
    """
    if timezone: # Use timezone api
        time_url = 'http://worldtimeapi.org/api/timezone/' + timezone
    else: # Use IP geolocation
        time_url = 'http://worldtimeapi.org/api/ip'

    print("")
    print("Start update_time function NETWORK.fetch_data")
    if DEMO == False:
        time_data = NETWORK.fetch_data(time_url,
                                       json_path=[['datetime'], ['dst'],
                                                  ['utc_offset'], ['day_of_week']])
    else:
        month = str(random.randint(1,12))
        day = str(random.randint(1,28))
        if demo_hour:
            hour = demo_hour
        else:
            hour = str(random.randint(6,20))
            # hour = str(random.randint(0,23)) # will occasionally show night mode
        minute = str(random.randint(10,59))
        demoDateTime = '2020-' + month + '-' + day + 'T' + hour + ':' + minute + ':15.813019-08:00'
        # time data JSON example: ['2020-11-28T20:45:15.813019-08:00', False, '-08:00', 6]
        time_data = [demoDateTime, False, '-08:00', demo_num]

    print("time_data: ", time_data)

    time_struct = parse_time(time_data[0], time_data[1])
    if time_data[3] == 0: # Sunday
        weekday = "SUN"
        garbage = "3 days"
        color = "green"
        hcolor = 0x33CC33
    elif time_data[3] == 1: # Monday
        weekday = "MON"
        garbage = "2 days"
        color = "green"
        hcolor = 0x33CC33
    elif time_data[3] == 2 and time_struct.tm_hour < 19: # Tuesday before 7pm
        weekday = "TUE"
        garbage = "2nite"
        color = "yellow"
        hcolor = 0xFFFF00
    elif time_data[3] == 2 and time_struct.tm_hour >= 19: # Tuesday after 7pm
        weekday = "TUE"
        garbage = "NOW"
        color = "red"
        hcolor = 0xFF0000
    elif time_data[3] == 3 and time_struct.tm_hour <= 7 and time_struct.tm_min <= 59: # Wednesday 5am - 7:59am
        weekday = "WED"
        garbage = "NOW"
        color = "red"
        hcolor = 0xFF0000
    elif time_data[3] == 3 and time_struct.tm_hour >= 8 : # Wednesday after 9am
        weekday = "WED"
        garbage = "done"
        color = "green"
        hcolor = 0x33CC33
    elif time_data[3] == 4: # Thursday
        weekday = "THU"
        garbage = "6 days"
        color = "green"
        hcolor = 0x33CC33
    elif time_data[3] == 5: # Friday
        weekday = "FRI"
        garbage = "5 days"
        color = "green"
        hcolor = 0x33CC33
    elif time_data[3] == 6: # Saturday
        weekday = "SAT"
        garbage = "4 days"
        color = "green"
        hcolor = 0x33CC33

    RTC().datetime = time_struct
    print("update_time complete")
    print("time_struct: ", time_struct)
    print("utc_offset: ", time_data[2])
    print("weekday: ", weekday)
    print("garbage: ", garbage)
    print("")
    return time_struct, time_data[2], weekday, garbage, color, hcolor
예제 #6
0
        if choice >= 2:
            choice = 0
        continue

print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

# Get NTP time, may make several attempts
ntp = NTP(esp)
ntp.set_time()
while not ntp.valid_time:
    print("time not valid...")
    time.sleep(5)
    ntp.set_time(60 * 60)

# Get the RTC time, not NTP updates RTC silently
rtc = RTC()
print("{:02}/{:02}/{:04} {:02}:{:02}".format(rtc.datetime.tm_mday,
                                             rtc.datetime.tm_mon,
                                             rtc.datetime.tm_year,
                                             rtc.datetime.tm_hour,
                                             rtc.datetime.tm_min))

# Use the OpenWeather API
# london,gb is the location and can be changed appropriately
WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather?q=london,gb&units=metric&appid=" + secrets[
    "open_weather"]


def update_weather():
    print("updating weather...")
    response = requests.get(WEATHER_URL)