191 lines
6.5 KiB
C#
191 lines
6.5 KiB
C#
![]() |
using Epost.Common;
|
|||
|
using Epost.Model;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Configuration;
|
|||
|
using System.Data;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Xml;
|
|||
|
using System.Xml.Linq;
|
|||
|
|
|||
|
namespace Epost.DAL
|
|||
|
{
|
|||
|
public class ControlDAL
|
|||
|
{
|
|||
|
//DataBaseOpration.OprationSqlDAL db = DB_DLL.GetInstance();//【sql】
|
|||
|
|
|||
|
DataBaseOpration.OprationSqlDAL db = DB_DLL.GetInstance();//【mysql】
|
|||
|
XmlHelper xml = new XmlHelper();
|
|||
|
string path = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["ControlXML"]);
|
|||
|
string nodename = "EpostController";
|
|||
|
#region 新增控制器
|
|||
|
public bool CreateControl(ControlModel model)
|
|||
|
{
|
|||
|
|
|||
|
XElement xe = XElement.Load(path);
|
|||
|
IEnumerable<XAttribute> elements = from ele in xe.Elements("EpostController").Attributes().OrderByDescending(e => (string)e.Value)
|
|||
|
select ele;
|
|||
|
int maxNum = 0;
|
|||
|
if (elements.Any())
|
|||
|
{
|
|||
|
maxNum = Convert.ToInt32(elements.First().Value == null ? 0 : Convert.ToInt32(elements.First().Value));
|
|||
|
}
|
|||
|
XElement controlXE = new XElement(nodename, new XAttribute("name", maxNum + 1),
|
|||
|
new XElement("ControlName", model.ControlName),
|
|||
|
new XElement("ControlIP", model.ControlIP)
|
|||
|
// new XElement("ControlBus", model.ControlBus)
|
|||
|
);
|
|||
|
xe.Add(controlXE);
|
|||
|
xe.Save(path);
|
|||
|
return true;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 修改控制器信息
|
|||
|
public bool UpdateControl(ControlModel Model)
|
|||
|
{
|
|||
|
|
|||
|
XElement xe = XElement.Load(path);
|
|||
|
IEnumerable<XElement> elements = from ele in xe.Elements(nodename).Where(p => p.Attribute("name").Value.Equals(Model.ControlID))
|
|||
|
select ele;
|
|||
|
foreach (var ele in elements)
|
|||
|
{
|
|||
|
if (ele.FirstAttribute.Value == Model.ControlID)
|
|||
|
{
|
|||
|
ele.Element("ControlIP").SetValue(Model.ControlIP);
|
|||
|
ele.Element("ControlName").SetValue(Model.ControlName);
|
|||
|
// ele.Element("ControlBus").SetValue(Model.ControlBus);
|
|||
|
xe.Save(path);
|
|||
|
break;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
return true;
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 删除控制器
|
|||
|
public bool DeleteControl(ControlModel Model)
|
|||
|
{
|
|||
|
|
|||
|
XElement xe = XElement.Load(path);
|
|||
|
IEnumerable<XElement> elements = from ele in xe.Elements(nodename).Where(p => p.Attribute("name").Value.Equals(Model.ControlID))
|
|||
|
select ele;
|
|||
|
foreach (var ele in elements)
|
|||
|
{
|
|||
|
if (ele.FirstAttribute.Value == Model.ControlID)
|
|||
|
{
|
|||
|
ele.Remove();
|
|||
|
xe.Save(path);
|
|||
|
break;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
return true;
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 控制器列表
|
|||
|
public List<ControlModel> ControlList()
|
|||
|
{
|
|||
|
XElement xe = XElement.Load(path);
|
|||
|
IEnumerable<XElement> elements = from ele in xe.Elements(nodename)
|
|||
|
select ele;
|
|||
|
List<ControlModel> list = new List<ControlModel>();
|
|||
|
|
|||
|
foreach (var ele in elements)
|
|||
|
{
|
|||
|
ControlModel model = new ControlModel();
|
|||
|
model.ControlName = ele.Element("ControlName").Value;
|
|||
|
// model.ControlBus = ele.Element("ControlBus").Value;
|
|||
|
model.ControlIP = ele.Element("ControlIP").Value;
|
|||
|
model.ControlID = ele.FirstAttribute.Value;
|
|||
|
list.Add(model);
|
|||
|
}
|
|||
|
return list;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 根据控制器ControlID查询相关信息
|
|||
|
public ControlModel GetControlByControlID(string ControlID)
|
|||
|
{
|
|||
|
XElement xe = XElement.Load(path);
|
|||
|
IEnumerable<XElement> elements = from ele in xe.Elements(nodename).Where(p => p.Attribute("name").Value.Equals(ControlID))
|
|||
|
select ele;
|
|||
|
ControlModel model = new ControlModel();
|
|||
|
foreach (var ele in elements)
|
|||
|
{
|
|||
|
if (ele.FirstAttribute.Value == ControlID)
|
|||
|
{
|
|||
|
model.ControlName = ele.Element("ControlName").Value;
|
|||
|
// model.ControlBus = ele.Element("ControlBus").Value;
|
|||
|
model.ControlIP = ele.Element("ControlIP").Value;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
return model;
|
|||
|
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 检查数据库是否连接
|
|||
|
public bool OpenSql()
|
|||
|
{
|
|||
|
string sql = "select count(*) from Addressstorage";
|
|||
|
DataTable dt = db.GetsqlForDT(sql);
|
|||
|
if (dt.Rows.Count > 0)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
|
|||
|
#region 查看控制器数量
|
|||
|
public DataTable Xgate()
|
|||
|
{
|
|||
|
string sql = "select ID,ControlIP from Controller";
|
|||
|
DataTable dt = db.GetsqlForDT(sql);
|
|||
|
return dt;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 查看控制器id
|
|||
|
public int Xgateid(string gateip)
|
|||
|
{
|
|||
|
string sql = "select ID from Controller where ControlIP='{0}'";
|
|||
|
sql = string.Format(sql, gateip);
|
|||
|
DataTable dt = db.GetsqlForDT(sql);
|
|||
|
int gateid = Convert.ToInt32(dt.Rows[0]["ID"]);
|
|||
|
return gateid;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region 查询每个控制器下面的所有的地址
|
|||
|
public DataTable Xgate_Address(int gateid)
|
|||
|
{
|
|||
|
//string sql = "select distinct cast(address as int) as address,bkaddress,type,addresstype,state,block,area,way,shelfid,layer,SUBSTRING ( location,1,4) as Area_area from Addressstorage where ControlID={0} order by address";//【sql】
|
|||
|
string sql = "select distinct convert(address,signed) as address,bkaddress,type,addresstype,state,block,area,way,shelfid,layer,location as Area_area from Addressstorage where ControlID={0} group by address order by address";//【sql】
|
|||
|
sql = string.Format(sql, gateid);
|
|||
|
DataTable dt = db.GetsqlForDT(sql);
|
|||
|
return dt;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
}
|
|||
|
}
|