|
|
@@ -1,5 +1,13 @@
|
|
|
import { compare } from 'bcrypt'
|
|
|
-import { forwardRef, Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common'
|
|
|
+import {
|
|
|
+ forwardRef,
|
|
|
+ Inject,
|
|
|
+ Injectable,
|
|
|
+ InternalServerErrorException,
|
|
|
+ Logger,
|
|
|
+ NotFoundException,
|
|
|
+ OnModuleInit
|
|
|
+} from '@nestjs/common'
|
|
|
import { PageRequest } from '../common/dto/page-request'
|
|
|
import { Device } from './entities/device.entity'
|
|
|
import { Pagination, paginate } from 'nestjs-typeorm-paginate'
|
|
|
@@ -312,4 +320,36 @@ export class DeviceService implements OnModuleInit {
|
|
|
status: DeviceTaskStatus.COMPLETE
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+ async findOnelineDevice(deviceId: string) {
|
|
|
+ const device = await this.deviceRepository.findOneBy({ id: deviceId })
|
|
|
+ if (!device) {
|
|
|
+ throw new NotFoundException('Device not found')
|
|
|
+ }
|
|
|
+ if (!device.online) {
|
|
|
+ throw new InternalServerErrorException('Device offline')
|
|
|
+ }
|
|
|
+ return device
|
|
|
+ }
|
|
|
+
|
|
|
+ async sendToDeviceWithAck(socketId: string, action: string, data: any) {
|
|
|
+ return await this.eventsGateway.sendForResult(
|
|
|
+ {
|
|
|
+ id: randomUUID(),
|
|
|
+ action,
|
|
|
+ data
|
|
|
+ },
|
|
|
+ socketId
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ async installApk(deviceId: string, apkUrl: string) {
|
|
|
+ const device = await this.findOnelineDevice(deviceId)
|
|
|
+ return await this.sendToDeviceWithAck(device.socketId, 'installApk', { apkUrl })
|
|
|
+ }
|
|
|
+
|
|
|
+ async runScript(deviceId: string, script: string) {
|
|
|
+ const device = await this.findOnelineDevice(deviceId)
|
|
|
+ return await this.sendToDeviceWithAck(device.socketId, 'runScript', { script })
|
|
|
+ }
|
|
|
}
|