Fixed scene

This commit is contained in:
hannathkadher
2024-11-02 19:36:56 +04:00
parent 2b6168058a
commit 7e9894b1d3
22 changed files with 672 additions and 413 deletions

View File

@ -7,7 +7,7 @@ import { ConfigModule } from '@nestjs/config';
import config from './config';
import { EmailService } from './util/email.service';
import { ErrorMessageService } from 'src/error-message/error-message.service';
import { TuyaService } from './integrations/tuya/tuya.service';
import { TuyaService } from './integrations/tuya/services/tuya.service';
@Module({
providers: [CommonService, EmailService, ErrorMessageService, TuyaService],
exports: [

View File

@ -80,6 +80,36 @@ export class ControllerRoute {
};
};
static SCENE = class {
public static readonly ROUTE = 'scene';
static ACTIONS = class {
public static readonly CREATE_TAP_TO_RUN_SCENE_SUMMARY =
'Create a Tap-to-Run Scene';
public static readonly CREATE_TAP_TO_RUN_SCENE_DESCRIPTION =
'Creates a new Tap-to-Run scene in Tuya and stores the scene in the local database.';
public static readonly DELETE_TAP_TO_RUN_SCENE_SUMMARY =
'Delete a Tap-to-Run Scene';
public static readonly DELETE_TAP_TO_RUN_SCENE_DESCRIPTION =
'Deletes a Tap-to-Run scene from Tuya and removes it from the local database.';
public static readonly TRIGGER_TAP_TO_RUN_SCENE_SUMMARY =
'Trigger a Tap-to-Run Scene';
public static readonly TRIGGER_TAP_TO_RUN_SCENE_DESCRIPTION =
'Triggers an existing Tap-to-Run scene in Tuya by scene UUID, executing its actions immediately.';
public static readonly GET_TAP_TO_RUN_SCENE_SUMMARY =
'Get Tap-to-Run Scene Details';
public static readonly GET_TAP_TO_RUN_SCENE_DESCRIPTION =
'Retrieves detailed information of a specific Tap-to-Run scene identified by the scene UUID.';
public static readonly UPDATE_TAP_TO_RUN_SCENE_SUMMARY =
'Update a Tap-to-Run Scene';
public static readonly UPDATE_TAP_TO_RUN_SCENE_DESCRIPTION =
'Updates an existing Tap-to-Run scene in Tuya and updates the scene in the local database, reflecting any new configurations or actions.';
};
};
static SPACE = class {
public static readonly ROUTE = '/communities/:communityUuid/spaces';
static ACTIONS = class {
@ -120,6 +150,17 @@ export class ControllerRoute {
};
};
static SPACE_SCENE = class {
public static readonly ROUTE =
'/communities/:communityUuid/spaces/:spaceUuid/scenes';
static ACTIONS = class {
public static readonly GET_TAP_TO_RUN_SCENE_BY_SPACE_SUMMARY =
'Retrieve Tap-to-Run Scenes by Space';
public static readonly GET_TAP_TO_RUN_SCENE_BY_SPACE_DESCRIPTION =
'Fetches all Tap-to-Run scenes associated with a specified space UUID. An optional query parameter can filter the results to show only scenes marked for the homepage display.';
};
};
static SPACE_USER = class {
public static readonly ROUTE =
'/communities/:communityUuid/spaces/:spaceUuid/user';

View File

@ -0,0 +1,2 @@
export * from './tuya.response.interface';
export * from './tap-to-run-action.interface';

View File

@ -0,0 +1,11 @@
export interface ConvertedExecutorProperty {
function_code?: string;
function_value?: any;
delay_seconds?: number;
}
export interface ConvertedAction {
entity_id: string;
action_executor: string;
executor_property?: ConvertedExecutorProperty;
}

View File

@ -0,0 +1,5 @@
export interface TuyaResponseInterface {
success: boolean;
msg?: string;
result: boolean;
}

View File

@ -1,6 +1,7 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConvertedAction, TuyaResponseInterface } from '../interfaces';
@Injectable()
export class TuyaService {
@ -86,4 +87,50 @@ export class TuyaService {
);
}
}
async addTapToRunScene(
spaceId: string,
sceneName: string,
actions: ConvertedAction[],
decisionExpr: string,
) {
const path = `/v2.0/cloud/scene/rule`;
const response = await this.tuya.request({
method: 'POST',
path,
body: {
space_id: spaceId,
name: sceneName,
type: 'scene',
decision_expr: decisionExpr,
actions: actions,
},
});
if (response.success) {
return response;
} else {
throw new HttpException(
`Error fetching scene rule: ${response.msg}`,
HttpStatus.BAD_REQUEST,
);
}
}
async triggerScene(sceneId: string): Promise<TuyaResponseInterface> {
const path = `/v2.0/cloud/scene/rule/${sceneId}/actions/trigger`;
const response: TuyaResponseInterface = await this.tuya.request({
method: 'POST',
path,
});
if (!response.success) {
throw new HttpException(
response.msg || 'Error triggering scene',
HttpStatus.BAD_REQUEST,
);
}
return response;
}
}

View File

@ -36,8 +36,6 @@ export function buildTypeORMIncludeQuery(
}
});
console.log(`Including relations for ${modelName}:`, relations);
return relations;
}