using System; using System.Management; using System.Collections.Generic; using System.Linq; using System.Text; namespace Application { class BrickManager { private static BrickManager instance; private Dictionary namesToBricks; private Dictionary namesToComms; private BrickManager() { namesToBricks = new Dictionary(); namesToComms = new Dictionary(); } public static BrickManager getInstance() { if (instance == null) instance = new BrickManager(); return instance; } /*********************************************/ public List getListOfAttachedBricks() { // Runs WMI queries to see which bricks are listed in Bluetooth // Then compares the results against the dictionaries to see if any bricks were Bluetooth-attached since the last refresh // Returns annotated list of bricks by name (annotated with their states) List brickList = new List(); Dictionary btToComDict = new Dictionary(); const string ComQueryString = "SELECT Caption,PNPDeviceID FROM Win32_PnPEntity " + "WHERE ConfigManagerErrorCode = 0 AND " + "Caption LIKE 'Standard Serial over Bluetooth link (COM%' AND " + "PNPDeviceID LIKE '%&001653%'"; SelectQuery ComWMIquery = new SelectQuery(ComQueryString); ManagementObjectSearcher ComWMIqueryResults = new ManagementObjectSearcher(ComWMIquery); if (ComWMIqueryResults != null) { foreach (object result in ComWMIqueryResults.Get()) { ManagementObject mo = (ManagementObject)result; object captionObject = mo.GetPropertyValue("Caption"); object pnpIdObject = mo.GetPropertyValue("PNPDeviceID"); // Get the COM port name out of the Caption, requires a little search and replacing. string caption = captionObject.ToString(); string comPort = caption.Substring(caption.LastIndexOf("(COM")). Replace("(", string.Empty).Replace(")", string.Empty); // Extract the BT address from the PNPObjectID property string BTaddress = pnpIdObject.ToString().Split('&')[4].Substring(0, 12); // add to dictionary btToComDict.Add(BTaddress, comPort); } } else { Console.WriteLine("Error executing query"); } const string NameQueryString = "select Caption,Description,DeviceID,Name,PNPDeviceID from Win32_PnPEntity " + "where Description = 'Bluetooth Device' AND " + "PNPDeviceID LIKE '%_001653%'"; SelectQuery NameWMIquery = new SelectQuery(NameQueryString); ManagementObjectSearcher NameWMIqueryResults = new ManagementObjectSearcher(NameWMIquery); if (NameWMIqueryResults != null) { //WriteLine("DEBUG: Name query... The following bricks were found on your system:"); foreach (object result in NameWMIqueryResults.Get()) { ManagementObject mo = (ManagementObject)result; object captionObject = mo.GetPropertyValue("Caption"); object pnpIdObject = mo.GetPropertyValue("PNPDeviceID"); string caption = captionObject.ToString(); // Extract the BT address from the PNPObjectID property string BTaddress = pnpIdObject.ToString().Split('_')[1].Substring(0, 12); // Get the COM port string comPort = btToComDict[BTaddress]; //Console.WriteLine("BT Addr: {0} ", BTaddress); //Console.WriteLine("Name : {0} ", caption); //Console.WriteLine("COM : {0} ", comPort); if (!namesToComms.ContainsKey(caption)) namesToComms.Add(caption, comPort); string listName = caption + ""; if (namesToBricks.ContainsKey(caption)) { GenericBrick checkBrick = namesToBricks[caption]; listName = listName + " [" + checkBrick.getStateName() + "]"; } brickList.Add(listName); } } else { Console.WriteLine("Error executing query"); } brickList.Sort(); return brickList; } public GenericBrick getBrickByName(string name) { // is it already connected? if (namesToBricks.ContainsKey(name)) return namesToBricks[name]; // otherwise, return nothing return null; } public GenericBrick getBrickByName(string name, bool isEv3) { // is it already connected? if (namesToBricks.ContainsKey(name)) return namesToBricks[name]; // is there a COM port associated? if (!namesToComms.ContainsKey(name)) return null; // what is the COM port string newComPort = namesToComms[name]; // try to connect GenericBrick newBrick = new GenericBrick(name, newComPort, isEv3); if (newBrick.getState() > 0) { // add the brick to the dictionary namesToBricks.Add(name, newBrick); return newBrick; } // if we got to here, connection didn't work return null; } } }