Skip to main content
版本:v1.0

WEATHER

创建传感器

const weather = hmSensor.createSensor(hmSensor.id.WEATHER)

weather 实例

weather.getForecastWeather()

类型

() => ForecastWeather

ForecastWeather: object

属性说明类型
cityName城市名称string
forecastData天气数据ForecastData
tideData日出/日落数据TideData

ForecastData: object

属性说明类型
data天气数据数组,索引 0 位置代表当天Array<ForecastDataItem>
count天气数据数组长度number

ForecastDataItem: object

属性说明类型
high最高气温number
low最低气温number
index天气索引,值详见下表number
index说明
0多云
1阵雨
2阵雪
3
4
5小雨
6小雪
7中雨
8中雪
9大雪
10大雨
11沙尘暴
12雨夹雪
13
14
15雷阵雨
16暴雪
17浮尘
18特大暴雨
19雨加冰雹
20雷阵雨伴有冰雹
21大暴雨
22扬尘
23强沙尘暴
24暴雨
25未知天气
26夜间多云
27夜间阵雨
28夜间晴

TideData: object

属性说明类型
data日出/日落数据数组,索引 0 位置代表当天Array<TideDataItem>
count日出/日落数据数组长度number

TideDataItem: object

属性说明类型
sunrise日出数据Sunrise
sunset日落数据Sunset

Sunrise: object

属性说明类型
hour小时number
minute分钟number

Sunset: object

属性说明类型
hour小时number
minute分钟number

代码示例

class TextByLine {
constructor(params) {
const { text = '', y = undefined, line = 0 } = params

this.text = text
this.y = y
this.line = line
this.y_computed = Number.isInteger(this.y) ? this.y : px(this.line * 60 + 120)
}

render() {
return hmUI.createWidget(hmUI.widget.TEXT, {
x: px(0),
y: this.y_computed,
w: px(480),
h: px(46),
color: 0xffffff,
text_size: px(20),
align_h: hmUI.align.CENTER_H,
align_v: hmUI.align.CENTER_V,
text_style: hmUI.text_style.NONE,
text: this.text
})
}
}

Page({
build() {
const weather = hmSensor.createSensor(hmSensor.id.WEATHER)
const weatherData = weather.getForecastWeather()
const { forecastData, tideData } = weatherData

new TextByLine({
text: `cityName:${weatherData.cityName}`,
line: 0
}).render()

new TextByLine({
text: `forecastCount:${forecastData.count};tideCount:${tideData.count}`,
line: 1
}).render()

for (let i = 0; i < forecastData.count; i++) {
const { index, high, low } = forecastData.data[i]

new TextByLine({
text: `index:${index};high:${high};low:${low}`,
line: 2 + i
}).render()
}

for (let i = 0; i < tideData.count; i++) {
const {
sunrise: { hour: sunriseHour, minute: sunriseMinute },
sunset: { hour: sunsetHour, minute: sunsetMinute }
} = tideData.data[i]

new TextByLine({
text: `sunrise:${sunriseHour}:${sunriseMinute};sunset:${sunsetHour}:${sunsetMinute}`,
line: 2 + i + forecastData.count
}).render()
}
}
})