import { M9zService, LgnLatData, SunRiseSetData, SunRiseTimeData } from '../../utils/M9zService'; import { promptAction } from '@kit.ArkUI'; @Component export struct ParameterSettings { @Prop deviceId: string; @Prop @Watch('onTabChange') currentTabIndex: number = 0; @State isLoading: boolean = false; @State lgnLat: LgnLatData = new LgnLatData(); @State sunRiseSet: SunRiseSetData = new SunRiseSetData(); @State sunRiseTime: SunRiseTimeData = new SunRiseTimeData(); private m9zService: M9zService = M9zService.getInstance(); aboutToAppear() { if (this.deviceId) { // initial load handled by onTabChange? No, initial index might be 0, this is 1. IF this is 1 initially? // BluetoothControlPage sets index 0 default. // If user goes to 1, onTabChange fires. // If user starts at 0, this component might load but not show? // Let's rely on onTabChange mostly, but for initial: if (this.currentTabIndex === 1) { this.refreshData(); } } } onTabChange() { if (this.currentTabIndex === 1 && this.deviceId) { this.refreshData(); } } refreshData() { this.isLoading = true; // 1. Get LgnLat this.m9zService.getLgnLat({ onSuccess: (data: LgnLatData): void => { this.lgnLat = data; this.fetchSunRiseSet(); }, onError: (err: string): void => { // console.error('Get lgnlat failed', err); this.fetchSunRiseSet(); } }); } fetchSunRiseSet() { // 2. Get SunRiseSet this.m9zService.getSunRiseSet({ onSuccess: (data: SunRiseSetData): void => { this.sunRiseSet = data; this.fetchSunRiseTime(); }, onError: (err: string): void => { this.fetchSunRiseTime(); } }); } fetchSunRiseTime() { // 3. Get SunRiseTime this.m9zService.getSunRiseTime({ onSuccess: (data: SunRiseTimeData): void => { this.sunRiseTime = data; this.isLoading = false; }, onError: (err: string): void => { this.isLoading = false; } }); } syncLocation() { // 模拟同步定位,实际应调用定位API const newData = new LgnLatData(); newData.lgn = 113.123456; newData.lat = 23.123456; newData.loc = '广东省广州市'; this.m9zService.setLgnLat(newData, { onSuccess: (success: boolean): void => { this.lgnLat = newData; promptAction.showToast({ message: '同步成功' }); }, onError: (err: string): void => { promptAction.showToast({ message: '同步失败:' + err }); } }); } setLgnLat() { this.m9zService.setLgnLat(this.lgnLat, { onSuccess: (success: boolean): void => { promptAction.showToast({ message: '设置经纬度成功' }); }, onError: (err: string): void => { promptAction.showToast({ message: '设置失败:' + err }); } }); } setSunRiseSet() { this.m9zService.setSunRiseSet(this.sunRiseSet, { onSuccess: (success: boolean): void => { promptAction.showToast({ message: '设置偏差成功' }); }, onError: (err: string): void => { promptAction.showToast({ message: '设置失败:' + err }); } }); } restartDevice() { AlertDialog.show({ title: '确认', message: '确定要重启设备吗?', primaryButton: { value: '取消', action: (): void => {} }, secondaryButton: { value: '重启', action: (): void => { this.m9zService.restartDevice({ onSuccess: (val: boolean): void => { promptAction.showToast({ message: '发送重启命令成功' }); }, onError: (err: string): void => { promptAction.showToast({ message: '失败:' + err }); } }); } } }); } build() { Stack() { Scroll() { Column() { // 经纬度设置 Column() { Text('经纬度设置').fontSize(16).fontWeight(FontWeight.Bold).fontColor('#22fdc8').width('100%').margin({bottom: 10}) Row() { Text('经度: ').fontColor('#eaf6fc') TextInput({ text: this.lgnLat.lgn.toString() }) .width(100) .onChange((val) => { this.lgnLat.lgn = Number(val); }) .fontColor('#fff').backgroundColor('#333') }.width('100%').margin({bottom: 5}) Row() { Text('纬度: ').fontColor('#eaf6fc') TextInput({ text: this.lgnLat.lat.toString() }) .width(100) .onChange((val) => { this.lgnLat.lat = Number(val); }) .fontColor('#fff').backgroundColor('#333') }.width('100%').margin({bottom: 10}) Row() { Button('同步手机定位').fontSize(12).onClick(() => this.syncLocation()).margin({right: 10}) Button('保存').fontSize(12).onClick(() => this.setLgnLat()) } } .padding(10) .backgroundColor('rgba(20, 38, 81, 0.95)') .borderRadius(8) .margin({bottom: 15}) .width('100%') // 日出日落偏差 Column() { Text('日出日落偏差 (分钟)').fontSize(16).fontWeight(FontWeight.Bold).fontColor('#22fdc8').width('100%').margin({bottom: 10}) Row() { Text('日出偏差: ').fontColor('#eaf6fc') TextInput({ text: this.sunRiseSet.rise.toString() }) .width(80) .onChange((val) => { this.sunRiseSet.rise = Number(val); }) .fontColor('#fff').backgroundColor('#333') }.width('100%').margin({bottom: 5}) Row() { Text('日落偏差: ').fontColor('#eaf6fc') TextInput({ text: this.sunRiseSet.set.toString() }) .width(80) .onChange((val) => { this.sunRiseSet.set = Number(val); }) .fontColor('#fff').backgroundColor('#333') }.width('100%').margin({bottom: 10}) Row() { Button('保存').fontSize(12).onClick(() => this.setSunRiseSet()) } } .padding(10) .backgroundColor('rgba(20, 38, 81, 0.95)') .borderRadius(8) .margin({bottom: 15}) .width('100%') // 当前日出日落时间 Column() { Text('今日光照时间').fontSize(16).fontWeight(FontWeight.Bold).fontColor('#22fdc8').width('100%').margin({bottom: 10}) Text(`日出: ${this.sunRiseTime.sunrise ? this.formatTime(this.sunRiseTime.sunrise) : '--:--'}`).fontColor('#eaf6fc') Text(`日落: ${this.sunRiseTime.sunset ? this.formatTime(this.sunRiseTime.sunset) : '--:--'}`).fontColor('#eaf6fc') } .padding(10) .backgroundColor('rgba(20, 38, 81, 0.95)') .borderRadius(8) .margin({bottom: 15}) .width('100%') // 重启 Button('重启设备') .backgroundColor('#fa5050') .width('80%') .margin({top: 20}) .onClick(() => this.restartDevice()) }.width('100%').padding(10) } if (this.isLoading) { Stack() { Rect().width('100%').height('100%').fill('rgba(0,0,0,0.5)') Column() { LoadingProgress().width(50).height(50).color('#22fdc8') Text('数据加载中...').fontColor('white').margin({top: 10}) } } .width('100%').height('100%') .zIndex(1000) } } } formatTime(date: Date): string { const h = String(date.getHours()).padStart(2, '0'); const m = String(date.getMinutes()).padStart(2, '0'); return `${h}:${m}`; } }