/// <summary>
        /// Queries WMI for Bluetooth COM Ports. Provides useful information on the Console as it does.
        /// </summary>
        /// <returns></returns>
        private ManagementObject[] getWmiCOMData()
        {
            ManagementObject[]       wmiObjects  = null;
            ManagementObjectSearcher wmiSearcher = null;

            try
            {
                wmiSearcher = new ManagementObjectSearcher(@"select DeviceID,PNPDeviceID from Win32_SerialPort");

                ManagementObjectCollection tmpSearchResults = null;

                Logger.Info("Searching for Bluetooth COM devices");
                wmiSearcher      = new ManagementObjectSearcher(@"root\cimv2", "SELECT Name,DeviceID,PNPDeviceID FROM Win32_SerialPort WHERE Name LIKE \"%Bluetooth%\"");
                tmpSearchResults = wmiSearcher.Get();

                Logger.Info("Found {0} devices, verifying...", tmpSearchResults.Count);
                wmiObjects = new ManagementObject[tmpSearchResults.Count];
                tmpSearchResults.CopyTo(wmiObjects, 0);
            }
            catch (System.Management.ManagementException e)
            {
                Console.WriteLine("Query " + wmiSearcher.Query.ToString() + " threw an exception!\n");
                throw e;
            }
            try
            {
                int i = 0;
                foreach (ManagementObject obj in wmiObjects)
                {
                    if ((obj["Name"] as String).Contains("Bluetooth") == false)
                    {
                        Logger.Warning("{0} is not a Bluetooth device! Removing from search results", obj["DeviceID"] as String);
                    }
                    else
                    {
                        wmiObjects[i] = obj;
                        ++i;
                    }
                }
            }
            catch (System.Management.ManagementException e)
            {
                Console.WriteLine("Exception: Unable to read a property from wmi object!\n");
                throw e;
            }

            /* Extremeley self-referencing code to get rid of WMI-detected COM ports that
             * have a MAC address of 000000000000 */
            wmiObjects = wmiObjects.Where(obj =>
                                          (obj["PNPDeviceID"] as String).Substring(
                                              (obj["PNPDeviceID"] as String).LastIndexOf('_') - MAC_ADDRESS_LENGTH, MAC_ADDRESS_LENGTH)
                                          != "000000000000").ToArray();

            Logger.Info("Verified {0} Bluetooth COM Device(s)", wmiObjects.Length);
            return(wmiObjects);
        }
示例#2
0
        public void Execute(IJobExecutionContext context)
        {
            List <StoreModel> storeList = new List <StoreModel>();

            storeList = this._IStoreServerService.GetStoresDetails();

            List <StoreServerModel> storeServerList = new List <StoreServerModel>();

            List <StoreServerModel> storeServerStatusList = new List <StoreServerModel>();

            List <ServerServiceStatus> windowsServiceList    = new List <ServerServiceStatus>();
            List <ServerServiceStatus> windowsServiceRunList = new List <ServerServiceStatus>();


            storeServerList    = this._IStoreServerService.GetStoresServerDetails();
            windowsServiceList = this._IStoreServerService.GetWindowsServiceDetails();

            StoreServerModel storeServerDetails = new StoreServerModel();

            storeServerDetails.UserId = 1;

            Int64 batchId = this._IStoreServerService.GenerateServerServiceStatusBatch(storeServerDetails);

            Int64 windowsBatchId = this._IStoreServerService.GenerateWindowsServiceStatusBatch(storeServerDetails);

            foreach (var item in storeServerList)
            {
                StoreServerModel storeServerStatus = new StoreServerModel();
                storeServerStatus = item;
                storeServerStatus.ServerStatusBatchId = batchId;
                try
                {
                    Ping      myPing = new Ping();
                    PingReply reply  = myPing.Send(item.ISSIpAddress, 1000);

                    if (reply != null)
                    {
                        if (reply.Status == IPStatus.Success)
                        {
                            storeServerStatus.IsServerActive     = true;
                            storeServerStatus.ServerResponseTime = Convert.ToInt32(reply.RoundtripTime);
                        }
                        else
                        {
                            storeServerStatus.IsServerActive = false;
                        }
                    }
                }
                catch (Exception)
                {
                    storeServerStatus.IsServerActive = false;
                }

                List <ServerServiceStatus> windowsServiceCurrentRunList = new List <ServerServiceStatus>();

                windowsServiceCurrentRunList = windowsServiceList.Where(
                    w => w.StoreNo == item.StoreNo
                    ).ToList();

                if (storeServerStatus.IsServerActive == true &&
                    windowsServiceCurrentRunList != null &&
                    windowsServiceCurrentRunList.Count > 0
                    )
                {
                    try
                    {
                        ConnectionOptions op = new ConnectionOptions();
                        op.Username = "******";
                        op.Password = "******";
                        ManagementScope scope = new ManagementScope(@"\\" +
                                                                    item.ISSIpAddress
                                                                    + @"\root\cimv2", null);
                        scope.Connect();
                        ManagementPath  path = new ManagementPath("Win32_Service");
                        ManagementClass services;
                        services = new ManagementClass(scope, path, null);

                        ManagementObjectCollection moc         = services.GetInstances();
                        ManagementObject[]         deviceArray = new ManagementObject[moc.Count];
                        moc.CopyTo(deviceArray, 0);

                        windowsServiceCurrentRunList.ForEach(x =>
                        {
                            ServerServiceStatus serverServiceStatus = new ServerServiceStatus();
                            serverServiceStatus = x;
                            var vvv             = deviceArray.Where(w => w.GetPropertyValue("Name").ToString() == x.ServiceName);

                            if (vvv.Any(a => a.GetPropertyValue("State").ToString().ToLower().Equals("running"))
                                )
                            {
                                serverServiceStatus.IsServiceActive = true;
                            }
                            else
                            {
                                serverServiceStatus.IsServiceActive = false;
                            }
                        }
                                                             );

                        windowsServiceRunList.AddRange(windowsServiceCurrentRunList);
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                }

                lock (storeServerStatusList)
                {
                    storeServerStatusList.Add(storeServerStatus);
                }
            }



            //_heartbeat.UpdateServiceState("alive");
            var isUpdateSuccess = this._IStoreServerService.UpdateServerServiceStatusBatch(storeServerStatusList);
        }