Esempio n. 1
0
		public DeviceDetailsViewModel(Device device)
			: this()
		{
			Title = "Редактирование устройства " + device.Name + " " + device.FullAddress;
			_parent = device.Parent;
			Address = device.Address;
			Initialize(device);
		}
Esempio n. 2
0
		public static List<Device> GetAllChildren(Device device, bool isWithSelf = true)
		{
			var result = new List<Device>();
			if (isWithSelf)
				result.Add(device);
			result.AddRange(device.Children.SelectMany(x => GetAllChildren(x)));
			return result;
		}
Esempio n. 3
0
		public DeviceDetailsViewModel(DriverType driverType, Device parent)
			: this()
		{
			_parent = parent;
			Address = parent.Children.Count + 1;
			var device = new Device(driverType);
			Title = "Создание устройства " + device.Name;
			Initialize(device);
		}
Esempio n. 4
0
		private FilterDeviceViewModel AddDeviceInternal(Device device, FilterDeviceViewModel parentDeviceViewModel =null)
		{
			var deviceViewModel = new FilterDeviceViewModel(device);
			if (parentDeviceViewModel != null)
				parentDeviceViewModel.AddChild(deviceViewModel);

			foreach (var childDevice in device.Children)
				AddDeviceInternal(childDevice, deviceViewModel);
			return deviceViewModel;
		}
Esempio n. 5
0
		private DeviceViewModel AddDeviceInternal(Device device, DeviceViewModel parentDeviceViewModel, List<Guid> exceptDeviceUids)
		{
			var deviceViewModel = new DeviceViewModel(device);
			foreach (var childDevice in device.Children)
			{
				if (!exceptDeviceUids.Any(x => x == childDevice.UID))
					AddDeviceInternal(childDevice, deviceViewModel, exceptDeviceUids);
			}
			if (parentDeviceViewModel != null && (deviceViewModel.ChildrenCount > 0 || device.Children.Count == 0))
				parentDeviceViewModel.AddChild(deviceViewModel);
			return deviceViewModel;
		}
Esempio n. 6
0
		public void SetParent(Device parent, int? address = null)
		{
			if (parent == null)
				return;
			Parent = parent;
			ParentUID = parent.UID;
			Parent.Children.Add(this);
			if (address != null)
				Address = address.Value;
			else
				Address = Parent.Children.Count;
			SetFullAddress();
		}
Esempio n. 7
0
		void GetAllDevices(Device device)
		{
			foreach (var child in device.Children)
			{
				if (child.DeviceType == DeviceType.Counter)
				{
					avaliableDevices.Add(child);
				}
				else
				{
					GetAllDevices(child);
				}
			}
		}
Esempio n. 8
0
		public static bool SaveDevice(Device device)
		{
			try
			{
				bool isNew = false;
				foreach (var item in device.Parameters)
				{
					var validateResult = item.Validate();
					if (validateResult != null)
					{
						MessageBoxService.Show(string.Format("Устройство {0} \n Параметр {1} \n {2}", device.Name, item.DriverParameter.Description, validateResult));
						return false;
					}
				}
				using (var context = DatabaseContext.Initialize())
				{
					var tableDevice = context.Devices.Include(x => x.Parameters).FirstOrDefault(x => x.UID == device.UID);
					if (tableDevice != null)
					{
						context.Parameters.RemoveRange(tableDevice.Parameters);
						CopyDevice(device, tableDevice, context);
						var parent = GetAllChildren(RootDevice).FirstOrDefault(x => x.UID == device.Parent.UID);
						parent.Children.RemoveAll(x => x.UID == device.UID);
						parent.Children.Add(device);
					}
					else
					{
						isNew = true;
						tableDevice = new Device { UID = device.UID };
						CopyDevice(device, tableDevice, context);
						context.Devices.Add(tableDevice);
						if (device.Parent != null)
						{
							var parent = GetAllChildren(RootDevice).FirstOrDefault(x => x.UID == device.Parent.UID);
							if (!parent.Children.Any(x => x.UID == device.UID))
								parent.Children.Add(device);
						}
					}
					context.SaveChanges();
					AddJournalForUser(isNew ? JournalType.AddDevice : JournalType.EditDevice, device);
				}
				return true;
			}
			catch (Exception e)
			{
				MessageBoxService.Show(e.Message);
				return false;
			}
		}
