向霍尼韦尔PHD写入数据

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Uniformance.PHD;

namespace OPCDriver
{
class ZPJDataAcq
{
Tag MyTag;
PHDHistorian phd;
PHDServer defaultServer;
string PHDhostname = "";
string PHDport = "";
Dictionary<Tag, string> TagList = new Dictionary<Tag, string>();
string path = System.Environment.CurrentDirectory + "\\LOG\\ZPJ_Log.txt";
string Pointpath = System.Environment.CurrentDirectory + "\\LOG\\ZPJ_没有的点.txt";
List<string> NotExistWell = new List<string>();

public ZPJDataAcq()
{
PHDhostname = ConfigurationManager.AppSettings["PHDHostName"]; //value="11.10.96.175"
PHDport = ConfigurationManager.AppSettings["PHDport"]; //3100

phd = new PHDHistorian();
phd.Sampletype = SAMPLETYPE.Raw;
phd.ReductionType = REDUCTIONTYPE.None;

defaultServer = RealTimeDBConnection();
phd.DefaultServer = defaultServer;

}

/// <summary>
/// 连接实时数据库
/// </summary>
/// <returns></returns>
public PHDServer RealTimeDBConnection()
{
// PHDServer defaultServer = new PHDServer(System.Configuration.ConfigurationManager.AppSettings["RTDServerIp"], SERVERVERSION.RAPI200);


string strIP = PHDhostname;
PHDServer defaultServer = new PHDServer(strIP, SERVERVERSION.RAPI200);
defaultServer.WindowsUsername = ConfigurationManager.AppSettings["Account"];
defaultServer.WindowsPassword = ConfigurationManager.AppSettings["Password"];
defaultServer.Port = Convert.ToInt32(PHDport);
return defaultServer;
}

private void InsertValueToPHD(Tag Tag, string Tagvalue, string timeStamp, string wellname)
{
try
{
//phd.PutData(Tag, Tagvalue, phd.ConvertToPHDTime(Convert.ToDateTime(timeStamp)));
phd.ModifyTag(Tag, Tagvalue, phd.ConvertToPHDTime(Convert.ToDateTime(timeStamp)));
Thread.Sleep(10);
}
catch (Exception ex)
{
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.WriteLine("写入PHD数据库失败," + "点名是:" + Tag.TagName + "------井名是" + wellname + "----值是:" + Tagvalue + "错误原因:" + ex.Message.ToString());
}
if (ex.Message.ToString().Contains("Specified tag does not exist"))
{
if (!NotExistWell.Contains(Tag.TagName))
{
NotExistWell.Add(Tag.TagName);
using (StreamWriter sw1 = new StreamWriter(Pointpath, true))
{
sw1.WriteLine(Tag.TagName);
}
}
}
Console.WriteLine("写入PHD数据库失败," + "点名是:" + Tag.TagName + "----值是:" + Tagvalue + "错误原因:" + ex.Message.ToString());
}
}

public string GetCode(string code)
{
int code_length = 0;
string resault = code;
code_length = resault.Length;
code_length = 6 - code_length;
if (code_length != 0)
{
switch (code_length)
{
case 0:
break;
case 1:
resault += "0";
break;
case 2:
resault += "00";
break;
case 3:
resault += "000";
break;
case 4:
resault += "0000";
break;
case 5:
resault += "00000";
break;
case 6:
resault += "000000";
break;
default:
break;
}
}
return resault;
}

public string GetStanderName(string wellname)
{
string standerName = CommonFunc.TransChineseToChar(wellname);
standerName = standerName.Replace("-", "v");
return standerName;
}

public void CombinationTagList(DataRow dr)
{
//油压
MyTag = new Tag();
MyTag.TagName = "JG" + GetCode(dr["code"].ToString()) + GetStanderName(dr["wellname"].ToString()) + "TGP";
if (dr["TGP"].ToString() != null && dr["TGP"].ToString() != "")
{
TagList.Add(MyTag, dr["TGP"].ToString());
}
else
{
TagList.Add(MyTag, "-9999");
}

//套压
MyTag = new Tag();
MyTag.TagName = "JG" + GetCode(dr["code"].ToString()) + GetStanderName(dr["wellname"].ToString()) + "CPV";
if (dr["CPV"].ToString() != null && dr["CPV"].ToString() != "")
{
TagList.Add(MyTag, dr["CPV"].ToString());
}
else
{
TagList.Add(MyTag, "-9999");
}

//回压
MyTag = new Tag();
MyTag.TagName = "JG" + GetCode(dr["code"].ToString()) + GetStanderName(dr["wellname"].ToString()) + "BPV";
if (dr["BPV"].ToString() != null && dr["BPV"].ToString() != "")
{
TagList.Add(MyTag, dr["BPV"].ToString());
}
else
{
TagList.Add(MyTag, "-9999");
}

//井口温度
MyTag = new Tag();
MyTag.TagName = "JG" + GetCode(dr["code"].ToString()) + GetStanderName(dr["wellname"].ToString()) + "TWT";
if (dr["TWT"].ToString() != null && dr["TWT"].ToString() != "")
{
TagList.Add(MyTag, dr["TWT"].ToString());
}
else
{
TagList.Add(MyTag, "-9999");
}

}

public void DataAcq(object sender, System.EventArgs e)
{
Console.WriteLine("自喷井数据传输开始");
string sql = "select DWDM as code,";
sql += "DXMC as wellname,";
sql += "cjShijian as TIP,";
//sql += "cjZhouQi as CJZQ,"; //采集周期--没有该项对应的后缀
sql += "youYa as TGP,";
//sql += "maxYaLi as ZDYL,"; //最大压力--没有该项对应的后缀
//sql += "minYaLi as ZDYL,"; //最小压力--没有该项对应的后缀
sql += "taoYa as CPV,";
sql += "huiYa as BPV,";
sql += "jingKouWenDu as TWT";
sql += " from realtimeparameter_zpj_view";
DataTable dt;
string timestamp = "";
string wellname = "";

try
{
//Get Value from oracel
dt = CommonFunc.GetDataFromDB(sql);
//Combination TagName And value
foreach (DataRow dr in dt.Rows)
{
timestamp = dr["TIP"].ToString();
wellname = dr["wellname"].ToString();
CombinationTagList(dr);
//Insert value into PHD
foreach (var item in TagList)
{
InsertValueToPHD(item.Key, item.Value, timestamp, wellname);
}
TagList.Clear();
}
Console.WriteLine("自喷井数据传输完毕");
using (StreamWriter sw1 = new StreamWriter(path, true))
{
sw1.WriteLine("自喷井数据传输完毕");
}

TagList.Clear();
defaultServer.Dispose();
phd.Dispose();
}
catch (Exception ex)
{
TagList.Clear();
defaultServer.Dispose();
phd.Dispose();
Console.WriteLine("操作PHD失败,异常原因:" + ex.Message.ToString());
}
}

public void Test()
{
Console.WriteLine("自喷井数据传输开始");
string sql = "select DWDM as code,";
sql += "DXMC as wellname,";
sql += "cjShijian as TIP,";
//sql += "cjZhouQi as CJZQ,"; //采集周期--没有该项对应的后缀
sql += "youYa as TGP,";
//sql += "maxYaLi as ZDYL,"; //最大压力--没有该项对应的后缀
//sql += "minYaLi as ZDYL,"; //最小压力--没有该项对应的后缀
sql += "taoYa as CPV,";
sql += "huiYa as BPV,";
sql += "jingKouWenDu as TWT";
sql += " from realtimeparameter_zpj_view";
DataTable dt;
string timestamp = "";
string wellname = "";
try
{
//Get Value from oracel
dt = CommonFunc.GetDataFromDB(sql);
//Combination TagName And value
foreach (DataRow dr in dt.Rows)
{
timestamp = dr["TIP"].ToString();
CombinationTagList(dr);
//Insert value into PHD
foreach (var item in TagList)
{
InsertValueToPHD(item.Key, item.Value, timestamp, wellname);
}
TagList.Clear();
}
Console.WriteLine("自喷井数据传输完毕");

TagList.Clear();
defaultServer.Dispose();
phd.Dispose();
Console.Read();
}
catch (Exception ex)
{
Console.WriteLine("操作PHD失败,异常原因:" + ex.Message.ToString());
}


}


}
}

posted @ 2018-01-25 11:38  樱花2  阅读(1301)  评论(1)    收藏  举报