/// <summary> /// 给定名称和类型获得常量单元 /// </summary> /// <param name="name">常量名称</param> /// <param name="type">常量类型(BIT/WORD/DWORD/FLOAT/DOUBLE)</param> /// <returns></returns> public SimulateVariableUnit GetConstantUnit(string name, string type) { SimulateVariableUnit unit = null; switch (type) { case "BIT": unit = new SimulateBitUnit(); unit.Value = Int32.Parse(name.Substring(1)); break; case "WORD": unit = new SimulateWordUnit(); unit.Value = Int32.Parse(name.Substring(1), name[0] == 'H' ? System.Globalization.NumberStyles.AllowHexSpecifier : System.Globalization.NumberStyles.Integer); break; case "DWORD": unit = new SimulateDWordUnit(); unit.Value = Int64.Parse(name.Substring(1), name[0] == 'H' ? System.Globalization.NumberStyles.AllowHexSpecifier : System.Globalization.NumberStyles.Integer); break; case "FLOAT": unit = new SimulateFloatUnit(); unit.Value = double.Parse(name.Substring(1)); break; default: return(unit); } unit.Name = name; return(unit); }
public ValueTextBox(SimulateVariableUnit _svunit) : base() { svunit = _svunit; Text = svunit.Value.ToString(); IsReadOnly = true; svunit.ValueChanged += OnValueChanged; }
public void Write(ElementModel emodel) { SimulateVariableUnit svunit = smodel.GetVariableUnit(emodel.ShowName, _Type(emodel.DataType)); switch (svunit.Type) { case "BIT": switch (emodel.SetValue.ToUpper()) { case "0": case "OFF": case "FALSE": svunit.Value = 0; break; case "1": case "ON": case "TRUE": svunit.Value = 1; break; } break; case "WORD": svunit.Value = Int32.Parse(emodel.SetValue); break; case "DWORD": svunit.Value = Int64.Parse(emodel.SetValue); break; case "FLOAT": svunit.Value = double.Parse(emodel.SetValue); break; } svunit.Set(smodel.SManager.DllModel); //emodel.CurrentValue = svunit.Value.ToString(); }
/// <summary> /// 给定名称和类型获得变量单元 /// </summary> /// <param name="name">变量名称</param> /// <param name="type">变量类型(BIT/WORD/DWORD/FLOAT/DOUBLE)</param> /// <returns></returns> public SimulateVariableUnit GetVariableUnit(string name, string type) { // 如果是常量的头 switch (name[0]) { case 'K': case 'F': case 'H': return(GetConstantUnit(name, type)); default: break; } // 剩下的是变量的头 // 在管理器中查找 SimulateVariableUnit unit = null; unit = smanager.Find(name, type); if (unit != null) { return(unit); } // 不存在则新建 unit = SimulateVariableUnit.Create(name, type); // 注册到管理器中 smanager.Add(unit); return(unit); }
/// <summary> /// 替换一个变量单元 /// </summary> /// <param name="oldUnit">旧的变量单元</param> /// <param name="newUnit">新的变量单元</param> public void Replace(SimulateVariableUnit oldUnit, SimulateVariableUnit newUnit) { // 终止更新线程,防止资源冲突 // 调用的子方法来处理,这里不用 // UpdateStop(); // 旧的不为空则删除 if (oldUnit != null) { Remove(oldUnit); } // 新的不为空则添加 if (newUnit != null) { Add(newUnit); // 如果是已经锁定的变量,注册到锁定侧 if (newUnit.Islocked) { Lock(newUnit); } } // 重启更新线程 //UpdateStart(); }
/// <summary> /// /// </summary> /// <param name="svunit"></param> /// <returns></returns> public string GetVariableName(SimulateVariableUnit svunit) { if (vndict.ContainsKey(svunit.Name)) { return(vndict[svunit.Name]); } return(svunit.Name); }
/// <summary> /// 修改变量的值 /// </summary> /// <param name="svunit"></param> public void Change(SimulateVariableUnit svunit) { if (svunit.Islocked) { svunit.Set(dllmodel); svunit.Islocked = false; } }
public static SimulateDataModel Create(string name) { SimulateVariableUnit svunit = SimulateVariableUnit.Create(name); if (svunit == null) { return(null); } return(new SimulateDataModel(svunit)); }
/// <summary> /// 移除一个变量单元 /// </summary> /// <param name="svunit"></param> public void Remove(SimulateVariableUnit svunit) { if (svunit is SimulateUnitSeries) { SimulateUnitSeries series = (SimulateUnitSeries)svunit; Remove(series.Model); return; } // 终止更新线程,防止资源冲突 UpdateStop(); // 变量单元名称所在的非锁定变量列表 List <SimulateVariableUnit> svulist = null; // 变量单元名称所在的锁定变量列表 List <SimulateVariableUnit> svllist = null; // 非锁定变量列表不存在则新建 if (!udict.ContainsKey(svunit.Name)) { svulist = new List <SimulateVariableUnit>(); udict.Add(svunit.Name, svulist); } // 非锁定变量列表存在则获取 else { svulist = udict[svunit.Name]; } // 锁定变量列表不存在则新建 if (!ldict.ContainsKey(svunit.Name)) { svllist = new List <SimulateVariableUnit>(); ldict.Add(svunit.Name, svllist); } // 锁定变量列表存在则获取 else { svllist = ldict[svunit.Name]; } // 非锁定变量列表存在则移除 if (svulist.Contains(svunit)) { svulist.Remove(svunit); } // 锁定变量列表存在则移除 if (svllist.Contains(svunit)) { svllist.Remove(svunit); // 注意要解锁 svunit.Islocked = false; } // 重启更新线程 UpdateStart(); }
public void Lock(ElementModel emodel) { Write(emodel); SimulateVariableUnit svunit = smodel.GetVariableUnit(emodel.ShowName, _Type(emodel.DataType)); if (svunit.Islocked) { return; } smodel.SManager.Lock(svunit); }
public void Unlock(ElementModel emodel) { SimulateVariableUnit svunit = smodel.GetVariableUnit(emodel.ShowName, _Type(emodel.DataType)); if (!svunit.Islocked) { return; } smodel.SManager.Unlock(svunit); emodel.SetValue = String.Empty; }
/// <summary> /// 解锁所有的单元 /// </summary> public void UnlockAll() { foreach (string name in ldict.Keys.ToArray()) { SimulateVariableUnit svunit = ldict[name].FirstOrDefault(); if (svunit != null) { Unlock(svunit); } } }
public ElementModel Get(ElementModel emodel) { SimulateVariableUnit svunit = smodel.GetVariableUnit(emodel.ShowName, _Type(emodel.DataType)); if (dict.ContainsKey(svunit)) { return(dict[svunit]); } else { return(null); } }
/// <summary> /// 解锁一个变量单元 /// </summary> /// <param name="svunit">变量单元</param> public void Unlock(SimulateVariableUnit svunit) { // 终止更新线程,防止资源冲突 UpdateStop(); // 变量单元名称所在的非锁定变量列表 List <SimulateVariableUnit> svulist = null; // 变量单元名称所在的锁定变量列表 List <SimulateVariableUnit> svllist = null; // 非锁定变量列表不存在则新建 if (!udict.ContainsKey(svunit.Name)) { svulist = new List <SimulateVariableUnit>(); udict.Add(svunit.Name, svulist); } // 非锁定变量列表存在则获取 else { svulist = udict[svunit.Name]; } // 锁定变量列表不存在则新建 if (!ldict.ContainsKey(svunit.Name)) { svllist = new List <SimulateVariableUnit>(); ldict.Add(svunit.Name, svllist); } // 锁定变量列表存在则获取 else { svllist = ldict[svunit.Name]; } // 锁定变量列表不存在 if (!svllist.Contains(svunit)) { //throw new ArgumentException(String.Format("Cannot found {0:s} in variable unit collection.", svunit.ToString())); } // 将锁定列表中的变量单元变为非锁定,并加入到非锁定列表中 foreach (SimulateVariableUnit _svunit in svllist) { _svunit.Islocked = false; svulist.Add(_svunit); } // 清空非锁定列表 svllist.Clear(); // 调用dll的解锁接口 dllmodel.Unlock(svunit.Name); // 重启更新线程 UpdateStart(); }
private static void Setup(LadderNetworkViewModel lnvmodel) { foreach (BaseViewModel bvmodel in lnvmodel.GetElements()) { bvmodel.ViewCtrl = smmanager; BaseModel bmodel = bvmodel.Model; if (bmodel == null) { continue; } for (int i = 0; i < bmodel.ParaCount; i++) { IValueModel vmodel = bmodel.GetPara(i); SimulateVariableUnit svunit = null; if (vmodel.ValueString.Equals(String.Empty)) { continue; } switch (vmodel.Type) { case LadderValueType.Bool: svunit = smodel.GetVariableUnit(vmodel.ValueString, "BIT"); break; case LadderValueType.Word: svunit = smodel.GetVariableUnit(vmodel.ValueString, "WORD"); break; case LadderValueType.DoubleWord: svunit = smodel.GetVariableUnit(vmodel.ValueString, "DWORD"); break; case LadderValueType.Float: svunit = smodel.GetVariableUnit(vmodel.ValueString, "FLOAT"); break; case LadderValueType.String: svunit = new SimulateStringUnit(vmodel.ValueString); break; } svunit.CanClose = false; SimuMoniValueModel smvmodel = new SimuMoniValueModel(svunit, smodel); bvmodel.SetValueModel(i, smvmodel); } } }
public void LoadXml(XElement node_SDModel) { string name = (string)(node_SDModel.Attribute("Name")); string type = (string)(node_SDModel.Attribute("Type")); string var = (string)(node_SDModel.Attribute("var")); SVUnit = SimulateVariableUnit.Create(name, type); SVUnit.Var = var; IEnumerable <XElement> node_VSs = node_SDModel.Elements("ValueSegment"); values = new List <ValueSegment>(); foreach (XElement node_VS in node_VSs) { ValueSegment vs; switch (Type) { case "BIT": vs = new BitSegment(); vs.Value = (Int32)node_VS.Attribute("Value"); break; case "WORD": vs = new WordSegment(); vs.Value = (Int32)node_VS.Attribute("Value"); break; case "DWORD": vs = new DWordSegment(); vs.Value = (Int64)node_VS.Attribute("Value"); break; case "FLOAT": vs = new FloatSegment(); vs.Value = (double)node_VS.Attribute("Value"); break; default: throw new FormatException(); } vs.TimeStart = (int)(node_VS.Attribute("TimeStart")); vs.TimeEnd = (int)(node_VS.Attribute("TimeEnd")); values.Add(vs); } IsLock = (bool)(node_SDModel.Attribute("IsLock")); IsView = (bool)(node_SDModel.Attribute("IsView")); }
public void Add(ElementModel emodel) { SimulateVariableUnit svunit = smodel.GetVariableUnit(emodel.ShowName, _Type(emodel.DataType)); if (!dict.ContainsKey(svunit)) { dict.Add(svunit, emodel); emodel.RefCount = 1; svunit.ValueChanged += OnValueChanged; } else { emodel = dict[svunit]; emodel.RefCount++; } UpdateValue(emodel, svunit); }
private void OnValueChanged(object sender, RoutedEventArgs e) { if (sender is SimulateVariableUnit) { if (!IsRunning) { return; } SimulateVariableUnit svunit = (SimulateVariableUnit)sender; if (!dict.ContainsKey(svunit)) { return; } ElementModel emodel = dict[svunit]; UpdateValue(emodel, svunit); } }
public void Remove(ElementModel emodel) { emodel = Get(emodel); if (emodel == null) { return; } if (--emodel.RefCount == 0) { SimulateVariableUnit svunit = smodel.GetVariableUnit(emodel.ShowName, _Type(emodel.DataType)); if (svunit.CanClose) { smodel.SManager.Remove(svunit); dict.Remove(svunit); svunit.ValueChanged -= OnValueChanged; } } }
public void Dispose() { SVUnit = null; }
private void UpdateValue(ElementModel emodel, SimulateVariableUnit svunit) { if (!IsRunning) { emodel.CurrentValue = "????"; emodel.SetValue = String.Empty; return; } if (svunit.Type.Equals("BIT")) { switch (svunit.Value.ToString()) { case "0": case "OFF": case "FALSE": emodel.CurrentValue = "OFF"; break; case "1": case "ON": case "TRUE": emodel.CurrentValue = "ON"; break; } } else { switch (emodel.ShowType) { case "WORD": emodel.CurrentValue = String.Format("{0}", (Int16)((Int32)(svunit.Value))); break; case "UWORD": emodel.CurrentValue = String.Format("{0}", (UInt16)((Int32)(svunit.Value))); break; case "DWORD": emodel.CurrentValue = String.Format("{0}", (Int32)((Int64)(svunit.Value))); break; case "UDWORD": emodel.CurrentValue = String.Format("{0}", (UInt32)((Int64)(svunit.Value))); break; case "BCD": emodel.CurrentValue = String.Format("{0}", ValueConverter.ToBCD( (UInt16)((Int32)(svunit.Value)))); break; case "FLOAT": emodel.CurrentValue = String.Format("{0}", (double)(svunit.Value)); break; } } }
public SimulateDataModel(SimulateVariableUnit _svunit) { SVUnit = _svunit; values = new List <ValueSegment>(); }
public virtual SimulateVariableUnit this[int id] { get { switch (id) { case 1: return(this._args1); case 2: return(this._args2); case 3: return(this._args3); case 4: return(this._args4); case 5: return(this._args5); default: throw new ArgumentOutOfRangeException(); } } set { switch (id) { case 1: if (this._args1 != null) { this._args1.ValueChanged -= OnValueChanged; this._args1.LockChanged -= OnLockChanged; } this._args1 = value; this._args1.ValueChanged += OnValueChanged; this._args1.LockChanged += OnLockChanged; break; case 2: if (this._args2 != null) { this._args2.ValueChanged -= OnValueChanged; this._args2.LockChanged -= OnLockChanged; } this._args2 = value; this._args2.ValueChanged += OnValueChanged; this._args2.LockChanged += OnLockChanged; break; case 3: if (this._args3 != null) { this._args3.ValueChanged -= OnValueChanged; this._args3.LockChanged -= OnLockChanged; } this._args3 = value; this._args3.ValueChanged += OnValueChanged; this._args3.LockChanged += OnLockChanged; break; case 4: if (this._args4 != null) { this._args4.ValueChanged -= OnValueChanged; this._args4.LockChanged -= OnLockChanged; } this._args4 = value; this._args4.ValueChanged += OnValueChanged; this._args4.LockChanged += OnLockChanged; break; case 5: if (this._args5 != null) { this._args5.ValueChanged -= OnValueChanged; this._args5.LockChanged -= OnLockChanged; } this._args5 = value; this._args5.ValueChanged += OnValueChanged; this._args5.LockChanged += OnLockChanged; break; default: throw new ArgumentOutOfRangeException(); } } }
/// <summary> /// 添加一个变量单元 /// </summary> /// <param name="svunit">变量单元</param> public void Add(SimulateVariableUnit svunit) { if (svunit is SimulateUnitSeries) { SimulateUnitSeries series = (SimulateUnitSeries)svunit; Add(series.Model); return; } // 终止更新线程,防止资源冲突 UpdateStop(); // 变量单元名称所在的非锁定变量列表 List <SimulateVariableUnit> svulist = null; // 变量单元名称所在的锁定变量列表 List <SimulateVariableUnit> svllist = null; // 非锁定变量列表不存在则新建 if (!udict.ContainsKey(svunit.Name)) { svulist = new List <SimulateVariableUnit>(); udict.Add(svunit.Name, svulist); } // 非锁定变量列表存在则获取 else { svulist = udict[svunit.Name]; } // 锁定变量列表不存在则新建 if (!ldict.ContainsKey(svunit.Name)) { svllist = new List <SimulateVariableUnit>(); ldict.Add(svunit.Name, svllist); } // 锁定变量列表存在则获取 else { svllist = ldict[svunit.Name]; } // 如果存在同名的锁定变量 if (svllist.Count() > 0) { SimulateVariableUnit lunit = svllist.First(); // 将同名的变量单元都设为锁定并添加到锁定列表中 foreach (SimulateVariableUnit _svunit in svulist) { _svunit.Value = lunit.Value; _svunit.Islocked = true; svllist.Add(_svunit); } svunit.Value = lunit.Value; svunit.Islocked = true; svllist.Add(svunit); // 非锁定列表清空 svulist.Clear(); } // 不存在同名的锁定变量 else { svulist.Add(svunit); } // 存在别名则设置 if (vndict.ContainsKey(svunit.Name)) { svunit.Var = vndict[svunit.Name]; } // 安装管理器 svunit.Setup(this); // 重启更新线程 UpdateStart(); }
/// <summary> /// 锁定一个变量单元 /// </summary> /// <param name="svunit">变量单元</param> public void Lock(SimulateVariableUnit svunit) { // 终止更新线程,防止资源冲突 UpdateStop(); // 变量单元名称所在的非锁定变量列表 List <SimulateVariableUnit> svulist = null; // 变量单元名称所在的锁定变量列表 List <SimulateVariableUnit> svllist = null; // 非锁定变量列表不存在则新建 if (!udict.ContainsKey(svunit.Name)) { svulist = new List <SimulateVariableUnit>(); udict.Add(svunit.Name, svulist); } // 非锁定变量列表存在则获取 else { svulist = udict[svunit.Name]; } // 锁定变量列表不存在则新建 if (!ldict.ContainsKey(svunit.Name)) { svllist = new List <SimulateVariableUnit>(); ldict.Add(svunit.Name, svllist); } // 锁定变量列表存在则获取 else { svllist = ldict[svunit.Name]; } // 非锁定变量列表不存在 if (!svulist.Contains(svunit)) { // 存在锁定重设的情况,不算做错误 //throw new ArgumentException(String.Format("Cannot found {0:s} in variable unit collection.", svunit.ToString())); } // 将非锁定列表中的变量加入到锁定列表中 foreach (SimulateVariableUnit _svunit in svulist) { //_svunit.Value = svunit.Value; //_svunit.Islocked = true; svllist.Add(_svunit); } // 清空非锁定列表 svulist.Clear(); // 将锁定列表中的变量重设锁定 foreach (SimulateVariableUnit _svunit in svllist) { if (svunit.Type.Equals(_svunit.Type)) { _svunit.Value = svunit.Value; } _svunit.Islocked = true; } // 调用dll的锁定接口 dllmodel.Lock(svunit.Name); // 重启更新线程 UpdateStart(); }