|
|
@@ -1,17 +1,25 @@
|
|
|
-import { Injectable } from '@nestjs/common'
|
|
|
+import { Injectable, Logger } from '@nestjs/common'
|
|
|
import { PageRequest } from '../common/dto/page-request'
|
|
|
import { Device } from './entities/device.entity'
|
|
|
import { Pagination, paginate } from 'nestjs-typeorm-paginate'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
import { In, Like, Repository } from 'typeorm'
|
|
|
+import { IpmoyuProvider } from '../proxy-provider/ipmoyu-provider'
|
|
|
|
|
|
@Injectable()
|
|
|
export class DeviceService {
|
|
|
+ private ipmoyuProvider: ProxyProvider = new IpmoyuProvider()
|
|
|
constructor(
|
|
|
@InjectRepository(Device)
|
|
|
private deviceRepository: Repository<Device>
|
|
|
) {}
|
|
|
|
|
|
+ async findById(id: string) {
|
|
|
+ return await this.deviceRepository.findOneBy({
|
|
|
+ id
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
async findAllDevice(req: PageRequest<Device>): Promise<Pagination<Device>> {
|
|
|
return await paginate<Device>(this.deviceRepository, req.page, req.search)
|
|
|
}
|
|
|
@@ -51,13 +59,39 @@ export class DeviceService {
|
|
|
}
|
|
|
|
|
|
async updateDevice(socketId: string, data: any) {
|
|
|
- const device = await this.deviceRepository.findOneBy({ socketId })
|
|
|
+ let device = await this.deviceRepository.findOneBy({ socketId })
|
|
|
if (device) {
|
|
|
Object.assign(device, data)
|
|
|
await this.deviceRepository.save(device)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ async updateDevice1(id: string, country: string, clashProfile: string) {
|
|
|
+ let device = await this.deviceRepository.findOneBy({ id })
|
|
|
+ if (device) {
|
|
|
+ device.pinCountry = country
|
|
|
+ device.clashProfile = clashProfile
|
|
|
+ if (device.pinCountry && !device.clashProfile) {
|
|
|
+ const profiles = await this.ipmoyuProvider.genProfiles(device.pinCountry, 1)
|
|
|
+ device.clashProfile = profiles[0]
|
|
|
+ }
|
|
|
+ await this.deviceRepository.save(device)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async pinCountry(ids: number[], country: string) {
|
|
|
+ if (ids && ids.length > 0) {
|
|
|
+ if (country) {
|
|
|
+ const profiles = await this.ipmoyuProvider.genProfiles(country, ids.length)
|
|
|
+ for (let i = 0; i < ids.length; i++) {
|
|
|
+ await this.deviceRepository.update(ids[i], { pinCountry: country, clashProfile: profiles[i] })
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ await this.deviceRepository.update(ids, { pinCountry: null, clashProfile: null })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async findAvailableDevice(matchDevice?: string) {
|
|
|
const where: any = { online: true, busy: false, canSend: true }
|
|
|
if (matchDevice) {
|