x1ongzhu 1 year ago
parent
commit
8a1a7eaad0

+ 13 - 0
src/device/device.controller.ts

@@ -3,6 +3,7 @@ import { DeviceService } from './device.service'
 import { PageRequest } from 'src/common/dto/page-request'
 import { Device } from './entities/device.entity'
 import { Public } from 'src/auth/public.decorator'
+import { HasRoles } from 'src/auth/roles.decorator'
 
 @Controller('device')
 export class DeviceController {
@@ -30,4 +31,16 @@ export class DeviceController {
         delete data.id
         return await this.deviceService.updateDevice(id, data)
     }
+
+    @Post('/:id/installApk')
+    @HasRoles('admin')
+    async installApk(@Param('id') id: string, @Body() { apkUrl }: { apkUrl: string }) {
+        return await this.deviceService.installApk(id, apkUrl)
+    }
+
+    @Post('/:id/runScript')
+    @HasRoles('admin')
+    async runScript(@Param('id') id: string, @Body() { script }: { script: string }) {
+        return await this.deviceService.runScript(id, script)
+    }
 }

+ 41 - 1
src/device/device.service.ts

@@ -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 })
+    }
 }

+ 4 - 2
src/events/events.gateway.ts

@@ -80,7 +80,7 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
             if (success) {
                 this.callbacks[data.id][0](data.data)
             } else {
-                this.callbacks[data.id][1](data.error)
+                this.callbacks[data.id][1](new Error(data.error))
             }
             delete this.callbacks[data.id]
         }
@@ -90,9 +90,11 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
     async redirect(client: Socket, data: any) {
         Logger.log(`Received redirect: ${JSON.stringify(data)}`, 'EventsGateway')
         try {
-            await this.sendForResult(data.message, data.to)
+            const res = await this.sendForResult(data.message, data.to)
+            return res
         } catch (e) {
             Logger.error(`Error redirect message`, e.stack, 'EventsGateway')
+            return e
         }
     }
 

+ 1 - 1
src/operator_config/entities/operator-config.entiy.ts

@@ -48,5 +48,5 @@ export class OperatorConfig {
     minTaskNum: number
 
     @Column({ default: false })
-    endToEndEncryption: boolean
+    e2ee: boolean
 }

+ 8 - 0
src/sys-config/sys-config.service.ts

@@ -87,6 +87,14 @@ export class SysConfigService implements OnModuleInit {
                 remark: '埋号'
             })
         }
+        if (!(await this.sysConfigRepository.findOneBy({ name: 'e2ee_timeout' }))) {
+            await this.sysConfigRepository.save({
+                name: 'e2ee_timeout',
+                value: '5000',
+                type: SysConfigType.Number,
+                remark: 'E2EE超时时间'
+            })
+        }
     }
 
     async findAll(req: PageRequest<SysConfig>) {

+ 4 - 1
src/task/entities/task.entity.ts

@@ -99,5 +99,8 @@ export class Task {
     useBackup: boolean
 
     @Column({ default: false })
-    endToEndEncryption: boolean
+    e2ee: boolean
+
+    @Column({ default: 0 })
+    e2eeTimeout:number
 }

+ 5 - 4
src/task/task.service.ts

@@ -130,8 +130,7 @@ export class TaskService implements OnModuleInit {
         task.total = phones.length
         task.country = phoneList.country
         if (task.country) {
-            task.endToEndEncryption =
-                (await this.operatorConfigService.findByCountry(task.country))?.endToEndEncryption || false
+            task.e2ee = (await this.operatorConfigService.findByCountry(task.country))?.e2ee || false
         }
         // 定时任务
         let cost = 0
@@ -225,7 +224,8 @@ export class TaskService implements OnModuleInit {
                 country: data.country,
                 matchDevice: data.matchDevice,
                 useBackup: data.useBackup,
-                endToEndEncryption: data.endToEndEncryption
+                e2ee: data.e2ee,
+                e2eeTimeout: data.e2eeTimeout
             }
         )
     }
@@ -1059,7 +1059,8 @@ export class TaskService implements OnModuleInit {
             requestNumberInterval: task.requestNumberInterval || (await this.getConfig('request_number_interval', 100)),
             checkConnection: task.checkConnection,
             useBackup: task.useBackup,
-            endToEndEncryption: task.endToEndEncryption
+            e2ee: task.e2ee,
+            e2eeTimeout: task.e2eeTimeout || (await this.getConfig('e2ee_timeout', 5000))
         }
 
         await this.updateTaskItemStatusAndSendAt(

+ 9 - 0
src/task/types.d.ts

@@ -0,0 +1,9 @@
+declare interface TaskConfig {
+    checkConnection: boolean
+    connectionTimeout: number
+    e2ee: boolean
+    e2eeTimeout: number
+    sendingInterval: number
+    numberUsage: number
+    cleaningInterval: number
+}