Lufaneng 6 months ago
parent 381a58dc01
commit 9a3a0d27d2

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

@ -470,13 +470,32 @@ export class M9zService {
private parseSunRiseTime(data: number[]): SunRiseTimeData { private parseSunRiseTime(data: number[]): SunRiseTimeData {
console.info('M9zService: parseSunRiseTime raw data:', JSON.stringify(data)); console.info('M9zService: parseSunRiseTime raw data:', JSON.stringify(data));
const res = new SunRiseTimeData(); const res = new SunRiseTimeData();
// 假设返回两个 uint32 时间戳 (日出,日落)
if (data.length >= 8) { // Expect at least 4 bytes (2x uint16)
const riseTs = this.bytesToUint32LE(data.slice(0, 4)); if (data.length >= 4) {
const setTs = this.bytesToUint32LE(data.slice(4, 8)); // 1. Sunrise (minutes from midnight)
res.sunrise = new Date(riseTs * 1000); const rawSunrise = data[0] | (data[1] << 8);
res.sunset = new Date(setTs * 1000);
console.info('M9zService: parsed sunrise:', res.sunrise, 'sunset:', res.sunset); // 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 { } else {
console.warn('M9zService: parseSunRiseTime data length insufficient:', data.length); console.warn('M9zService: parseSunRiseTime data length insufficient:', data.length);
} }

Loading…
Cancel
Save