private bool dispatchRecvMsg(burn.Message msg) { // Handle messages received from network Detector det = null; DetectorType detType = null; switch (msg.Command) { case "connect_ok": // Connection successful Utils.Log.Add("RECV: Connected to " + msg.Arguments["host"] + ":" + msg.Arguments["port"]); // Send a ping command to ensure a healthy connection burn.Message msgPing = new burn.Message("ping", null); sendMsg(msgPing); Utils.Log.Add("SEND: Sending ping message"); break; case "connect_failed": // Connection failed connected = false; Utils.Log.Add("RECV: Connection failed for " + msg.Arguments["host"] + ":" + msg.Arguments["port"] + " " + msg.Arguments["message"]); break; case "disconnect_ok": // Disconnect successful connected = false; lblConnectionStatus.ForeColor = Color.Red; lblConnectionStatus.Text = "Not connected"; Utils.Log.Add("RECV: Disconnected from peer"); // Change to session tab if (tabs.SelectedTab == pageSetup) tabs.SelectedTab = pageSessions; break; case "ping_ok": // Ping command received, update state connected = true; lblConnectionStatus.ForeColor = Color.Green; lblConnectionStatus.Text = "Connected to " + settings.LastIP + ":" + settings.LastPort; Utils.Log.Add("RECV: Received ping_ok from peer"); break; case "close_ok": // Remove service closed, close down network thread and update state netService.RequestStop(); netThread.Join(); connected = false; lblConnectionStatus.ForeColor = Color.Red; lblConnectionStatus.Text = "Not connected"; Utils.Log.Add("RECV: Disconnected from peer, peer closed"); break; case "new_session_ok": // New session created successfully bool prev = msg.Arguments["preview"].ToString() == "1"; if (prev) { // New session is a preview session, update log Utils.Log.Add("RECV: Preview session started"); } else { // New session is a normal session string sessionName = msg.Arguments["session_name"].ToString(); Utils.Log.Add("RECV: New session started: " + sessionName); // Create a session object and update state float livetime = Convert.ToSingle(msg.Arguments["livetime"]); int iterations = Convert.ToInt32(msg.Arguments["iterations"]); det = (Detector)cboxSetupDetector.SelectedItem; detType = settings.DetectorTypes.Find(dt => dt.Name == det.TypeName); session = new Session(settings.SessionRootDirectory, sessionName, "", livetime, iterations, det, detType); // Create session files and directories SaveSession(session); // Notify external forms about new session formWaterfallLive.SetSession(session); formROILive.SetSession(session); frmMap.SetSession(session); sessionRunning = true; } btnSetupStartTest.Enabled = false; btnSetupStopTest.Enabled = true; break; case "new_session_failed": // Creation of new session failed, log error message Utils.Log.Add("RECV: New session failed: " + msg.Arguments["message"]); break; case "stop_session_ok": // Stop session successful, update state Utils.Log.Add("RECV: Session stopped"); sessionRunning = false; btnSetupStartTest.Enabled = true; btnSetupStopTest.Enabled = false; break; case "session_finished": // Session finished successfully Utils.Log.Add("RECV: Session " + msg.Arguments["session_name"] + " finished"); sessionRunning = false; break; case "error": // An error occurred, log error message Utils.Log.Add("RECV: Error: " + msg.Arguments["message"]); break; case "error_socket": // An socket error occurred, log error message Utils.Log.Add("RECV: Socket error: " + msg.Arguments["error_code"] + " " + msg.Arguments["message"]); break; case "set_gain_ok": // Set gain command executed successfully Utils.Log.Add("RECV: set_gain ok: " + msg.Arguments["voltage"] + " " + msg.Arguments["coarse_gain"] + " " + msg.Arguments["fine_gain"] + " " + msg.Arguments["num_channels"] + " " + msg.Arguments["lld"] + " " + msg.Arguments["uld"]); // Update selected detector parameters det = (Detector)cboxSetupDetector.SelectedItem; det.CurrentHV = Convert.ToInt32(msg.Arguments["voltage"]); det.CurrentCoarseGain = Convert.ToDouble(msg.Arguments["coarse_gain"]); det.CurrentFineGain = Convert.ToDouble(msg.Arguments["fine_gain"]); det.CurrentNumChannels = Convert.ToInt32(msg.Arguments["num_channels"]); det.CurrentLLD = Convert.ToInt32(msg.Arguments["lld"]); det.CurrentULD = Convert.ToInt32(msg.Arguments["uld"]); // Update state btnSetupNext.Enabled = true; panelSetupGraph.Enabled = true; break; case "spectrum": // Session spectrum received successfully Spectrum spec = new Spectrum(msg); spec.CalculateDoserate(session.Detector, session.GEFactor); if (spec.IsPreview) { // Spectrum is a preview spectrum Utils.Log.Add("RECV: " + spec.Label + " preview spectrum received"); // Merge spectrum with our preview spectrum if (previewSpec == null) previewSpec = spec; else previewSpec.Merge(spec); // Reset and prepare setup graph GraphPane pane = graphSetup.GraphPane; pane.Chart.Fill = new Fill(SystemColors.ButtonFace); pane.Fill = new Fill(SystemColors.ButtonFace); pane.Title.Text = "Setup"; pane.XAxis.Title.Text = "Channel"; pane.YAxis.Title.Text = "Counts"; // Update setup graph setupGraphList.Clear(); for (int i = 0; i < previewSpec.Channels.Count; i++) setupGraphList.Add((double)i, (double)previewSpec.Channels[i]); pane.XAxis.Scale.Min = 0; pane.XAxis.Scale.Max = previewSpec.MaxCount; pane.YAxis.Scale.Min = 0; pane.YAxis.Scale.Max = previewSpec.MaxCount + (previewSpec.MaxCount / 10.0); pane.CurveList.Clear(); LineItem curve = pane.AddCurve("Spectrum", setupGraphList, Color.Red, SymbolType.None); curve.Line.Fill = new Fill(SystemColors.ButtonFace, Color.Red, 45F); pane.Chart.Fill = new Fill(SystemColors.ButtonFace, SystemColors.ButtonFace); pane.Legend.Fill = new Fill(SystemColors.ButtonFace, SystemColors.ButtonFace); pane.Fill = new Fill(SystemColors.ButtonFace, SystemColors.ButtonFace); graphSetup.RestoreScale(pane); graphSetup.AxisChange(); graphSetup.Refresh(); } else { // Normal session spectrum received successfully Utils.Log.Add("RECV: " + spec.Label + " session spectrum received"); // FIXME: Make sure session is allocated in case spectrums are ticking in // Store spectrum to disk string sessionPath = settings.SessionRootDirectory + Path.DirectorySeparatorChar + session.Name; string jsonPath = sessionPath + Path.DirectorySeparatorChar + "json"; if (!Directory.Exists(jsonPath)) Directory.CreateDirectory(jsonPath); string json = JsonConvert.SerializeObject(msg, Newtonsoft.Json.Formatting.Indented); using (TextWriter writer = new StreamWriter(jsonPath + Path.DirectorySeparatorChar + spec.SessionIndex + ".json")) { writer.Write(json); } // Add spectrum to session session.Add(spec); // Add spectrum to UI list lbSession.Items.Insert(0, spec); // Notify external forms about new spectrum frmMap.AddMarker(spec); formWaterfallLive.UpdatePane(); formROILive.UpdatePane(); } break; default: // Unhandled message received, update log string info = msg.Command + " -> "; foreach (KeyValuePair<string, object> item in msg.Arguments) info += item.Key + ":" + item.Value.ToString() + ", "; Utils.Log.Add("RECV: Unhandeled command: " + info); break; } return true; }
private void btnSetupStartTest_Click(object sender, EventArgs e) { if (sessionRunning) { MessageBox.Show("A session is already running"); return; } burn.Message msg = new burn.Message("new_session", null); msg.AddParameter("session_name", String.Format("{0:ddMMyyyy_HHmmss}", DateTime.Now)); msg.AddParameter("preview", 1); msg.AddParameter("iterations", -1); msg.AddParameter("livetime", 1); msg.AddParameter("delay", 0); sendMsg(msg); ClearSetup(); previewSpec = null; Utils.Log.Add("SEND: new_session (preview)"); }
private void btnSetupSetParams_Click(object sender, EventArgs e) { if(String.IsNullOrEmpty(cboxSetupDetector.Text.Trim())) { MessageBox.Show("No detector selected"); return; } // Convert parameters int voltage = tbarSetupVoltage.Value; double coarse = 0f; double fine = 0f; int nchannels = 0; try { coarse = Convert.ToSingle(cboxSetupCoarseGain.Text); fine = Convert.ToDouble((double)tbarSetupFineGain.Value / 1000d); nchannels = Convert.ToInt32(cboxSetupChannels.Text); } catch { MessageBox.Show("Gain: Invalid format (fine gain or coarse gain"); return; } int lld = tbarSetupLLD.Value; int uld = tbarSetupULD.Value; if(lld > uld) { MessageBox.Show("LLD can not be bigger than ULD"); return; } // Create and send network message burn.Message msg = new burn.Message("set_gain", null); msg.AddParameter("voltage", voltage); msg.AddParameter("coarse_gain", coarse); msg.AddParameter("fine_gain", fine); msg.AddParameter("num_channels", nchannels); msg.AddParameter("lld", lld); msg.AddParameter("uld", uld); sendMsg(msg); Utils.Log.Add("SEND: set_gain"); }
private void btnSetupNext_Click(object sender, EventArgs e) { SaveSettings(); Detector det = (Detector)cboxSetupDetector.SelectedItem; if(!String.IsNullOrEmpty(tbSetupLivetime.Text.Trim())) det.CurrentLivetime = Convert.ToInt32(tbSetupLivetime.Text.Trim()); int iterations = String.IsNullOrEmpty(tbSetupSpectrumCount.Text.Trim()) ? -1 : Convert.ToInt32(tbSetupSpectrumCount.Text.Trim()); float delay = 0f; if (!String.IsNullOrEmpty(tbSetupDelay.Text.Trim())) delay = Convert.ToSingle(tbSetupDelay.Text.Trim()); ClearSession(); burn.Message msg = new burn.Message("new_session", null); msg.AddParameter("session_name", String.Format("{0:ddMMyyyy_HHmmss}", DateTime.Now)); msg.AddParameter("preview", 0); msg.AddParameter("iterations", iterations); msg.AddParameter("livetime", det.CurrentLivetime); msg.AddParameter("delay", delay); sendMsg(msg); Utils.Log.Add("SEND: new_session"); tabs.SelectedTab = pageSessions; }
private void menuItemConnect_Click(object sender, EventArgs e) { // Show connection form FormConnect form = new FormConnect(settings); if (form.ShowDialog() != DialogResult.OK) return; // Send a connect message to networking thread burn.Message msg = new burn.Message("connect", null); msg.AddParameter("host", settings.LastIP); msg.AddParameter("port", settings.LastPort); sendMsg(msg); Utils.Log.Add("Connecting to " + settings.LastIP + ":" + settings.LastPort + "..."); }