IcosComm.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Sockets;
  7. using System.Net;
  8. using System.Threading;
  9. using System.Runtime.InteropServices;
  10. using System.Data.SqlClient;
  11. using System.Data;
  12. using System.Data.OleDb;
  13. namespace BEMSDataGateway
  14. {
  15. public class IcosComm
  16. {
  17. //iCos 통신패킷 선언
  18. iCosMonitorValue _valuePacket;
  19. iCosMonitorText _textPacket;
  20. iCosLogInResponse _loginPacket;
  21. iCosMonitorValueArray _valueArrayPacket;
  22. iCosMonitorTextArray _textArrayPacket;
  23. iCosAlarmAckFromServer _alarmAckServerPacket;
  24. iCosAlarmNotify _alarmNotifyPacket;
  25. iCosAlarmAllAck _alarmAllAckPacket;
  26. LogFileCreate LFC = new LogFileCreate(); //로그 기록 파일
  27. //관제점정보 딕셔너리
  28. Dictionary<string, iCosTag> AddressData = new Dictionary<string, iCosTag>();
  29. Dictionary<int, iCosTagIndex> AddressIndex = new Dictionary<int, iCosTagIndex>();
  30. //통신연결정보 딕셔너리
  31. Dictionary<string, ClientData> Clients = new Dictionary<string, ClientData>();
  32. //통신 Critical section용 오브젝트
  33. private Object iCosCommObj = new Object();
  34. //통신응답 상위 전송용 딜리게이트 함수
  35. public delegate void iCosCallback(string userid, int nType, int nSubType, Object obj);
  36. public event iCosCallback iCosResponse;
  37. //통신 재접속용 쓰레드
  38. Thread ConnectionManageThread;
  39. Thread thread_connect;
  40. Thread thread_managerconnection;
  41. //통신 재접속용 쓰레드 초기화
  42. public void InitThread()
  43. {
  44. try
  45. {
  46. ConnectionManageThread = new Thread(new ThreadStart(ManagerConnection));
  47. ConnectionManageThread.IsBackground = true;
  48. ConnectionManageThread.Start();
  49. LFC.Log("=============================");
  50. if (Form1.Enable_EnglishMode)//영어
  51. {
  52. LFC.Log("[IcosComm.cs] InitThread start");
  53. }
  54. else//한글
  55. {
  56. LFC.Log("[IcosComm.cs] InitThread 시작");
  57. }
  58. }
  59. catch (ThreadStartException)
  60. {
  61. if (Form1.Enable_EnglishMode)//영어
  62. {
  63. LFC.Log("[IcosComm.cs] InitThread down");
  64. }
  65. else//한글
  66. {
  67. LFC.Log("[IcosComm.cs] InitThread 다운" + Environment.NewLine);
  68. }
  69. }
  70. }
  71. //관제점정보 DB에서 가져오기
  72. public bool InitTagAddress(int nDbType, string dbPath, string dbname, string dbid, string dbpassword) // MS-SQL 디폴트
  73. {
  74. bool bRet = false;
  75. string strSql = "SELECT * FROM TB_POINT";
  76. string strTag;
  77. int nDataCnt;
  78. string strCon;
  79. try
  80. {
  81. if (nDbType == 0) // MDB
  82. {
  83. strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
  84. DataSet ds = new DataSet();
  85. OleDbConnection mdbcon = null;
  86. mdbcon = new OleDbConnection(strCon);
  87. OleDbCommand mdbcmd = new OleDbCommand(strSql, mdbcon);
  88. OleDbDataAdapter adp = new OleDbDataAdapter(mdbcmd);
  89. mdbcon.Open();
  90. adp.Fill(ds, "TB_POINT");
  91. nDataCnt = ds.Tables[0].Rows.Count;
  92. for (int i = 0; i < nDataCnt; i++)
  93. {
  94. iCosTag _tagAddr = new iCosTag();
  95. iCosTagIndex _tagIndex = new iCosTagIndex();
  96. _tagAddr.nChannel = Convert.ToInt32(ds.Tables[0].Rows[i][0]);
  97. _tagAddr.nController = Convert.ToInt32(ds.Tables[0].Rows[i][1]);
  98. _tagAddr.nDataType = Convert.ToInt32(ds.Tables[0].Rows[i][3]);
  99. _tagAddr.nPoint = Convert.ToInt32(ds.Tables[0].Rows[i][2]);
  100. _tagAddr.nState = 0;
  101. _tagAddr.szText = "";
  102. _tagAddr.analog = 0;
  103. _tagAddr.digital = 0;
  104. strTag = ds.Tables[0].Rows[i][4].ToString();
  105. _tagIndex.szTag = strTag;
  106. _tagIndex.nChannel = Convert.ToInt32(ds.Tables[0].Rows[i][0]);
  107. _tagIndex.nController = Convert.ToInt32(ds.Tables[0].Rows[i][1]);
  108. _tagIndex.nDataType = Convert.ToInt32(ds.Tables[0].Rows[i][3]);
  109. _tagIndex.nPoint = Convert.ToInt32(ds.Tables[0].Rows[i][2]);
  110. if (!String.IsNullOrEmpty(strTag))
  111. {
  112. if (AddressData.ContainsKey(strTag) == false)
  113. AddressData.Add(strTag, _tagAddr);
  114. bRet = true;
  115. }
  116. if (AddressIndex.ContainsKey(_tagAddr.nPoint) == false)
  117. {
  118. AddressIndex.Add(_tagIndex.nPoint, _tagIndex);
  119. }
  120. }
  121. ds.Dispose();
  122. mdbcon.Close();
  123. mdbcon.Dispose();
  124. mdbcmd.Dispose();
  125. adp.Dispose();
  126. }
  127. else if (nDbType == 1)//MS-SQL
  128. {
  129. strCon = @"Data Source = '" + dbPath + "'; Initial Catalog ='" + dbname + "';Persist Security Info=True; User ID = '" + dbid + "'; Password = '" + dbpassword + "'";
  130. SqlConnection dbcon = new SqlConnection();
  131. SqlCommand dbcmd = new SqlCommand();
  132. dbcmd.CommandText = strSql;
  133. dbcmd.Connection = dbcon;
  134. dbcon.ConnectionString = strCon;
  135. dbcon.Open();
  136. SqlDataAdapter adp = new SqlDataAdapter(dbcmd);
  137. DataSet ds = new DataSet();
  138. adp.Fill(ds);
  139. nDataCnt = ds.Tables[0].Rows.Count;
  140. for (int i = 0; i < nDataCnt; i++)// channel, controller, point, datatype
  141. {
  142. iCosTag _tagAddr = new iCosTag();
  143. iCosTagIndex _tagIndex = new iCosTagIndex();
  144. _tagAddr.nChannel = Convert.ToInt32(ds.Tables[0].Rows[i][0]);
  145. _tagAddr.nController = Convert.ToInt32(ds.Tables[0].Rows[i][1]);
  146. _tagAddr.nDataType = Convert.ToInt32(ds.Tables[0].Rows[i][3]);
  147. _tagAddr.nPoint = Convert.ToInt32(ds.Tables[0].Rows[i][2]);
  148. _tagAddr.nState = 0;
  149. _tagAddr.szText = "";
  150. _tagAddr.analog = 0;
  151. _tagAddr.digital = 0;
  152. strTag = ds.Tables[0].Rows[i][4].ToString();
  153. _tagIndex.szTag = strTag;
  154. _tagIndex.nChannel = Convert.ToInt32(ds.Tables[0].Rows[i][0]);
  155. _tagIndex.nController = Convert.ToInt32(ds.Tables[0].Rows[i][1]);
  156. _tagIndex.nDataType = Convert.ToInt32(ds.Tables[0].Rows[i][3]);
  157. _tagIndex.nPoint = Convert.ToInt32(ds.Tables[0].Rows[i][2]);
  158. if (!String.IsNullOrEmpty(strTag))
  159. {
  160. if (AddressData.ContainsKey(strTag) == false)
  161. AddressData.Add(strTag, _tagAddr);
  162. bRet = true;
  163. }
  164. if (AddressIndex.ContainsKey(_tagAddr.nPoint) == false)
  165. {
  166. AddressIndex.Add(_tagIndex.nPoint, _tagIndex);
  167. }
  168. }
  169. dbcon.Close();
  170. dbcon.Dispose();
  171. dbcmd.Dispose();
  172. adp.Dispose();
  173. ds.Dispose();
  174. }
  175. }
  176. catch
  177. {
  178. bRet = false;
  179. }
  180. return bRet;
  181. }
  182. //iCos통신 수신 확인 함수
  183. public void RecvThread(Object target)
  184. {
  185. ClientData client = (ClientData)target;
  186. byte[] buffer = new byte[8192];
  187. byte[] buffer2 = new byte[8192];
  188. byte[] blength = new byte[4];
  189. byte[] bPacketType = new byte[4];
  190. byte[] bPacketSubType = new byte[4];
  191. int nlength = 0;
  192. int nPacketType = 0;
  193. int nPacketSubType = 0;
  194. string strTag;
  195. while (client.clientSocket.Connected)
  196. {
  197. lock (iCosCommObj)
  198. {
  199. try
  200. {
  201. try
  202. {
  203. client.readStream.Read(blength, 0, 4);
  204. }
  205. catch (Exception e)
  206. {
  207. if (Form1.Enable_EnglishMode)//영어
  208. {
  209. LFC.Log("[IcosComm.cs] client.readStream.Read fail : " + e + ", " + Environment.NewLine);
  210. }
  211. else//한글
  212. {
  213. LFC.Log("[IcosComm.cs] client.readStream.Read 실패 : " + e + ", " + Environment.NewLine);
  214. }
  215. break;
  216. }
  217. nlength = BitConverter.ToInt32(blength, 0);
  218. if ((nlength > 0) && (nlength < 8192))
  219. {
  220. client.readStream.Read(buffer2, 0, nlength - 4);
  221. Array.Copy(blength, 0, buffer, 0, 4); //blength 0부터 4개를 buffer 0부터 복사
  222. Array.Copy(buffer2, 0, buffer, 4, nlength); //buffer2 0부터 nlength개를 buffer 4부터 복사
  223. Array.Copy(buffer, 4, bPacketType, 0, 4); //buffer 4부터 4개를 bPacketType 0부터 복사
  224. Array.Copy(buffer, 8, bPacketSubType, 0, 4);//buffer 8부터 4개를 bPacketSubType 0부터 복사
  225. nPacketType = BitConverter.ToInt32(bPacketType, 0);
  226. nPacketSubType = BitConverter.ToInt32(bPacketSubType, 0);
  227. } //buffer 0~4 길이, buffer 4~8 PacketType, buffer 8~12 PacketSubType
  228. else
  229. continue;
  230. if (nPacketType == 3)
  231. {
  232. if (nPacketSubType == 4)
  233. {
  234. if ((buffer[20] >= 0) && (buffer[20] <= 5))
  235. {
  236. _valuePacket.Deserialize(buffer);
  237. strTag = AddressIndex[_valuePacket.nPoint].szTag;
  238. AddressData[strTag].nState = _valuePacket.nState;
  239. AddressData[strTag].analog = _valuePacket.Values.analog;
  240. AddressData[strTag].digital = _valuePacket.Values.digital;
  241. }
  242. else if (buffer[20] == 6)
  243. {
  244. _textPacket.Deserialize(buffer);
  245. strTag = AddressIndex[_textPacket.nPoint].szTag;
  246. AddressData[strTag].nState = _textPacket.nState;
  247. AddressData[strTag].szText = _textPacket.szTextValue;
  248. }
  249. }
  250. else if (nPacketSubType == 22)
  251. {
  252. _valueArrayPacket.Values = new PointValue[50];
  253. _valueArrayPacket.Deserialize(buffer);
  254. for (int i = 0; i < _valueArrayPacket.nPntCnt; i++)
  255. {
  256. strTag = AddressIndex[_valueArrayPacket.Values[i].nPoint].szTag;
  257. AddressData[strTag].nState = _valueArrayPacket.Values[i].nState;
  258. AddressData[strTag].analog = _valueArrayPacket.Values[i].Values.analog;
  259. AddressData[strTag].digital = _valueArrayPacket.Values[i].Values.digital;
  260. }
  261. }
  262. else if (nPacketSubType == 23)
  263. {
  264. _textArrayPacket.Values = new PointText[5];
  265. _textArrayPacket.Deserialize(buffer);
  266. for (int i = 0; i < _textArrayPacket.nPntCnt; i++)
  267. {
  268. strTag = AddressIndex[_textArrayPacket.Values[i].nPoint].szTag;
  269. AddressData[strTag].nState = _textArrayPacket.Values[i].nState;
  270. AddressData[strTag].szText = _textArrayPacket.Values[i].szTextValue;
  271. }
  272. }
  273. else
  274. {
  275. int fail = buffer[8];
  276. }
  277. }
  278. else if (nPacketType == 6)
  279. {
  280. if (nPacketSubType == 2)
  281. {
  282. _loginPacket.Deserialize(buffer);
  283. iCosResponse(client.icosId, nPacketType, nPacketSubType, _loginPacket);
  284. }
  285. }
  286. else if (nPacketType == 4) //PTYPE_ALARM
  287. {
  288. if (nPacketSubType == 11) //ALARM_NOTIFY
  289. {
  290. _alarmNotifyPacket.Deserialize(buffer);
  291. iCosResponse(client.icosId, nPacketType, nPacketSubType, _alarmNotifyPacket);
  292. }
  293. else if (nPacketSubType == 9) //ALARM_ACK_FROM_SERVER
  294. {
  295. _alarmAckServerPacket.Deserialize(buffer);
  296. iCosResponse(client.icosId, nPacketType, nPacketSubType, _alarmAckServerPacket);
  297. }
  298. else if (nPacketSubType == 16) //ALARM_CHACK_ALL_PROCESS_FROM_SERVER
  299. {
  300. _alarmAllAckPacket.Deserialize(buffer);
  301. iCosResponse(client.icosId, nPacketType, nPacketSubType, _alarmAllAckPacket);
  302. }
  303. else
  304. {
  305. iCosResponse(client.icosId, nPacketType, nPacketSubType, null);
  306. }
  307. }
  308. }
  309. catch (Exception e)
  310. {
  311. if (Form1.Enable_EnglishMode)//영어
  312. {
  313. LFC.Log("[IcosComm.cs] Icos Communication failure : " + e + ", " + Environment.NewLine);
  314. }
  315. else//한글
  316. {
  317. LFC.Log("[IcosComm.cs] Icos 통신실패 : " + e + ", " + Environment.NewLine);
  318. }
  319. }
  320. }
  321. }
  322. if (Form1.Enable_EnglishMode)//영어
  323. {
  324. LFC.Log("[IcosComm.cs] Icos Terminate control point thread" + Environment.NewLine);
  325. }
  326. else//한글
  327. {
  328. LFC.Log("[IcosComm.cs] Icos 관제점 쓰레드 종료" + Environment.NewLine);
  329. }
  330. }
  331. public void ManagerConnection()
  332. {
  333. while (true)
  334. {
  335. System.Threading.Thread.Sleep(10000);
  336. lock (iCosCommObj)
  337. {
  338. if (Clients.Count < 1)
  339. continue;
  340. foreach (KeyValuePair<string, ClientData> client in Clients)
  341. {
  342. if (client.Value.clientSocket.Connected == false)
  343. {
  344. string icosAddr = client.Value.ipAddress;
  345. int icosPort = client.Value.nPort;
  346. string icosId = client.Value.icosId;
  347. string icosPassword = client.Value.icosPassword;
  348. Socket newclient = null;
  349. IPEndPoint icosCon = new IPEndPoint(IPAddress.Parse(icosAddr), icosPort);
  350. newclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  351. try
  352. {
  353. newclient.Connect(icosCon);
  354. NetworkStream readStream = new NetworkStream(newclient);
  355. Clients[icosId].clientSocket = newclient;
  356. Clients[icosId].readStream = readStream;
  357. ClientData _conData = new ClientData();
  358. _conData.clientSocket = newclient;
  359. _conData.readStream = readStream;
  360. _conData.ipAddress = icosAddr;
  361. _conData.nPort = icosPort;
  362. _conData.icosId = icosId;
  363. _conData.icosPassword = icosPassword;
  364. thread_managerconnection = new Thread(new ParameterizedThreadStart(RecvThread));
  365. thread_managerconnection.IsBackground = true;
  366. thread_managerconnection.Start(_conData);
  367. IcosLogin(icosId, icosPassword);
  368. System.Threading.Thread.Sleep(1000);
  369. if (Form1.Enable_EnglishMode)//영어
  370. {
  371. LFC.Log("[IcosComm.cs] ManagerConnection reconnect succeeded" + Environment.NewLine);
  372. }
  373. else//한글
  374. {
  375. LFC.Log("[IcosComm.cs] ManagerConnection 재접속 성공" + Environment.NewLine);
  376. }
  377. }
  378. catch
  379. {
  380. newclient.Close();
  381. newclient.Dispose();
  382. if (Form1.Enable_EnglishMode)//영어
  383. {
  384. LFC.Log("[IcosComm.cs] ManagerConnection reconnect failed" + Environment.NewLine);
  385. }
  386. else//한글
  387. {
  388. LFC.Log("[IcosComm.cs] ManagerConnection 재접속 실패" + Environment.NewLine);
  389. }
  390. }
  391. }
  392. }
  393. }
  394. }
  395. }
  396. //iCos 통신 접속 함수
  397. public bool Connect(string icosAddr, int icosPort, string icosId, string icosPassword)
  398. {
  399. bool bRet = false;
  400. lock (iCosCommObj)
  401. {
  402. Socket client = null;
  403. IPEndPoint icosCon = new IPEndPoint(IPAddress.Parse(icosAddr), icosPort);
  404. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
  405. try
  406. {
  407. if ((!String.IsNullOrEmpty(icosId)) && (Clients.ContainsKey(icosId) == false))
  408. {
  409. client.Connect(icosCon);
  410. NetworkStream readStream = new NetworkStream(client);
  411. ClientData _conData = new ClientData();
  412. _conData.clientSocket = client;
  413. _conData.readStream = readStream;
  414. _conData.ipAddress = icosAddr;
  415. _conData.nPort = icosPort;
  416. _conData.icosId = icosId;
  417. _conData.icosPassword = icosPassword;
  418. Clients.Add(icosId, _conData);
  419. thread_connect = new Thread(new ParameterizedThreadStart(RecvThread));
  420. thread_connect.IsBackground = true;
  421. thread_connect.Start(_conData);
  422. bRet = true;
  423. }
  424. else
  425. {
  426. return bRet;
  427. }
  428. }
  429. catch
  430. {
  431. if (Form1.Enable_EnglishMode)//영어
  432. {
  433. LFC.Log("[IcosComm.cs] Icos Connect Failed" + Environment.NewLine);
  434. }
  435. else//한글
  436. {
  437. LFC.Log("[IcosComm.cs] Icos Connect 실패" + Environment.NewLine);
  438. }
  439. client.Close();
  440. client.Dispose();
  441. return bRet;
  442. }
  443. }
  444. return bRet;
  445. }
  446. //iCos 통신 끊기 함수
  447. public void Disconnect(string icosId)
  448. {
  449. if ((String.IsNullOrEmpty(icosId)) || (Clients.ContainsKey(icosId) == false))
  450. return;
  451. lock (iCosCommObj)
  452. {
  453. Clients[icosId].clientSocket.Close();
  454. Clients.Remove(icosId);
  455. }
  456. }
  457. public void reDisconnect(string icosId)
  458. {
  459. iCosCommObj = new Object();
  460. if ((String.IsNullOrEmpty(icosId)) || (Clients.ContainsKey(icosId) == false))
  461. return;
  462. IcosLogout(icosId);
  463. lock (iCosCommObj)
  464. {
  465. Clients[icosId].clientSocket.Close();
  466. Clients.Remove(icosId);
  467. }
  468. }
  469. //iCos 로그인 함수
  470. public void IcosLogin(string userid, string password)
  471. {
  472. if ((String.IsNullOrEmpty(userid)) && (Clients.ContainsKey(userid) == false))
  473. return;
  474. iCosLogInRequest PacketData = new iCosLogInRequest();
  475. if (Clients[userid].clientSocket.Connected == true)
  476. {
  477. PacketData.nLength = Marshal.SizeOf(PacketData);
  478. PacketData.nType = 6;
  479. PacketData.nSubType = 1;
  480. PacketData.szId = userid;
  481. PacketData.szPassword = password;
  482. Clients[userid].icosId = userid;
  483. Clients[userid].icosPassword = password;
  484. NetworkStream writeStream = new NetworkStream(Clients[userid].clientSocket);
  485. writeStream.Write(PacketData.Serialize(), 0, Marshal.SizeOf(PacketData));
  486. writeStream.Close();
  487. writeStream.Dispose();
  488. }
  489. }
  490. //iCos 로그아웃 함수
  491. public void IcosLogout(string userid)
  492. {
  493. if ((String.IsNullOrEmpty(userid)) || (Clients.ContainsKey(userid) == false))
  494. return;
  495. iCosLogInRequest PacketData = new iCosLogInRequest();
  496. if (Clients[userid].clientSocket.Connected == true)
  497. {
  498. PacketData.nLength = Marshal.SizeOf(PacketData);
  499. PacketData.nType = 6;
  500. PacketData.nSubType = 3;
  501. PacketData.szId = userid;
  502. PacketData.szPassword = "";
  503. NetworkStream writeStream = new NetworkStream(Clients[userid].clientSocket);
  504. writeStream.Write(PacketData.Serialize(), 0, Marshal.SizeOf(PacketData));
  505. writeStream.Close();
  506. writeStream.Dispose();
  507. }
  508. }
  509. //iCos 관제점 등록 함수
  510. public int IcosRegTag(string userid, int nCnt, string tags)
  511. {
  512. if ((String.IsNullOrEmpty(userid)) && (Clients.ContainsKey(userid) == false))
  513. return 0;
  514. if (nCnt < 1)
  515. return 0;
  516. string[] icosTag = tags.Split(',');
  517. foreach (string tag in icosTag)
  518. {
  519. if (!AddressData.ContainsKey(tag))
  520. continue;
  521. iCosMonitorRegTag PacketData = new iCosMonitorRegTag();
  522. if (Clients[userid].clientSocket.Connected == true)
  523. {
  524. PacketData.nLength = Marshal.SizeOf(PacketData);
  525. PacketData.nType = 3;
  526. PacketData.nSubType = 2;
  527. PacketData.nChannel = AddressData[tag].nChannel;
  528. PacketData.nController = AddressData[tag].nController;
  529. PacketData.nDataType = AddressData[tag].nDataType;
  530. PacketData.nPoint = AddressData[tag].nPoint;
  531. if (Clients[userid].RegTag.Contains(tag) == false)
  532. Clients[userid].RegTag.Add(tag);
  533. NetworkStream writeStream = new NetworkStream(Clients[userid].clientSocket);
  534. writeStream.Write(PacketData.Serialize(), 0, Marshal.SizeOf(PacketData));
  535. writeStream.Close();
  536. writeStream.Dispose();
  537. }
  538. }
  539. return 1;
  540. }
  541. //iCos 관제점 등록삭제 함수
  542. public int IcosUnregTag(string userid, int nCnt, string tags)
  543. {
  544. if ((String.IsNullOrEmpty(userid)) && (Clients.ContainsKey(userid) == false))
  545. return 0;
  546. if (nCnt < 1)
  547. return 0;
  548. string[] icosTag = tags.Split(',');
  549. foreach (string tag in icosTag)
  550. {
  551. if (!AddressData.ContainsKey(tag))
  552. continue;
  553. iCosMonitorRegTag PacketData = new iCosMonitorRegTag();
  554. if (Clients[userid].clientSocket.Connected == true)
  555. {
  556. PacketData.nLength = Marshal.SizeOf(PacketData);
  557. PacketData.nType = 3;
  558. PacketData.nSubType = 3;
  559. PacketData.nChannel = AddressData[tag].nChannel;
  560. PacketData.nController = AddressData[tag].nController;
  561. PacketData.nDataType = AddressData[tag].nDataType;
  562. PacketData.nPoint = AddressData[tag].nPoint;
  563. if (Clients[userid].RegTag.Contains(tag) == true)
  564. Clients[userid].RegTag.Remove(tag);
  565. NetworkStream writeStream = new NetworkStream(Clients[userid].clientSocket);
  566. writeStream.Write(PacketData.Serialize(), 0, Marshal.SizeOf(PacketData));
  567. writeStream.Close();
  568. writeStream.Dispose();
  569. }
  570. }
  571. return 1;
  572. }
  573. //iCos 관제점 값제어 함수, 쓰기
  574. public void IcosControlValue(string userid, string tag, float fvalue)
  575. {
  576. if ((String.IsNullOrEmpty(userid)) && (Clients.ContainsKey(userid) == false))
  577. return;
  578. if (!AddressData.ContainsKey(tag))
  579. return;
  580. iCosMonitorValue PacketData = new iCosMonitorValue();
  581. if (Clients[userid].clientSocket.Connected == true)
  582. {
  583. NetworkStream writeStream = new NetworkStream(Clients[userid].clientSocket);
  584. PacketData.nLength = Marshal.SizeOf(PacketData);
  585. PacketData.nType = 3;
  586. PacketData.nSubType = 5;
  587. PacketData.nChannel = AddressData[tag].nChannel;
  588. PacketData.nController = AddressData[tag].nController;
  589. PacketData.nDataType = AddressData[tag].nDataType;
  590. PacketData.nPoint = AddressData[tag].nPoint;
  591. PacketData.nState = 1;
  592. PacketData.nReserved1 = 0;
  593. PacketData.nReserved2 = 0;
  594. if (PacketData.nDataType < 2)
  595. PacketData.Values.analog = fvalue;
  596. else
  597. PacketData.Values.digital = (byte)fvalue;
  598. writeStream.Write(PacketData.Serialize(), 0, Marshal.SizeOf(PacketData));
  599. writeStream.Close();
  600. writeStream.Dispose();
  601. }
  602. }
  603. //iCos 관제점 문자열제어 함수
  604. public void IcosControlText(string userid, string tag, string txtvalue)
  605. {
  606. if ((String.IsNullOrEmpty(userid)) && (Clients.ContainsKey(userid) == false))
  607. return;
  608. if (!AddressData.ContainsKey(tag))
  609. return;
  610. iCosMonitorText PacketData = new iCosMonitorText();
  611. if (Clients[userid].clientSocket.Connected == true)
  612. {
  613. NetworkStream writeStream = new NetworkStream(Clients[userid].clientSocket);
  614. PacketData.nLength = Marshal.SizeOf(PacketData);
  615. PacketData.nType = 3;
  616. PacketData.nSubType = 5;
  617. PacketData.nChannel = AddressData[tag].nChannel;
  618. PacketData.nController = AddressData[tag].nController;
  619. PacketData.nDataType = AddressData[tag].nDataType;
  620. PacketData.nPoint = AddressData[tag].nPoint;
  621. PacketData.nState = 1;
  622. PacketData.nReserved1 = 0;
  623. PacketData.nReserved2 = 0;
  624. PacketData.szTextValue = txtvalue;
  625. writeStream.Write(PacketData.Serialize(), 0, Marshal.SizeOf(PacketData));
  626. writeStream.Close();
  627. writeStream.Dispose();
  628. }
  629. }
  630. //관제점
  631. public void IcosRegTagArray(string userid, int nCnt, string tags)
  632. {
  633. if ((String.IsNullOrEmpty(userid)) && (Clients.ContainsKey(userid) == false))
  634. return;
  635. if (nCnt < 1)
  636. return;
  637. if (Clients[userid].clientSocket.Connected == false)
  638. return;
  639. iCosMonitorRegTagArray PacketData = new iCosMonitorRegTagArray();
  640. PacketData.Tags = new TagAddr[50];
  641. NetworkStream writeStream = new NetworkStream(Clients[userid].clientSocket);
  642. string[] icosTag = tags.Split(',');
  643. int nIndex = 0;
  644. int nSendCnt = 0;
  645. foreach (string tag in icosTag)
  646. {
  647. if (!AddressData.ContainsKey(tag))
  648. {
  649. nSendCnt++;
  650. continue;
  651. }
  652. PacketData.nLength = Marshal.SizeOf(PacketData);
  653. PacketData.nType = 3;
  654. PacketData.nSubType = 26;
  655. PacketData.nPntCnt = nIndex + 1;
  656. PacketData.Tags[nIndex].nChannel = AddressData[tag].nChannel;
  657. PacketData.Tags[nIndex].nController = AddressData[tag].nController;
  658. PacketData.Tags[nIndex].nDataType = AddressData[tag].nDataType;
  659. PacketData.Tags[nIndex].nPoint = AddressData[tag].nPoint;
  660. nSendCnt++;
  661. nIndex++;
  662. if (Clients[userid].RegTag.Contains(tag) == false)
  663. Clients[userid].RegTag.Add(tag);
  664. if ((nIndex >= 50) || (nIndex >= nCnt) || (nSendCnt >= nCnt))
  665. {
  666. writeStream.Write(PacketData.Serialize(), 0, Marshal.SizeOf(PacketData));
  667. nIndex = 0;
  668. PacketData.nPntCnt = 0;
  669. }
  670. }
  671. }
  672. public void IcosUnregTagArray(string userid, int nCnt, string tags)
  673. {
  674. if ((String.IsNullOrEmpty(userid)) && (Clients.ContainsKey(userid) == false))
  675. return;
  676. if (nCnt < 1)
  677. return;
  678. if (Clients[userid].clientSocket.Connected == false)
  679. return;
  680. iCosMonitorRegTagArray PacketData = new iCosMonitorRegTagArray();
  681. PacketData.Tags = new TagAddr[50];
  682. NetworkStream writeStream = new NetworkStream(Clients[userid].clientSocket);
  683. string[] icosTag = tags.Split(',');
  684. int nIndex = 0;
  685. int nSendCnt = 0;
  686. foreach (string tag in icosTag)
  687. {
  688. if (!AddressData.ContainsKey(tag))
  689. {
  690. nSendCnt++;
  691. continue;
  692. }
  693. PacketData.nLength = Marshal.SizeOf(PacketData);
  694. PacketData.nType = 3;
  695. PacketData.nSubType = 27;
  696. PacketData.nPntCnt = nIndex + 1;
  697. PacketData.Tags[nIndex].nChannel = AddressData[tag].nChannel;
  698. PacketData.Tags[nIndex].nController = AddressData[tag].nController;
  699. PacketData.Tags[nIndex].nDataType = AddressData[tag].nDataType;
  700. PacketData.Tags[nIndex].nPoint = AddressData[tag].nPoint;
  701. nSendCnt++;
  702. nIndex++;
  703. if (Clients[userid].RegTag.Contains(tag) == true)
  704. Clients[userid].RegTag.Remove(tag);
  705. if ((nIndex >= 50) || (nIndex >= nCnt) || (nSendCnt >= nCnt))
  706. {
  707. writeStream.Write(PacketData.Serialize(), 0, Marshal.SizeOf(PacketData));
  708. nIndex = 0;
  709. PacketData.nPntCnt = 0;
  710. }
  711. }
  712. }
  713. public string IcosGetTagText(string strTag)
  714. {
  715. string szRet = "";
  716. if ((String.IsNullOrEmpty(strTag)) || (AddressData.ContainsKey(strTag) == false))
  717. return szRet;
  718. szRet = AddressData[strTag].szText;
  719. return szRet;
  720. }
  721. //읽기
  722. public double IcosGetTagValue(string strTag)
  723. {
  724. double fRet = 0;
  725. if ((String.IsNullOrEmpty(strTag)) || (AddressData.ContainsKey(strTag) == false))
  726. return fRet;
  727. if (AddressData[strTag].nDataType < 2)
  728. fRet = AddressData[strTag].analog;
  729. else if ((AddressData[strTag].nDataType > 1) && (AddressData[strTag].nDataType < 6))
  730. fRet = AddressData[strTag].digital;
  731. return fRet;
  732. }
  733. public string GetTagNameByIndex(int nPnt)
  734. {
  735. string szRet = "";
  736. if (AddressIndex.ContainsKey(nPnt) == true)
  737. {
  738. szRet = AddressIndex[nPnt].szTag;
  739. }
  740. return szRet;
  741. }
  742. public int GetRegTagCount(string userid)
  743. {
  744. int nRet = 0;
  745. if ((String.IsNullOrEmpty(userid)) || (Clients.ContainsKey(userid) == false))
  746. return nRet;
  747. nRet = Clients[userid].RegTag.Count;
  748. return nRet;
  749. }
  750. public string GetRegTags(string userid)
  751. {
  752. string szReg = "";
  753. if ((String.IsNullOrEmpty(userid)) || (Clients.ContainsKey(userid) == false))
  754. return szReg;
  755. for (int i = 0; i < Clients[userid].RegTag.Count; i++)
  756. {
  757. szReg = szReg + Clients[userid].RegTag[i];
  758. if (i < Clients[userid].RegTag.Count - 1)
  759. szReg = szReg + ",";
  760. }
  761. return szReg;
  762. }
  763. public bool GetConnectionStatus(string userid)
  764. {
  765. if ((String.IsNullOrEmpty(userid)) || (Clients.ContainsKey(userid) == false))
  766. return false;
  767. bool bRet = Clients[userid].clientSocket.Connected;
  768. return bRet;
  769. }
  770. public bool GetTagAddress(string strTag, ref int nChannel, ref int nController, ref int nType, ref int nPoint, ref int nState)
  771. {
  772. if ((String.IsNullOrEmpty(strTag)) || (AddressData.ContainsKey(strTag) == false))
  773. return false;
  774. nChannel = AddressData[strTag].nChannel;
  775. nController = AddressData[strTag].nController;
  776. nType = AddressData[strTag].nDataType;
  777. nPoint = AddressData[strTag].nPoint;
  778. nState = AddressData[strTag].nState;
  779. return true;
  780. }
  781. }
  782. }