Example #1
0
        private void m_service_SendingClientResponse(object sender, EventArgs <Guid, ServiceResponse, bool> e)
        {
            Guid            responseClientID = e.Argument1;
            ServiceResponse response         = e.Argument2;

            if (!Guid.TryParse(ConnectionID, out Guid clientID) || !clientID.Equals(responseClientID) ||
                !ClientHelper.TryParseActionableResponse(response, out string sourceCommand, out _))
            {
                return;
            }

            // If actionable client response is successful and targeted for this hub client,
            // inform service helper that it does not need to broadcast a response
            e.Argument3 = false;

            Guid[] parseSignalIDs(out string sourceAdapter)
            {
                sourceAdapter = null;

                try
                {
                    List <object> attachments = response.Attachments;

                    if (attachments is null || attachments.Count < 2 ||
                        !(attachments[0] is byte[][] signalIDs) ||
                        !(attachments[1] is Arguments arguments) ||
                        !arguments.Exists("OrderedArg1"))
                    {
                        return(Array.Empty <Guid>());
                    }

                    sourceAdapter = arguments["OrderedArg1"];
                    return(signalIDs.Select(bytes => new Guid(bytes)).Where(id => id != Guid.Empty).ToArray());
                }
                catch (Exception ex)
                {
                    Program.Host.LogException(new InvalidOperationException($"Failed to parse actionable service response with Guid buffers: {ex.Message}", ex));
                    return(Array.Empty <Guid>());
                }
            }

            string command = sourceCommand.ToLower().Trim();

            if (command.Equals("getinputmeasurements"))
            {
                Guid[] signalIDs = parseSignalIDs(out string sourceAdapter);

                if (!string.IsNullOrWhiteSpace(sourceAdapter))
                {
                    ClientScript?.parsedInputMeasurements(sourceAdapter, signalIDs.Select(LookupPointTag).Where(tag => !string.IsNullOrWhiteSpace(tag)));
                }
            }
            else if (command.Equals("getoutputmeasurements"))
            {
                Guid[] signalIDs = parseSignalIDs(out string sourceAdapter);

                if (!string.IsNullOrWhiteSpace(sourceAdapter))
                {
                    ClientScript?.parsedOutputMeasurements(sourceAdapter, signalIDs.Select(LookupPointTag).Where(tag => !string.IsNullOrWhiteSpace(tag)));
                }
            }
        }