Esempio n. 9
0
		public Device(ResursAPI.DriverType driverType, Device parent = null)
			: this()
		{
			Name = driverType.ToDescription();
			Driver = ResursAPI.DriversConfiguration.GetDriver(driverType);
			DriverUID = Driver.UID;
			TariffType = Driver.DefaultTariffType;
			foreach (var item in Driver.DriverParameters)
			{
				var parameter = new Parameter { Device = this };
				parameter.Initialize(item);
				Parameters.Add(parameter);
			}

			SetParent(parent);
		}
Esempio n. 10
0
		void Initialize(Device device)
		{
			Device = device;
			_oldDevice = device;
			Name = device.Name;
			Description = device.Description;
			IsActive = Device.IsActive;
			Consumer = device.Consumer;
			IsNetwork = device.DeviceType == DeviceType.Network;
			if (!IsActive)
			{
				IsEditComPort = IsNetwork;
				if (IsEditComPort)
				{
					ComPorts = new ObservableCollection<string>(GetComPorts());
					ComPort = ComPorts.FirstOrDefault(x => x == device.ComPort);
				}
				IsEditAddress = !IsEditComPort;
			}
			else
			{
				AddressString = IsNetwork ? device.ComPort : device.Address.ToString();
			}
			Parameters = new ObservableCollection<DetailsParameterViewModel>(Device.Parameters.Select(x => new DetailsParameterViewModel(x, this)));
			Commands = new ObservableCollection<CommandViewModel>(Device.Commands.Select(x => new CommandViewModel(Device, x)));
			TariffTypes = new ObservableCollection<TariffType>();
			foreach (TariffType item in Enum.GetValues(typeof(TariffType)))
			{
				TariffTypes.Add(item);
			}
			Tariff = device.Tariff;
			TariffType = device.TariffType;
			HasTariffType = device.DeviceType == DeviceType.Counter;
			CanEditTariffType = device.Driver.CanEditTariffType && HasTariffType;
			HasReadOnlyTariffType = !device.Driver.CanEditTariffType && HasTariffType;
		}
Esempio n. 11
0
		public DeviceViewModel AddDevice(Device device, DeviceViewModel parentDeviceViewModel)
		{
			var deviceViewModel = AddDeviceInternal(device, parentDeviceViewModel);
			FillAllDevices();
			return deviceViewModel;
		}
Esempio n. 12
0
		void UpdateConsumerDetailsViewModels(Device device, Consumer oldConsumer = null)
		{
			var newConsumer = device.Consumer;
			if (oldConsumer == newConsumer)
				return;
			if (oldConsumer != null)
			{
				var consumerDetailsViewModel = Bootstrapper.MainViewModel.ConsumersViewModel.FindConsumerDetailsViewModel(oldConsumer.UID);
				if (consumerDetailsViewModel != null)
				{
					var deviceViewModel = consumerDetailsViewModel.Devices.FirstOrDefault(x => x.Device.UID == device.UID);
					if (deviceViewModel != null)
						consumerDetailsViewModel.Devices.Remove(deviceViewModel);
				}
			}
			if (newConsumer != null)
			{
				var consumerDetailsViewModel = Bootstrapper.MainViewModel.ConsumersViewModel.FindConsumerDetailsViewModel(newConsumer.UID);
				if (consumerDetailsViewModel != null)
				{
					var deviceViewModel = consumerDetailsViewModel.Devices.FirstOrDefault(x => x.Device.UID == device.UID);
					if (deviceViewModel == null)
						consumerDetailsViewModel.Devices.Add(new DeviceViewModel(device));
				}
			}
		}
Esempio n. 13
0
		public static void CreateSystem()
		{
			try
			{
				using (var context = DatabaseContext.Initialize())
				{
					RootDevice = new Device(DriverType.System);
					RootDevice.DateTime = RootDevice.DateTime.CheckDate();
					context.Devices.Add(RootDevice);
					context.SaveChanges();
				}
			}
			catch (Exception e)
			{
				MessageBoxService.Show(e.Message);
			}
		}
