示例#1
0
 private static void CleanDatabase(PrinterDatabaseEntities database)
 {
     foreach (var printer in database.Printers)
     {
         database.Printers.DeleteObject(printer);
     }
     database.SaveChanges();
 }
示例#2
0
        private void BackgroundWorkerDoWork(object sender, DoWorkEventArgs e)
        {
            var bg = (BackgroundWorker) sender;
            var database = new PrinterDatabaseEntities();
            var arguments = e.Argument as object[];

            var community = "";
            var ipFrom = "";
            var ipTo = "";
            var scan = false;

            if (arguments != null)
            {
                community = (string) arguments[0];
                scan = (bool) arguments[1];
                ipFrom = (string) arguments[2];
                ipTo = (string) arguments[3];
            }

            var snmp = new SimpleSnmp("", community)
            {
                PeerPort = 161,
                Timeout = 25,
                MaxRepetitions = 20
            };

            if (scan)
            {
                #region
                CleanDatabase(database);

                var ipFromSplit = ipFrom.Split('.');
                var ipToSplit = ipTo.Split('.');
                var ipFromLastOctet = Convert.ToInt32(ipFromSplit[3]);
                var ipToLastOctet = Convert.ToInt32(ipToSplit[3]);

                var ipRestOctets = string.Format("{0}.{1}.{2}.", ipFromSplit[0], ipFromSplit[1], ipFromSplit[2]);

                if (ipToLastOctet - ipFromLastOctet == 0)
                    bg.ReportProgress(0, 1);
                else
                    bg.ReportProgress(0, ipToLastOctet - ipFromLastOctet + 1);

                for (var i = ipFromLastOctet; i <= ipToLastOctet; i++)
                {
                    var ipAddress = string.Format("{0}{1}", ipRestOctets, i);

                    snmp.PeerIP = IPAddress.Parse(string.Format("{0}{1}", ipRestOctets, i));

                    try
                    {
                        var printerFound = new Printer
                                           	{
                                           		ID = Guid.NewGuid(),
                                           		IpAddress = ipAddress,
                                           		LastStatus = GetPrinterStatus(snmp),
                                           		MacAddress = GetMac(ipAddress)
                                           	};

                        database.Printers.AddObject(printerFound);
                    }
            // ReSharper disable EmptyGeneralCatchClause
                    catch{ }
            // ReSharper restore EmptyGeneralCatchClause

                    bg.ReportProgress(0);
                }
                database.SaveChanges();
                #endregion
            }
            else
            {
                #region
                bg.ReportProgress(0, 1);
                var tempPrintersList = new List<Printer>();
                foreach (var printer in database.Printers)
                {
                    snmp.PeerIP = IPAddress.Parse(printer.IpAddress);
                    tempPrintersList.Add(new Printer
                                            {
                                                ID = Guid.NewGuid(),
                                                IpAddress = printer.IpAddress,
                                                LastStatus = GetPrinterStatus(snmp),
                                                MacAddress = GetMac(printer.IpAddress),
                                            });

                    bg.ReportProgress(0);
                }

                //usuwanie z bazy
                CleanDatabase(database);

                //dodawanie do bazy
                foreach (var printer in tempPrintersList)
                {
                    database.Printers.AddObject(printer);
                }
                database.SaveChanges();
                #endregion
            }
        }