private void btnDelete_Click(object sender, EventArgs e)
 {
     sql.ExecuteNonQueryACon($"DELETE FROM DeviceSites WHERE ID = '{lbxDeviceSites.SelectedValue}'");
     LoadEntries();
     lbxDeviceSites.SelectedIndex = -1;
     DisableInput();
 }
        private void bgwMonitorProgress_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            lblWOLStatus.Text = "Device Online!";
            tmrTimeElapsed.Stop();
            prgWOLState.Value = 100;
            prgWOLState.Style = ProgressBarStyle.Continuous;

            sql.ExecuteNonQueryACon($"UPDATE Devices SET LastPowerState = '1' WHERE MACAddress = '{MACAddress}'");

            MessageBox.Show("Device is Online!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void bgwUpdateEntries_DoWork(object sender, DoWorkEventArgs e)
        {
            Dictionary <string, bool> DevicePowerState = new Dictionary <string, bool>();

            // Get all devices, set powerstate to false
            DevicePowerState.Clear();
            sql.Open();
            using (SQLiteDataReader reader = sql.ExecuteQuery("SELECT * FROM Devices"))
                while (reader.Read())
                {
                    DevicePowerState.Add(Convert.ToString(reader["MACAddress"]), false);
                }
            sql.Close();

            Ping          ping  = new Ping();
            StringBuilder sqlSB = new StringBuilder();

            if (ipStartParts[0] == ipEndParts[0] && ipStartParts[1] == ipEndParts[1] && ipStartParts[2] == ipEndParts[2])
            {
                for (int i = Convert.ToInt32(ipStartParts[3]); i <= Convert.ToInt32(ipEndParts[3]); i++)
                {
                    if (bgwUpdateEntries.CancellationPending)
                    {
                        return;
                    }

                    bgwUpdateEntries.ReportProgress(i);

                    string    currentIP = ipStartParts[0] + "." + ipStartParts[1] + "." + ipStartParts[2] + "." + i.ToString();
                    PingReply reply     = ping.Send(currentIP, 100);

                    if (reply.Status == IPStatus.Success)
                    {
                        string currentMAC = NetExplore.GetMacAddress(currentIP);

                        if (!string.IsNullOrEmpty(currentMAC))
                        {
                            // Update the power-state of the device
                            if (DevicePowerState.ContainsKey(currentMAC))
                            {
                                // Known device, only update values
                                DevicePowerState[currentMAC] = true;
                                sqlSB.Append($"UPDATE Devices SET IP4Address = '{currentIP}', LastSeen = '{DateTime.Now:yyyy-MM-dd H:mm:ss}' WHERE MACAddress = '{currentMAC}';");
                            }
                            else
                            {
                                // New, unknown device, add to DB
                                DevicePowerState.Add(currentMAC, true);
                                sqlSB.Append($"INSERT INTO Devices (MACAddress, DeviceType, Name, IP4Address, LastSeen) VALUES ('{currentMAC}','{DeviceType.UnknownDevice}','Device-{currentMAC}','{currentIP}','{DateTime.Now:yyyy-MM-dd H:mm:ss}')");
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

                if (bgwUpdateEntries.CancellationPending)
                {
                    return;
                }

                // Update IP and LastSeen, add new entries
                sql.ExecuteNonQueryACon(sqlSB.ToString());

                // Update last power-state
                StringBuilder sb = new StringBuilder();
                foreach (KeyValuePair <string, bool> entry in DevicePowerState)
                {
                    int state = 0;
                    if (entry.Value)
                    {
                        state = 1;
                    }
                    sb.Append($"UPDATE Devices SET LastPowerState = '{state}' WHERE MACAddress = '{entry.Key}';");
                }
                sql.ExecuteNonQueryACon(sb.ToString());
            }
        }
Example #4
0
        public static void PowerStateCheck(string pIPRangeStart, string pIPRangeEnd, BackgroundWorker pProgressReplyBGW = null)
        {
            using (WrapSQLite tsql = new WrapSQLite(SurgitManager.SurgitDatabaseLocation, true))
            {
                Dictionary <string, bool> DevicePowerState = new Dictionary <string, bool>();

                // Get all devices, set powerstate to false
                DevicePowerState.Clear();
                tsql.Open();
                using (SQLiteDataReader reader = tsql.ExecuteQuery("SELECT * FROM Devices"))
                    while (reader.Read())
                    {
                        DevicePowerState.Add(Convert.ToString(reader["MACAddress"]), false);
                    }
                tsql.Close();


                // Ping all devices in Network Range
                Ping          ping  = new Ping();
                StringBuilder sqlSB = new StringBuilder();

                if (CheckIPClassCValidity(pIPRangeStart, pIPRangeEnd))
                {
                    string[] ipStartParts = pIPRangeStart.Split('.');
                    string[] ipEndParts   = pIPRangeEnd.Split('.');

                    for (int i = Convert.ToInt32(ipStartParts[3]); i <= Convert.ToInt32(ipEndParts[3]); i++)
                    {
                        if (pProgressReplyBGW.CancellationPending)
                        {
                            return;
                        }

                        if (pProgressReplyBGW != null)
                        {
                            pProgressReplyBGW.ReportProgress(i);
                        }

                        string    currentIP = ipStartParts[0] + "." + ipStartParts[1] + "." + ipStartParts[2] + "." + i.ToString();
                        PingReply reply     = ping.Send(currentIP, 100);

                        if (reply.Status == IPStatus.Success)
                        {
                            string currentMAC = NetExplore.GetMacAddress(currentIP);

                            if (!string.IsNullOrEmpty(currentMAC))
                            {
                                // Update the power-state of the device
                                if (DevicePowerState.ContainsKey(currentMAC))
                                {
                                    // Known device, only update values
                                    DevicePowerState[currentMAC] = true;
                                    sqlSB.Append($"UPDATE Devices SET IP4Address = '{currentIP}', LastSeen = '{DateTime.Now:yyyy-MM-dd H:mm:ss}' WHERE MACAddress = '{currentMAC}';");
                                }
                                else
                                {
                                    // New, unknown device, add to DB
                                    DevicePowerState.Add(currentMAC, true);
                                    sqlSB.Append($"INSERT INTO Devices (MACAddress, DeviceType, Name, IP4Address, LastSeen) VALUES ('{currentMAC}','{DeviceType.UnknownDevice}','Device-{currentMAC}','{currentIP}','{DateTime.Now:yyyy-MM-dd H:mm:ss}')");
                                }
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }

                    // Update IP and LastSeen, add new entries
                    tsql.ExecuteNonQueryACon(sqlSB.ToString());

                    // Update last power-state
                    StringBuilder sb = new StringBuilder();
                    foreach (KeyValuePair <string, bool> entry in DevicePowerState)
                    {
                        sb.Append($"UPDATE Devices SET LastPowerState = '{(entry.Value ? 1 : 0)}' WHERE MACAddress = '{entry.Key}';");
                    }
                    tsql.ExecuteNonQueryACon(sb.ToString());
                }
                else
                {
                    if (pProgressReplyBGW.CancellationPending)
                    {
                        return;
                    }
                    MessageBox.Show("The given range is not valid. Make sure the IPs are in the same subnet. Note: Only C-Class IP-Adresses are currently supported", "Invalid Range", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }
 private void grvDevicesInGroup_GroupViewItemDoubleClick(GroupView sender, GroupViewItemDoubleClickEventArgs e)
 {
     // Remove from group
     sql.ExecuteNonQueryACon($"DELETE FROM GroupAssigns WHERE GroupID = '{GroupID}' AND MACAddress = '{grvDevicesInGroup.GroupViewItems[grvDevicesInGroup.SelectedItem].Tag}'");
     UpdateGroupDeviceList();
 }