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; }
public void Load(ScalesDescriptor sd) { if (sd.StreamType == "serial") { SerialPortSettings settings = JsonConvert.DeserializeObject <SerialPortSettings>(sd.Settings); Id = sd.Id; Code = sd.Code; Name = sd.Name; Decoder = sd.Decoder; Active = sd.Active; Description = sd.Description; Exposure = TimeSpan.FromMilliseconds(sd.Exposure); Timeout = TimeSpan.FromMilliseconds(sd.Timeout); TriggerLoading = sd.TriggerLoading; TriggerEmpty = sd.TriggerEmpty; SettingsType = settings.SettingsType; PortName = settings.PortName; Address = ""; BaudRate = settings.BaudRate; DataBits = settings.DataBits; Parity = settings.Parity; StopBits = settings.StopBits; } if (sd.StreamType == "tcp") { TcpChannelSettings settings = JsonConvert.DeserializeObject <TcpChannelSettings>(sd.Settings); Id = sd.Id; Code = sd.Code; Name = sd.Name; Decoder = sd.Decoder; Active = sd.Active; Description = sd.Description; Exposure = TimeSpan.FromMilliseconds(sd.Exposure); Timeout = TimeSpan.FromMilliseconds(sd.Timeout); TriggerLoading = sd.TriggerLoading; TriggerEmpty = sd.TriggerEmpty; SettingsType = settings.SettingsType; PortName = ""; Address = settings.Address; BaudRate = 0; DataBits = 0; Parity = 0; StopBits = 0; } }
public void ActivateChannel(ScalesChannel channel) { if (channel != null) { using (LiteDatabase dba = new LiteDatabase(string.Format("filename={0};", Program.dbPath))) { ScalesDescriptor sd = dba.GetCollection <ScalesDescriptor>("scales").FindById(channel.Id); sd.Active = true; dba.GetCollection <ScalesDescriptor>("scales").Update(sd); channel.EventSource = SetupSource(sd); channel.Active = true; } LogManager.GetLogger(Application.ProductName).Info(string.Format("Активация канала {0} выполнена успешно!", channel.Name)); } }
public static ScalesChannelViewModel LoadFromDb(int id) { ScalesChannelViewModel result = null; using (LiteDatabase dba = new LiteDatabase(string.Format("filename={0};", Program.dbPath))) { ScalesDescriptor sd = dba.GetCollection <ScalesDescriptor>("scales").FindById(id); if (sd != null) { ScalesChannelViewModel tmp = new ScalesChannelViewModel(); tmp.Load(sd); result = tmp; } } return(result); }
public void DeactivateChannel(ScalesChannel channel) { if (channel != null) { var source = channel.EventSource; channel.EventSource = null; channel.Active = false; using (LiteDatabase dba = new LiteDatabase(string.Format("filename={0};", Program.dbPath))) { ScalesDescriptor sd = dba.GetCollection <ScalesDescriptor>("scales").FindById(channel.Id); sd.Active = false; dba.GetCollection <ScalesDescriptor>("scales").Update(sd); } if (source != null ? Program.MeasuringChannels.FirstOrDefault(tmp => tmp.EventSource == source) == null : false) { if (source.Settings is SerialPortSettings) { if ((source.Settings as SerialPortSettings).PortName.StartsWith("MAN") && Program.mainForm != null) { var panel = Program.mainForm.dockManager.Panels.FirstOrDefault(x => Object.Equals(x.Tag, (source.Settings as SerialPortSettings).PortName)); if (panel != null) { Program.mainForm.dockManager.RemovePanel(panel); } } LogManager.GetLogger(Application.ProductName).Info(string.Format("Остановка источника {0}", (source.Settings as SerialPortSettings).PortName)); source.Stop(); eventSource.Remove(source); } if (source.Settings is TcpChannelSettings) { LogManager.GetLogger(Application.ProductName).Info(string.Format("Остановка источника {0}", (source.Settings as TcpChannelSettings).Address)); source.Stop(); eventSource.Remove(source); } LogManager.GetLogger(Application.ProductName).Info(string.Format("Декативация канала {0} выполнена успешно!", channel.Name)); } } }
public bool Update() { using (LiteDatabase dba = new LiteDatabase(string.Format("filename={0};", Program.dbPath))) { var vr = Validate(dba); if (vr.Count() != 0) { return(false); } ScalesDescriptor sd = dba.GetCollection <ScalesDescriptor>("scales").FindById(Id); if (sd != null) { sd.Code = this.Code; sd.Name = this.Name; sd.Decoder = this.Decoder; sd.Active = this.Active; sd.Description = this.Description; sd.Exposure = Exposure.TotalMilliseconds; sd.Timeout = Timeout.TotalMilliseconds; sd.TriggerLoading = this.TriggerLoading; sd.TriggerEmpty = this.TriggerEmpty; sd.StreamType = this.SettingsType; sd.Settings = this.SettingsType == "serial" ? JsonConvert.SerializeObject(new SerialPortSettings { SettingsType = this.SettingsType, PortName = this.PortName, BaudRate = this.BaudRate, DataBits = this.DataBits, Parity = this.Parity, StopBits = this.StopBits }) : JsonConvert.SerializeObject(new TcpChannelSettings { SettingsType = this.SettingsType, Address = this.Address, }); return(dba.GetCollection <ScalesDescriptor>("scales").Update(sd)); } return(false); } }
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); }