void OnCompareHashes() { var localHash1 = GKFileInfo.CreateHash1(XManager.DeviceConfiguration, DevicesViewModel.Current.SelectedDevice.Device); var localHash2 = GKFileInfo.CreateHash2(XManager.DeviceConfiguration); var gkFileReaderWriter = new GKFileReaderWriter(); var infoBlock = gkFileReaderWriter.ReadInfoBlock(DevicesViewModel.Current.SelectedDevice.Device); if (gkFileReaderWriter.Error != null) { MessageBoxService.ShowError(gkFileReaderWriter.Error); return; } var remoteHash1 = infoBlock.Hash1; var remoteHash2 = infoBlock.Hash2; var message = new StringBuilder(); message.Append(localHash1.SequenceEqual(remoteHash1) ? "Хеш1 совпадает\n" : "Хеш1 НЕ совпадает\n"); message.Append(localHash2.SequenceEqual(remoteHash2) ? "Хеш2 совпадает" : "Хеш2 НЕ совпадает"); MessageBoxService.ShowWarning(message.ToString(), "Сравнение хешей"); }
void ReadConfiguration(GKDevice gkControllerDevice, GKProgressCallback progressCallback, Guid clientUID) { var gkFileReaderWriter = new GKFileReaderWriter(); var gkFileInfo = gkFileReaderWriter.ReadInfoBlock(gkControllerDevice); if (gkFileReaderWriter.Error != null) { Error = gkFileReaderWriter.Error; GKProcessorManager.StopProgress(progressCallback, clientUID); return; } progressCallback = GKProcessorManager.StartProgress("Чтение конфигурации " + gkControllerDevice.PresentationName, "", gkFileInfo.DescriptorsCount, true, GKProgressClientType.Administrator, clientUID); ushort descriptorNo = 0; while (true) { if (progressCallback.IsCanceled) { Error = "Операция отменена"; break; } descriptorNo++; GKProcessorManager.DoProgress("Чтение базы данных объектов ГК " + descriptorNo, progressCallback, clientUID); const byte packNo = 1; var data = new List<byte>(BitConverter.GetBytes(descriptorNo)) { packNo }; for (int i = 0; i < 3; i++) { var sendResult = SendManager.Send(gkControllerDevice, 3, 19, ushort.MaxValue, data); var bytes = sendResult.Bytes; if (!sendResult.HasError && bytes.Count >= 5) { if (bytes[3] == 0xff && bytes[4] == 0xff) return; if (!Parse(bytes.Skip(3).ToList(), descriptorNo)) return; break; } if (i == 2) { Error = "Возникла ошибка при чтении объекта " + descriptorNo; return; } } } }
public void ConnectionChanged(bool isConnected) { lock (connectionChangedLocker) { if (!isConnected) { ConnectionLostCount++; if (ConnectionLostCount < 3) return; } else ConnectionLostCount = 0; if (IsConnected != isConnected) { var journalItem = new JournalItem() { SystemDateTime = DateTime.Now, DeviceDateTime = DateTime.Now, GKIpAddress = XManager.GetIpAddress(GkDatabase.RootDevice), JournalItemType = JournalItemType.System, StateClass = XStateClass.Unknown, ObjectStateClass = XStateClass.Norm, Name = isConnected ? EventName.Восстановление_связи_с_прибором.ToDescription() : EventName.Потеря_связи_с_прибором.ToDescription() }; AddJournalItem(journalItem); IsConnected = isConnected; if (isConnected) { var hashBytes = GKFileInfo.CreateHash1(XManager.DeviceConfiguration, GkDatabase.RootDevice); var gkFileReaderWriter = new GKFileReaderWriter(); var gkFileInfo = gkFileReaderWriter.ReadInfoBlock(GkDatabase.RootDevice); IsHashFailure = gkFileInfo == null || !GKFileInfo.CompareHashes(hashBytes, gkFileInfo.Hash1); } foreach (var descriptor in GkDatabase.Descriptors) { descriptor.XBase.BaseState.IsConnectionLost = !isConnected; } NotifyAllObjectsStateChanged(); } } }
public static OperationResult<List<byte>> GKGKHash(XDevice device) { var gkFileReaderWriter = new GKFileReaderWriter(); var readInfoBlock = gkFileReaderWriter.ReadInfoBlock(device); if (gkFileReaderWriter.Error != null) return new OperationResult<List<byte>>(gkFileReaderWriter.Error); return new OperationResult<List<byte>>() { Result = readInfoBlock.Hash1 }; }
override public bool ReadConfiguration(XDevice gkDevice) { var progressCallback = GKProcessorManager.StartProgress("Чтение конфигурации " + gkDevice.PresentationName, "Проверка связи", 2, true, GKProgressClientType.Administrator); var result = DeviceBytesHelper.Ping(gkDevice); if (!result) { Error = "Устройство " + gkDevice.PresentationName + " недоступно"; return false; } IpAddress = gkDevice.GetGKIpAddress(); ControllerDevices = new Dictionary<ushort, XDevice>(); DeviceConfiguration = new XDeviceConfiguration(); var rootDriver = XManager.Drivers.FirstOrDefault(x => x.DriverType == XDriverType.System); DeviceConfiguration.RootDevice = new XDevice { Driver = rootDriver, DriverUID = rootDriver.UID }; GKProcessorManager.DoProgress("Перевод ГК в технологический режим", progressCallback); if (!DeviceBytesHelper.GoToTechnologicalRegime(gkDevice, progressCallback)) { Error = "Не удалось перевести " + gkDevice.PresentationName + " в технологический режим\n" + "Устройство не доступно, либо вашего " + "IP адреса нет в списке разрешенного адреса ГК"; GKProcessorManager.StopProgress(progressCallback); return false; } var gkFileReaderWriter = new GKFileReaderWriter(); var gkFileInfo = gkFileReaderWriter.ReadInfoBlock(gkDevice); if (gkFileReaderWriter.Error != null) { Error = gkFileReaderWriter.Error; GKProcessorManager.StopProgress(progressCallback); return false; } progressCallback = GKProcessorManager.StartProgress("Чтение конфигурации " + gkDevice.PresentationName, "", gkFileInfo.DescriptorsCount, true, GKProgressClientType.Administrator); ushort descriptorNo = 0; #if SETCONFIGTOFILE var allBytes = new List<List<byte>>(); #endif while (true) { if (progressCallback.IsCanceled) { Error = "Операция отменена"; break; } descriptorNo++; GKProcessorManager.DoProgress("Чтение базы данных объектов ГК " + descriptorNo, progressCallback); const byte packNo = 1; var data = new List<byte>(BitConverter.GetBytes(descriptorNo)) {packNo}; var sendResult = SendManager.Send(gkDevice, 3, 19, ushort.MaxValue, data); var bytes = sendResult.Bytes; #if SETCONFIGTOFILE allBytes.Add(bytes); #endif if (sendResult.HasError || bytes.Count < 5) { Error = "Возникла ошибка при чтении объекта " + descriptorNo; break; } if (bytes[3] == 0xff && bytes[4] == 0xff) break; if (!Parse(bytes.Skip(3).ToList(), descriptorNo)) break; } #if SETCONFIGTOFILE /* Опция включения записи конфигурации в файл */ BytesHelper.BytesToFile("GKConfiguration.txt", allBytes); #endif GKProcessorManager.DoProgress("Перевод ГК в рабочий режим", progressCallback); if (!DeviceBytesHelper.GoToWorkingRegime(gkDevice, progressCallback)) { Error = "Не удалось перевести устройство в рабочий режим в заданное время"; } GKProcessorManager.StopProgress(progressCallback); if(Error != null) return false; DeviceConfiguration.Update(); return true; }
Tuple<bool, GKFileInfo> GetHashResult() { var hashBytes = GKFileInfo.CreateHash1(GkDatabase.RootDevice); var gkFileReaderWriter = new GKFileReaderWriter(); var gkFileInfo = gkFileReaderWriter.ReadInfoBlock(GkDatabase.RootDevice); return Tuple.Create<bool, GKFileInfo>(!GKFileInfo.CompareHashes(hashBytes, gkFileInfo.Hash1), gkFileInfo); }
bool InitializeMonitoring() { bool IsPingFailure = false; bool IsInTechnologicalRegime = false; bool IsGetStatesFailure = false; IsHashFailure = false; foreach (var descriptor in GkDatabase.Descriptors) { descriptor.XBase.BaseState.Clear(); } while (true) { LastUpdateTime = DateTime.Now; GKCallbackResult = new GKCallbackResult(); foreach (var descriptor in GkDatabase.Descriptors) { descriptor.XBase.BaseState.IsInitialState = true; } var deviceInfo = DeviceBytesHelper.GetDeviceInfo(GkDatabase.RootDevice); var result = string.IsNullOrEmpty(deviceInfo); if (IsPingFailure != result) { GKCallbackResult = new GKCallbackResult(); IsPingFailure = result; if (IsPingFailure) AddFailureJournalItem(EventName.Нет_связи_с_ГК, EventDescription.Старт_мониторинга); else AddFailureJournalItem(EventName.Связь_с_ГК_восстановлена, EventDescription.Старт_мониторинга); foreach (var descriptor in GkDatabase.Descriptors) { descriptor.XBase.BaseState.IsConnectionLost = IsPingFailure; descriptor.XBase.BaseState.IsInitialState = !IsPingFailure; } NotifyAllObjectsStateChanged(); OnGKCallbackResult(GKCallbackResult); } if (IsPingFailure) { if (ReturnArterWait(5000)) return false; continue; } result = CheckTechnologicalRegime(); if (IsInTechnologicalRegime != result) { GKCallbackResult = new GKCallbackResult(); IsInTechnologicalRegime = result; if (IsInTechnologicalRegime) AddFailureJournalItem(EventName.ГК_в_технологическом_режиме, "Старт мониторинга"); else AddFailureJournalItem(EventName.ГК_в_рабочем_режиме, "Старт мониторинга"); NotifyAllObjectsStateChanged(); OnGKCallbackResult(GKCallbackResult); } if (IsInTechnologicalRegime) { if (ReturnArterWait(5000)) return false; continue; } var hashBytes = GKFileInfo.CreateHash1(XManager.DeviceConfiguration, GkDatabase.RootDevice); var gkFileReaderWriter = new GKFileReaderWriter(); var gkFileInfo = gkFileReaderWriter.ReadInfoBlock(GkDatabase.RootDevice); result = gkFileInfo == null || !GKFileInfo.CompareHashes(hashBytes, gkFileInfo.Hash1); if (IsHashFailure != result) { GKCallbackResult = new GKCallbackResult(); IsHashFailure = result; if (IsHashFailure) AddFailureJournalItem(EventName.Конфигурация_прибора_не_соответствует_конфигурации_ПК, EventDescription.Не_совпадает_хэш); else AddFailureJournalItem(EventName.Конфигурация_прибора_соответствует_конфигурации_ПК, EventDescription.Совпадает_хэш); foreach (var descriptor in GkDatabase.Descriptors) { descriptor.XBase.BaseState.IsDBMissmatch = IsHashFailure; descriptor.XBase.BaseState.IsInitialState = false; } NotifyAllObjectsStateChanged(); OnGKCallbackResult(GKCallbackResult); } if (IsHashFailure) { if (ReturnArterWait(5000)) return false; continue; } GKCallbackResult = new GKCallbackResult(); if (!ReadMissingJournalItems()) AddFailureJournalItem(EventName.Ошибка_при_синхронизации_журнала); OnGKCallbackResult(GKCallbackResult); GKCallbackResult = new GKCallbackResult(); GetAllStates(); result = IsDBMissmatchDuringMonitoring || !IsConnected; if (IsGetStatesFailure != result) { IsGetStatesFailure = result; if (IsGetStatesFailure) AddFailureJournalItem(EventName.Ошибка_при_опросе_состояний_компонентов_ГК, DBMissmatchDuringMonitoringReason); else AddFailureJournalItem(EventName.Устранена_ошибка_при_опросе_состояний_компонентов_ГК); } OnGKCallbackResult(GKCallbackResult); if (IsGetStatesFailure) { if (ReturnArterWait(5000)) return false; continue; } GKCallbackResult = new GKCallbackResult(); foreach (var descriptor in GkDatabase.Descriptors) { descriptor.XBase.BaseState.IsInitialState = false; } NotifyAllObjectsStateChanged(); OnGKCallbackResult(GKCallbackResult); return true; } }