Esempio n. 1
0
		public void ExportDataToFile(string filePath, DataModel dataModel, string valuesSeparator)
		{
			using (var streamWriter = File.CreateText(filePath))
			{
				Action<List<object>> dataLineAction =
					lineList =>
					{
						var lineStringList = new List<string>(lineList.Count);
						foreach (var obj in lineList)
						{
							if (obj is decimal)
							{
								var number = (decimal)obj;
								lineStringList.Add(number.ToString(CultureInfo.InstalledUICulture));
							}
							else
							{
								lineStringList.Add(obj.ToString());
							}
						}

						var line = string.Join(valuesSeparator, lineStringList.ToArray());
						streamWriter.WriteLine(line);
					};

				DoHeader(dataModel, dataLineAction);
				DoBody(dataModel, dataLineAction);
			}
		}
Esempio n. 2
0
		public void ExportToExcel(DataModel dataModel)
		{
			var data = DoDataArray(dataModel);
			var excel = new Application { Visible = true };
			var workbook = excel.Workbooks.Add();
			var worksheet = (Worksheet)(workbook.Worksheets[1]);
			var range = worksheet.Range["A1"].Resize[data.GetLength(0), data.GetLength(1)];
			range.Value = data;
		}
		public void LoadData_EmptyStream()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(0, 0, DataType.Byte, DataType.Byte, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			using (var stream = new MemoryStream())
			{
				dataService.LoadData(stream, model);
			}
		}
		public void LoadData_OneWordArgument_OneDataEntry()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(1, 0, DataType.Word, DataType.Byte, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			var data = new byte[] {0x42, 0x35};
			using (var stream = new MemoryStream(data))
			{
				dataService.LoadData(stream, model);
			}

			Assert.That(model.DataEntriesCount, Is.EqualTo(1));
			Assert.That(model.GetMachineArgument(0, 0), Is.EqualTo("3542"));
		}
		public void LoadData_TwoWordArguments_TwoDataEntries()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(2, 0, DataType.Word, DataType.Byte, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			var data = new byte[] {0x42, 0x54, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC};
			using (var stream = new MemoryStream(data))
			{
				dataService.LoadData(stream, model);
			}

			Assert.That(model.DataEntriesCount, Is.EqualTo(2));
			Assert.That(model.GetMachineArgument(0, 0), Is.EqualTo("5442"));
			Assert.That(model.GetMachineArgument(1, 0), Is.EqualTo("3412"));
			Assert.That(model.GetMachineArgument(0, 1), Is.EqualTo("7856"));
			Assert.That(model.GetMachineArgument(1, 1), Is.EqualTo("BC9A"));
		}
Esempio n. 6
0
		private void LoadDataSuccessCallback(DataModel model)
		{
			log.Info(InternalResources.DataWasLoadedSuccessfully);
			currentDataModel = model;

			view.CreateColumns(
				model.NumberOfArguments, model.NumberOfFunctions,
				GetScaleDtos(model.CreateArgumentScales(settingsService.CurrentSettings.MaxAbsoluteScalePower)),
				GetScaleDtos(model.CreateFunctionScales(settingsService.CurrentSettings.MaxAbsoluteScalePower)));
			view.SetDataRowsCount(model.DataEntriesCount);
			view.ShowFilePath(model.FilePath);
			view.SetStatus(Resources.ReadyStatus);
			view.GoToNormalMode();
			view.EnableFileDependentControls();
		}
Esempio n. 7
0
		private static void DoBody(DataModel dataModel, Action<List<object>> dataLineAction)
		{
			var lineList = new List<object>();
			for (var dataEntryIndex = 0; dataEntryIndex < dataModel.DataEntriesCount; dataEntryIndex++)
			{
				for (var argumentIndex = 0; argumentIndex < dataModel.NumberOfArguments; argumentIndex++)
				{
					var value = dataModel.GetMachineArgument(argumentIndex, dataEntryIndex);
					lineList.Add(value);
				}
				for (var functionIndex = 0; functionIndex < dataModel.NumberOfFunctions; functionIndex++)
				{
					var value = dataModel.GetMachineFunction(functionIndex, dataEntryIndex);
					lineList.Add(value);
				}
				for (var argumentIndex = 0; argumentIndex < dataModel.NumberOfArguments; argumentIndex++)
				{
					var value = dataModel.GetHumanArgument(argumentIndex, dataEntryIndex);
					lineList.Add(value);
				}
				for (var functionIndex = 0; functionIndex < dataModel.NumberOfFunctions; functionIndex++)
				{
					var value = dataModel.GetHumanFunction(functionIndex, dataEntryIndex);
					lineList.Add(value);
				}
				dataLineAction(lineList);
				lineList.Clear();
			}
		}
