From eee9be019c1aa42a568d717ce0721bb8f192b909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Tue, 4 Apr 2023 21:29:18 +0800 Subject: [PATCH] - update caiyunapp --- library.go | 2 +- service/caiyunapp/alert.go | 77 ++++++++ service/caiyunapp/aqi.go | 41 ++++ service/caiyunapp/carWashing.go | 15 ++ service/caiyunapp/coldRisk.go | 15 ++ service/caiyunapp/comfort.go | 35 ++++ service/caiyunapp/daily.go | 46 ++++- service/caiyunapp/dressing.go | 25 +++ service/caiyunapp/hourly.go | 30 ++- service/caiyunapp/minutely.go | 30 ++- service/caiyunapp/realtime.go | 112 ++++++++--- service/caiyunapp/skycon.go | 48 +++++ service/caiyunapp/weather.go | 336 +++++++++++++++++++++++++++++++- service/caiyunapp/wind.speed.go | 125 ++++++++++++ 14 files changed, 891 insertions(+), 46 deletions(-) create mode 100644 service/caiyunapp/alert.go create mode 100644 service/caiyunapp/aqi.go create mode 100644 service/caiyunapp/carWashing.go create mode 100644 service/caiyunapp/coldRisk.go create mode 100644 service/caiyunapp/comfort.go create mode 100644 service/caiyunapp/dressing.go create mode 100644 service/caiyunapp/skycon.go create mode 100644 service/caiyunapp/wind.speed.go diff --git a/library.go b/library.go index cee30c1c..63c5d0de 100644 --- a/library.go +++ b/library.go @@ -1,5 +1,5 @@ package go_library func Version() string { - return "1.0.130" + return "1.0.131" } diff --git a/service/caiyunapp/alert.go b/service/caiyunapp/alert.go new file mode 100644 index 00000000..475ad26a --- /dev/null +++ b/service/caiyunapp/alert.go @@ -0,0 +1,77 @@ +package caiyunapp + +func GetCodeType(code string) string { + firsTwo := code[:2] + switch firsTwo { + case "01": + return "台风" + case "02": + return "暴雨" + case "03": + return "暴雪" + case "04": + return "寒潮" + case "05": + return "大风" + case "06": + return "沙尘暴" + case "07": + return "高温" + case "08": + return "干旱" + case "09": + return "雷电" + case "10": + return "冰雹" + case "11": + return "霜冻" + case "12": + return "大雾" + case "13": + return "霾" + case "14": + return "道路结冰" + case "15": + return "森林火险" + case "16": + return "雷雨大风" + case "17": + return "春季沙尘天气趋势预警" + case "18": + return "沙尘" + } + return "" +} +func GetCodeId(code string) string { + lastTwo := code[len(code)-2 : 0] + switch lastTwo { + case "00": + return "白色" + case "01": + return "蓝色" + case "02": + return "黄色" + case "03": + return "橙色" + case "04": + return "红色" + } + return "" +} + +func GetCodeIdColor(code string) string { + lastTwo := code[len(code)-2 : 0] + switch lastTwo { + case "00": + return "white" + case "01": + return "blue" + case "02": + return "yellow" + case "03": + return "orange" + case "04": + return "red" + } + return "" +} diff --git a/service/caiyunapp/aqi.go b/service/caiyunapp/aqi.go new file mode 100644 index 00000000..d6ee67e2 --- /dev/null +++ b/service/caiyunapp/aqi.go @@ -0,0 +1,41 @@ +package caiyunapp + +// GetAqi 空气污染 +// https://docs.caiyunapp.com/docs/tables/aqi +// https://www.mee.gov.cn/ywgz/fgbz/bz/bzwb/jcffbz/201203/W020120410332725219541.pdf +func GetAqi(aqi float64) string { + if aqi <= 50 { + return "优" + } else if aqi <= 100 { + return "良" + } else if aqi <= 150 { + return "轻度污染" + } else if aqi <= 200 { + return "中度污染" + } else if aqi <= 300 { + return "重度污染" + } else if aqi > 300 { + return "严重污染" + } + return "缺数据" +} + +// GetAqiColor 空气污染 +// https://docs.caiyunapp.com/docs/tables/aqi +// https://www.mee.gov.cn/ywgz/fgbz/bz/bzwb/jcffbz/201203/W020120410332725219541.pdf +func GetAqiColor(aqi float64) string { + if aqi <= 50 { + return "green" + } else if aqi <= 100 { + return "yellow" + } else if aqi <= 150 { + return "orange" + } else if aqi <= 200 { + return "red" + } else if aqi <= 300 { + return "purple" + } else if aqi > 300 { + return "maroon" + } + return "" +} diff --git a/service/caiyunapp/carWashing.go b/service/caiyunapp/carWashing.go new file mode 100644 index 00000000..885d740f --- /dev/null +++ b/service/caiyunapp/carWashing.go @@ -0,0 +1,15 @@ +package caiyunapp + +// GetCarWashingDesc 洗车指数 https://docs.caiyunapp.com/docs/tables/lifeindex +func GetCarWashingDesc(carWashing float64) string { + if carWashing <= 1 { + return "适宜" + } else if carWashing <= 2 { + return "较适宜" + } else if carWashing <= 3 { + return "较不适宜" + } else if carWashing <= 4 { + return "不适应" + } + return "" +} diff --git a/service/caiyunapp/coldRisk.go b/service/caiyunapp/coldRisk.go new file mode 100644 index 00000000..ff38f455 --- /dev/null +++ b/service/caiyunapp/coldRisk.go @@ -0,0 +1,15 @@ +package caiyunapp + +// GetColdRiskDesc 感冒指数 https://docs.caiyunapp.com/docs/tables/lifeindex +func GetColdRiskDesc(coldRisk float64) string { + if coldRisk <= 1 { + return "少发" + } else if coldRisk <= 2 { + return "较易发" + } else if coldRisk <= 3 { + return "易发" + } else if coldRisk <= 4 { + return "极易发" + } + return "" +} diff --git a/service/caiyunapp/comfort.go b/service/caiyunapp/comfort.go new file mode 100644 index 00000000..95465989 --- /dev/null +++ b/service/caiyunapp/comfort.go @@ -0,0 +1,35 @@ +package caiyunapp + +// GetComfortDesc 舒适度指数 https://docs.caiyunapp.com/docs/tables/lifeindex +func GetComfortDesc(comfort float64) string { + if comfort <= 0 { + return "闷热" + } else if comfort <= 1 { + return "酷热" + } else if comfort <= 2 { + return "很热" + } else if comfort <= 3 { + return "热" + } else if comfort <= 4 { + return "温暖" + } else if comfort <= 5 { + return "舒适" + } else if comfort <= 6 { + return "凉爽" + } else if comfort <= 7 { + return "冷" + } else if comfort <= 8 { + return "很冷" + } else if comfort <= 9 { + return "寒冷" + } else if comfort <= 10 { + return "极冷" + } else if comfort <= 11 { + return "刺骨的冷" + } else if comfort <= 12 { + return "湿冷" + } else if comfort <= 13 { + return "干冷" + } + return "" +} diff --git a/service/caiyunapp/daily.go b/service/caiyunapp/daily.go index 21771a0d..7decf7e4 100644 --- a/service/caiyunapp/daily.go +++ b/service/caiyunapp/daily.go @@ -19,6 +19,30 @@ type DailyResponse struct { ServerTime float64 `json:"server_time"` Location []float64 `json:"location"` Result struct { + Alert struct { + Status string `json:"status"` + Content []struct { + Pubtimestamp int `json:"pubtimestamp"` // 发布时间,单位是 Unix 时间戳 + AlertID string `json:"alertId"` // 预警 ID + Status string `json:"status"` // 预警信息的状态 + Adcode string `json:"adcode"` // 区域代码 + Location string `json:"location"` // 位置 + Province string `json:"province"` // 省 + City string `json:"city"` // 市 + County string `json:"county"` // 县 + Code string `json:"code"` // 预警代码 + Source string `json:"source"` // 发布单位 + Title string `json:"title"` // 标题 + Description string `json:"description"` // 描述 + RegionID string `json:"regionId"` + Latlon []float64 `json:"latlon"` + RequestStatus string `json:"request_status"` + } `json:"content"` + Adcodes []struct { + Adcode int `json:"adcode"` + Name string `json:"name"` + } `json:"adcodes"` // 行政区划层级信息 + } `json:"alert"` // 预警数据 Daily struct { Status string `json:"status"` Astro []struct { @@ -206,7 +230,7 @@ type DailyResponse struct { Desc string `json:"desc"` // 感冒指数自然语言 } `json:"coldRisk"` } `json:"life_index"` - } `json:"daily"` + } `json:"daily"` // 天级别预报 Primary float64 `json:"primary"` } `json:"result"` } @@ -223,11 +247,11 @@ func newDailyResult(result DailyResponse, body []byte, http gorequest.Response) // Daily 天级别预报 // https://docs.caiyunapp.com/docs/daily -func (c *Client) Daily(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*DailyResult, error) { +func (c *Client) Daily(ctx context.Context, location string, notMustParams ...gorequest.Params) (*DailyResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s,%s/daily?dailysteps=1", locationLatitude, locationLongitude), params, http.MethodGet) + request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s/daily?dailysteps=1", location), params, http.MethodGet) if err != nil { return newDailyResult(DailyResponse{}, request.ResponseBody, request), err } @@ -236,3 +260,19 @@ func (c *Client) Daily(ctx context.Context, locationLongitude, locationLatitude err = gojson.Unmarshal(request.ResponseBody, &response) return newDailyResult(response, request.ResponseBody, request), err } + +// GetUltravioletDesc 紫外线 https://docs.caiyunapp.com/docs/tables/lifeindex +func (DailyResult) GetUltravioletDesc(ultraviolet string) string { + if ultraviolet <= "1" { + return "最弱" + } else if ultraviolet <= "2" { + return "弱" + } else if ultraviolet <= "3" { + return "中等" + } else if ultraviolet <= "4" { + return "强" + } else if ultraviolet <= "5" { + return "很强" + } + return "无" +} diff --git a/service/caiyunapp/dressing.go b/service/caiyunapp/dressing.go new file mode 100644 index 00000000..ebb1cd77 --- /dev/null +++ b/service/caiyunapp/dressing.go @@ -0,0 +1,25 @@ +package caiyunapp + +// GetDressingDesc 穿衣指数 https://docs.caiyunapp.com/docs/tables/lifeindex +func GetDressingDesc(dressing float64) string { + if dressing <= 0 { + return "极热" + } else if dressing <= 1 { + return "极热" + } else if dressing <= 2 { + return "很热" + } else if dressing <= 3 { + return "热" + } else if dressing <= 4 { + return "温暖" + } else if dressing <= 5 { + return "凉爽" + } else if dressing <= 6 { + return "冷" + } else if dressing <= 7 { + return "寒冷" + } else if dressing <= 8 { + return "极冷" + } + return "" +} diff --git a/service/caiyunapp/hourly.go b/service/caiyunapp/hourly.go index 0f84d65d..7c27d937 100644 --- a/service/caiyunapp/hourly.go +++ b/service/caiyunapp/hourly.go @@ -19,6 +19,30 @@ type HourlyResponse struct { ServerTime int `json:"server_time"` Location []float64 `json:"location"` Result struct { + Alert struct { + Status string `json:"status"` + Content []struct { + Pubtimestamp int `json:"pubtimestamp"` // 发布时间,单位是 Unix 时间戳 + AlertID string `json:"alertId"` // 预警 ID + Status string `json:"status"` // 预警信息的状态 + Adcode string `json:"adcode"` // 区域代码 + Location string `json:"location"` // 位置 + Province string `json:"province"` // 省 + City string `json:"city"` // 市 + County string `json:"county"` // 县 + Code string `json:"code"` // 预警代码 + Source string `json:"source"` // 发布单位 + Title string `json:"title"` // 标题 + Description string `json:"description"` // 描述 + RegionID string `json:"regionId"` + Latlon []float64 `json:"latlon"` + RequestStatus string `json:"request_status"` + } `json:"content"` + Adcodes []struct { + Adcode int `json:"adcode"` + Name string `json:"name"` + } `json:"adcodes"` // 行政区划层级信息 + } `json:"alert"` // 预警数据 Hourly struct { Status string `json:"status"` Description string `json:"description"` @@ -77,7 +101,7 @@ type HourlyResponse struct { Value float64 `json:"value"` } `json:"pm25"` } `json:"air_quality"` - } `json:"hourly"` + } `json:"hourly"` // 小时级别预报 Primary float64 `json:"primary"` ForecastKeypoint string `json:"forecast_keypoint"` } `json:"result"` @@ -95,11 +119,11 @@ func newHourlyResult(result HourlyResponse, body []byte, http gorequest.Response // Hourly 小时级别预报 // https://docs.caiyunapp.com/docs/hourly -func (c *Client) Hourly(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*HourlyResult, error) { +func (c *Client) Hourly(ctx context.Context, location string, notMustParams ...gorequest.Params) (*HourlyResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s,%s/hourly?hourlysteps=1", locationLatitude, locationLongitude), params, http.MethodGet) + request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s/hourly?hourlysteps=1", location), params, http.MethodGet) if err != nil { return newHourlyResult(HourlyResponse{}, request.ResponseBody, request), err } diff --git a/service/caiyunapp/minutely.go b/service/caiyunapp/minutely.go index 939df2ef..de30766a 100644 --- a/service/caiyunapp/minutely.go +++ b/service/caiyunapp/minutely.go @@ -19,6 +19,30 @@ type MinutelyResponse struct { ServerTime float64 `json:"server_time"` Location []float64 `json:"location"` Result struct { + Alert struct { + Status string `json:"status"` + Content []struct { + Pubtimestamp int `json:"pubtimestamp"` // 发布时间,单位是 Unix 时间戳 + AlertID string `json:"alertId"` // 预警 ID + Status string `json:"status"` // 预警信息的状态 + Adcode string `json:"adcode"` // 区域代码 + Location string `json:"location"` // 位置 + Province string `json:"province"` // 省 + City string `json:"city"` // 市 + County string `json:"county"` // 县 + Code string `json:"code"` // 预警代码 + Source string `json:"source"` // 发布单位 + Title string `json:"title"` // 标题 + Description string `json:"description"` // 描述 + RegionID string `json:"regionId"` + Latlon []float64 `json:"latlon"` + RequestStatus string `json:"request_status"` + } `json:"content"` + Adcodes []struct { + Adcode int `json:"adcode"` + Name string `json:"name"` + } `json:"adcodes"` // 行政区划层级信息 + } `json:"alert"` // 预警数据 Minutely struct { Status string `json:"status"` Datasource string `json:"datasource"` @@ -26,7 +50,7 @@ type MinutelyResponse struct { Precipitation []float64 `json:"precipitation"` Probability []float64 `json:"probability"` Description string `json:"description"` - } `json:"minutely"` + } `json:"minutely"` // 分钟级预报 Primary float64 `json:"primary"` ForecastKeypoint string `json:"forecast_keypoint"` } `json:"result"` @@ -44,11 +68,11 @@ func newMinutelyResult(result MinutelyResponse, body []byte, http gorequest.Resp // Minutely 分钟级预报 // https://docs.caiyunapp.com/docs/minutely -func (c *Client) Minutely(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*MinutelyResult, error) { +func (c *Client) Minutely(ctx context.Context, location string, notMustParams ...gorequest.Params) (*MinutelyResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s,%s/minutely", locationLatitude, locationLongitude), params, http.MethodGet) + request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s/minutely", location), params, http.MethodGet) if err != nil { return newMinutelyResult(MinutelyResponse{}, request.ResponseBody, request), err } diff --git a/service/caiyunapp/realtime.go b/service/caiyunapp/realtime.go index 3fd37b38..22f02645 100644 --- a/service/caiyunapp/realtime.go +++ b/service/caiyunapp/realtime.go @@ -19,41 +19,65 @@ type RealtimeResponse struct { ServerTime float64 `json:"server_time"` Location []float64 `json:"location"` Result struct { + Alert struct { + Status string `json:"status"` + Content []struct { + Pubtimestamp int `json:"pubtimestamp"` // 发布时间,单位是 Unix 时间戳 + AlertID string `json:"alertId"` // 预警 ID + Status string `json:"status"` // 预警信息的状态 + Adcode string `json:"adcode"` // 区域代码 + Location string `json:"location"` // 位置 + Province string `json:"province"` // 省 + City string `json:"city"` // 市 + County string `json:"county"` // 县 + Code string `json:"code"` // 预警代码 + Source string `json:"source"` // 发布单位 + Title string `json:"title"` // 标题 + Description string `json:"description"` // 描述 + RegionID string `json:"regionId"` + Latlon []float64 `json:"latlon"` + RequestStatus string `json:"request_status"` + } `json:"content"` + Adcodes []struct { + Adcode int `json:"adcode"` + Name string `json:"name"` + } `json:"adcodes"` // 行政区划层级信息 + } `json:"alert"` // 预警数据 Realtime struct { - Status string `json:"status"` - Temperature float64 `json:"temperature"` - Humidity float64 `json:"humidity"` - Cloudrate float64 `json:"cloudrate"` - Skycon string `json:"skycon"` - Visibility float64 `json:"visibility"` - Dswrf float64 `json:"dswrf"` + Temperature float64 `json:"temperature"` // 地表2米气温 + Humidity float64 `json:"humidity"` // 地表2米湿度相对湿度(%) + Cloudrate float64 `json:"cloudrate"` // 总云量(0.0-1.0) + Skycon string `json:"skycon"` // 天气现象 + SkyconDesc string `json:"skycon_desc"` // 天气现象 + Visibility float64 `json:"visibility"` // 地表水平能见度 + Dswrf float64 `json:"dswrf"` // 向下短波辐射通量(W/M2) Wind struct { - Speed float64 `json:"speed"` - Direction float64 `json:"direction"` + Speed float64 `json:"speed"` // 地表 10 米风速 + Direction float64 `json:"direction"` // 地表 10 米风向 } `json:"wind"` - Pressure float64 `json:"pressure"` - ApparentTemperature float64 `json:"apparent_temperature"` + Pressure float64 `json:"pressure"` // 地面气压 + ApparentTemperature float64 `json:"apparent_temperature"` // 体感温度 Precipitation struct { Local struct { Status string `json:"status"` - Datasource string `json:"datasource"` - Intensity float64 `json:"intensity"` - } `json:"local"` + Datasource string `json:"datasource"` // 本地降水带与本地的距离 + Intensity float64 `json:"intensity"` // 本地降水处的降水强度 + } `json:"local"` // 本地降水强度 Nearest struct { Status string `json:"status"` - Distance float64 `json:"distance"` - Intensity float64 `json:"intensity"` - } `json:"nearest"` + Distance float64 `json:"distance"` // 最近降水带与本地的距离 + Intensity float64 `json:"intensity"` // 最近降水处的降水强度 + } `json:"nearest"` // 最近降水强度 } `json:"precipitation"` AirQuality struct { - Pm25 float64 `json:"pm25"` - Pm10 float64 `json:"pm10"` - O3 float64 `json:"o3"` - So2 float64 `json:"so2"` - No2 float64 `json:"no2"` - Co float64 `json:"co"` + Pm25 float64 `json:"pm25"` // PM25 浓度(μg/m3) + Pm10 float64 `json:"pm10"` // PM10 浓度(μg/m3) + O3 float64 `json:"o3"` // 臭氧浓度(μg/m3) + So2 float64 `json:"so2"` // 二氧化氮浓度(μg/m3) + No2 float64 `json:"no2"` // 二氧化硫浓度(μg/m3) + Co float64 `json:"co"` // 一氧化碳浓度(mg/m3) Aqi struct { - Chn float64 `json:"chn"` + Chn float64 `json:"chn"` // 国标 AQI Usa float64 `json:"usa"` } `json:"aqi"` Description struct { @@ -64,14 +88,14 @@ type RealtimeResponse struct { LifeIndex struct { Ultraviolet struct { Index float64 `json:"index"` - Desc string `json:"desc"` + Desc string `json:"desc"` // 生活指数 } `json:"ultraviolet"` Comfort struct { Index float64 `json:"index"` - Desc string `json:"desc"` + Desc string `json:"desc"` // 生活指数 } `json:"comfort"` } `json:"life_index"` - } `json:"realtime"` + } `json:"realtime"` // 实况 Primary float64 `json:"primary"` } `json:"result"` } @@ -88,11 +112,11 @@ func newRealtimeResult(result RealtimeResponse, body []byte, http gorequest.Resp // Realtime 实况 // https://docs.caiyunapp.com/docs/realtime -func (c *Client) Realtime(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*RealtimeResult, error) { +func (c *Client) Realtime(ctx context.Context, location string, notMustParams ...gorequest.Params) (*RealtimeResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s,%s/realtime", locationLatitude, locationLongitude), params, http.MethodGet) + request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s/realtime", location), params, http.MethodGet) if err != nil { return newRealtimeResult(RealtimeResponse{}, request.ResponseBody, request), err } @@ -101,3 +125,33 @@ func (c *Client) Realtime(ctx context.Context, locationLongitude, locationLatitu err = gojson.Unmarshal(request.ResponseBody, &response) return newRealtimeResult(response, request.ResponseBody, request), err } + +// GetUltravioletDesc 紫外线 https://docs.caiyunapp.com/docs/tables/lifeindex +func (RealtimeResult) GetUltravioletDesc(ultraviolet float64) string { + if ultraviolet <= 0 { + return "无" + } else if ultraviolet <= 1 { + return "很弱" + } else if ultraviolet <= 2 { + return "很弱" + } else if ultraviolet <= 3 { + return "弱" + } else if ultraviolet <= 4 { + return "弱" + } else if ultraviolet <= 5 { + return "中等" + } else if ultraviolet <= 6 { + return "中等" + } else if ultraviolet <= 7 { + return "强" + } else if ultraviolet <= 8 { + return "强" + } else if ultraviolet <= 9 { + return "强" + } else if ultraviolet <= 10 { + return "很强" + } else if ultraviolet <= 11 { + return "极强" + } + return "无" +} diff --git a/service/caiyunapp/skycon.go b/service/caiyunapp/skycon.go new file mode 100644 index 00000000..aca7770b --- /dev/null +++ b/service/caiyunapp/skycon.go @@ -0,0 +1,48 @@ +package caiyunapp + +// GetSkyCon https://docs.caiyunapp.com/docs/tables/skycon +func GetSkyCon(skycon string) string { + switch skycon { + case "CLEAR_DAY": + return "晴(白天)" + case "CLEAR_NIGHT": + return "晴(夜间)" + case "PARTLY_CLOUDY_DAY": + return "多云(白天)" + case "PARTLY_CLOUDY_NIGHT": + return "多云(夜间)" + case "CLOUDY": + return "阴" + case "LIGHT_HAZE": + return "轻度雾霾" + case "MODERATE_HAZE": + return "中度雾霾" + case "HEAVY_HAZE": + return "重度雾霾" + case "LIGHT_RAIN": + return "小雨" + case "MODERATE_RAIN": + return "中雨" + case "HEAVY_RAIN": + return "大雨" + case "STORM_RAIN": + return "暴雨" + case "FOG": + return "雾" + case "LIGHT_SNOW": + return "小雪" + case "MODERATE_SNOW": + return "中雪" + case "HEAVY_SNOW": + return "大雪" + case "STORM_SNOW": + return "暴雪" + case "DUST": + return "浮尘" + case "SAND": + return "沙尘" + case "WIND": + return "大风" + } + return skycon +} diff --git a/service/caiyunapp/weather.go b/service/caiyunapp/weather.go index 31ed1388..e7961658 100644 --- a/service/caiyunapp/weather.go +++ b/service/caiyunapp/weather.go @@ -20,15 +20,337 @@ type WeatherResponse struct { Location []float64 `json:"location"` Result struct { Alert struct { - } `json:"alert"` + Status string `json:"status"` + Content []struct { + Pubtimestamp int `json:"pubtimestamp"` // 发布时间,单位是 Unix 时间戳 + AlertID string `json:"alertId"` // 预警 ID + Status string `json:"status"` // 预警信息的状态 + Adcode string `json:"adcode"` // 区域代码 + Location string `json:"location"` // 位置 + Province string `json:"province"` // 省 + City string `json:"city"` // 市 + County string `json:"county"` // 县 + Code string `json:"code"` // 预警代码 + Source string `json:"source"` // 发布单位 + Title string `json:"title"` // 标题 + Description string `json:"description"` // 描述 + RegionID string `json:"regionId"` + Latlon []float64 `json:"latlon"` + RequestStatus string `json:"request_status"` + } `json:"content"` + Adcodes []struct { + Adcode int `json:"adcode"` + Name string `json:"name"` + } `json:"adcodes"` // 行政区划层级信息 + } `json:"alert"` // 预警数据 Realtime struct { - } `json:"realtime"` + Temperature float64 `json:"temperature"` // 地表2米气温 + Humidity float64 `json:"humidity"` // 地表2米湿度相对湿度(%) + Cloudrate float64 `json:"cloudrate"` // 总云量(0.0-1.0) + Skycon string `json:"skycon"` // 天气现象 + SkyconDesc string `json:"skycon_desc"` // 天气现象 + Visibility float64 `json:"visibility"` // 地表水平能见度 + Dswrf float64 `json:"dswrf"` // 向下短波辐射通量(W/M2) + Wind struct { + Speed float64 `json:"speed"` // 地表 10 米风速 + Direction float64 `json:"direction"` // 地表 10 米风向 + } `json:"wind"` + Pressure float64 `json:"pressure"` // 地面气压 + ApparentTemperature float64 `json:"apparent_temperature"` // 体感温度 + Precipitation struct { + Local struct { + Status string `json:"status"` + Datasource string `json:"datasource"` // 本地降水带与本地的距离 + Intensity float64 `json:"intensity"` // 本地降水处的降水强度 + } `json:"local"` // 本地降水强度 + Nearest struct { + Status string `json:"status"` + Distance float64 `json:"distance"` // 最近降水带与本地的距离 + Intensity float64 `json:"intensity"` // 最近降水处的降水强度 + } `json:"nearest"` // 最近降水强度 + } `json:"precipitation"` + AirQuality struct { + Pm25 float64 `json:"pm25"` // PM25 浓度(μg/m3) + Pm10 float64 `json:"pm10"` // PM10 浓度(μg/m3) + O3 float64 `json:"o3"` // 臭氧浓度(μg/m3) + So2 float64 `json:"so2"` // 二氧化氮浓度(μg/m3) + No2 float64 `json:"no2"` // 二氧化硫浓度(μg/m3) + Co float64 `json:"co"` // 一氧化碳浓度(mg/m3) + Aqi struct { + Chn float64 `json:"chn"` // 国标 AQI + Usa float64 `json:"usa"` + } `json:"aqi"` + Description struct { + Chn string `json:"chn"` + Usa string `json:"usa"` + } `json:"description"` + } `json:"air_quality"` + LifeIndex struct { + Ultraviolet struct { + Index float64 `json:"index"` + Desc string `json:"desc"` // 生活指数 + } `json:"ultraviolet"` + Comfort struct { + Index float64 `json:"index"` + Desc string `json:"desc"` // 生活指数 + } `json:"comfort"` + } `json:"life_index"` + } `json:"realtime"` // 实况 Minutely struct { - } `json:"minutely"` + Status string `json:"status"` + Datasource string `json:"datasource"` + Precipitation2H []float64 `json:"precipitation_2h"` + Precipitation []float64 `json:"precipitation"` + Probability []float64 `json:"probability"` + Description string `json:"description"` + } `json:"minutely"` // 分钟级预报 Hourly struct { - } `json:"hourly"` + Status string `json:"status"` + Description string `json:"description"` + Precipitation []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` + Probability float64 `json:"probability"` + } `json:"precipitation"` + Temperature []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` // 地表 2 米气温 + } `json:"temperature"` + ApparentTemperature []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` // 体感温度 + } `json:"apparent_temperature"` + Wind []struct { + Datetime string `json:"datetime"` + Speed float64 `json:"speed"` + Direction float64 `json:"direction"` + } `json:"wind"` + Humidity []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` // 地表 2 米相对湿度(%) + } `json:"humidity"` + Cloudrate []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` + } `json:"cloudrate"` + Skycon []struct { + Datetime string `json:"datetime"` + Value string `json:"value"` + } `json:"skycon"` + Pressure []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` // 地面气压 + } `json:"pressure"` + Visibility []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` + } `json:"visibility"` + Dswrf []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` + } `json:"dswrf"` + AirQuality struct { + Aqi []struct { + Datetime string `json:"datetime"` + Value struct { + Chn float64 `json:"chn"` + Usa float64 `json:"usa"` + } `json:"value"` + } `json:"aqi"` + Pm25 []struct { + Datetime string `json:"datetime"` + Value float64 `json:"value"` + } `json:"pm25"` + } `json:"air_quality"` + } `json:"hourly"` // 小时级别预报 Daily struct { - } `json:"daily"` + Status string `json:"status"` + Astro []struct { + Date string `json:"date"` + Sunrise struct { + Time string `json:"time"` + } `json:"sunrise"` + Sunset struct { + Time string `json:"time"` + } `json:"sunset"` + } `json:"astro"` // 日出日落时间,当地时区的时刻,tzshift 不作用在这个变量) + Precipitation08H20H []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + Probability float64 `json:"probability"` + } `json:"precipitation_08h_20h"` // 白天降水数据 + Precipitation20H32H []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + Probability float64 `json:"probability"` + } `json:"precipitation_20h_32h"` // 夜晚降水数据 + Precipitation []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + Probability float64 `json:"probability"` + } `json:"precipitation"` // 降水数据 + Temperature []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + } `json:"temperature"` // 全天地表 2 米气温 + Temperature08H20H []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + } `json:"temperature_08h_20h"` // 白天地表 2 米气温 + Temperature20H32H []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + } `json:"temperature_20h_32h"` // 夜晚地表 2 米气温 + Wind []struct { + Date string `json:"date"` + Max struct { + Speed float64 `json:"speed"` // 全天地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"max"` + Min struct { + Speed float64 `json:"speed"` // 全天地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"min"` + Avg struct { + Speed float64 `json:"speed"` // 全天地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"avg"` + } `json:"wind"` + Wind08H20H []struct { + Date string `json:"date"` + Max struct { + Speed float64 `json:"speed"` // 白天地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"max"` + Min struct { + Speed float64 `json:"speed"` // 白天地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"min"` + Avg struct { + Speed float64 `json:"speed"` // 白天地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"avg"` + } `json:"wind_08h_20h"` + Wind20H32H []struct { + Date string `json:"date"` + Max struct { + Speed float64 `json:"speed"` // 夜晚地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"max"` + Min struct { + Speed float64 `json:"speed"` // 夜晚地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"min"` + Avg struct { + Speed float64 `json:"speed"` // 夜晚地表 10 米风速 + Direction float64 `json:"direction"` + } `json:"avg"` + } `json:"wind_20h_32h"` + Humidity []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + } `json:"humidity"` // 地表 2 米相对湿度(%) + Cloudrate []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + } `json:"cloudrate"` // 云量(0.0-1.0) + Pressure []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + } `json:"pressure"` // 地面气压 + Visibility []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + } `json:"visibility"` // 地表水平能见度 + Dswrf []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Min float64 `json:"min"` + Avg float64 `json:"avg"` + } `json:"dswrf"` // 向下短波辐射通量(W/M2) + AirQuality struct { + Aqi []struct { + Date string `json:"date"` + Max struct { + Chn float64 `json:"chn"` + Usa float64 `json:"usa"` + } `json:"max"` + Avg struct { + Chn float64 `json:"chn"` + Usa float64 `json:"usa"` + } `json:"avg"` + Min struct { + Chn float64 `json:"chn"` + Usa float64 `json:"usa"` + } `json:"min"` + } `json:"aqi"` // 国标 AQI + Pm25 []struct { + Date string `json:"date"` + Max float64 `json:"max"` + Avg float64 `json:"avg"` + Min float64 `json:"min"` + } `json:"pm25"` // PM2.5 浓度(μg/m3) + } `json:"air_quality"` + Skycon []struct { + Date string `json:"date"` + Value string `json:"value"` // 全天主要 天气现象 + } `json:"skycon"` + Skycon08H20H []struct { + Date string `json:"date"` + Value string `json:"value"` // 白天主要 天气现象 + } `json:"skycon_08h_20h"` + Skycon20H32H []struct { + Date string `json:"date"` + Value string `json:"value"` // 夜晚主要 天气现象 + } `json:"skycon_20h_32h"` + LifeIndex struct { + Ultraviolet []struct { + Date string `json:"date"` + Index string `json:"index"` + Desc string `json:"desc"` // 紫外线指数自然语言 + } `json:"ultraviolet"` + CarWashing []struct { + Date string `json:"date"` + Index string `json:"index"` + Desc string `json:"desc"` // 洗车指数自然语言 + } `json:"carWashing"` + Dressing []struct { + Date string `json:"date"` + Index string `json:"index"` + Desc string `json:"desc"` // 穿衣指数自然语言 + } `json:"dressing"` + Comfort []struct { + Date string `json:"date"` + Index string `json:"index"` + Desc string `json:"desc"` // 舒适度指数自然语言 + } `json:"comfort"` + ColdRisk []struct { + Date string `json:"date"` + Index string `json:"index"` + Desc string `json:"desc"` // 感冒指数自然语言 + } `json:"coldRisk"` + } `json:"life_index"` + } `json:"daily"` // 天级别预报 Primary int `json:"primary"` ForecastKeypoint string `json:"forecast_keypoint"` } `json:"result"` @@ -46,11 +368,11 @@ func newWeatherResult(result WeatherResponse, body []byte, http gorequest.Respon // Weather 综合 // https://docs.caiyunapp.com/docs/weather -func (c *Client) Weather(ctx context.Context, locationLongitude, locationLatitude string, notMustParams ...gorequest.Params) (*WeatherResult, error) { +func (c *Client) Weather(ctx context.Context, location string, notMustParams ...gorequest.Params) (*WeatherResult, error) { // 参数 params := gorequest.NewParamsWith(notMustParams...) // 请求 - request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s,%s/weather", locationLatitude, locationLongitude), params, http.MethodGet) + request, err := c.request(ctx, c.getApiUrl()+fmt.Sprintf("/%s/weather", location), params, http.MethodGet) if err != nil { return newWeatherResult(WeatherResponse{}, request.ResponseBody, request), err } diff --git a/service/caiyunapp/wind.speed.go b/service/caiyunapp/wind.speed.go new file mode 100644 index 00000000..17d0745a --- /dev/null +++ b/service/caiyunapp/wind.speed.go @@ -0,0 +1,125 @@ +package caiyunapp + +import "fmt" + +// GetWindSpeed https://docs.caiyunapp.com/docs/tables/wind +func GetWindSpeed(speed float64) string { + if speed <= 0 { + return "0级" + } else if speed <= 5 { + return "1级" + } else if speed <= 11 { + return "2级" + } else if speed <= 19 { + return "3级" + } else if speed <= 28 { + return "4级" + } else if speed <= 38 { + return "5级" + } else if speed <= 49 { + return "6级" + } else if speed <= 61 { + return "7级" + } else if speed <= 74 { + return "8级" + } else if speed <= 88 { + return "9级" + } else if speed <= 102 { + return "10级" + } else if speed <= 117 { + return "11级" + } else if speed <= 133 { + return "12级" + } else if speed <= 149 { + return "13级" + } else if speed <= 166 { + return "14级" + } else if speed <= 183 { + return "15级" + } else if speed <= 201 { + return "16级" + } else if speed <= 220 { + return "17级" + } + return fmt.Sprintf("%v", speed) +} + +// GetWindSpeedDesc https://docs.caiyunapp.com/docs/tables/wind +func GetWindSpeedDesc(speed float64) string { + if speed <= 0 { + return "无风" + } else if speed <= 5 { + return "微风徐徐" + } else if speed <= 11 { + return "清风" + } else if speed <= 19 { + return "树叶摇摆" + } else if speed <= 28 { + return "树枝摇动" + } else if speed <= 38 { + return "风力强劲" + } else if speed <= 49 { + return "风力强劲" + } else if speed <= 61 { + return "风力超强" + } else if speed <= 74 { + return "狂风大作" + } else if speed <= 88 { + return "狂风呼啸" + } else if speed <= 102 { + return "暴风毁树" + } else if speed <= 117 { + return "暴风毁树" + } else if speed <= 133 { + return "飓风" + } else if speed <= 149 { + return "台风" + } else if speed <= 166 { + return "强台风" + } else if speed <= 183 { + return "强台风" + } else if speed <= 201 { + return "超强台风" + } else if speed <= 220 { + return "超强台风" + } + return fmt.Sprintf("%v", speed) +} + +// GetWindDirectionDesc https://docs.caiyunapp.com/docs/tables/wind +func GetWindDirectionDesc(direction float64) string { + if direction <= 11.26 { + return "北" + } else if direction <= 33.75 { + return "北东北" + } else if direction <= 56.25 { + return "东北" + } else if direction <= 78.75 { + return "东东北" + } else if direction <= 101.25 { + return "东" + } else if direction <= 123.75 { + return "东东南" + } else if direction <= 146.25 { + return "东南" + } else if direction <= 168.75 { + return "南东南" + } else if direction <= 191.25 { + return "南" + } else if direction <= 213.75 { + return "南西南" + } else if direction <= 236.25 { + return "西南" + } else if direction <= 258.75 { + return "西西南" + } else if direction <= 281.25 { + return "西" + } else if direction <= 303.75 { + return "西西北" + } else if direction <= 326.25 { + return "西北" + } else if direction <= 348.75 { + return "北西北" + } + return "北" +}