1- import { getStatus } from '../blueos'
1+ import ky from 'ky'
2+
3+ import { NetworkInfo } from '@/types/network'
4+
25import { isElectron } from '../utils'
36
47/**
@@ -41,47 +44,20 @@ class VehicleDiscover {
4144 private async checkAddress ( address : string ) : Promise < NetworkVehicle | null > {
4245 try {
4346 // First check if the vehicle is online
44- const hasRespondingStatusEndpoint = await getStatus ( address )
45- if ( ! hasRespondingStatusEndpoint ) {
46- return null
47- }
47+ const statusResponse = await ky . get ( `http://${ address } /status` , { timeout : 3000 } )
48+ if ( ! statusResponse . ok ) return null
4849
4950 // Try to get the vehicle name
50- try {
51- const response = await fetch ( `http://${ address } /beacon/v1.0/vehicle_name` )
52- if ( ! response . ok ) {
53- return null
54- }
55- const name = await response . text ( )
56- return { address, name }
57- } catch {
58- // If we can't get the name, it's because it's not a vehicle (or maybe BlueOS's Beacon service is not running)
59- return null
60- }
51+ const nameResponse = await ky . get ( `http://${ address } /beacon/v1.0/vehicle_name` , { timeout : 5000 } )
52+ if ( ! nameResponse . ok ) return null
53+ const name = await nameResponse . text ( )
54+ return { address, name }
6155 } catch {
62- // If we can't get the status , it's because the vehicle is not online
56+ // If we can't get the name , it's because it's not a vehicle (or maybe BlueOS's Beacon service is not running)
6357 return null
6458 }
6559 }
6660
67- /**
68- * Get the local subnet
69- * @returns {string | null } The local subnet, or null if not running in Electron
70- */
71- private async getLocalSubnet ( ) : Promise < string > {
72- if ( ! isElectron ( ) || ! window . electronAPI ?. getNetworkInfo ) {
73- const msg = 'For technical reasons, getting information about the local subnet is only available in Electron.'
74- throw new Error ( msg )
75- }
76-
77- try {
78- const { subnet } = await window . electronAPI . getNetworkInfo ( )
79- return subnet
80- } catch ( error ) {
81- throw new Error ( `Failed to get information about the local subnet. ${ error } ` )
82- }
83- }
84-
8561 /**
8662 * Find vehicles on the local network
8763 * @returns {NetworkVehicle[] } The vehicles found
@@ -96,23 +72,37 @@ class VehicleDiscover {
9672 }
9773
9874 const search = async ( ) : Promise < NetworkVehicle [ ] > => {
99- const subnet = await this . getLocalSubnet ( )
100-
101- if ( ! subnet ) {
102- throw new Error ( 'Failed to get information about the local subnet.' )
75+ if ( ! isElectron ( ) || ! window . electronAPI ?. getInfoOnSubnets ) {
76+ const msg = 'For technical reasons, getting information about the local subnet is only available in Electron.'
77+ throw new Error ( msg )
10378 }
10479
105- const promises : Promise < NetworkVehicle | null > [ ] = [ ]
80+ let localSubnets : NetworkInfo [ ] | undefined
81+ try {
82+ localSubnets = await window . electronAPI . getInfoOnSubnets ( )
83+ } catch ( error ) {
84+ throw new Error ( `Failed to get information about the local subnets. ${ error } ` )
85+ }
10686
107- // Check all IPs in the subnet
108- for ( let i = 1 ; i <= 254 ; i ++ ) {
109- const address = `${ subnet } .${ i } `
110- promises . push ( this . checkAddress ( address ) )
87+ if ( localSubnets . length === 0 ) {
88+ throw new Error ( 'Failed to get information about the local subnets.' )
11189 }
11290
113- const vehiclesFound = await Promise . all ( promises ) . then ( ( results ) => {
114- return results . filter ( ( result ) : result is NetworkVehicle => result !== null )
115- } )
91+ const vehiclesFound : NetworkVehicle [ ] = [ ]
92+ for ( const subnet of localSubnets ) {
93+ const topSideAddress = subnet . topSideAddress
94+ const possibleAddresses = subnet . availableAddresses . filter ( ( address ) => address !== topSideAddress )
95+
96+ const promises : Promise < NetworkVehicle | null > [ ] = possibleAddresses . map ( ( address ) => {
97+ return this . checkAddress ( address )
98+ } )
99+
100+ const vehicles = await Promise . all ( promises ) . then ( ( results ) => {
101+ return results . filter ( ( result ) : result is NetworkVehicle => result !== null )
102+ } )
103+
104+ vehiclesFound . push ( ...vehicles )
105+ }
116106
117107 this . currentSearch = undefined
118108
0 commit comments