import datetime
from smartfarming.models.device import MongoDevice
from kedong.decoration import kedong_deco, PortError
from smartfarming.utils import get_address_by_lntlat, get_weather_info



@kedong_deco(login_required=False)
def weathers(request):
    """
    天气接口
    参数
    lat             必传(str)               纬度
    lng             必传(str)               经度

    返回值：
     "data": [
        {
        "id": 1201,
        "province": "新疆维吾尔自治区",             省
        "city": "克孜勒苏柯尔克孜自治州",           市
        "district": "阿克陶县",                     县
        "lng": "75.945159",                         经度
        "lat": "39.147079",                         纬度
        "at": "31",                                 温度
        "ah": "14",                                 湿度
        "upl_time": "2021-10-25 14:39:48",          时间
        "win": "东风",                              风向
        "win_speed": "1级",                         风速
        "win_meter": "5km/h",                       风力
        "wea": "多云",                              天气
        "visibility": "30km",                       能见度
        "pressure": "863",                          压力
        "air": "40",                                空气
        "air_pm25": "19",                           空气PM2.5
        "air_level": "优",                          空气等级
        "air_tips": "空气很好,可以外出活动,呼吸新鲜空气,拥抱大自然！"   空气说明
        }
    ],
    "params": {
    "lat": "039.1850853",
    "lng": "075.8749465"
    }
    }


    """

    lat = request.POST.get('lat', '')
    lng = request.POST.get('lng', '')
    device_id = request.POST.get('device_id')

    if not lat and not lng:
        raise PortError('', '未传经纬度')

    province, city, district = "", "", ""
    if device_id:
        try:
            instance = MongoDevice.objects.get(device_id=device_id)
            province, city, district = instance.province, instance.city, instance.district
            if (not city) and district:
                city = district
        except Exception as e:
            pass

    if not (province and city):
        province, city, district = get_address_by_lntlat(lng, lat)

    result = []
    weather_info = get_weather_info(lng, lat)
    try:
        upl_time = weather_info.get('upl_time')
        upl_time = datetime.datetime.fromtimestamp(int(upl_time)).strftime("%Y-%m-%d %H:%M:%S")
    except Exception as e:
        upl_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    result.append({
        "province": province,
        "city": city,
        "district": district,
        "lng": lng,
        "lat": lat,
        "at": weather_info.get('at', ''),
        "ah": weather_info.get('ah', ''),
        "upl_time": upl_time,
        "win": weather_info.get('win', ''),
        "win_speed": weather_info.get('win_speed', ''),
        "win_meter": weather_info.get('win_meter', ''),
        "wea": weather_info.get('wea', ''),
        "visibility": weather_info.get('visibility', ''),
        "pressure": weather_info.get('pressure', ''),
        "air": weather_info.get('air', ''),
        "air_pm25": weather_info.get('air_pm25', ''),
        "air_level": weather_info.get('air_level', ''),
        "air_tips": weather_info.get('air_tips', ''),
    })
    return result