Exemple #1
0
        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;
        }        
Exemple #2
0
        public bool LoadSpectrums(string path)
        {
            // Load spectrums from a given path

            string jsonDir = path + Path.DirectorySeparatorChar + "json";
            if (!Directory.Exists(jsonDir))
                return false;

            string[] files = Directory.GetFiles(jsonDir, "*.json", SearchOption.TopDirectoryOnly);
            foreach (string filename in files)
            {
                string json = File.ReadAllText(filename);
                burn.Message msg = JsonConvert.DeserializeObject<burn.Message>(json);
                Spectrum spec = new Spectrum(msg);                                
                spec.CalculateDoserate(Detector, GEFactor);
                Add(spec);
            }

            Spectrums.Sort((a, b) => a.SessionIndex.CompareTo(b.SessionIndex));
            return true;
        }