mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
Merge pull request #80 from SyncrowIOT/door-lock-open-door
Add Default Door Lock Password and Open Door Lock Endpoint
This commit is contained in:
1
libs/common/src/constants/default.door-lock-pass.ts
Normal file
1
libs/common/src/constants/default.door-lock-pass.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const defaultDoorLockPass = '1233214';
|
@ -211,4 +211,23 @@ export class DoorLockController {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ApiBearerAuth()
|
||||||
|
@UseGuards(JwtAuthGuard)
|
||||||
|
@Post('open/:doorLockUuid')
|
||||||
|
async openDoorLock(@Param('doorLockUuid') doorLockUuid: string) {
|
||||||
|
try {
|
||||||
|
await this.doorLockService.openDoorLock(doorLockUuid);
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'door lock opened successfully',
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
throw new HttpException(
|
||||||
|
error.message || 'Internal server error',
|
||||||
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import { PasswordEncryptionService } from './encryption.services';
|
|||||||
import { AddDoorLockOfflineTempMultipleTimeDto } from '../dtos/add.offline-temp.dto';
|
import { AddDoorLockOfflineTempMultipleTimeDto } from '../dtos/add.offline-temp.dto';
|
||||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||||
import { UpdateDoorLockOfflineTempDto } from '../dtos/update.offline-temp.dto';
|
import { UpdateDoorLockOfflineTempDto } from '../dtos/update.offline-temp.dto';
|
||||||
|
import { defaultDoorLockPass } from '@app/common/constants/default.door-lock-pass';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DoorLockService {
|
export class DoorLockService {
|
||||||
@ -639,4 +640,56 @@ export class DoorLockService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async openDoorLock(
|
||||||
|
doorLockUuid: string,
|
||||||
|
): Promise<deleteTemporaryPasswordInterface> {
|
||||||
|
try {
|
||||||
|
// Fetch ticket and encrypted password
|
||||||
|
const { ticketKey, encryptedPassword, deviceTuyaUuid, ticketId } =
|
||||||
|
await this.getTicketAndEncryptedPassword(
|
||||||
|
doorLockUuid,
|
||||||
|
defaultDoorLockPass,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Validate ticket and device Tuya UUID
|
||||||
|
if (!ticketKey || !encryptedPassword || !deviceTuyaUuid) {
|
||||||
|
throw new HttpException(
|
||||||
|
'Invalid Ticket or Device UUID',
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve device details
|
||||||
|
const deviceDetails = await this.getDeviceByDeviceUuid(doorLockUuid);
|
||||||
|
|
||||||
|
// Validate device details
|
||||||
|
if (!deviceDetails?.deviceTuyaUuid) {
|
||||||
|
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (deviceDetails.productDevice.prodType !== ProductType.DL) {
|
||||||
|
throw new HttpException(
|
||||||
|
'This is not a door lock device',
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct the path for the Tuya API request
|
||||||
|
const path = `/v1.0/devices/${deviceDetails.deviceTuyaUuid}/door-lock/password-free/open-door`;
|
||||||
|
|
||||||
|
// Make the Tuya API request to open the door
|
||||||
|
const response = await this.tuya.request({
|
||||||
|
method: 'POST',
|
||||||
|
path,
|
||||||
|
body: { ticket_id: ticketId },
|
||||||
|
});
|
||||||
|
|
||||||
|
return response as deleteTemporaryPasswordInterface;
|
||||||
|
} catch (error) {
|
||||||
|
const status = error.status || HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
|
const message = error.message || 'Error opening door lock from Tuya';
|
||||||
|
|
||||||
|
throw new HttpException(message, status);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user