@omar#4289 same as, I am getting the
@omar same as, I am getting the same issue, I have explained my issue






router.get('/water/start', async (request: Request, env: Env) => {
const doId = env.WATER.idFromName('water_instance');
const WaterInstance = env.WATER.get(doId);
return WaterInstance.fetch(request);
});
router.get('/water/stop', async (request: Request, env: Env) => {
const doId = env.WATER.idFromName('water_instance');
const WaterInstance = env.WATER.get(doId);
return WaterInstance.fetch(request);
});export class Water {
id: string | DurableObjectId;
storage: DurableObjectStorage;
doEverySeconds: number;
env: Env;
constructor(state: DurableObjectState, env: Env) {
this.storage = state.storage;
this.id = state.id;
this.doEverySeconds = 2;
this.env = env;
}
async fetch(request: Request) {
const url = new URL(request.url);
const action = url.pathname.split('/')[2];
switch (action) {
case 'start':
const alarmTime = await this.storage.get('alarmTime');
if (!alarmTime) {
await this.scheduleAlarm();
return new Response('Water schedule Successfully!');
} else {
return new Response('Water alarm is already scheduled.');
}
case 'stop':
await this.storage.deleteAlarm();
await this.storage.delete('alarmTime');
return new Response("Water alarm stopped");
default:
return new Response('', { status: 404 });
}
}
async alarm() {
console.log('Water Alarm Doing');
await this.scheduleAlarm();
}
async scheduleAlarm() {
let scheduledTime: number = Date.now() + this.doEverySeconds * 1000;
await this.storage.put('alarmTime', scheduledTime.toString());
await this.storage.setAlarm(scheduledTime);
}
}