import { sensor } from '@kit.SensorServiceKit';
import { BusinessError } from '@kit.BasicServicesKit';
class LightIntensityItem {
luxStart: number;
luxEnd: number;
type: string;
description: string;
recommendation: string;
constructor(luxStart: number, luxEnd: number, type: string, description: string, recommendation: string) {
this.luxStart = luxStart;
this.luxEnd = luxEnd;
this.type = type;
this.description = description;
this.recommendation = recommendation;
}
}
@Entry
@Component
struct LightIntensityMeter {
@State currentType: string = "";
@State currentIntensity: number = 0;
@State lightIntensityList: LightIntensityItem[] = [
new LightIntensityItem(0, 1, '极暗', '夜晚户外,几乎没有光源。', '不宜进行任何活动,适合完全休息。'),
new LightIntensityItem(1, 10, '很暗', '夜晚室内,只有微弱的灯光或月光。', '只适合睡觉,避免使用电子设备。'),
new LightIntensityItem(10, 50, '暗', '清晨或傍晚,自然光较弱。', '轻松休闲,避免长时间阅读,适合放松。'),
new LightIntensityItem(50, 100, '较暗', '白天阴天,室内光线柔和。', '日常生活,短时间阅读,适合轻度活动。'),
new LightIntensityItem(100, 300, '适中', '白天多云,室内光线适中。', '工作学习,适度阅读,适合大部分室内活动。'),
new LightIntensityItem(300, 500, '较亮', '白天晴朗,室内光线充足。', '正常工作学习,长时间阅读,适合大部分活动。'),
new LightIntensityItem(500, 1000, '亮', '阴天室外,自然光较强。', '户外活动,注意防晒,适合户外休闲。'),
new LightIntensityItem(1000, 100000, '爆表了', '夏季正午直射阳光,自然光极其强烈。',
'尽可能避免直视太阳,户外活动需戴太阳镜,注意防晒。'),
];
aboutToAppear(): void {
sensor.getSensorList((error: BusinessError) => {
if (error) {
console.error('获取传感器列表失败', error);
return;
}
this.startLightIntensityUpdates();
});
}
private startLightIntensityUpdates(): void {
sensor.on(sensor.SensorId.AMBIENT_LIGHT, (data) => {
console.info(`data.intensity: ${data.intensity}`);
this.currentIntensity = data.intensity;
for (const item of this.lightIntensityList) {
if (data.intensity >= item.luxStart && data.intensity <= item.luxEnd) {
this.currentType = item.type;
break;
}
}
}, { interval: 10000000 });
}
build() {
Column() {
Text("光强仪")
.width('100%')
.height(44)
.backgroundColor("#fe9900")
.textAlign(TextAlign.Center)
.fontColor(Color.White);
Row() {
Gauge({
value: this.currentIntensity > 1000 ? 1000 : this.currentIntensity,
min: 0,
max: 1000
}) {
Column() {
Text(`${Math.floor(this.currentIntensity)}`)
.fontSize(25)
.fontWeight(FontWeight.Medium)
.fontColor("#323232")
.height('30%')
.textAlign(TextAlign.Center)
.margin({ top: '22.2%' })
.textOverflow({ overflow: TextOverflow.Ellipsis })
.maxLines(1);
Text(`${this.currentType}`)
.fontSize(16)
.fontColor("#848484")
.fontWeight(FontWeight.Regular)
.width('47.4%')
.height('15%')
.textAlign(TextAlign.Center)
.backgroundColor("#e4e4e4")
.borderRadius(5);
}.width('100%');
}
.startAngle(225)
.endAngle(135)
.height(250)
.strokeWidth(18)
.description(null)
.trackShadow({ radius: 7, offsetX: 7, offsetY: 7 })
.padding({ top: 30 });
}.width('100%').justifyContent(FlexAlign.Center);
Column() {
ForEach(this.lightIntensityList, (item: LightIntensityItem, index: number) => {
Row() {
Text(`${item.luxStart}~${item.luxEnd}Lux `)
.fontSize('25lpx')
.textAlign(TextAlign.Start)
.fontColor("#3d3d3d")
.width('220lpx')
Text(`${item.description}\n${item.recommendation}`)
.fontSize('23lpx')
.textAlign(TextAlign.Start)
.fontColor("#3d3d3d")
.layoutWeight(1)
}.width('660lpx')
.padding({ bottom: 10, top: 10 })
.borderWidth({ bottom: 1 })
.borderColor("#737977");
});
}.width('100%');
}
.height('100%')
.width('100%');
}
}
所有评论(0)