Esempio n. 1
0
        public void Merge(Spectrum s)
        {   
            // Merge a given spectrum

            Livetime += s.Livetime;
            Realtime += s.Realtime;
            for(int i=0; i<mChannels.Count; i++)            
                mChannels[i] += s.mChannels[i];        
        }
Esempio n. 2
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;
        }        
Esempio n. 3
0
        public Spectrum Clone()
        {
            // Create a clone of this spectrum

            Spectrum res = new Spectrum();                        
            res.mChannels.AddRange(mChannels.ToArray());
            res.SessionName = SessionName;
            res.SessionIndex = SessionIndex;
            res.Label = Label;
            res.NumChannels = NumChannels;
            res.MaxCount = MaxCount;
            res.MinCount = MinCount;
            res.TotalCount = TotalCount;
            res.IsPreview = IsPreview;
            res.LatitudeStart = LatitudeStart;
            res.LongitudeStart = LongitudeStart;
            res.AltitudeStart = AltitudeStart;
            res.LatitudeEnd = LatitudeEnd;
            res.LongitudeEnd = LongitudeEnd;
            res.AltitudeEnd = AltitudeEnd;
            res.GpsTimeStart = GpsTimeStart;
            res.GpsTimeEnd = GpsTimeEnd;
            res.GpsSpeedStart = GpsSpeedStart;
            res.GpsSpeedEnd = GpsSpeedEnd;
            res.Realtime = Realtime;
            res.Livetime = Livetime;
            res.SpectralInput = SpectralInput;
            res.Doserate = Doserate;
            return res;
        }
Esempio n. 4
0
        public void Add(Spectrum spec)
        {
            // Add a new spectrum to the list of spectrums

            Spectrums.Add(spec);
            NumChannels = spec.NumChannels;

            // Update state

            if (spec.MaxCount > MaxChannelCount)
                MaxChannelCount = spec.MaxCount;
            if (spec.MinCount < MinChannelCount)
                MinChannelCount = spec.MinCount;            
        }        
Esempio n. 5
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;
        }   
Esempio n. 6
0
        public void SetSelectedSessionIndices(int index1, int index2)
        {
            int idx1 = lbSession.FindStringExact(session.Name + " - " + index1);
            int idx2 = lbSession.FindStringExact(session.Name + " - " + index2);

            if (idx1 == -1 || idx2 == -1)
            {
                return;
            }

            if (idx1 > idx2)
            {
                Utils.Swap(ref idx1, ref idx2);
            }

            ClearSpectrumInfo();

            lbSession.SelectedIndexChanged -= lbSession_SelectedIndexChanged;
            lbSession.ClearSelected();
            for (int i = idx1; i <= idx2; i++)
            {
                lbSession.SetSelected(i, true);
            }
            lbSession.SelectedIndexChanged += lbSession_SelectedIndexChanged;

            bkgScale = (float)lbSession.SelectedIndices.Count; // Store scalefactor for background livetime

            Spectrum s1 = (Spectrum)lbSession.Items[idx2];
            Spectrum s2 = (Spectrum)lbSession.Items[idx1];

            double realTime = 0;
            double liveTime = 0;

            // Merge spectrums
            string title = "Merged: " + s1.SessionIndex + " - " + s2.SessionIndex;

            float[] chans = new float[(int)s1.NumChannels];
            float   maxCnt = s1.MaxCount, minCnt = s1.MinCount;
            float   totCnt = 0;

            for (int i = 0; i < lbSession.SelectedItems.Count; i++)
            {
                Spectrum s = (Spectrum)lbSession.SelectedItems[i];
                for (int j = 0; j < s.NumChannels; j++)
                {
                    chans[j] += s.Channels[j];
                }

                if (s.MaxCount > maxCnt)
                {
                    maxCnt = s.MaxCount;
                }
                if (s.MinCount < minCnt)
                {
                    minCnt = s.MinCount;
                }

                totCnt += s.TotalCount;

                realTime += ((double)s.Realtime) / 1000000.0;
                liveTime += ((double)s.Livetime) / 1000000.0;
            }

            // Populate controls
            ShowSpectrum(title, chans, maxCnt, minCnt);

            lblRealtime.Text   = "Realtime:" + realTime;
            lblLivetime.Text   = "Livetime:" + liveTime;
            lblSession.Text    = "Session: " + s1.SessionName;
            lblIndex.Text      = "Index: " + s1.SessionIndex + " - " + s2.SessionIndex;
            lblLatitude.Text   = "";
            lblLongitude.Text  = "";
            lblAltitude.Text   = "";
            lblGpsTime.Text    = "";
            lblMaxCount.Text   = "Max count: " + maxCnt;
            lblMinCount.Text   = "Min count: " + minCnt;
            lblTotalCount.Text = "Total count: " + totCnt;
            lblDoserate.Text   = "";
        }