Esempio n. 14
0
		public TariffDeviceViewModel(Device device)
		{
			Device = device;
		}
Esempio n. 15
0
		public static void GenerateTestDevices()
		{
			try
			{
				using (var context = DatabaseContext.Initialize())
				{
					context.Devices.RemoveRange(context.Devices);
					context.SaveChanges();
				}
				var devices = new List<Device>();
				Random random = new Random();
				RootDevice = new Device(DriverType.System);
				devices.Add(RootDevice);
#if DEBUG
				int interfaces = 10;
				int devicesPerInterface = 100;
				for (int i = 0; i < interfaces / 2; i++)
				{
					var interfaceDevice = new Device(DriverType.BeregunNetwork, RootDevice);
					interfaceDevice.ComPort = "COM" + (i + 1);
					InitializeTestDevice(interfaceDevice, random);
					devices.Add(interfaceDevice);
					for (int j = 0; j < devicesPerInterface; j++)
					{
						var counter = new Device(DriverType.BeregunCounter, interfaceDevice);
						InitializeTestDevice(counter, random);
						devices.Add(counter);
					}
				}
				for (int i = 0; i < interfaces / 2; i++)
				{
					var interfaceDevice = new Device(DriverType.MZEP55Network, RootDevice);
					interfaceDevice.ComPort = "COM" + (interfaces / 2 + i + 1);
					devices.Add(interfaceDevice);
					InitializeTestDevice(interfaceDevice, random);
					for (int j = 0; j < devicesPerInterface; j++)
					{
						var counter = new Device(DriverType.MZEP55Counter, interfaceDevice);
						InitializeTestDevice(counter, random);
						devices.Add(counter);
					}
				}
				var dateTimes = devices.SelectMany(x => x.Parameters).Select(x => x.DateTimeValue).Distinct();
#endif

				AddRangeDevicesQuery(devices);
				AddRangeParametersQuery(devices.SelectMany(x => x.Parameters));
				
			}
			catch (Exception e)
			{
				MessageBoxService.Show(e.Message);
			}
		}
Esempio n. 16
0
		public CommandViewModel(Device device, DeviceCommand model)
		{
			_device = device;
			Model = model;
		}
Esempio n. 17
0
		static void InitializeTestDevice(Device device, Random random)
		{
			device.Description = device.Name + " " + device.FullAddress;
			if(device.DeviceType == DeviceType.Counter && device.Driver.CanEditTariffType)
			{
				device.TariffType = (TariffType)random.Next(4);
			}
			foreach (var item in device.Parameters)
			{
				switch (item.DriverParameter.ParameterType)
				{
					case ParameterType.Enum:
						var maxValue = item.DriverParameter.ParameterEnumItems.Max(x => x.Value);
						item.IntValue = random.Next(maxValue + 1);
						break;
					case ParameterType.String:
						item.StringValue = Guid.NewGuid().ToString();
						break;
					case ParameterType.Int:
						var intMinValue = item.DriverParameter.IntMinValue ?? 0;
						var intMaxValue = item.DriverParameter.IntMaxValue ?? 10000;
						item.IntValue = random.Next(intMinValue, intMaxValue);
						break;
					case ParameterType.Double:
						var doubleMinValue = item.DriverParameter.DoubleMinValue ?? 0;
						var doubleMaxValue = item.DriverParameter.DoubleMaxValue ?? 10000;
						item.DoubleValue = Math.Truncate(doubleMinValue + random.NextDouble() * doubleMaxValue * 100) / 100;
						break;
					case ParameterType.Bool:
						item.BoolValue = random.Next(2) > 0;
						break;
					case ParameterType.DateTime:
						var minDateTime = item.DriverParameter.DateTimeMinValue ?? new DateTime(2000, 1, 1);
						var maxDateTime = item.DriverParameter.DateTimeMaxValue != null && item.DriverParameter.DateTimeMaxValue.Value < DateTime.Now ? item.DriverParameter.DateTimeMaxValue.Value : DateTime.Now;
						var delta = (maxDateTime - minDateTime).TotalSeconds;
						var randomDelta = TimeSpan.FromSeconds(random.NextDouble() * delta);
						item.DateTimeValue = minDateTime + randomDelta;
						break;
					default:
						break;
				}
			}
		}
