mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 13:04:54 +00:00
Fixed scene
This commit is contained in:
2
libs/common/src/integrations/tuya/interfaces/index.ts
Normal file
2
libs/common/src/integrations/tuya/interfaces/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './tuya.response.interface';
|
||||
export * from './tap-to-run-action.interface';
|
||||
@ -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;
|
||||
}
|
||||
@ -0,0 +1,5 @@
|
||||
export interface TuyaResponseInterface {
|
||||
success: boolean;
|
||||
msg?: string;
|
||||
result: boolean;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user