private void SendCmd_Click(object sender, RoutedEventArgs e) { // Validate KeyCode Length Should be 1 to 14 Characters try { string trimed_code = Code_txt.Text.Trim(); string Sel_Devicename = Device_cmb.SelectedItem as string; /* Fetch the Device ID from Devices */ byte[] Deviceid = new byte[8]; for (int i = 0; i <= Global.Allowed_DevicesList.Count - 1; i++) { if (Global.Allowed_DevicesList[i].Device_name.Equals(Sel_Devicename)) { Global.Allowed_DevicesList[i].Device_Id.CopyTo(Deviceid, 0); break; } } bool IsDevice_Connected = false; for (int i = 0; i <= ServerClass.Devices_Buffer.Count - 1; i++) { if (ServerClass.Devices_Buffer[i].IsConnected && (ServerClass.Devices_Buffer[i].DeviceId.SequenceEqual(Deviceid))) // Device Has Connection { IsDevice_Connected = true; } } if (trimed_code == "") { // Error Related Port Number cannot be empty. MessageBox.Show("Port number cannot be empty !!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } else if (!IsDevice_Connected) { MessageBox.Show("Device Is not Connected !!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } else { /* Everything Is Ok I.e Valid port number, Device Has Connection */ CommandClass CmdObj = new CommandClass(); Deviceid.CopyTo(CmdObj.DeviceId, 0); Global.Command_KeyCodePkt_TypeID.CopyTo(CmdObj.TypeId, 0); // .CopyTo is the Best Method to copy the bytes CmdObj.Status = false; // Command- Status CmdObj.Senttime = DateTime.Now; // Command-Sentime CmdObj.SentCnt = 0; // COmmand - SentCnt byte[] databytes = InsertKeyCodeStringasbytes(trimed_code); // Prepared Command bytes byte[] Final_bytes = GetFinalbytesOfCommand(Deviceid, Global.Command_KeyCodePkt_TypeID, databytes); /* Initialize the CommandData Array */ CmdObj.CommandData = new byte[Final_bytes.Length]; // To Store Whole Command prepared byte[] array Final_bytes.CopyTo(CmdObj.CommandData, 0); // Storing Total Command bytes of CommandObj. Global.Command_KeyCodePkt_TypeID.CopyTo(CmdObj.TypeId, 0); //Command-TypeID ServerClass.Command_Buffer.Add(CmdObj); } } catch (Exception ex) { DateTime now_time = DateTime.Now; string time = Convert.ToString(now_time); Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured On SendSmd_Click Click: " + ex.Message + time); // Logging the Execptions Ocuured } // DeviceId Validation Whether It Is Connected Or Not. }
public void ProcessCommand_Buffer(object sender, ElapsedEventArgs elapsedEventArg) { /* Timer to process Failed Commands Again */ try { if (!Global.Is_Commandprocessing) { Global.Is_Commandprocessing = true; if (Command_Buffer != null) // If Data Buffer has some data elements { while (Command_Buffer.Count > 0) { CommandClass Command = Command_Buffer.Take(); // To Take the First Inserted Command TimeSpan Difference = DateTime.Now - Command.Senttime; // Diff B/w Sent time - Present Time long seconds = (long)Difference.TotalSeconds; // Convertig Differnce in to number of Seconds if ((Command.Status) || (Command.SentCnt == Global.MaxResendCnt)) // i.e Command Response Recieved , Reached MaxResendCnt (3) { /* Remove the Command */ } else if ((seconds > Global.ResendwaitTime) || (Command.SentCnt == 0)) /* Command Status Is False Then Only Process It. i.e We are Updatin Sentime when Setcnt=0;*/ { Command.SentCnt = (byte)Command.SentCnt + 1; Command.Senttime = DateTime.Now; bool IsDeviceConnected = false; int i; for (i = 0; i <= Devices_Buffer.Count - 1; i++) { if (Devices_Buffer[i].DeviceId.SequenceEqual(Command.DeviceId)) { if (Devices_Buffer[i].IsConnected == true) /* Device Is Connected */ { IsDeviceConnected = true; break; } } } if (IsDeviceConnected) /* So Device HAs Valid COnnection*/ { SendDatatoClient(Command.CommandData, Devices_Buffer[i].ClientStream); // } else { /* Show Error Dailog or Log the Command to Failed Commands */ } Command_Buffer.Add(Command); // Adding Command Back to the Commands_Buffer at last } else { Command_Buffer.Add(Command); // Adding Command Back to the Commands_Buffer at last } } } Global.Is_Commandprocessing = false; }// End of Is_CommandProcessing } catch (Exception ex) { DateTime now_time = DateTime.Now; string time = Convert.ToString(now_time); Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured on ProcessCommand_Buffer: " + ex.Message + time); // Logging the Execptions Ocuured Global.Is_Commandprocessing = false; } }