/// <summary> /// Constructor /// </summary> public DAPEmulatorUI() { //ignore cross-thread check System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; InitializeComponent(); this.version.Text = dapParameters.sw_version; this.sn.Text = dapParameters.InternalSN; this.factor.Text = dapParameters.CorrectionFactor.ToString(); //bind method UpdateUIDelegate = new DelegateCollection.updateUIControlDelegate(this.UpdateControl); }
/// <summary> /// method for receiving and processing packets from client in a loop; /// there are different prefixes in data string: /// "%SFD", it is used to write Correction Factor of DAP to DAP Emulator; /// /// </summary> /// <param name="obj"></param> public void RunThread(object UpdataUIDelegate) { threadDelegate = UpdataUIDelegate as DelegateCollection.updateUIControlDelegate; Dictionary <string, string> factDict; string jsonStr = string.Empty; string command = string.Empty; string dataContent = string.Empty; string logStr = string.Empty; while (true) { byte[] buffer = new byte[2048]; int count = commSocket.Receive(buffer); if (count == 0) { break; } else { jsonStr = Encoding.Default.GetString(buffer, 0, count); dapProtocol = JsonConvert.DeserializeObject <DAPProtocol>(jsonStr); command = dapProtocol.Head; switch (command) { case "TST": if (threadDelegate != null) { threadDelegate(command, true, commSocket.RemoteEndPoint.ToString()); } factDict = new Dictionary <string, string> { { "data", command } }; dapProtocol = new DAPProtocol("TST", factDict); buffer = utils.JsonDataOperationProtocol(dapProtocol); commSocket.Send(buffer); if (threadDelegate != null) { threadDelegate(command, false, commSocket.RemoteEndPoint.ToString()); } break; case "%RFD": if (threadDelegate != null) { threadDelegate(command, true, commSocket.RemoteEndPoint.ToString()); } dataContent = dapParameters.CorrectionFactor.ToString(); factDict = new Dictionary <string, string> { { "data", dataContent } }; dapProtocol = new DAPProtocol("%RFD", factDict); buffer = utils.JsonDataOperationProtocol(dapProtocol); commSocket.Send(buffer); if (threadDelegate != null) { threadDelegate(command, false, commSocket.RemoteEndPoint.ToString()); } break; case "%SFD": if (threadDelegate != null) { threadDelegate(command, true, commSocket.RemoteEndPoint.ToString()); } double tempCorrectionFactor = double.Parse(dapProtocol.Body["data"]); if ((int)(tempCorrectionFactor * 100) != (int)(dapParameters.CorrectionFactor * 100)) { dapParameters.CorrectionFactor = tempCorrectionFactor; emulatorUI.factor.Text = dapParameters.CorrectionFactor.ToString(); Application.DoEvents(); dataContent = "Correction factor save successfully"; } else { dataContent = "Correction factor does not change"; } factDict = new Dictionary <string, string> { { "data", dataContent } }; dapProtocol = new DAPProtocol("%SFD", factDict); buffer = utils.JsonDataOperationProtocol(dapProtocol); commSocket.Send(buffer); if (threadDelegate != null) { threadDelegate(command, false, commSocket.RemoteEndPoint.ToString()); } MessageBox.Show(dapProtocol.Body["data"]); break; case "LoadAllData": if (threadDelegate != null) { threadDelegate(command, true, commSocket.RemoteEndPoint.ToString()); } factDict = new Dictionary <string, string> { { "sw_version", dapParameters.sw_version }, { "InternalSN", dapParameters.InternalSN }, { "CorrectionFactor", dapParameters.CorrectionFactor.ToString() }, { "AirPressure", dapParameters.AirPressure.ToString() }, { "Temperature", dapParameters.Temperature.ToString() }, }; if (threadDelegate != null) { threadDelegate(command, false, commSocket.RemoteEndPoint.ToString()); } dapProtocol = new DAPProtocol("LoadAllData", factDict); buffer = utils.JsonDataOperationProtocol(dapProtocol); commSocket.Send(buffer); break; case "reset": if (threadDelegate != null) { threadDelegate(command, true, commSocket.RemoteEndPoint.ToString()); } factDict = new Dictionary <string, string> { { "sw_version", dapParameters.sw_version_factory }, { "InternalSN", dapParameters.InternalSN_factory }, { "CorrectionFactor", dapParameters.CorrectionFactor_factory.ToString() }, { "AirPressure", dapParameters.AirPressure_factory.ToString() }, { "Temperature", dapParameters.Temperature_factory.ToString() }, }; dapProtocol = new DAPProtocol("reset", factDict); buffer = utils.JsonDataOperationProtocol(dapProtocol); commSocket.Send(buffer); if (threadDelegate != null) { threadDelegate(command, false, commSocket.RemoteEndPoint.ToString()); } break; case "savePT": if (threadDelegate != null) { threadDelegate(command, true, commSocket.RemoteEndPoint.ToString()); } int tempAirPressure = int.Parse(dapProtocol.Body["AirPressure"]); int tempTemperature = int.Parse(dapProtocol.Body["Temperature"]); if ((tempAirPressure != dapParameters.AirPressure) || (tempTemperature != dapParameters.Temperature)) { dapParameters.AirPressure = tempAirPressure; dapParameters.Temperature = tempTemperature; dataContent = "AirPressure and Temperature saved successfully"; } else { dataContent = "the value of pressure or temperature does not change"; } factDict = new Dictionary <string, string> { { "data", dataContent } }; dapProtocol = new DAPProtocol("savePT", factDict); buffer = utils.JsonDataOperationProtocol(dapProtocol); commSocket.Send(buffer); if (threadDelegate != null) { threadDelegate(command, false, commSocket.RemoteEndPoint.ToString()); } break; case "Validate": if (threadDelegate != null) { threadDelegate(command, true, commSocket.RemoteEndPoint.ToString()); } double tempFactor = double.Parse(dapProtocol.Body["CorrectionFactor"]); dataContent = "success,Validate Successfully"; if ((tempFactor < 2.50) && (tempFactor > 0.25)) { dataContent = "failure,sorry, inputed correction factor is not within tolerance"; } factDict = new Dictionary <string, string> { { "data", dataContent } }; dapProtocol = new DAPProtocol("Validate", factDict); buffer = utils.JsonDataOperationProtocol(dapProtocol); commSocket.Send(buffer); if (threadDelegate != null) { threadDelegate(command, false, commSocket.RemoteEndPoint.ToString()); } break; default: break; } } } }