You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
8.4 KiB
C#
173 lines
8.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BirdyFlash.Lib {
|
|
public class DeviceFinder {
|
|
public delegate void OnPortDataUpdated(object sender, EventArgs e);
|
|
public event OnPortDataUpdated PortDataUpdated;
|
|
|
|
public List<string> registeredPorts;
|
|
Dictionary<string, CommPortInfo> enabledPorts;
|
|
|
|
public DeviceFinder() {
|
|
this.registeredPorts = new List<string>();
|
|
this.enabledPorts = new Dictionary<string, CommPortInfo>();
|
|
//
|
|
string[] portsToReRegister = Properties.Settings.Default.registeredPorts.Split('\n');
|
|
foreach (string registerPort in portsToReRegister)
|
|
if (registerPort.Contains("://"))
|
|
this.RegisterNewPort(registerPort);
|
|
|
|
string[] enabledPortsLoaded = Properties.Settings.Default.enabledPorts.Split('\n');
|
|
List<string> ports = System.IO.Ports.SerialPort.GetPortNames().ToList().Select(s => s.ToLower()).ToList();
|
|
foreach (string enablePort in enabledPortsLoaded)
|
|
if (enablePort.Contains("://") && !(enablePort.StartsWith("serial://") && ports.IndexOf(new Uri(enablePort).Host) == -1))
|
|
this.EnablePort(enablePort);
|
|
|
|
/*foreach (string enablePort in enabledPortsLoaded)
|
|
if (enablePort.Contains("://"))
|
|
Console.WriteLine("{0} {1} {2}", enablePort, enablePort.StartsWith("serial://"), ports.IndexOf(new Uri(enablePort).Host));*/
|
|
}
|
|
|
|
private static DeviceFinder _instance;
|
|
public static DeviceFinder instance {
|
|
get {
|
|
if (_instance == null)
|
|
_instance = new DeviceFinder();
|
|
return _instance;
|
|
}
|
|
}
|
|
public void ScanPagers() {
|
|
Task.WaitAll(
|
|
this.enabledPorts.Keys.ToList()
|
|
.Select(portName => {
|
|
return Task.Run(() => TryDetectPager(portName));
|
|
})
|
|
.ToArray()
|
|
);
|
|
this.PortDataUpdated(this, new EventArgs());
|
|
}
|
|
public List<CommPortInfo> GetAvailablePagers() {
|
|
/*foreach(var commPortInfo in this.ports.Values)
|
|
Console.WriteLine("{0} {1} {2} {3} {4}", commPortInfo.port.ToString(), commPortInfo.unkown, commPortInfo.success, commPortInfo.deviceInfo.model, commPortInfo.deviceInfo.deviceSerial);*/
|
|
return this.enabledPorts.Values.ToList().Where(x => x.success).ToList();
|
|
}
|
|
private async Task<bool> TryDetectPager(string portName) {
|
|
if (!this.enabledPorts.ContainsKey(portName))
|
|
return false;
|
|
CommPortInfo commPortInfo = this.enabledPorts[portName];
|
|
commPortInfo.success = false;
|
|
commPortInfo.unkown = true;
|
|
this.enabledPorts[portName] = commPortInfo;
|
|
//
|
|
using (var comm = new BirdyComm()) {
|
|
comm.SetPort(commPortInfo.port);
|
|
//if (comm.commPort is CommPortSerial) ((CommPortSerial)comm.commPort).ResetReOpen();
|
|
DeviceInfo deviceInfo;
|
|
try {
|
|
deviceInfo = await comm.TryGetDeviceInfo();
|
|
} catch (TimeoutException te) {
|
|
Console.WriteLine(te.Message);
|
|
try {
|
|
deviceInfo = await comm.TryGetDeviceInfo();
|
|
} catch (TimeoutException te2) {
|
|
Console.WriteLine(te2.Message);
|
|
try {
|
|
deviceInfo = await comm.TryGetDeviceInfo();
|
|
} catch (TimeoutException te3) {
|
|
Console.WriteLine(te3.Message);
|
|
await comm.ForceGotoProg();
|
|
try {
|
|
deviceInfo = await comm.TryGetDeviceInfo();
|
|
} catch (TimeoutException te4) {
|
|
Console.WriteLine(te4.Message);
|
|
try {
|
|
deviceInfo = await comm.TryGetDeviceInfo();
|
|
} catch (TimeoutException te5) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (deviceInfo.deviceSerial != null) {
|
|
var data = await comm.TryGetDeviceInfo();
|
|
Console.WriteLine(deviceInfo.deviceSerial);
|
|
/*this.Invoke(new Action(() => {
|
|
txtLog.AppendText(string.Format("{0} type {1} serial {2}\n", portName, data.model, data.deviceSerial));
|
|
}));*/
|
|
commPortInfo.deviceInfo = deviceInfo;
|
|
commPortInfo.success = true;
|
|
commPortInfo.unkown = false;
|
|
this.enabledPorts[portName] = commPortInfo;
|
|
return true;
|
|
} else {
|
|
/*this.Invoke(new Action(() => {
|
|
txtLog.AppendText(string.Format("{0} timeout\n", portName));
|
|
}));*/
|
|
commPortInfo.success = false;
|
|
commPortInfo.unkown = false;
|
|
this.enabledPorts[portName] = commPortInfo;
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
internal void SetPortOption(string port, string option) {
|
|
var portURI = new Uri(port);
|
|
string strippedURI = portURI.Scheme + "://" + portURI.Host + "/";
|
|
if (this.enabledPorts.ContainsKey(port))
|
|
this.enabledPorts.Remove(port);
|
|
Properties.Settings.Default.enabledPorts = string.Join("\n", this.enabledPorts.Keys);
|
|
Properties.Settings.Default.Save();
|
|
if (this.PortDataUpdated != null) this.PortDataUpdated(this, new EventArgs());
|
|
}
|
|
|
|
public void RescanLocalSerialPorts() {
|
|
List<string> ports = System.IO.Ports.SerialPort.GetPortNames().ToList().Select(s => s.ToLower()).ToList();
|
|
foreach (string portName in this.registeredPorts.ToList())
|
|
if (new Uri(portName).Scheme == "serial" && ports.IndexOf(portName.Substring(9)) == -1) {
|
|
this.registeredPorts.Remove(portName);
|
|
if (this.enabledPorts.ContainsKey(portName))
|
|
this.enabledPorts.Remove(portName);
|
|
}
|
|
foreach (string portName in ports)
|
|
if (this.registeredPorts.Where(x => x.StartsWith("serial://" + portName + "/")).Count() == 0)
|
|
this.registeredPorts.Add("serial://" + portName);
|
|
if (this.PortDataUpdated != null) this.PortDataUpdated(this, new EventArgs());
|
|
}
|
|
|
|
public void RegisterNewPort(string port) {
|
|
if (registeredPorts.Contains(port)) return;
|
|
registeredPorts.Add(port);
|
|
Properties.Settings.Default.registeredPorts = string.Join("\n", this.registeredPorts);
|
|
Properties.Settings.Default.Save();
|
|
if (this.PortDataUpdated != null) this.PortDataUpdated(this, new EventArgs());
|
|
}
|
|
public void EnablePort(string port) {
|
|
if (!this.enabledPorts.ContainsKey(port))
|
|
this.enabledPorts.Add(port, new CommPortInfo {
|
|
unkown = true,
|
|
port = CommPort.From(port),
|
|
});
|
|
Properties.Settings.Default.enabledPorts = string.Join("\n", this.enabledPorts.Keys);
|
|
Properties.Settings.Default.Save();
|
|
if (this.PortDataUpdated != null) this.PortDataUpdated(this, new EventArgs());
|
|
}
|
|
public void DisablePort(string port) {
|
|
if (this.enabledPorts.ContainsKey(port))
|
|
this.enabledPorts.Remove(port);
|
|
Properties.Settings.Default.enabledPorts = string.Join("\n", this.enabledPorts.Keys);
|
|
Properties.Settings.Default.Save();
|
|
if (this.PortDataUpdated != null) this.PortDataUpdated(this, new EventArgs());
|
|
}
|
|
public bool IsPortEnabled(string port) {
|
|
return this.enabledPorts.ContainsKey(port);
|
|
}
|
|
}
|
|
}
|