Esempio n. 8
0
		private static object[,] DoDataArray(DataModel dataModel)
		{
			var columnCount = 2 * dataModel.NumberOfArguments + 2 * dataModel.NumberOfFunctions;
			// All data plus header.
			var rowCount = dataModel.DataEntriesCount + 1;
			var data = new object[rowCount, columnCount];
			var currentRowIndex = 0;
			Action<List<object>> dataLineAction =
				lineList =>
				{
					if (lineList.Count != columnCount)
					{
						throw new ArgumentOutOfRangeException(
							string.Format(InternalResources.DataLineItemsCountDoesNotMatchColumnCount, lineList.Count, columnCount));
					}
					if (currentRowIndex >= rowCount)
					{
						throw new ArgumentOutOfRangeException(
							string.Format(InternalResources.CurrentRowIndexIsTooBigForRowCount, currentRowIndex, rowCount));
					}

					for (var columnIndex = 0; columnIndex < columnCount; columnIndex++)
					{
						data[currentRowIndex, columnIndex] = lineList[columnIndex];
					}
					currentRowIndex++;
				};

			DoHeader(dataModel, dataLineAction);
			DoBody(dataModel, dataLineAction);

			return data;
		}
Esempio n. 9
0
		private static DataEntry CreateDataEntry(DataModel dataModel)
		{
			var argumentBufferLength = GetBufferLength(dataModel.ArgumentsType);
			var functionBufferLength = GetBufferLength(dataModel.FunctionsType);

			var argumentsArray = new byte[dataModel.NumberOfArguments][];
			for (var i = 0; i < argumentsArray.Length; i++)
			{
				argumentsArray[i] = new byte[argumentBufferLength];
			}

			var functionsArray = new byte[dataModel.NumberOfFunctions][];
			for (var i = 0; i < functionsArray.Length; i++)
			{
				functionsArray[i] = new byte[functionBufferLength];
			}

			var dataEntry = new DataEntry(argumentsArray, functionsArray);

			return dataEntry;
		}
Esempio n. 10
0
		private static void DoHeader(DataModel dataModel, Action<List<object>> dataLineAction)
		{
			var lineList = new List<object>();

			for (var argumentIndex = 0; argumentIndex < dataModel.NumberOfArguments; argumentIndex++)
			{
				var columnName = dataModel.NumberOfArguments == 1
				                 	? InternalResources.MachineArgumentSingleColumnTitle
				                 	: string.Format(InternalResources.MachineArgumentMultipleColumnTitle, argumentIndex + 1);
				lineList.Add(columnName);
			}
			for (var functionIndex = 0; functionIndex < dataModel.NumberOfFunctions; functionIndex++)
			{
				var columnName = dataModel.NumberOfFunctions == 1
				           	? InternalResources.MachineFunctionSingleColumnTitle
				           	: string.Format(InternalResources.MachineFunctionMultipleColumnTitle, functionIndex + 1);
				lineList.Add(columnName);
			}
			for (var argumentIndex = 0; argumentIndex < dataModel.NumberOfArguments; argumentIndex++)
			{
				var columnName = dataModel.NumberOfArguments == 1
				           	? InternalResources.HumanArgumentSingleColumnTitle
				           	: string.Format(InternalResources.HumanArgumentMultipleColumnTitle, argumentIndex + 1);
				lineList.Add(columnName);
			}
			for (var functionIndex = 0; functionIndex < dataModel.NumberOfFunctions; functionIndex++)
			{
				var columnName = dataModel.NumberOfFunctions == 1
				           	? InternalResources.HumanFunctiontSingleColumnTitle
				           	: string.Format(InternalResources.HumanFunctionMultipleColumnTitle, functionIndex + 1);
				lineList.Add(columnName);
			}

			dataLineAction(lineList);
		}
		public void LoadData_OneQwordFunction_OneDataEntry()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(0, 1, DataType.Byte, DataType.Qword, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			var data = new byte[] {0x42, 0x35, 0xAB, 0xCD, 0xEF, 0xFF, 0x11, 0x22};
			using (var stream = new MemoryStream(data))
			{
				dataService.LoadData(stream, model);
			}

			Assert.That(model.DataEntriesCount, Is.EqualTo(1));
			Assert.That(model.GetMachineFunction(0, 0), Is.EqualTo("2211FFEFCDAB3542"));
		}
Esempio n. 12
0
		private void LoadDataErrorCallback(Exception exception)
		{
			log.Error(exception);
			currentDataModel = null;

			var message = string.Format(Resources.DataLoadingFailedMessage, exception.Message);
			view.ShowError(message);
			view.SetStatus(Resources.ReadyStatus);
			view.GoToNormalMode();
		}
