/// <summary> /// �����û����� /// </summary> /// <param name="by"> DTU ���� </param> /// <param name="bufLen"></param> /// <param name="skt"></param> internal void ProcessUserData(DeviceInfo deviceInfo, byte[] by, int bufLen, Socket skt, LogItem li) { #region ͨѶЭ�� // ---------------------------------------------------------------- // 3.5.1 DTU ����DSC �����ݰ�DTU->DSC // // ��ʼ��־ ������ ������ DTU���ʶ���� ������־ �û����� // 1byte 1byte 2bytes 11bytes 1byte <=1024bytes // 0x7B 0x09 0x10 ... 0x7B ... // ---------------------------------------------------------------- #endregion //ͨѶЭ�� bool picked = false; // ��ȡ�û����� // byte[] inbyte = SGDtu.GetUserData(by); bufLen = inbyte.Length; byte[][] gateCds = GateController.Pick(inbyte); if (gateCds != null) { picked = true; foreach (byte[] bs in gateCds) { Gate_Read(bs, /*bs.Length,*/ li); } } byte[][] pumpCds = PumpController.Pick(inbyte); if (pumpCds != null) { picked = true; foreach (byte[] bs in pumpCds) { ProcessPumpData(bs, bs.Length, li); } } //byte[][] pumpCds705 = PumpController705.Pick( inbyte ); //if ( pumpCds705 != null ) //{ // picked = true; // foreach( byte[] bs in pumpCds705 ) // { // Pump_Read( bs, bs.Length, li ); // } //} // for device xd202 // if (StringHelper.Equal(deviceInfo.DeviceKind, "Gate") && StringHelper.Equal(deviceInfo.DeviceType, XD202.DeviceType) ) { Gate_His gateHistroyData; picked = XD202.Process(deviceInfo, inbyte, li, out gateHistroyData); if (picked) { Gate_View_His(gateHistroyData, this.listView_Gate); } } if (!picked) { li.Add("pick ctrl data", false); li.ShouldLog = true; } }
/// <summary> /// /// </summary> /// <param name="sck"></param> /// <param name="li"></param> private void AddReceiveSocketInfo(Socket sck, LogItem li) { Debug.Assert(li != null); IPAddress ip = null; try { ip = ((IPEndPoint)sck.RemoteEndPoint).Address; } catch (SocketException socketEx) { LogItem liT = new LogItem("Get socket.remoteEndPoint Ex", socketEx); _log.AddCommon(liT); return; } DeviceInfo[] infos = DeviceInfoManager.GetDeviceInfo(ip); if (infos != null) { string name = string.Empty; string sim = string.Empty; string sign = string.Empty; foreach (DeviceInfo info in infos) { name += info.Address; sim += info.Sim; sign += info.DeviceKind; } li.Add("IP", ip.ToString()); li.Add("name", name); li.Add("sim", sim); li.Add("sign", sign); } }
/// <summary> /// ���������߳� /// </summary> private void ReceiveFunction() { while (true) { foreach (DeviceInfo info in InfoList_All) { if (info.Socket != null) { Socket sck = info.Socket; if (sck.Connected && sck.Available > 0 && info.IsUse == 1 ) { byte[] receivedData = new byte[sck.Available]; int receivedCount = sck.Receive(receivedData); // add log // LogItem li2 = new LogItem(); li2.ShouldLog = true; li2.Add("Received datas", CT.BytesToString(receivedData)); AddReceiveSocketInfo(sck, li2); // // if (receivedCount > 0) { //ProcessUserData(receivedData, receivedCount, sck, li2); //continue; if (info.DeviceInfoState.IsSended()) { info.DeviceInfoState.AppendReceivedData(receivedData); } info.FireReceivedDataEvent(receivedData); byte[][] dtuDatas = SGDtu.GetDtuData(receivedData); if (dtuDatas == null) { li2.ShouldLog = true; li2.Add("GetDtuData", null); _log.AddCommon(li2); continue; } foreach (byte[] bs in dtuDatas) { // ���� // if (SGDtu.IsDtuHeartBeat(bs)) { byte[] cmd = MakeDtuRegisteAnswerCommand(bs); info.Tally.Add(DeviceInfo.TEXT_HEARTBEAT, DateTime.Now); // 2007-07-06 Added try // try { sck.Send(cmd, cmd.Length, 0); } catch (SocketException socketEx) { LogItem exLog = new LogItem("Send heart beat Ex", socketEx.ToString()); _log.AddCommon(exLog); } } else if (SGDtu.IsDtuUserData(bs)) { // get active deviceinfo // if (info.Group.IsUsed()) { DeviceInfo activedDeviceInfo = info.Group.LastDeviceInfo; if (activedDeviceInfo != null) { ProcessUserData(activedDeviceInfo, bs, bs.Length, sck, li2); } } else { ProcessUserData(info, bs, bs.Length, sck, li2); } } else { li2.Add("data error"); li2.ShouldLog = true; } } } if (li2.ShouldLog) _log.AddCommon(li2.ToString()); } } } Thread.Sleep(Config.Default.ReceiveThreadSleepTime); } }
/// <summary> /// ��ȡ��վ���� /// </summary> /// <param name="by"></param> /// <param name="bufLen"></param> //private internal void ProcessPumpData(byte[] by, int bufLen, LogItem li) { //��ȡ���� object readpar = new object(); // ������ // PumpProcessor pumpReader = new PumpProcessor(by, bufLen, ref readpar); if (pumpReader.DataFlag != DataFlag.OK) { li.Add("PumpReader parse error", pumpReader.DataFlag); li.ShouldLog = true; return; } //Debug.Assert(readpar != null); switch (by[5]) { //ʵʱ����+���һ����ˮ��¼ // case 0x1A: { //��ʾʵʱ״̬ PumpRealData prd = (PumpRealData)readpar; UpdatePumpListView(prd, this.listView_Pump); //�洢��¼ Db.SavePumpHistoryData(ref readpar); PumpRealData pumpRealdata = readpar as PumpRealData; DeviceInfo info = FindDeviceInfo(pumpRealdata.ComAdr, DeviceInfoManager.TEXT_PUMP); if (info != null) { info.LastUpdateDateTime = DateTime.Now; info.Tally.Add(DeviceInfo.TEXT_PUMP_CMD1A, DateTime.Now); } } break; //��¼ case 0x26: //�洢��¼ Db.Pump_Inp_Save(ref readpar); break; //��ȡ״̬�� default: // Pump_Info(readpar); _log.AddCommon(new LogItem("Unparse pump data", CT.BytesToString(by))); break; } }
/// <summary> /// ��ȡ�豸���� /// </summary> private void ReadDeviceDatas() { for (int i = 0; i < InfoList_All.Count; i++) { DeviceInfo deviceInfo = (DeviceInfo)InfoList_All[i]; Socket socket = deviceInfo.Socket; if (socket == null) continue; if (!IsConnective(socket)) { _log.AddCommon(GetSocketString(socket) + " is disconnected"); ProcessDisconnective(socket, deviceInfo); continue; } if (deviceInfo.IsUse != 1) continue; if (deviceInfo.DeviceInfoState.IsSended()) { if (deviceInfo.DeviceInfoState.IsTimeOut(DateTime.Now)) { if (CsInfoTimeOutEvnet != null) CsInfoTimeOutEvnet(this, new TimeOutEventArgs(deviceInfo)); deviceInfo.DeviceInfoState.Reset(); } continue; } if ( deviceInfo.NeedColl(DateTime.Now) && (!deviceInfo.Group.IsUsed()) ) { deviceInfo.Group.MarkLastDeviceInfo(deviceInfo); deviceInfo.UpdateLastCollDateTime(DateTime.Now); switch (deviceInfo.DeviceKind) { case "Gate": byte[] gateCommand = null; if (StringHelper.Equal(deviceInfo.DeviceType, XD202.DeviceType)) { gateCommand = XD202.CreateReadRealCommand((byte)deviceInfo.DeviceAddress); } else { //�ɼ����һ����¼ // gateCommand = _gateCommandMaker.Send_zP_zB(deviceInfo.DeviceAddress, 0x3C); //����ɼ�ָ�� // } gateCommand = HDBytesWrap(gateCommand, deviceInfo.Sim); // TODO: 207-06-26 Addd // System.Net.Sockets.SocketException: ���������е����������һ���ѽ��������ӡ� // try { socket.Send(gateCommand, gateCommand.Length, 0); _log.AddCommon("send: " + GetDeviceInfoString(deviceInfo)); deviceInfo.DeviceInfoState.SetSendData(gateCommand); } catch (Exception ex) { LogItem li = new LogItem("Send exception", ex.ToString()); li.Add("RemoteEndPoint", socket.RemoteEndPoint.ToString()); _log.AddCommon(li); deviceInfo.Socket = null; } break; case "Pump": //�ɼ�ʵʱ����+���һ����¼ // byte[] bsCmd = _pumpCommandMaker.MakeCommandHelp(deviceInfo.DeviceAddress, 0x1A); //����ɼ�ָ�� // bsCmd = HDBytesWrap(bsCmd, deviceInfo.Sim); try { socket.Send(bsCmd, bsCmd.Length, 0); deviceInfo.DeviceInfoState.SetSendData(bsCmd); _log.AddCommon("send: " + GetDeviceInfoString(deviceInfo)); } catch (Exception ex) { LogItem li = new LogItem("Send exception", ex.ToString()); li.Add("RemoteEndPoint", socket.RemoteEndPoint.ToString()); _log.AddCommon(li); deviceInfo.Socket = null; } //} break; default: _log.AddCommon("not find send.Sign type : " + deviceInfo.DeviceKind); break; } } } }
/// <summary> /// ��ȡ��������,����ز��������桢��ʾ��Ϣ��� /// </summary> /// <param name="by">�û�����</param> /// <param name="bufLen">�û����ݳ��ȣ�[x]</param> internal void Gate_Read(byte[] by, LogItem li) { //��ȡ���� object readpar = new object(); //������ //GateReader gateReader = new GateReader(by,bufLen,ref readpar); GateReader gateReader = new GateReader(by, ref readpar); // 2007.06.07 Added check gateReader state // if (gateReader.DataFlag != DataFlag.OK) { // log error info if (li != null) { li.Add("GateReader parse error", gateReader.DataFlag.ToString()); li.ShouldLog = true; } return; } Debug.Assert(readpar != null, "readpar == null"); // by[5] - Function code // switch (by[5]) { //�������� case 0x23: Gate_Msg_Par(readpar); break; //ʵʱ���� case 0x24: // TODO: 2007-07-10 Exception // Gate_View_Rlt(readpar, this.listView_Gate); break; // //��ʷ��¼ // case 0x25: // Db.Gate_His_Save(ref readpar); // break; case 0x37: //TODO: ����ˮ����¼?? // Gate_Inp gate_inp = (Gate_Inp)readpar; break; case 0x3C: // ��ѭ���ɼ��� //���һ����¼ //�洢��¼ // Db.Gate_His_Save(ref readpar); Gate_His gateHistoryData = readpar as Gate_His; DeviceInfo info = FindDeviceInfo(gateHistoryData.ComAdr, DeviceInfoManager.TEXT_GATE); if (info != null) { info.LastUpdateDateTime = DateTime.Now; info.Tally.Add(DeviceInfo.TEXT_GATE_CMD3C, DateTime.Now); } //��ʾ��¼ // Gate_View_His(readpar, this.listView_Gate); break; //��ȡ״̬�� default: // Gate_Info(readpar); LogItem aLog = new LogItem("Unparse gate data", CT.BytesToString(by)); _log.AddCommon(aLog); break; } }
/// <summary> /// /// </summary> /// <param name="sck"></param> private void ProcessNewConnect(Socket sck) { Debug.Assert(sck != null && sck.Connected == true); IPEndPoint ipep = (IPEndPoint)sck.RemoteEndPoint; IPAddress ipAddress = ipep.Address; DeviceInfo[] infos = DeviceInfoManager.GetDeviceInfo(ipAddress); if (infos == null) { LogItem li = new LogItem("not find IP in cs_info", ipAddress.ToString()); _log.AddCommon(li); } else { bool closed = false; foreach (DeviceInfo info in infos) { if (!closed && info.Socket != null) { try { info.Socket.Shutdown(SocketShutdown.Both); } catch (ObjectDisposedException) { } info.Socket.Close(); info.Tally.Add(DeviceInfo.TEXT_DISCONNECT, DateTime.Now); closed = true; } info.Socket = sck; info.Tally.Add(DeviceInfo.TEXT_CONNECT, DateTime.Now); if (_socketEvent != null) { _socketEvent(this, new SocketEventArgs(info, true)); } } } }
/// <summary> /// /// </summary> /// <param name="sck"></param> private void ProcessDisconnective(Socket sck, DeviceInfo sender) { //TODO: process disconnective socket // LogItem li = new LogItem("Socket disconnect", sender.IP != null ? sender.IP.ToString() : "<null>"); _log.AddCommon(li); Socket temp = sck; foreach (object obj in InfoList_All) { DeviceInfo info = (DeviceInfo)obj; if (info.Socket == temp) { if (_socketEvent != null) { _socketEvent(this, new SocketEventArgs(info, false)); } info.Socket = null; } } try { temp.Shutdown(SocketShutdown.Both); } catch (ObjectDisposedException) { } temp.Close(); }
/// <summary> /// /// </summary> /// <param name="sck"></param> /// <returns></returns> private bool IsConnective(Socket sck) { if (!sck.Connected) return false; try { if (sck.Poll(0, SelectMode.SelectRead)) { // _log.AddCommon( " sck.Poll(0, SelectMode.SelectRead): " + "true" ); // byte[] abytes = new byte[1]; int nRead = sck.Receive(abytes, 0, 1, SocketFlags.Peek); if (nRead == 0) return false; } return true; } catch (SocketException ex) { try { LogItem li = new LogItem("At IsConnective(Socket)", ex.ToString()); if (sck.RemoteEndPoint != null) li.Add("RemoteEndPoint", sck.RemoteEndPoint.ToString()); _log.AddCommon(li.ToString()); } catch { } return false; } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void frmCsinfo_Load(object sender, System.EventArgs e) { Text = _info.Address; LogItem li = new LogItem(); li.HasDateTime = false; li.Add( "���� ", _info.Address ); li.Add( "ͨѶ��ַ", _info.DeviceAddress); li.Add( "IP��ַ ", _info.IP.ToString()); li.Add( "�豸����", _info.DeviceKind ); li.Add( "�绰����", _info.Sim ); if ( _info.DeviceKind == DeviceInfoManager.TEXT_PUMP ) { li.Add( "��վЭ��", _info.PumpCommunicationProtocolVersion ); } li.Add( string.Empty ); for( int i=0; i<_info.Tally.Count; i++ ) { TallyItem ti = _info.Tally[ i ]; li.Add( ti.Name, ti.List.Count ); cmbListName.Items.Add( ti.Name ); } if ( cmbListName.Items.Count > 0 ) { cmbListName.SelectedIndex = 0; } this.txtDetail.Text = li.ToString(); }
/// <summary> /// /// </summary> /// <param name="deviceInfo"></param> /// <param name="inbyte"></param> /// <param name="li"></param> /// <returns></returns> public static bool Process(DeviceInfo deviceInfo, byte[] inbyte, LogItem li, out GateDriver.Gate_His gateHisortyData) { gateHisortyData = null; // parse xd202 data // string errormsg; object gateHis = Parse(inbyte,out errormsg); // save to db // if (gateHis != null) { DbClass db = new DbClass(); db.Gate_His_Save(ref gateHis); gateHisortyData = (GateDriver.Gate_His)gateHis; return true; } else { li.Add("Error", errormsg); return false; } }