Ejemplo n.º 1
0
def update():
    geolocation_data = 'http://pogoda.wp.pl/city.json?lat=%s&lon=%s' % (settings.LATITUDE_AND_LONGITUDE[0],
                                                                       settings.LATITUDE_AND_LONGITUDE[1])

    city_id = requests.get(geolocation_data).json()['cityid']


    weather_data = requests.get('https://api.o2.pl/weather/api/o2/weather?cid=%s&days=4' % city_id).json()

    city_name = weather_data['cityName']

    for day in weather_data['days']:
        date_time = datetime.fromtimestamp(day['date'])

        highest_temp = -sys.maxint
        icon = ""
        precipitation = 0

        for hour in day['timeOfDay']:
            if hour['temperature'] > highest_temp:
                highest_temp = hour['temperature']
                icon = ICON_MAP[hour['icon']]
                precipitation = hour['precipitation']


        db.insert('weather', fields=('city_name', 'date_time', 'temperature', 'icon', 'precipitation'),
                    values=(city_name, date_time, highest_temp, icon, precipitation))
Ejemplo n.º 2
0
def save(level=0, date_time=datetime.now()):
    if level > 100:
        level = 100
    elif level < 0:
        level = 0

    db.insert('water_supply', fields=('date_time', 'level'),
                              values=(date_time, level))
Ejemplo n.º 3
0
def save(sensor_id, level=0, date_time=datetime.now()):
    if level > 100:
        level = 100
    elif level < 0:
        level = 0

    # Round to the nearest hour
    date_time = date_time - timedelta(minutes=date_time.minute % 60,
                                      seconds=date_time.second,
                                      microseconds=date_time.microsecond)

    db.insert('moisture_level', fields=('sensor_id', 'date_time', 'level'),
                                values=(sensor_id, date_time, level))