Esempio n. 13
0
		public void LoadData(Stream stream, DataModel dataModel)
		{
			LoadDataImpl(stream, dataModel);
		}
		public void LoadData_TwoQwordFunctions_TwoDataEntries()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(0, 2, DataType.Byte, DataType.Qword, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			var data = new byte[]
			           {
			           	0x42, 0x35, 0xAB, 0xCD, 0x01, 0x23, 0x45, 0x67,
			           	0x89, 0x9A, 0xBC, 0xDE, 0xFF, 0x00, 0x11, 0x22,
			           	0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA,
			           	0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x01, 0x02
			           };
			using (var stream = new MemoryStream(data))
			{
				dataService.LoadData(stream, model);
			}

			Assert.That(model.DataEntriesCount, Is.EqualTo(2));
			Assert.That(model.GetMachineFunction(0, 0), Is.EqualTo("67452301CDAB3542"));
			Assert.That(model.GetMachineFunction(1, 0), Is.EqualTo("221100FFDEBC9A89"));
			Assert.That(model.GetMachineFunction(0, 1), Is.EqualTo("AA99887766554433"));
			Assert.That(model.GetMachineFunction(1, 1), Is.EqualTo("020100FFEEDDCCBB"));
		}
		public void LoadData_TwoDwordFunctions_TwoDataEntries()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(0, 2, DataType.Byte, DataType.Dword, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			var data = new byte[]
			           {0x42, 0x54, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xFF, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
			using (var stream = new MemoryStream(data))
			{
				dataService.LoadData(stream, model);
			}

			Assert.That(model.DataEntriesCount, Is.EqualTo(2));
			Assert.That(model.GetMachineFunction(0, 0), Is.EqualTo("34125442"));
			Assert.That(model.GetMachineFunction(1, 0), Is.EqualTo("BC9A7856"));
			Assert.That(model.GetMachineFunction(0, 1), Is.EqualTo("1100FFDE"));
			Assert.That(model.GetMachineFunction(1, 1), Is.EqualTo("55443322"));
		}
		public void LoadData_TwoWordFunctions_OneDataEntry()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(0, 2, DataType.Byte, DataType.Word, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			var data = new byte[] {0x42, 0x54, 0x12, 0x34};
			using (var stream = new MemoryStream(data))
			{
				dataService.LoadData(stream, model);
			}

			Assert.That(model.DataEntriesCount, Is.EqualTo(1));
			Assert.That(model.GetMachineFunction(0, 0), Is.EqualTo("5442"));
			Assert.That(model.GetMachineFunction(1, 0), Is.EqualTo("3412"));
		}
		public void LoadData_TwoQwordArguments_OneDataEntry()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(2, 0, DataType.Qword, DataType.Byte, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			var data = new byte[]
			           {
			           	0x42, 0x35, 0xAB, 0xCD, 0x01, 0x23, 0x45, 0x67,
			           	0x89, 0x9A, 0xBC, 0xDE, 0xFF, 0x00, 0x11, 0x22
			           };
			using (var stream = new MemoryStream(data))
			{
				dataService.LoadData(stream, model);
			}

			Assert.That(model.DataEntriesCount, Is.EqualTo(1));
			Assert.That(model.GetMachineArgument(0, 0), Is.EqualTo("67452301CDAB3542"));
			Assert.That(model.GetMachineArgument(1, 0), Is.EqualTo("221100FFDEBC9A89"));
		}
Esempio n. 18
0
		private void LoadDataImpl(Stream stream, DataModel dataModel)
		{
			if (dataModel.NumberOfArguments == 0 && dataModel.NumberOfFunctions == 0)
			{
				return;
			}

			var stopFlag = false;
			var argumentBuffer = CreateBuffer(dataModel.ArgumentsType);
			var functionBuffer = CreateBuffer(dataModel.FunctionsType);

			while (true)
			{
				var dataEntry = CreateDataEntry(dataModel);

				for (var i = 0; i < dataModel.NumberOfArguments; i++)
				{
					var readBytesCount = stream.Read(argumentBuffer, 0, argumentBuffer.Length);
					if (readBytesCount == 0)
					{
						stopFlag = true;
						break;
					}

					CheckBufferFilling(argumentBuffer, readBytesCount);
					argumentBuffer.CopyTo(dataEntry.Arguments[i], 0);
				}

				if (stopFlag)
				{
					break;
				}

				for (var i = 0; i < dataModel.NumberOfFunctions; i++)
				{
					var readBytesCount = stream.Read(functionBuffer, 0, functionBuffer.Length);
					if (readBytesCount == 0)
					{
						stopFlag = true;
						break;
					}

					CheckBufferFilling(functionBuffer, readBytesCount);
					functionBuffer.CopyTo(dataEntry.Functions[i], 0);
				}

				if (stopFlag)
				{
					break;
				}

				dataModel.AddEntry(dataEntry);
			}
		}
		public void LoadData_OneDwordFunction_TwoDataEntries()
		{
			var dataPropertiesDto = new FullDataPropertiesDto(
				new CoreDataPropertiesDto(0, 1, DataType.Byte, DataType.Dword, ArithmeticType.Integer));
			var model = new DataModel(string.Empty, dataPropertiesDto, mathService);
			var data = new byte[] {0x42, 0x35, 0xAB, 0xCD, 0x01, 0x23, 0x45, 0x67};
			using (var stream = new MemoryStream(data))
			{
				dataService.LoadData(stream, model);
			}

			Assert.That(model.DataEntriesCount, Is.EqualTo(2));
			Assert.That(model.GetMachineFunction(0, 0), Is.EqualTo("CDAB3542"));
			Assert.That(model.GetMachineFunction(0, 1), Is.EqualTo("67452301"));
		}