diff --git a/Reseter2/CompName.cs b/Reseter2/CompName.cs index 14aa391..4136d1f 100644 --- a/Reseter2/CompName.cs +++ b/Reseter2/CompName.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; @@ -10,19 +11,24 @@ namespace Reseter2 { private string Name { get; set; } private string Description { get; set; } - private string Ip { get; set; } + private IPAddress Ip { get; set; } public CompId(string name) { this.Name = name; } + + public CompId(IPAddress ip) + { + this.Ip = ip; + } public CompId(string name, string description) { this.Name = name; this.Description = description; } - public CompId(string name, string description, string ip) + public CompId(string name, string description, IPAddress ip) { this.Name = name; this.Description = description; @@ -35,6 +41,22 @@ namespace Reseter2 return Name; } + public IPAddress GetIP() + { + return Ip; + } + + public void SetIP(IPAddress ip) + { + Ip = ip; + } + + public string GetResetName() + { + if (Name != null) return Name; + return Ip.ToString(); + } + public string GetDescription() { return Description; diff --git a/Reseter2/Form1.Designer.cs b/Reseter2/Form1.Designer.cs index ba35030..e1208c9 100644 --- a/Reseter2/Form1.Designer.cs +++ b/Reseter2/Form1.Designer.cs @@ -103,31 +103,33 @@ | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.flowLayoutPanel1.AutoScroll = true; + this.flowLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.flowLayoutPanel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown; this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 67); this.flowLayoutPanel1.Name = "flowLayoutPanel1"; - this.flowLayoutPanel1.Size = new System.Drawing.Size(394, 380); + this.flowLayoutPanel1.Size = new System.Drawing.Size(394, 650); this.flowLayoutPanel1.TabIndex = 4; this.flowLayoutPanel1.WrapContents = false; // // timer1 // this.timer1.Enabled = true; - this.timer1.Interval = 300; + this.timer1.Interval = 1000; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; - this.ClientSize = new System.Drawing.Size(394, 447); + this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.ClientSize = new System.Drawing.Size(394, 717); this.Controls.Add(this.flowLayoutPanel1); this.Controls.Add(this.toolStrip1); this.Controls.Add(this.bt_reset); this.Controls.Add(this.label1); this.Controls.Add(this.tb_comp); this.Name = "Form1"; - this.Text = "Form1"; + this.Text = "Reseter2"; this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); this.ResumeLayout(false); diff --git a/Reseter2/IComp.cs b/Reseter2/IComp.cs index 987ed09..7228365 100644 --- a/Reseter2/IComp.cs +++ b/Reseter2/IComp.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; @@ -9,6 +10,9 @@ namespace Reseter2 internal interface IComp { string GetName(); + string GetResetName(); + IPAddress GetIP(); + void SetIP(IPAddress ip); string GetDescription(); } diff --git a/Reseter2/IStatusTask.cs b/Reseter2/IStatusTask.cs index f34f161..232e26f 100644 --- a/Reseter2/IStatusTask.cs +++ b/Reseter2/IStatusTask.cs @@ -14,7 +14,14 @@ namespace Reseter2 resetertask = reseterTask; } public abstract Task Tick(); - public abstract void Stop(); public abstract void Next(); + public void Stop() { + resetertask.StatusTask = new StatusRebootStop(resetertask); + } + public void RebootReturn() + { + resetertask.StatusTask = new StatusPreReboot(resetertask); + } + } } diff --git a/Reseter2/PingResult.cs b/Reseter2/PingResult.cs index 12924b8..7bb6d28 100644 --- a/Reseter2/PingResult.cs +++ b/Reseter2/PingResult.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -11,10 +12,12 @@ namespace Reseter2 { public long Ping { get; set; } public int TimeoutPing { get; set; } - public PingResult(long ping, int timeoutPing) + public IPAddress Ip; + public PingResult(long ping, int timeoutPing, IPAddress ip) { Ping = ping; TimeoutPing = timeoutPing; + Ip = ip; } } } diff --git a/Reseter2/Pinger.cs b/Reseter2/Pinger.cs index b5baa55..2224069 100644 --- a/Reseter2/Pinger.cs +++ b/Reseter2/Pinger.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using System.Net.NetworkInformation; +using System.Net; namespace Reseter2 { @@ -12,6 +13,7 @@ namespace Reseter2 { private string NameOrAddress; private int TimeoutCount; + private IPAddress Ip; public Pinger(string nameOrAddress) { @@ -31,6 +33,7 @@ namespace Reseter2 PingReply reply = pinger.Send(this.NameOrAddress); pingable = reply.Status == IPStatus.TimedOut; ping = reply.RoundtripTime; + Ip = reply.Address; } catch (PingException) { @@ -44,7 +47,7 @@ namespace Reseter2 } } if (pingable) TimeoutCount++; - return new PingResult(ping, TimeoutCount); + return new PingResult(ping, TimeoutCount, Ip); } } diff --git a/Reseter2/ReseteTask.cs b/Reseter2/ReseteTask.cs index 8180169..d283931 100644 --- a/Reseter2/ReseteTask.cs +++ b/Reseter2/ReseteTask.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Reflection.Emit; using System.Text; @@ -10,26 +11,22 @@ namespace Reseter2 class ReseterTask { private Task task; - private IComp Comp; + public IComp Comp { get; } public AStatusTask StatusTask { get; set; } private TaskControl taskControl; private Pinger Pingers; - public delegate void DataEvents(string ping, string timeout); - public event DataEvents DataChange; + public Stopwatch sw = new Stopwatch(); + + public ReseterTask(IComp comp, TaskControl taskCntrl) { Comp = comp; taskControl = taskCntrl; StatusTask = new StatusPreReboot(this); - Pingers = new Pinger(Comp.GetName()); - DataChange += taskControl.DataContrl; + Pingers = new Pinger(Comp.GetResetName()); } - public string GetName() - { - return Comp.GetName(); - } - + public async Task Tick() { if (task != null) @@ -37,16 +34,34 @@ namespace Reseter2 if (task.IsCompleted){ //this.DataContrl(Ping().ToString(), Timeout().ToString()); PingResult pingResult = await task; - taskControl.DataContrl(pingResult.Ping.ToString() + "ms", pingResult.TimeoutPing.ToString()); + taskControl.DataContrl(pingResult.Ping.ToString() + "ms", pingResult.TimeoutPing.ToString(), pingResult.Ip, sw.Elapsed); + StatusTask.Next(); task = Task.Run(StatusTask.Tick); } } else { task = Task.Run(StatusTask.Tick); + } + + taskControl.TimeContrl(sw.Elapsed); - + } + + public string GetName() + { + return Comp.GetName(); + } + + public void RebootStop() + { + StatusTask.Stop(); + } + + public void RebootReturn() + { + StatusTask.RebootReturn(); } public void SetNameStage(string nameStage) diff --git a/Reseter2/Reseter2.csproj b/Reseter2/Reseter2.csproj index 0b05fe6..deb89bd 100644 --- a/Reseter2/Reseter2.csproj +++ b/Reseter2/Reseter2.csproj @@ -61,7 +61,11 @@ + + + + UserControl diff --git a/Reseter2/Shutdown.cs b/Reseter2/Shutdown.cs new file mode 100644 index 0000000..09791ec --- /dev/null +++ b/Reseter2/Shutdown.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Reseter2 +{ + class Shutdown + { + public static void Restart() + { + StartShutDown("-f -r -t 5"); + } + public static void RestartPC(string param) + { + StartShutDown("-f -r -t 5" + param); + } + public static void LogOff() + { + StartShutDown("-l"); + } + + public static void Shut() + { + StartShutDown("-f -s -t 5"); + } + private static void StartShutDown(string param) + { + ProcessStartInfo proc = new ProcessStartInfo(); + proc.FileName = "cmd"; + proc.WindowStyle = ProcessWindowStyle.Hidden; + proc.Arguments = "/C shutdown " + param; + Process.Start(proc); + } + } +} diff --git a/Reseter2/StatusReboot.cs b/Reseter2/StatusReboot.cs index 1e11fbb..e64b0e4 100644 --- a/Reseter2/StatusReboot.cs +++ b/Reseter2/StatusReboot.cs @@ -3,40 +3,35 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock; namespace Reseter2 { internal class StatusReboot : AStatusTask { private int TimeCount; + private PingResult PingResult = new PingResult(0, 0, null); public StatusReboot(ReseterTask reseterTask) : base(reseterTask) { - // resetertask.SetNameStage("Перезагрузка"); + resetertask.SetNameStage("Отправляем команду перезагрузки"); + //Shutdown.RestartPC(reseterTask.Comp.GetName()); } public override Task Tick() { - - PingResult result = resetertask.Ping(); - if (result.Ping == 0) + PingResult = resetertask.Ping(); + return Task.FromResult(PingResult); + } + public override void Next() + { + if (PingResult.Ping == 0) { TimeCount++; } if (TimeCount > 2) { - resetertask.StatusTask = new StatusPreReboot(resetertask); + resetertask.StatusTask = new StatusRebooting(resetertask); } - return Task.FromResult(result); - // return resetertask.DataContrl(pingResult.Ping.ToString(), pingResult.Ping.ToString()); - - } - public override void Next() - { - - } - public override void Stop() - { - } } } diff --git a/Reseter2/StatusRebootStop.cs b/Reseter2/StatusRebootStop.cs new file mode 100644 index 0000000..4f53fa3 --- /dev/null +++ b/Reseter2/StatusRebootStop.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Reseter2 +{ + internal class StatusRebootStop : AStatusTask + { + private PingResult PingResult; + public StatusRebootStop(ReseterTask reseterTask) : base(reseterTask) + { + resetertask.SetNameStage("Перезагрузка остановленна"); + PingResult = resetertask.Ping(); + reseterTask.sw.Stop(); + } + + public override Task Tick() + { + return Task.FromResult(PingResult); + } + public override void Next() + { + } + } +} diff --git a/Reseter2/StatusRebootSucces.cs b/Reseter2/StatusRebootSucces.cs new file mode 100644 index 0000000..2d6b126 --- /dev/null +++ b/Reseter2/StatusRebootSucces.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Reseter2 +{ + internal class StatusRebootSucces : AStatusTask + { + private PingResult PingResult; + public StatusRebootSucces(ReseterTask reseterTask) : base(reseterTask) + { + resetertask.SetNameStage("Успешно перезагруженно"); + PingResult = resetertask.Ping(); + reseterTask.sw.Stop(); + } + + public override Task Tick() + { + return Task.FromResult(PingResult); + } + public override void Next() + { + } + } +} diff --git a/Reseter2/StatusRebooting.cs b/Reseter2/StatusRebooting.cs new file mode 100644 index 0000000..24fe697 --- /dev/null +++ b/Reseter2/StatusRebooting.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Reseter2 +{ + internal class StatusRebooting : AStatusTask + { + private int TimeCount; + private PingResult PingResult = new PingResult(0, 0, null); + public StatusRebooting(ReseterTask reseterTask) : base(reseterTask) + { + resetertask.SetNameStage("Перезагрузка"); + + } + + public override Task Tick() + { + PingResult = resetertask.Ping(); + return Task.FromResult(PingResult); + } + public override void Next() + { + if (PingResult.Ping > 0) + { + TimeCount++; + } + if (TimeCount > 10) + { + resetertask.StatusTask = new StatusPreReboot(resetertask); + } + } + } +} diff --git a/Reseter2/StausPreReboot.cs b/Reseter2/StausPreReboot.cs index 6a03464..9c3c40d 100644 --- a/Reseter2/StausPreReboot.cs +++ b/Reseter2/StausPreReboot.cs @@ -9,30 +9,32 @@ namespace Reseter2 internal class StatusPreReboot : AStatusTask { private int time; + private PingResult PingResult = new PingResult(0,0,null); public StatusPreReboot(ReseterTask reseterTask) : base(reseterTask) { resetertask.SetNameStage("Проверка связи"); + resetertask.sw.Restart(); } public override Task Tick() { time++; - PingResult result = resetertask.Ping(); - if (result.Ping > 0) - { - resetertask.StatusTask = new StatusReboot(resetertask); - } - return Task.FromResult(result); + PingResult = resetertask.Ping(); + return Task.FromResult(PingResult); // return resetertask.DataContrl(pingResult.Ping.ToString(), pingResult.Ping.ToString()); } public override void Next() { - + if (PingResult.Ping != null) + { + if (PingResult.Ping > 0) + { + resetertask.StatusTask = new StatusReboot(resetertask); + } + } + } - public override void Stop() - { - } } } \ No newline at end of file diff --git a/Reseter2/TaskControl.Designer.cs b/Reseter2/TaskControl.Designer.cs index 2e43b2f..73df15e 100644 --- a/Reseter2/TaskControl.Designer.cs +++ b/Reseter2/TaskControl.Designer.cs @@ -40,6 +40,9 @@ this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); + this.button3 = new System.Windows.Forms.Button(); + this.label3 = new System.Windows.Forms.Label(); + this.lb_time = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); this.SuspendLayout(); // @@ -65,20 +68,20 @@ // lb_name // this.lb_name.AutoSize = true; - this.lb_name.Location = new System.Drawing.Point(128, 23); + this.lb_name.Location = new System.Drawing.Point(121, 23); this.lb_name.Name = "lb_name"; - this.lb_name.Size = new System.Drawing.Size(57, 13); + this.lb_name.Size = new System.Drawing.Size(64, 13); this.lb_name.TabIndex = 2; - this.lb_name.Text = "ma001234"; + this.lb_name.Text = "-------------------"; // // lb_ip // this.lb_ip.AutoSize = true; - this.lb_ip.Location = new System.Drawing.Point(97, 36); + this.lb_ip.Location = new System.Drawing.Point(90, 36); this.lb_ip.Name = "lb_ip"; this.lb_ip.Size = new System.Drawing.Size(70, 13); this.lb_ip.TabIndex = 3; - this.lb_ip.Text = "10.3.123.123"; + this.lb_ip.Text = "---------------------"; // // lb_stage // @@ -92,7 +95,7 @@ // label4 // this.label4.AutoSize = true; - this.label4.Location = new System.Drawing.Point(74, 49); + this.label4.Location = new System.Drawing.Point(173, 36); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(28, 13); this.label4.TabIndex = 5; @@ -101,7 +104,7 @@ // lb_ping // this.lb_ping.AutoSize = true; - this.lb_ping.Location = new System.Drawing.Point(108, 49); + this.lb_ping.Location = new System.Drawing.Point(207, 36); this.lb_ping.Name = "lb_ping"; this.lb_ping.Size = new System.Drawing.Size(0, 13); this.lb_ping.TabIndex = 6; @@ -109,7 +112,7 @@ // Timeout // this.Timeout.AutoSize = true; - this.Timeout.Location = new System.Drawing.Point(173, 49); + this.Timeout.Location = new System.Drawing.Point(173, 52); this.Timeout.Name = "Timeout"; this.Timeout.Size = new System.Drawing.Size(45, 13); this.Timeout.TabIndex = 7; @@ -118,29 +121,30 @@ // lb_timeout // this.lb_timeout.AutoSize = true; - this.lb_timeout.Location = new System.Drawing.Point(225, 49); + this.lb_timeout.Location = new System.Drawing.Point(224, 52); this.lb_timeout.Name = "lb_timeout"; this.lb_timeout.Size = new System.Drawing.Size(0, 13); this.lb_timeout.TabIndex = 8; // // button1 // - this.button1.Location = new System.Drawing.Point(267, 13); + this.button1.Location = new System.Drawing.Point(270, 5); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(96, 23); this.button1.TabIndex = 9; - this.button1.Text = "Остановить"; + this.button1.Text = "Закрыть"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // - this.button2.Location = new System.Drawing.Point(267, 42); + this.button2.Location = new System.Drawing.Point(270, 47); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(96, 23); this.button2.TabIndex = 10; this.button2.Text = "Перезапустить"; this.button2.UseVisualStyleBackColor = true; + this.button2.Click += new System.EventHandler(this.button2_Click); // // pictureBox1 // @@ -150,11 +154,42 @@ this.pictureBox1.TabIndex = 11; this.pictureBox1.TabStop = false; // + // button3 + // + this.button3.Location = new System.Drawing.Point(270, 26); + this.button3.Name = "button3"; + this.button3.Size = new System.Drawing.Size(96, 23); + this.button3.TabIndex = 12; + this.button3.Text = "Пауза"; + this.button3.UseVisualStyleBackColor = true; + this.button3.Click += new System.EventHandler(this.button3_Click); + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(73, 52); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(46, 13); + this.label3.TabIndex = 13; + this.label3.Text = "Таймер"; + // + // lb_time + // + this.lb_time.AutoSize = true; + this.lb_time.Location = new System.Drawing.Point(118, 52); + this.lb_time.Name = "lb_time"; + this.lb_time.Size = new System.Drawing.Size(34, 13); + this.lb_time.TabIndex = 14; + this.lb_time.Text = "00:00"; + // // TaskControl // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.Controls.Add(this.lb_time); + this.Controls.Add(this.label3); + this.Controls.Add(this.button3); this.Controls.Add(this.pictureBox1); this.Controls.Add(this.button2); this.Controls.Add(this.button1); @@ -189,5 +224,8 @@ private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.Button button3; + private System.Windows.Forms.Label label3; + private System.Windows.Forms.Label lb_time; } } diff --git a/Reseter2/TaskControl.cs b/Reseter2/TaskControl.cs index 8457b32..e99d212 100644 --- a/Reseter2/TaskControl.cs +++ b/Reseter2/TaskControl.cs @@ -4,6 +4,7 @@ using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -23,14 +24,22 @@ namespace Reseter2 public void Intit(ReseterTask res) { reseterTask = res; - lb_name.Text = reseterTask.GetName(); + if (reseterTask.GetName() != null) lb_name.Text = reseterTask.GetName(); + if (reseterTask.Comp.GetIP() != null) lb_ip.Text = reseterTask.Comp.GetIP().ToString(); } - public void DataContrl(string ping, string timeout) + public void DataContrl(string ping, string timeout, IPAddress ip, TimeSpan time) { lb_ping.Text = ping; lb_timeout.Text = timeout; + if(ip != null)lb_ip.Text = ip.ToString(); + lb_time.Text = time.ToString(@"mm\:ss"); + } + public void TimeContrl(TimeSpan time) + { + lb_time.Text = time.ToString(@"mm\:ss"); + } public void SetNameStage(string nameStage) { @@ -45,5 +54,15 @@ namespace Reseter2 { Reseter.Clear(reseterTask, this); } + + private void button3_Click(object sender, EventArgs e) + { + reseterTask.RebootStop(); + } + + private void button2_Click(object sender, EventArgs e) + { + reseterTask.RebootReturn(); + } } } diff --git a/Reseter2/reseter.cs b/Reseter2/reseter.cs index a6f3ecd..44b70ee 100644 --- a/Reseter2/reseter.cs +++ b/Reseter2/reseter.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; +using System.Net; using System.Reflection.Emit; using System.Runtime.Remoting.Metadata.W3cXsd2001; using System.Text; @@ -21,8 +22,22 @@ namespace Reseter2 } public static void AddTask(String name) { - CompId compid = new CompId(name); - AddTask(compid); + try + { + IPAddress ip = IPAddress.Parse(name); + CompId compid = new CompId(ip); + AddTask(compid); + } + catch (FormatException e) + { + CompId compid = new CompId(name); + AddTask(compid); + } + catch + { + + } + } public static void AddTask(IComp compName) { @@ -34,18 +49,27 @@ namespace Reseter2 list_task.Add(reseterTask); } - public static void Clear(ReseterTask reseterTask, TaskControl taskControl) + public static async void Clear(ReseterTask reseterTask, TaskControl taskControl) { flow_conteiner.Controls.Remove(taskControl); list_task.Remove(reseterTask); } - public static void Tick() + public static async void Tick() { - foreach (var item in list_task) + try { - item.Tick(); + foreach (var item in list_task) + { + + item.Tick(); + + + } } + catch + { + } } } }