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()
}
}
})