mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 22:34:54 +00:00
Add endpoint to update device firmware
This commit is contained in:
@ -152,6 +152,25 @@ export class DeviceController {
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@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')
|
||||
async getDevicesInGetaway(@Param('gatewayUuid') gatewayUuid: string) {
|
||||
try {
|
||||
|
||||
@ -59,3 +59,8 @@ export interface DeviceInstructionResponse {
|
||||
dataType: string;
|
||||
}[];
|
||||
}
|
||||
export interface updateDeviceFirmwareInterface {
|
||||
success: boolean;
|
||||
result: boolean;
|
||||
msg: string;
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import {
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
NotFoundException,
|
||||
BadRequestException,
|
||||
} from '@nestjs/common';
|
||||
import { TuyaContext } from '@tuya/tuya-connector-nodejs';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
@ -17,6 +18,7 @@ import {
|
||||
GetDeviceDetailsFunctionsStatusInterface,
|
||||
GetDeviceDetailsInterface,
|
||||
controlDeviceInterface,
|
||||
updateDeviceFirmwareInterface,
|
||||
} from '../interfaces/get.device.interface';
|
||||
import {
|
||||
GetDeviceByGroupIdDto,
|
||||
@ -26,7 +28,7 @@ import { ControlDeviceDto } from '../dtos/control.device.dto';
|
||||
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
||||
import { DeviceRepository } from '@app/common/modules/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()
|
||||
export class DeviceService {
|
||||
@ -163,6 +165,7 @@ export class DeviceService {
|
||||
try {
|
||||
const deviceDetails = await this.getDeviceByDeviceUuid(
|
||||
controlDeviceDto.deviceUuid,
|
||||
false,
|
||||
);
|
||||
|
||||
if (!deviceDetails || !deviceDetails.deviceTuyaUuid) {
|
||||
@ -362,7 +365,7 @@ export class DeviceService {
|
||||
if (!deviceDetails) {
|
||||
throw new NotFoundException('Device Not Found');
|
||||
} 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(
|
||||
@ -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({
|
||||
where: {
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user