Esempio n. 18
0
		static void InitializeDevice(Device device)
		{
			device.Driver = DriversConfiguration.Drivers.FirstOrDefault(x => x.UID == device.DriverUID);
			foreach (var item in device.Parameters)
			{
				var driverParameter = device.Driver.DriverParameters.FirstOrDefault(x => x.UID == item.DriverParameterUID);
				item.Initialize(driverParameter);
			}
			if (!device.Driver.CanEditTariffType)
				device.TariffType = device.Driver.DefaultTariffType;
		}
Esempio n. 19
0
		static void SetFullAddress(Device device)
		{
			device.SetFullAddress();
			foreach (var item in device.Children)
			{
				SetFullAddress(item);
			}
		}
Esempio n. 20
0
		public static bool DeleteDevice(Device device)
		{
			try
			{
				Guid deviceUID;
				string deviceName;
				using (var context = DatabaseContext.Initialize())
				{
					var tableItem = context.Devices.FirstOrDefault(x => x.UID == device.UID);
					if (tableItem == null)
						return false;
					deviceUID = tableItem.UID;
					deviceName = tableItem.Name;
					var items = new List<Device>();
					var currentItems = context.Devices.Where(x => x.Parent.UID == device.UID).ToList();
					items.AddRange(currentItems);
					while (currentItems.Count > 0)
					{
						var childrenItems = new List<Device>();
						foreach (var item in currentItems)
						{
							var childItems = context.Devices.Where(x => x.Parent.UID == item.UID).ToList();
							childrenItems.AddRange(childItems);
							items.AddRange(childItems);
						}
						currentItems = childrenItems;
					}
					items.Add(tableItem);
					context.Devices.RemoveRange(items);
					context.SaveChanges();
				
					var parent = GetAllChildren(RootDevice).FirstOrDefault(x => x.UID == device.Parent.UID);
					parent.Children.RemoveAll(x => x.UID == device.UID);
				}
				AddJournalForUser(JournalType.DeleteDevice, device);
				return true;
			}
			catch (Exception e)
			{
				MessageBoxService.Show(e.Message);
				return false;
			}
		}
Esempio n. 21
0
		static void CopyDevice(Device device, Device tableDevice, DatabaseContext context)
		{
			tableDevice.Address = device.Address;
			tableDevice.ConsumerUID = device.ConsumerUID;
			tableDevice.Consumer = device.Consumer != null ? context.Consumers.FirstOrDefault(x => x.UID == device.Consumer.UID) : null;
			tableDevice.Name = device.Name;
			tableDevice.Description = device.Description;
			tableDevice.IsActive = device.IsActive;
			tableDevice.TariffUID = device.Tariff != null ? (Guid?)device.Tariff.UID : null;
			tableDevice.TariffType = device.TariffType;
			tableDevice.Parent = device.Parent != null ? context.Devices.FirstOrDefault(x => x.UID == device.Parent.UID) : null;
			tableDevice.DriverUID = device.DriverUID;
			tableDevice.IsDbMissmatch = device.IsDbMissmatch;
			tableDevice.TariffUID = device.TariffUID;
			tableDevice.DateTime = device.DateTime.CheckDate();
			if (device.DeviceType == DeviceType.Network)
				tableDevice.ComPort = device.ComPort;
			tableDevice.Parameters = device.Parameters.Select(x => new Parameter
			{
				BoolValue = x.BoolValue,
				DateTimeValue = x.DateTimeValue,
				Device = tableDevice,
				DoubleValue = x.DoubleValue,
				IntValue = x.IntValue,
				DriverParameterUID = x.DriverParameterUID,
				StringValue = x.StringValue,
			}).ToList();
		}
Esempio n. 22
0
		static void SetChildren(Device device, List<Device> allDevices)
		{
			device.Children = new List<Device>(allDevices.Where(x => x.ParentUID != null && x.ParentUID == device.UID).OrderBy(x => x.Address));
			foreach (var item in device.Children)
			{
				item.Parent = device;
				SetChildren(item, allDevices);
			}
		}