Add endpoint to update device firmware

This commit is contained in:
faris Aljohari
2024-05-18 20:23:17 +03:00
parent 6b881ce01e
commit cd40dc897b
4 changed files with 78 additions and 4 deletions

View File

@ -152,6 +152,25 @@ export class DeviceController {
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post(':deviceUuid/firmware/:firmwareVersion')
async updateDeviceFirmware(
@Param('deviceUuid') deviceUuid: string,
@Param('firmwareVersion') firmwareVersion: number,
) {
try {
return await this.deviceService.updateDeviceFirmware(
deviceUuid,
firmwareVersion,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('getaway/:gatewayUuid/devices') @Get('getaway/:gatewayUuid/devices')
async getDevicesInGetaway(@Param('gatewayUuid') gatewayUuid: string) { async getDevicesInGetaway(@Param('gatewayUuid') gatewayUuid: string) {
try { try {

View File

@ -59,3 +59,8 @@ export interface DeviceInstructionResponse {
dataType: string; dataType: string;
}[]; }[];
} }
export interface updateDeviceFirmwareInterface {
success: boolean;
result: boolean;
msg: string;
}

View File

@ -4,6 +4,7 @@ import {
HttpException, HttpException,
HttpStatus, HttpStatus,
NotFoundException, NotFoundException,
BadRequestException,
} from '@nestjs/common'; } from '@nestjs/common';
import { TuyaContext } from '@tuya/tuya-connector-nodejs'; import { TuyaContext } from '@tuya/tuya-connector-nodejs';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
@ -17,6 +18,7 @@ import {
GetDeviceDetailsFunctionsStatusInterface, GetDeviceDetailsFunctionsStatusInterface,
GetDeviceDetailsInterface, GetDeviceDetailsInterface,
controlDeviceInterface, controlDeviceInterface,
updateDeviceFirmwareInterface,
} from '../interfaces/get.device.interface'; } from '../interfaces/get.device.interface';
import { import {
GetDeviceByGroupIdDto, GetDeviceByGroupIdDto,
@ -26,7 +28,7 @@ import { ControlDeviceDto } from '../dtos/control.device.dto';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter'; import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { DeviceRepository } from '@app/common/modules/device/repositories'; import { DeviceRepository } from '@app/common/modules/device/repositories';
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories'; import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
import { ProductType } from '@app/common/constants/permission-type.enum copy'; import { ProductType } from '@app/common/constants/product-type.enum';
@Injectable() @Injectable()
export class DeviceService { export class DeviceService {
@ -163,6 +165,7 @@ export class DeviceService {
try { try {
const deviceDetails = await this.getDeviceByDeviceUuid( const deviceDetails = await this.getDeviceByDeviceUuid(
controlDeviceDto.deviceUuid, controlDeviceDto.deviceUuid,
false,
); );
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) { if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
@ -362,7 +365,7 @@ export class DeviceService {
if (!deviceDetails) { if (!deviceDetails) {
throw new NotFoundException('Device Not Found'); throw new NotFoundException('Device Not Found');
} else if (deviceDetails.productDevice.prodType !== ProductType.GW) { } else if (deviceDetails.productDevice.prodType !== ProductType.GW) {
throw new NotFoundException('This is not a gateway device'); throw new BadRequestException('This is not a gateway device');
} }
const response = await this.getDevicesInGetawayTuya( const response = await this.getDevicesInGetawayTuya(
@ -407,12 +410,59 @@ export class DeviceService {
} }
} }
private async getDeviceByDeviceUuid(deviceUuid: string) { private async getDeviceByDeviceUuid(
deviceUuid: string,
withProductDevice: boolean = true,
) {
return await this.deviceRepository.findOne({ return await this.deviceRepository.findOne({
where: { where: {
uuid: deviceUuid, uuid: deviceUuid,
}, },
relations: ['productDevice'], ...(withProductDevice && { relations: ['productDevice'] }),
}); });
} }
async updateDeviceFirmware(deviceUuid: string, firmwareVersion: number) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid, false);
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
throw new NotFoundException('Device Not Found');
}
const response = await this.updateDeviceFirmwareTuya(
deviceDetails.deviceTuyaUuid,
firmwareVersion,
);
if (response.success) {
return response;
} else {
throw new HttpException(
response.msg || 'Unknown error',
HttpStatus.BAD_REQUEST,
);
}
} catch (error) {
throw new HttpException('Device Not Found', HttpStatus.NOT_FOUND);
}
}
async updateDeviceFirmwareTuya(
deviceUuid: string,
firmwareVersion: number,
): Promise<updateDeviceFirmwareInterface> {
try {
const path = `/v2.0/cloud/thing/${deviceUuid}/firmware/${firmwareVersion}`;
const response = await this.tuya.request({
method: 'POST',
path,
});
return response as updateDeviceFirmwareInterface;
} catch (error) {
throw new HttpException(
'Error updating device firmware from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
} }