Просмотр исходного кода

feat: Add online status to device update action

x1ongzhu 1 год назад
Родитель
Сommit
a63be19881
3 измененных файлов с 26 добавлено и 11 удалено
  1. 2 0
      src/device/device.service.ts
  2. 1 0
      src/events/events.gateway.ts
  3. 23 11
      src/proxy-provider/ipmoyu-provider.ts

+ 2 - 0
src/device/device.service.ts

@@ -74,6 +74,8 @@ export class DeviceService {
             if (device.pinCountry && !device.clashProfile) {
                 const profiles = await this.ipmoyuProvider.genProfiles(device.pinCountry, 1)
                 device.clashProfile = profiles[0]
+            } else if (device.pinCountry === '') {
+                device.clashProfile = ''
             }
             await this.deviceRepository.save(device)
         }

+ 1 - 0
src/events/events.gateway.ts

@@ -56,6 +56,7 @@ export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGate
     handleEvent(client: Socket, data: any) {
         // Logger.log(`Client ${client.id} sent: ${JSON.stringify(data)}`, 'EventsGateway')
         if ('updateDevice' === data.action) {
+            data.data.online = true
             this.deviceService.updateDevice(client.id, data.data)
         }
     }

+ 23 - 11
src/proxy-provider/ipmoyu-provider.ts

@@ -1,3 +1,4 @@
+import { count } from 'console'
 import { Logger } from '@nestjs/common'
 import axios from 'axios'
 import { readFileSync } from 'fs'
@@ -5,22 +6,33 @@ import { resolve } from 'path'
 import * as randomstring from 'randomstring'
 
 export class IpmoyuProvider implements ProxyProvider {
-    async genProfiles(country, num) {
-        const {
-            data: { data: countryList }
-        } = await axios.get('http://global.ipmoyu.com/proxy/web/map/countryList', {
-            params: {
-                page: 1,
-                limit: 1000
-            }
-        })
+    private countryList: {
+        abbr: string
+        chnName: string
+        continent: string
+        country: string
+        id: number
+    }[] = []
+    constructor() {
+        axios
+            .get('http://global.ipmoyu.com/proxy/web/map/countryList', {
+                params: {
+                    page: 1,
+                    limit: 1000
+                }
+            })
+            .then(({ data }) => {
+                this.countryList = data.data
+            })
+    }
 
-        let countryId = countryList.find((i) => i.abbr === country.toUpperCase())?.id
+    async genProfiles(country, num) {
+        let countryId = this.countryList.find((i) => i.abbr === country.toUpperCase())?.id
         if (!countryId) {
             Logger.log(`Invalid country ${country}, use US as default`)
             country = 'US'
         }
-        countryId = countryList.find((i) => i.abbr === country.toUpperCase())?.id
+        countryId = this.countryList.find((i) => i.abbr === country.toUpperCase())?.id
         if (!countryId) {
             throw new Error(`Invalid country ${country}`)
         }