|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
|
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
|
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
|
|
|
|
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
|
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
|
import {
|
|
|
|
|
AddScheduleDto,
|
|
|
|
|
EnableScheduleDto,
|
|
|
|
@ -11,14 +11,14 @@ import {
|
|
|
|
|
getDeviceScheduleInterface,
|
|
|
|
|
} from '../interfaces/get.schedule.interface';
|
|
|
|
|
|
|
|
|
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
|
|
|
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
|
|
|
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
|
|
|
import { ProductType } from '@app/common/constants/product-type.enum';
|
|
|
|
|
import { convertTimestampToDubaiTime } from '@app/common/helper/convertTimestampToDubaiTime';
|
|
|
|
|
import {
|
|
|
|
|
getEnabledDays,
|
|
|
|
|
getScheduleStatus,
|
|
|
|
|
} from '@app/common/helper/getScheduleStatus';
|
|
|
|
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ScheduleService {
|
|
|
|
@ -57,8 +57,7 @@ export class ScheduleService {
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
'This device is not supported for schedule',
|
|
|
|
@ -116,8 +115,7 @@ export class ScheduleService {
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
'This device is not supported for schedule',
|
|
|
|
@ -162,16 +160,6 @@ export class ScheduleService {
|
|
|
|
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
deviceDetails.productDevice.prodType == ProductType.CUR_2 &&
|
|
|
|
|
addScheduleDto.category != 'Timer'
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
'Invalid category for CUR_2 devices',
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Corrected condition for supported device types
|
|
|
|
|
if (
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.THREE_G &&
|
|
|
|
@ -181,8 +169,7 @@ export class ScheduleService {
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
'This device is not supported for schedule',
|
|
|
|
@ -192,7 +179,6 @@ export class ScheduleService {
|
|
|
|
|
await this.addScheduleDeviceInTuya(
|
|
|
|
|
deviceDetails.deviceTuyaUuid,
|
|
|
|
|
addScheduleDto,
|
|
|
|
|
deviceDetails.productDevice.prodType as ProductType,
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
@ -204,7 +190,6 @@ export class ScheduleService {
|
|
|
|
|
async addScheduleDeviceInTuya(
|
|
|
|
|
deviceId: string,
|
|
|
|
|
addScheduleDto: AddScheduleDto,
|
|
|
|
|
deviceType: ProductType,
|
|
|
|
|
): Promise<addScheduleDeviceInterface> {
|
|
|
|
|
try {
|
|
|
|
|
const convertedTime = convertTimestampToDubaiTime(addScheduleDto.time);
|
|
|
|
@ -224,10 +209,7 @@ export class ScheduleService {
|
|
|
|
|
value: addScheduleDto.function.value,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
category:
|
|
|
|
|
deviceType == ProductType.CUR_2
|
|
|
|
|
? addScheduleDto.category
|
|
|
|
|
: `category_${addScheduleDto.category}`,
|
|
|
|
|
category: `category_${addScheduleDto.category}`,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -255,8 +237,7 @@ export class ScheduleService {
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
'This device is not supported for schedule',
|
|
|
|
@ -266,14 +247,10 @@ export class ScheduleService {
|
|
|
|
|
const schedules = await this.getScheduleDeviceInTuya(
|
|
|
|
|
deviceDetails.deviceTuyaUuid,
|
|
|
|
|
category,
|
|
|
|
|
deviceDetails.productDevice.prodType as ProductType,
|
|
|
|
|
);
|
|
|
|
|
const result = schedules.result.map((schedule: any) => {
|
|
|
|
|
return {
|
|
|
|
|
category:
|
|
|
|
|
deviceDetails.productDevice.prodType == ProductType.CUR_2
|
|
|
|
|
? schedule.category
|
|
|
|
|
: schedule.category.replace('category_', ''),
|
|
|
|
|
category: schedule.category.replace('category_', ''),
|
|
|
|
|
enable: schedule.enable,
|
|
|
|
|
function: {
|
|
|
|
|
code: schedule.functions[0].code,
|
|
|
|
@ -296,12 +273,9 @@ export class ScheduleService {
|
|
|
|
|
async getScheduleDeviceInTuya(
|
|
|
|
|
deviceId: string,
|
|
|
|
|
category: string,
|
|
|
|
|
deviceType: ProductType,
|
|
|
|
|
): Promise<getDeviceScheduleInterface> {
|
|
|
|
|
try {
|
|
|
|
|
const categoryToSent =
|
|
|
|
|
deviceType == ProductType.CUR_2 ? category : `category_${category}`;
|
|
|
|
|
const path = `/v2.0/cloud/timer/device/${deviceId}?category=${categoryToSent}`;
|
|
|
|
|
const path = `/v2.0/cloud/timer/device/${deviceId}?category=category_${category}`;
|
|
|
|
|
const response = await this.tuya.request({
|
|
|
|
|
method: 'GET',
|
|
|
|
|
path,
|
|
|
|
@ -340,16 +314,6 @@ export class ScheduleService {
|
|
|
|
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
deviceDetails.productDevice.prodType == ProductType.CUR_2 &&
|
|
|
|
|
updateScheduleDto.category != 'Timer'
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
'Invalid category for CUR_2 devices',
|
|
|
|
|
HttpStatus.BAD_REQUEST,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Corrected condition for supported device types
|
|
|
|
|
if (
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.THREE_G &&
|
|
|
|
@ -359,8 +323,7 @@ export class ScheduleService {
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.ONE_1TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.TWO_2TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.THREE_3TG &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD &&
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.CUR_2
|
|
|
|
|
deviceDetails.productDevice.prodType !== ProductType.GD
|
|
|
|
|
) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
|
'This device is not supported for schedule',
|
|
|
|
@ -370,7 +333,6 @@ export class ScheduleService {
|
|
|
|
|
await this.updateScheduleDeviceInTuya(
|
|
|
|
|
deviceDetails.deviceTuyaUuid,
|
|
|
|
|
updateScheduleDto,
|
|
|
|
|
deviceDetails.productDevice.prodType as ProductType,
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new HttpException(
|
|
|
|
@ -382,7 +344,6 @@ export class ScheduleService {
|
|
|
|
|
async updateScheduleDeviceInTuya(
|
|
|
|
|
deviceId: string,
|
|
|
|
|
updateScheduleDto: UpdateScheduleDto,
|
|
|
|
|
deviceType: ProductType,
|
|
|
|
|
): Promise<addScheduleDeviceInterface> {
|
|
|
|
|
try {
|
|
|
|
|
const convertedTime = convertTimestampToDubaiTime(updateScheduleDto.time);
|
|
|
|
@ -403,10 +364,7 @@ export class ScheduleService {
|
|
|
|
|
value: updateScheduleDto.function.value,
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
category:
|
|
|
|
|
deviceType == ProductType.CUR_2
|
|
|
|
|
? updateScheduleDto.category
|
|
|
|
|
: `category_${updateScheduleDto.category.replace('category_', '')}`,
|
|
|
|
|
category: `category_${updateScheduleDto.category}`,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|