/// <summary>
        /// Retrieve data from the Omnik
        /// </summary>
        public async Task RetrieveData()
        {
            Logging.Logdata.LogMessage(TraceLevel.Verbose, string.Format($"Connecting to Omnik at {OmnikAddress}:{OmnikPort} with serialnumber {WifiSerialNumber} to pull data"));

            // Initiate the connection to the Omnik asynchronously
            TcpClient = new TcpClient(AddressFamily.InterNetwork);

            try
            {
                await TcpClient.ConnectAsync(OmnikAddress, OmnikPort);
            }
            catch (Exception exception)
            {
                Logging.Logdata.LogMessage(TraceLevel.Warning, string.Format($"Error while waiting for reply from Omnik at {OmnikAddress}:{OmnikPort} with serialnumber {WifiSerialNumber} in response to the initial statistics request. Exception: { exception.Message}. StackTrace: {exception.StackTrace}."));
                throw;
            }

            if (!TcpClient.Connected)
            {
                var errorMessage = string.Format($"Unable to connect to Omnik at {OmnikAddress}:{OmnikPort} with serialnumber {WifiSerialNumber} to pull data");
                Logging.Logdata.LogMessage(TraceLevel.Verbose, errorMessage);

                DataPullSessionFailed?.Invoke(OmnikAddress, OmnikPort, WifiSerialNumber, errorMessage);
                return;
            }

            // Send out the message to the Omnik to request the statistics
            SendDataPullMessage(TcpClient);

            // Wait for data to be received in response to sending out the data request
            var dataBuffer = new byte[DefaultDataBufferLength];

            try
            {
                TcpClient.GetStream().BeginRead(dataBuffer, 0, dataBuffer.Length, HandleReceivedData, dataBuffer);
            }
            catch (Exception exception)
            {
                Logging.Logdata.LogMessage(TraceLevel.Warning, string.Format($"Error while waiting for reply from Omnik at {OmnikAddress}:{OmnikPort} with serialnumber {WifiSerialNumber} in response to the initial statistics request. Exception: { exception.Message}. StackTrace: {exception.StackTrace}."));
            }
        }
Exemple #2
0
 /// <summary>
 /// Triggered when a data pull session failed
 /// </summary>
 /// <param name="omnikAddress">IP Address or DNS name of the Omnik</param>
 /// <param name="omnikPort">Port number of the Omnik</param>
 /// <param name="serialNumber">Serial number of the Omnik</param>
 /// <param name="reason">Reason why the connection failed</param>
 private void HandleDataPullSessionFailed(string omnikAddress, int omnikPort, string serialNumber, string reason)
 {
     DataPullSessionFailed?.Invoke(omnikAddress, omnikPort, serialNumber, reason);
 }