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:
faris Aljohari
2024-08-12 11:47:40 +03:00
committed by GitHub
3 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1 @@
export const defaultDoorLockPass = '1233214';

View File

@ -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,
);
}
}
}

View File

@ -16,6 +16,7 @@ import { PasswordEncryptionService } from './encryption.services';
import { AddDoorLockOfflineTempMultipleTimeDto } from '../dtos/add.offline-temp.dto';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { UpdateDoorLockOfflineTempDto } from '../dtos/update.offline-temp.dto';
import { defaultDoorLockPass } from '@app/common/constants/default.door-lock-pass';
@Injectable()
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);
}
}
}