Skip to content

Commit 4474306

Browse files
committed
Fix discoveryDevice
1 parent 52e04e5 commit 4474306

File tree

4 files changed

+71
-47
lines changed

4 files changed

+71
-47
lines changed

src/Helper/IoTHelper.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,25 @@ export class IoTHelper {
9999
private GetAllFilesByExtRecurc(pathFolder:string, fileExtension:string, depthLevel=1,currenDepthLevel=1): Array<string> {
100100
let result:Array<string>=[];
101101
if(currenDepthLevel>depthLevel) return result;
102-
//
103-
let files=fs.readdirSync(pathFolder);
104-
files.forEach((name) => {
105-
const filename=`${pathFolder}\\${name}`;
106-
if(fs.lstatSync(filename).isDirectory())
107-
{
108-
//if(!currenDepthLevel) currenDepthLevel=0;
109-
//currenDepthLevel++
102+
try {
103+
let files=fs.readdirSync(pathFolder);
104+
files.forEach((name) => {
105+
const filename=`${pathFolder}\\${name}`;
106+
if(fs.lstatSync(filename).isDirectory()) {
110107
const helper= new IoTHelper();
111108
const result2=helper.GetAllFilesByExtRecurc(filename, fileExtension, depthLevel,currenDepthLevel+1);
112109
result2.forEach((element) => {
113110
result.push(element);
114111
});
115-
}
116-
if(fs.lstatSync(filename).isFile())
117-
{
118-
if(`.${filename.split('.').pop()}`==fileExtension)
119-
{
120-
console.log(filename);
112+
}
113+
if(fs.lstatSync(filename).isFile()) {
114+
if(`.${filename.split('.').pop()}`==fileExtension) {
115+
//console.log(filename);
121116
result.push(filename);
122117
}
123118
}
124-
});
125-
//
119+
});
120+
} catch (err: any){}
126121
return result;
127122
}
128123

@@ -170,14 +165,16 @@ export class IoTHelper {
170165
let listFolders:Array<string>=[];
171166
//check path
172167
if (!fs.existsSync(path)) return listFolders;
173-
//getting a list of entity directories
174-
const files = fs.readdirSync(path);
175-
//getting a list of folders
176-
files.forEach(name => {
177-
//directory
178-
const dir=`${path}\\${name}`;
179-
if(fs.lstatSync(dir).isDirectory()) listFolders.push(dir);
168+
try {
169+
//getting a list of entity directories
170+
const files = fs.readdirSync(path);
171+
//getting a list of folders
172+
files.forEach(name => {
173+
//directory
174+
const dir=`${path}\\${name}`;
175+
if(fs.lstatSync(dir).isDirectory()) listFolders.push(dir);
180176
});
177+
} catch (err: any){}
181178
return listFolders;
182179
}
183180

src/Helper/dotnetHelper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ export class dotnetHelper {
5858
static GetDotNetTargets(): Map<string,string[]> {
5959
//from https://learn.microsoft.com/en-us/dotnet/standard/frameworks
6060
let dictionary = new Map<string,string[]>();
61-
dictionary.set("8.0",["net8.0",".NET 8"]);
61+
dictionary.set("8.0",["net8.0",".NET 8 (preview)"]);
6262
dictionary.set("7.0",["net7.0",".NET 7"]);
63-
dictionary.set("6.0",["net6.0",".NET 6"]);
63+
dictionary.set("6.0",["net6.0",".NET 6 (LTS)"]);
6464
dictionary.set("5.0",["net5.0",".NET 5 (support ended)"]);
6565
dictionary.set("3.1",["netcoreapp3.1",".NET Core 3.1 (support ended)"]);
6666
return dictionary;

src/Helper/networkHelper.ts

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,53 @@ export class networkHelper {
3737
return Promise.resolve(result);
3838
}
3939

40-
static async PingHost(hostName:string, numberOfEchos = 3, timeout = 1): Promise<IotResult>{
40+
static async PingHost(hostName:string, numberOfEchos = 3, timeout = 1,getHostname:boolean=false): Promise<IotResult>{
4141
let result:IotResult;
4242
result=await this.GetIpAddress(hostName);
4343
if(result.Status==StatusResult.Error) return Promise.resolve(result);
4444
const ipAddress=<string>result.returnObject;
4545
const msg=`The host is unavailable. Host: ${hostName} IP-Address: ${ipAddress}.`;
46+
const option:pingOptions = {
47+
numberOfEchos: numberOfEchos,
48+
timeout: timeout,
49+
logToFile: false,
50+
IPV4: true,
51+
numeric: getHostname
52+
};
4653
try
4754
{
48-
const response = await ping(ipAddress,{logToFile:false, numberOfEchos: numberOfEchos, timeout: timeout, IPV4: true});
55+
const response = await ping(ipAddress,option);
4956
const packetLoss =+(response.packetLoss ?? "100");
50-
if(packetLoss>50)
51-
result=new IotResult(StatusResult.Error, msg); else result=new IotResult(StatusResult.Ok,`Host ${ipAddress} is available.`);
57+
if(packetLoss>50) {
58+
result=new IotResult(StatusResult.Error, msg);
59+
}else {
60+
result=new IotResult(StatusResult.Ok,`Host ${ipAddress} is available.`);
61+
if (getHostname&&response.host&&response.host!=``) result.returnObject=response.host;
62+
}
5263
} catch (err:any) {
5364
result=new IotResult(StatusResult.Error, msg,err);
5465
}
5566
return Promise.resolve(result);
5667
}
5768

58-
static async CheckTcpPortUsed(hostName:string, port: number): Promise<IotResult>{
69+
static async CheckTcpPortUsed(hostName:string, port: number,retryTimeMs:number=200,timeOutMs:number=1000): Promise<IotResult>{
70+
//[retryTimeMs] the retry interval in milliseconds - defaultis is 200ms
71+
//[timeOutMs] the amount of time to wait until port is free default is 1000ms
5972
let result:IotResult;
6073
result=await this.GetIpAddress(hostName);
6174
if(result.Status==StatusResult.Error) return Promise.resolve(result);
6275
const ipAddress=<string>result.returnObject;
6376
//next
6477
const msg=`${port} port unavailable. Host: ${hostName} IP-Address: ${ipAddress}.`;
78+
const tcpPortUsedOptions:tcpPortUsed.TcpPortUsedOptions = {
79+
port: port,
80+
host: ipAddress,
81+
retryTimeMs: retryTimeMs,
82+
timeOutMs: timeOutMs
83+
};
6584
try
6685
{
67-
const inUse = await tcpPortUsed.check(port,ipAddress);
86+
const inUse = await tcpPortUsed.check(tcpPortUsedOptions);
6887
if(inUse)
6988
result=new IotResult(StatusResult.Ok,`Network port ${port} host ${ipAddress} available.`); else result=new IotResult(StatusResult.Error,msg);
7089
} catch (err:any) {
@@ -88,19 +107,22 @@ export class networkHelper {
88107
}
89108

90109
static GetLocalIPaddress(): Map<string,string> {
110+
//otput: 1. wireless => 192.168.10.24
91111
let results:Map<string,string> = new Map<string,string>();
92-
const { networkInterfaces } = require('os');
93-
const nets = networkInterfaces();
94-
for (const name of Object.keys(nets)) {
95-
for (const net of nets[name]) {
96-
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
97-
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
98-
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
99-
if (net.family === familyV4Value && !net.internal) {
100-
results.set(name,net.address);
112+
try {
113+
const { networkInterfaces } = require('os');
114+
const nets = networkInterfaces();
115+
for (const name of Object.keys(nets)) {
116+
for (const net of nets[name]) {
117+
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
118+
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
119+
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
120+
if (net.family === familyV4Value && !net.internal) {
121+
results.set(name,net.address);
122+
}
101123
}
102124
}
103-
}
125+
} catch (err: any){}
104126
return results;
105127
}
106128

@@ -123,5 +145,4 @@ export class networkHelper {
123145
return Promise.resolve(rangeIP);
124146
}
125147

126-
127148
}

src/actionsDevice/discoveryDevice.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export async function discoveryDevice(treeData: TreeDataDevicesProvider,treeView
102102
}
103103
//check 22 port and create ItemQuickPick
104104
const checkPortAsync = async (item:ItemQuickPick) => {
105-
let result = await networkHelper.CheckTcpPortUsed(item.value,checkPort);
105+
let result = await networkHelper.CheckTcpPortUsed(item.value,checkPort,150,700);
106106
if(result.Status==StatusResult.Ok) {
107107
item.description=`port ${checkPort} available`;
108108
item.tag=`${checkPort}`;
@@ -116,12 +116,18 @@ export async function discoveryDevice(treeData: TreeDataDevicesProvider,treeView
116116
//ip availability check
117117
const msg=`${i+1} of ${devices.length+1}. Checking host ${devices[i].ip}.`;
118118
progress.report({ message: msg });
119-
let result = await networkHelper.PingHost(devices[i].ip);
119+
let result = await networkHelper.PingHost(devices[i].ip,3,1,true);
120120
if(result.Status==StatusResult.Ok) {
121+
//label
122+
let labelHostname:string=devices[i].ip;
123+
if (devices[i].name!='?') labelHostname=devices[i].name;
124+
if (result.returnObject) labelHostname=result.returnObject;
121125
let label:string;
122-
if (devices[i].name=='?')
123-
label=devices[i].ip;
124-
else label=`${devices[i].name} ${devices[i].ip}`;
126+
if(labelHostname!=devices[i].ip) {
127+
label=`Hostname: ${labelHostname} IP: ${devices[i].ip}`;
128+
}else {
129+
label=`IP: ${devices[i].ip}`;
130+
}
125131
//create item and push
126132
let item = new ItemQuickPick(label,"", devices[i].ip);
127133
itemDevices.push(item);

0 commit comments

Comments
 (0)