public void Execute(IJobExecutionContext context) { try { logger.Info("DataAnalyseJob 任务开始运行"); ArrayList readedBuf = (ArrayList)context.MergedJobDataMap.Get(Consts.C_READED_BUFFER_KEY); mSensorGroup = (string)context.MergedJobDataMap.Get(Consts.C_SENSOR_GROUP_KEY); SerialPortCmdFormat = Global.UserProfile.SensorNode.SerialPortCmdFormat; if (readedBuf.Count > 0) { logger.Info("开始解析……"); IList <IList <byte> > lstBytesRow = SplitBuffer(readedBuf); logger.Debug("分拆成行的Byte数据:"); foreach (IList <byte> row in lstBytesRow) { logger.Debug(ToolHelper.ByteArrayToHexString(row.ToArray())); } if (lstBytesRow != null && lstBytesRow.Count > 0) { for (int i = 0; i < lstBytesRow.Count; i++) { string hexString = ToolHelper.ByteArrayToHexString(lstBytesRow[i].ToArray()); logger.DebugFormat("正在处理数据:", hexString); SensorData sensorData = PraseHexString(hexString, lstBytesRow[i]); if (sensorData != null) { SaveData(sensorData); } } } logger.Info("解析完成。"); } logger.Info("DataAnalyseJob 任务运行结束"); } catch (Exception ex) { logger.Error("DataAnalyseJob 运行异常", ex); throw ex; } }
public void Execute(IJobExecutionContext context) { try { logger.Info("DataReadJob 任务开始运行"); JobDataMap dataMap = context.MergedJobDataMap; mPipeHandle = (IntPtr)dataMap.Get(PARAM_NAME_PIPE_HANDLE); //"PipeHandle" string pipeName = dataMap.GetString(PARAM_NAME_PIPE_NAME); //"PipeName" ArrayList readedBuf = (ArrayList)dataMap.Get(Consts.C_READED_BUFFER_KEY); //"ReadedBuffer" byte[] buf = new byte[512]; byte[] numReadWritten = new byte[4]; int readedCount = 0; if (NamedPipeHelper.ReadFile(mPipeHandle, buf, (uint)buf.Length, numReadWritten, 0)) { readedCount = GetReceiveLength(numReadWritten); if (readedCount > 0) { logOutput.Info(ToolHelper.ByteArrayToHexString(buf)); lock (readedBuf.SyncRoot) { readedBuf.Add(buf); } } } else { NamedPipeHelper.CloseNamedPipe(mPipeHandle); mPipeHandle = NamedPipeHelper.OpenNamedPipe(pipeName); if (mPipeHandle.ToInt32() > -1) { dataMap.Put(PARAM_NAME_PIPE_HANDLE, mPipeHandle); logger.Warn("成功重新打开命名管道连接。"); lock (readedBuf.SyncRoot) { if (readedBuf.Count > 0) { logger.WarnFormat("重新打开命名管道连接,舍弃已接收的数据:[{0}]", ToolHelper.ByteArrayToHexString((byte[])readedBuf[0])); readedBuf.Clear(); } } } else { logger.Error("重新打开命名管道连接失败。"); } } logger.Info("DataReadJob 任务运行结束"); } catch (Exception ex) { logger.Error("DataReadJob 运行异常", ex); } }
public void Execute(IJobExecutionContext context) { try { logger.Info("DataReadJob 任务开始运行"); JobDataMap dataMap = context.MergedJobDataMap; mServer = (TcpListener)dataMap.Get(Consts.C_TCP_LISTENER_KEY); ArrayList readedBuf = (ArrayList)dataMap.Get(Consts.C_READED_BUFFER_KEY); TcpClient client = mServer.AcceptTcpClient(); NetworkStream clientStream = client.GetStream(); byte[] buffer = new byte[cBufferSize]; int readBytes = 0; try { readBytes = clientStream.Read(buffer, 0, buffer.Length); } catch (IOException ex) { logger.Error(ex); mServer.Stop(); mServer.Start(); if (readedBuf.Count > 0) { lock (readedBuf.SyncRoot) { logger.WarnFormat("重新打开侦听器,舍弃已接收的数据:[{0}]", ToolHelper.ByteArrayToHexString((byte[])readedBuf[0])); readedBuf.Clear(); } } logger.Error("重新打开侦听器。"); } catch (ObjectDisposedException ex) { logger.Error(ex); } if (readBytes > 0) { Array.Resize <byte>(ref buffer, readBytes); logOutput.Info(ToolHelper.ByteArrayToHexString(buffer)); lock (readedBuf.SyncRoot) { readedBuf.Add(buffer); } } clientStream.Close(); client.Close(); logger.Info("DataReadJob 任务运行结束"); } catch (Exception ex) { logger.Error("DataReadJob 运行异常", ex); } }
public void Execute(IJobExecutionContext context) { try { logger.Info("DataAnalyseJob 任务开始运行"); ArrayList readedBuf = (ArrayList)context.MergedJobDataMap.Get(Consts.C_READED_BUFFER_KEY); //"ReadedBuffer" mSensorGroup = (string)context.MergedJobDataMap.Get(Consts.C_SENSOR_GROUP); //"SensorGroup" if (readedBuf.Count > 0) { logger.Info("开始解析……"); IList <byte> lstTodoBytes = SplitBuffer(readedBuf); if (lstTodoBytes.Count > 0) { IList <IList <byte> > lstBytesRow = ChangeToRows(lstTodoBytes); logger.Debug("分拆成行的Byte数据:"); foreach (IList <byte> row in lstBytesRow) { logger.Debug(ToolHelper.ByteArrayToHexString(row.ToArray())); } IList <IList <string> > lstStringRow = AnalyseData(lstBytesRow); logger.Debug("解析后的行数据:"); foreach (IList <string> row in lstStringRow) { logger.Debug(row); } if (lstStringRow.Count > 0) { // 解析后的数据存入数据库 for (int i = 0; i < lstStringRow.Count; i++) { IList <string> row = lstStringRow[i]; string msgType = row[0].Trim(); string strByteData = string.Empty; string strData = string.Empty; foreach (byte b in lstBytesRow[i]) { strByteData += b.ToString() + " "; } foreach (string str in row) { strData += str + ";"; } switch (msgType) { case "1": // Message "System Data" logger.Debug("System Data(原始): " + strByteData); logger.Debug("System Data(转换): " + strData); AnalyseData(row, strByteData); break; case "2": // Message "Batch Data" logger.Warn("Batch Data(原始): " + strByteData); logger.Warn("Batch Data(转换): " + strData); break; case "3": // Message "Batch Data INT" logger.Warn("Batch Data INT(原始): " + strByteData); logger.Warn("Batch Data INT(转换): " + strData); break; case "4": // Message "Batch Parameters CHAR" logger.Warn("Batch Parameters CHAR(原始): " + strByteData); logger.Warn("Batch Parameters CHAR(转换): " + strData); break; default: // 未定义 信息类型 logger.Warn("未定义信息(原始): " + strByteData); logger.Warn("未定义信息(转换): " + strData); break; } } } } logger.Info("解析完成。"); } logger.Info("DataAnalyseJob 任务运行结束"); } catch (Exception ex) { logger.Error("DataAnalyseJob 运行异常", ex); throw ex; } }
private IList <byte> SplitToLine(IList <byte> lstBytes, ref int startIndex) { int syncIndex = -1; int stxIndex = -1; int dataLen = 0; int endIndex = 0; IList <byte> lstCols = null; for (int i = startIndex; i < lstBytes.Count - 1; i++) { if (lstBytes[i] == 0xAA) { // 同步标识 syncIndex = i; } else { if (syncIndex != -1) { break; } else { continue; } } } if (syncIndex > -1) { if (syncIndex + 1 < lstBytes.Count) { if (lstBytes[syncIndex + 1] == 0x75) { // 起始标识 stxIndex = syncIndex + 1; if (stxIndex + 1 < lstBytes.Count) { // 数据长度(包括同步,起始标识) dataLen = lstBytes[stxIndex + 1] + 2; endIndex = syncIndex + dataLen - 1; if (endIndex < lstBytes.Count) { lstCols = new List <byte>(); for (int j = syncIndex; j <= endIndex; j++) { lstCols.Add(lstBytes[j]); } startIndex = endIndex + 1; return(lstCols); } //if (syncIndex + dataLen - 1 < lstBytes.Count) else { // 接收的数据不够,留待下次处理 startIndex = syncIndex; } } //if (stxIndex + 1 < lstBytes.Count) else { // 接收的数据不够,留待下次处理 startIndex = syncIndex; } } //if (lstBytes[syncIndex + 1] == 0x75) else { // 接收的数据有问题,非(同步标识+起始标识) logger.WarnFormat("同步起始标识识别错误:同步[{0}],起始[{1}]\n数据:\n", ToolHelper.ByteArrayToHexString(lstBytes.ToArray())); // 略过当前的同步标识 startIndex = syncIndex + 1; } } //if (syncIndex + 1 < lstBytes.Count) else { // 接收的数据不够,留待下次处理 startIndex = syncIndex; } } else { // 无法找到同步标识,忽略所有数据 startIndex = lstBytes.Count; } return(null); }