Lufaneng 6 months ago
parent 381a58dc01
commit 9a3a0d27d2

@ -63,22 +63,24 @@ export struct AutoMode {
refreshData() {
this.isLoading = true;
// 1. Get Mode
this.m9zService.getDeviceMode({
onSuccess: (mode: number): void => {
this.currentMode = mode;
// 2. Get Tasks
this.getTasksForMode(this.selectedViewMode);
},
onError: (err: string): void => {
console.error('Get mode failed', err);
this.getTasksForMode(this.selectedViewMode);
}
this.fetchSunRiseTime(() => {
// 1. Get Mode
this.m9zService.getDeviceMode({
onSuccess: (mode: number): void => {
this.currentMode = mode;
// 2. Get Tasks
this.getTasksForMode(this.selectedViewMode);
},
onError: (err: string): void => {
console.error('Get mode failed', err);
this.getTasksForMode(this.selectedViewMode);
}
});
});
}
getTasksForMode(modeIndex: number) {
this.isLoading = true;
// this.isLoading = true; // Already loading
// 1. Start Task
this.m9zService.getTask(M9zCmd.StartTask, modeIndex, {
@ -120,24 +122,37 @@ export struct AutoMode {
});
}
fetchSunRiseTime() {
fetchSunRiseTime(callback?: () => void) {
console.info('AutoMode: Fetching SunRiseTime...');
this.m9zService.getSunRiseTime({
onSuccess: (data: SunRiseTimeData): void => {
console.info(`AutoMode: fetchSunRiseTime success. Sunrise: ${data.sunrise}, Sunset: ${data.sunset}`);
if (data.sunrise) this.sunriseTime = this.formatTimeOnly(data.sunrise);
if (data.sunset) this.sunsetTime = this.formatTimeOnly(data.sunset);
this.isLoading = false;
console.info(`AutoMode: Updated times - Sunrise: ${this.sunriseTime}, Sunset: ${this.sunsetTime}`);
// this.isLoading = false; // Don't stop loading here
if (callback) callback();
},
onError: (err: string): void => {
this.isLoading = false;
console.error('AutoMode: fetchSunRiseTime failed:', err);
// this.isLoading = false; // Don't stop loading here
if (callback) callback();
}
});
}
formatTimeOnly(date: Date): string {
const h = String(date.getHours()).padStart(2, '0');
const min = String(date.getMinutes()).padStart(2, '0');
const s = String(date.getSeconds()).padStart(2, '0');
return `${h}:${min}:${s}`;
console.info('AutoMode: formatTimeOnly input:', date);
try {
const h = String(date.getHours()).padStart(2, '0');
const min = String(date.getMinutes()).padStart(2, '0');
const s = String(date.getSeconds()).padStart(2, '0');
const result = `${h}:${min}:${s}`;
return result;
} catch (e) {
console.error('AutoMode: formatTimeOnly error:', e);
return '00:00:00';
}
}
changeViewMode(index: number) {
@ -159,7 +174,7 @@ export struct AutoMode {
}
// Compiler Logic
compileTask(task: ControlTask): TaskProgram {
compileTask(task: ControlTask, isMiddle: boolean = false): TaskProgram {
const instructions: TaskInstruction[] = [];
// 1. Time Instruction
@ -188,7 +203,15 @@ export struct AutoMode {
if (true) { // Always generate instruction if we have time
const ins1: TaskInstruction = new TaskInstruction();
ins1.type = 0x01;
ins1.param = task.timeType === 0 ? 0x01 : 0x02;
if (isMiddle) {
// Middle Task: Timer=0x05, Sun=0x04
ins1.param = task.timeType === 0 ? 0x05 : 0x04;
} else {
// Start/Stop Task: Timer=0x01, Sun=0x02
ins1.param = task.timeType === 0 ? 0x01 : 0x02;
}
ins1.value = minutes;
instructions.push(ins1);
}
@ -251,7 +274,7 @@ export struct AutoMode {
// Sort tasks by time? The user should ensure order, simplified here.
tasks.forEach((task: ControlTask) => {
const prog: TaskProgram = this.compileTask(task);
const prog: TaskProgram = this.compileTask(task, true);
prog.instructions.forEach((ins: TaskInstruction) => { allInstructions.push(ins); });
});
@ -471,6 +494,8 @@ export struct AutoMode {
TaskItem({
title: '开始任务',
task: this.startTask,
sunriseTime: this.sunriseTime,
sunsetTime: this.sunsetTime,
onEdit: (): void => { this.openEditDialog('start', -1); }
})
}
@ -480,6 +505,8 @@ export struct AutoMode {
TaskItem({
title: `中间任务 ${index+1}`,
task: task,
sunriseTime: this.sunriseTime,
sunsetTime: this.sunsetTime,
onEdit: (): void => { this.openEditDialog('middle', index); },
isDeletable: true,
onDelete: (): void => { this.deleteMiddleTask(index); }
@ -500,6 +527,8 @@ export struct AutoMode {
TaskItem({
title: '结束任务',
task: this.stopTask,
sunriseTime: this.sunriseTime,
sunsetTime: this.sunsetTime,
onEdit: (): void => { this.openEditDialog('stop', -1); }
})
}
@ -571,7 +600,14 @@ export struct AutoMode {
Button('经纬度时间')
.backgroundColor(this.editingTask.timeType === 1 ? '#22fdc8' : '#333')
.fontColor(this.editingTask.timeType === 1 ? '#000' : '#fff')
.onClick(() => { this.editingTask.timeType = 1; })
.onClick(() => {
this.editingTask.timeType = 1;
if (this.editingTaskType === 'start' && this.sunsetTime !== '--:--:--') {
this.editingTask.execTime = this.sunsetTime.substring(0, 5);
} else if (this.editingTaskType === 'stop' && this.sunriseTime !== '--:--:--') {
this.editingTask.execTime = this.sunriseTime.substring(0, 5);
}
})
.height(30).fontSize(12)
}.width('100%').justifyContent(FlexAlign.Start)
@ -607,6 +643,10 @@ export struct AutoMode {
.margin({left: 10}).fontSize(12).height(30).backgroundColor('#2d3a51')
.fontColor('#22fdc8').border({width:1, color: '#22fdc8'})
}.width('100%').justifyContent(FlexAlign.Start)
} else {
Text('执行时间').fontColor('#b2ebf2').margin({top: 15, bottom: 5}).alignSelf(ItemAlign.Start).fontSize(14)
Text(this.editingTask.execTime)
.fontColor('#22fdc8').fontSize(16).margin({top: 5})
}
// 3. Loop Selection
@ -691,10 +731,21 @@ export struct AutoMode {
struct TaskItem {
@Prop title: string;
@ObjectLink task: ControlTask;
@Prop sunriseTime: string = '--:--:--';
@Prop sunsetTime: string = '--:--:--';
onEdit: () => void = () => {};
@Prop isDeletable: boolean = false;
onDelete: () => void = () => {};
getDisplayTime(): string {
const rawTime = (this.task.execTime.includes(' ') ? this.task.execTime.split(' ')[1] : this.task.execTime).substring(0, 5);
if (this.task.timeType === 1) {
if (this.title === '开始任务' && this.sunsetTime !== '--:--:--') return this.sunsetTime.substring(0, 5);
if (this.title === '结束任务' && this.sunriseTime !== '--:--:--') return this.sunriseTime.substring(0, 5);
}
return rawTime;
}
build() {
Column() {
Row() {
@ -705,7 +756,7 @@ struct TaskItem {
.fontSize(10)
.fontColor('#aaa')
.alignSelf(ItemAlign.End)
Text((this.task.execTime.includes(' ') ? this.task.execTime.split(' ')[1] : this.task.execTime).substring(0, 5))
Text(this.getDisplayTime())
.fontColor('#fff')
.alignSelf(ItemAlign.End)
.onAppear(() => {

@ -470,13 +470,32 @@ export class M9zService {
private parseSunRiseTime(data: number[]): SunRiseTimeData {
console.info('M9zService: parseSunRiseTime raw data:', JSON.stringify(data));
const res = new SunRiseTimeData();
// 假设返回两个 uint32 时间戳 (日出,日落)
if (data.length >= 8) {
const riseTs = this.bytesToUint32LE(data.slice(0, 4));
const setTs = this.bytesToUint32LE(data.slice(4, 8));
res.sunrise = new Date(riseTs * 1000);
res.sunset = new Date(setTs * 1000);
console.info('M9zService: parsed sunrise:', res.sunrise, 'sunset:', res.sunset);
// Expect at least 4 bytes (2x uint16)
if (data.length >= 4) {
// 1. Sunrise (minutes from midnight)
const rawSunrise = data[0] | (data[1] << 8);
// 2. Sunset (minutes from midnight)
const rawSunset = data[2] | (data[3] << 8);
// Convert to Date for today
const now = new Date();
const year = now.getFullYear();
const month = now.getMonth();
const day = now.getDate();
// Calculate Sunrise Date
const riseHour = Math.floor(rawSunrise / 60);
const riseMin = rawSunrise % 60;
res.sunrise = new Date(year, month, day, riseHour, riseMin, 0);
// Calculate Sunset Date
const setHour = Math.floor(rawSunset / 60);
const setMin = rawSunset % 60;
res.sunset = new Date(year, month, day, setHour, setMin, 0);
console.info(`M9zService: parsed raw=${rawSunrise}/${rawSunset}, time=${res.sunrise.toTimeString()}/${res.sunset.toTimeString()}`);
} else {
console.warn('M9zService: parseSunRiseTime data length insufficient:', data.length);
}

Loading…
Cancel
Save