Esempio n. 7
0
        private bool dispatchRecvMsg(APISpectrum apiSpec)
        {
            // Handle messages received from network

            try
            {
                // Session spectrum received successfully
                Spectrum spec = new Spectrum(apiSpec);

                // Normal session spectrum received
                log.Info(spec.Label + " received");

                // Add spectrum to session
                if (session != null && session.IsLoaded && session.Name == spec.SessionName)
                {
                    session.Add(spec);

                    // Add spectrum to UI list
                    bool updateSelectedIndex = false;
                    if (lbSession.SelectedIndex == 0)
                    {
                        updateSelectedIndex = true;
                    }

                    int index = 0, last_index = 0;

                    for (int i = 0; i < lbSession.Items.Count; i++)
                    {
                        Spectrum s = lbSession.Items[i] as Spectrum;
                        last_index = s.SessionIndex;
                        if (last_index < spec.SessionIndex)
                        {
                            index = i;
                            break;
                        }
                    }

                    if (last_index > spec.SessionIndex)
                    {
                        lbSession.Items.Add(spec);
                    }
                    else
                    {
                        lbSession.Items.Insert(index, spec);
                    }

                    if (updateSelectedIndex)
                    {
                        lbSession.ClearSelected();
                        lbSession.SetSelected(0, true);
                    }

                    // Notify external forms about new spectrum

                    parent.AddSpectrum(spec);

                    if (session.Spectrums.Count == 1)
                    {
                        parent.PositionMap(spec.Latitude, spec.Longitude);
                    }

                    if (progress.Value < progress.Maximum)
                    {
                        progress.Value++;
                    }

                    if (progress.Value >= progress.Maximum)
                    {
                        progress.Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return(false);
            }

            return(true);
        }
Esempio n. 8
0
        private void gmap_OnMarkerClick(GMapMarker item, MouseEventArgs e)
        {
            Spectrum s = item.Tag as Spectrum;

            parent.SetSelectedSessionIndex(s.SessionIndex);
        }
Esempio n. 9
0
        public void AddMarker(Spectrum s)
        {
            if (currentSession == null)
                return;            

            Bitmap bmp = null;
            double dose = s.Doserate / 1000.0;

            if (dose <= 1.0)
                bmp = bmpBlue;
            else if (dose <= 5.0)
                bmp = bmpGreen;
            else if (dose <= 10.0)
                bmp = bmpYellow;
            else if (dose <= 20.0)
                bmp = bmpOrange;
            else bmp = bmpRed;

            // Add map marker            
            GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(s.LatitudeStart, s.LongitudeStart), bmp);
            marker.Tag = s;
            marker.ToolTipMode = MarkerTooltipMode.OnMouseOver;
            marker.ToolTipText = s.ToString() 
                + Environment.NewLine + "Lat start: " + s.LatitudeStart 
                + Environment.NewLine + "Lon start: " + s.LongitudeStart 
                + Environment.NewLine + "Alt start: " + s.AltitudeStart;
            overlay.Markers.Add(marker);
        }
Esempio n. 10
0
 public FormSourceActivity(CrashSettings settings, Spectrum spec)
 {
     InitializeComponent();
     mSettings = settings;
     mSpectrum = spec;
 }