예제 #1
0
        public void AddChannel(ScalesDescriptor sd, DbScalesMeasureData lastMeaure = null)
        {
            IScales       scales  = SetupSource(sd);
            ScalesChannel channel = null;

            if (sd.StreamType == "serial")
            {
                SerialPortSettings settings = JsonConvert.DeserializeObject <SerialPortSettings>(sd.Settings);

                channel = new ScalesChannel
                {
                    Id              = sd.Id,
                    Code            = sd.Code,
                    Name            = string.Format("{0} ({1})", sd.Name, settings.PortName),
                    Active          = scales != null ? sd.Active : false,
                    Description     = sd.Description,
                    Exposure        = TimeSpan.FromMilliseconds(sd.Exposure),
                    Timeout         = TimeSpan.FromMilliseconds(sd.Timeout),
                    TriggerEmpty    = sd.TriggerEmpty,
                    TriggerLoading  = sd.TriggerLoading,
                    ChannelSettings = settings,
                    owner           = Program.mainForm
                };
            }

            if (sd.StreamType == "tcp")
            {
                TcpChannelSettings settings = JsonConvert.DeserializeObject <TcpChannelSettings>(sd.Settings);

                channel = new ScalesChannel
                {
                    Id              = sd.Id,
                    Code            = sd.Code,
                    Name            = string.Format("{0} ({1})", "TCP", settings.Address),
                    Active          = scales != null ? sd.Active : false,
                    Description     = sd.Description,
                    Exposure        = TimeSpan.FromMilliseconds(sd.Exposure),
                    Timeout         = TimeSpan.FromMilliseconds(sd.Timeout),
                    TriggerEmpty    = sd.TriggerEmpty,
                    TriggerLoading  = sd.TriggerLoading,
                    ChannelSettings = settings,
                    owner           = Program.mainForm
                };
            }

            if (lastMeaure != null)
            {
                channel.AcceptedTime  = lastMeaure.Time;
                channel.AcceptedValue = lastMeaure.Value;
            }

            Program.MeasuringChannels.Add(channel);

            channel.EventSource = scales;
        }
예제 #2
0
        public IScales SetupSource(ScalesDescriptor sd)
        {
            if (sd.StreamType == "serial")
            {
                SerialPortSettings settings = JsonConvert.DeserializeObject <SerialPortSettings>(sd.Settings);

                IScales scales = eventSource.FirstOrDefault(tmp => tmp.Id == settings.PortName);
                if (scales == null && sd.Active)
                {
                    LogManager.GetLogger(Application.ProductName).Info(string.Format("Настройка источника {0}", sd.Settings));

                    if (settings.PortName.StartsWith("MAN"))
                    {
                        ManualScales tmp = new ManualScales();
                        tmp.Id       = settings.PortName;
                        tmp.Settings = settings;
                        tmp.Exposure = TimeSpan.FromMilliseconds(sd.Exposure);
                        tmp.Timeout  = TimeSpan.FromMilliseconds(sd.Timeout);
                        scales       = tmp;

                        if (Program.mainForm != null)
                        {
                            ManualSourceControl ctrl = new ManualSourceControl(settings.PortName);
                            ctrl.Dock = DockStyle.Fill;
                            var panel = Program.mainForm.dockManager.AddPanel(DevExpress.XtraBars.Docking.DockingStyle.Float);
                            panel.Text = settings.PortName;
                            panel.Tag  = settings.PortName;
                            panel.Controls.Add(ctrl);
                            panel.ClosingPanel += (sender, e) =>
                            {
                                e.Cancel = true;
                                XtraMessageBox.Show("Необходимо деактивировать связанные каналы!", Program.mainForm.Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                            };
                        }
                    }
                    else
                    {
                        try
                        {
                            Scales tmp = new Scales();
                            tmp.Id       = settings.PortName;
                            tmp.Settings = settings;
                            tmp.Exposure = TimeSpan.FromMilliseconds(sd.Exposure);
                            tmp.Timeout  = TimeSpan.FromMilliseconds(sd.Timeout);
                            scales       = tmp;
                        }
                        catch
                        {
                            scales = null;
                        }
                    }

                    if (scales != null)
                    {
                        try
                        {
                            scales.Start();
                            eventSource.Add(scales);
                        }
                        catch (Exception ex)
                        {
                            LogManager.GetCurrentClassLogger().Error("Ошибка запуска источника\r\n", ex);
                            scales = null;
                        }
                    }
                }

                return(scales);
            }

            if (sd.StreamType == "tcp")
            {
                TcpChannelSettings settings = JsonConvert.DeserializeObject <TcpChannelSettings>(sd.Settings);

                IScales scales = eventSource.FirstOrDefault(tmp => tmp.Id == settings.Address);
                if (scales == null && sd.Active)
                {
                    LogManager.GetCurrentClassLogger().Info(string.Format("Настройка источника {0}", sd.Settings));

                    try
                    {
                        TcpScales tmp = new TcpScales();
                        tmp.Id       = settings.Address;
                        tmp.Settings = settings;
                        tmp.Exposure = TimeSpan.FromMilliseconds(sd.Exposure);
                        tmp.Timeout  = TimeSpan.FromMilliseconds(sd.Timeout);
                        scales       = tmp;
                    }
                    catch
                    {
                        scales = null;
                    }

                    if (scales != null)
                    {
                        try
                        {
                            scales.Start();
                            eventSource.Add(scales);
                        }
                        catch (Exception ex)
                        {
                            LogManager.GetLogger(Application.ProductName).Error("Ошибка запуска источника\r\n", ex);
                            scales = null;
                        }
                    }
                }

                return(scales);
            }

            return(null);
        }