Beispiel #1
0
        public static TheRecordHolder MeshQueryRecordHolder(int idRecord, Guid node, string strEngineName, Guid cdeMIdThing)
        {
            TheRecordHolder trh = null;

            // Package up request info.
            MsgMileRecordHolder msgRequest = new MsgMileRecordHolder()
            {
                id = idRecord
            };

            // Start asynchronous task to send a message and wait for a reply.
            // Sends a message named nameof(MsgMileRecordHolder)
            // Receives a reply named nameof(MsgMileRecordHolderResponse)
            // See function "HandleMessage" for actual handling.

            Task <MsgMileRecordHolderResponse> t = null;

            try
            {
                TheMessageAddress tma = new TheMessageAddress()
                {
                    Node       = Guid.Empty,
                    EngineName = strEngineName,
                    ThingMID   = cdeMIdThing,
                    SendToProvisioningService = false,
                };
                t = TheCommRequestResponse.PublishRequestJSonAsync <MsgMileRecordHolder, MsgMileRecordHolderResponse>(tma, msgRequest);
            }
            catch (Exception ex)
            {
                string strMessage = ex.Message;
            }

            // Wait for a bit
            t.Wait(20000);
            bool bTaskCompleted = t.IsCompleted;

            // Check for success.
            if (bTaskCompleted)
            {
                MsgMileRecordHolderResponse msgResponse = t.Result;
                trh = msgResponse.data;
            }

            return(trh);
        }
        /// <summary>
        /// Handles Messages sent from a host sub-engine to its clients
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="pIncoming"></param>
        public void HandleMessage(ICDEThing sender, object pIncoming)
        {
            TheProcessMessage pMsg = pIncoming as TheProcessMessage;

            if (pMsg == null)
            {
                return;
            }

            string[] cmd = pMsg.Message.TXT.Split(':');
            switch (cmd[0])
            {
            case "CDE_INITIALIZED":
                MyBaseEngine.SetInitialized(pMsg.Message);
                break;

            case nameof(MsgMileRecordHolder):
                if (g_EnableMeshDataResponse)
                {
                    // Request from another node for mile record holder information.
                    var request     = TheCommRequestResponse.ParseRequestMessageJSON <MsgMileRecordHolder>(pMsg.Message);
                    var MsgResponse = new MsgMileRecordHolderResponse();
                    if (request != null)
                    {
                        MsgResponse.data = TheRecordHolder.QueryRecordHolder(request.id);
                    }
                    TheCommRequestResponse.PublishResponseMessageJson(pMsg.Message, MsgResponse);

                    MsgResponse = null;       // Prevent legacy response handler for being sent.
                }
                break;

            default:
                break;
            }
        }
Beispiel #3
0
        /// <summary>
        /// sinkThingApiInterceptor - Called when our URL is referenced.
        /// </summary>
        /// <param name="pRequest"></param>
        public void sinkThingApiInterceptor(TheRequestData pRequest)
        {
            string strQuery = pRequest.RequestUri.Query;

            if (strQuery.StartsWith("?"))
            {
                strQuery = strQuery.Substring(1);
            }

            string strLocalPathLower = pRequest.RequestUri.LocalPath.ToLower();

            if (strLocalPathLower == "/api/milerecordholder/logon")
            {
                Dictionary <string, string> aParameters = new Dictionary <string, string>();
                utils.ParseQueryParameters(pRequest.RequestUri.Query, aParameters);
                utils.ValidateUserCredentials(pRequest, aParameters);
            }
            else if (strLocalPathLower == "/api/milerecordholder/logoff")
            {
                Dictionary <string, string> aParameters = new Dictionary <string, string>();
                utils.ParseQueryParameters(pRequest.RequestUri.Query, aParameters);
                if (utils.IsTokenValid(aParameters))
                {
                    // TBD
                }
            }
            else if (strLocalPathLower == "/api/milerecordholder/query")
            {
                // Local: validate access token.
                Dictionary <string, string> aParameters = new Dictionary <string, string>();
                utils.ParseQueryParameters(pRequest.RequestUri.Query, aParameters);
                TheRecordHolder trh = null;
                if (utils.IsTokenValid(aParameters))
                {
                    // Local: parse incoming parameters.
                    if (utils.QueryIntFromDictionary(aParameters, "id", out int id))
                    {
                        if (g_EnableMeshDataQuery)
                        {
                            // Remote: query for data.
                            string strEngineName = this.GetBaseEngine().GetEngineName();
                            Guid   guidNode      = TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID;
                            Guid   guidThing     = this.GetBaseThing().cdeMID;

                            trh = WebToMesh.MeshQueryRecordHolder(id, guidNode, strEngineName, guidThing);
                        }
                        else
                        {
                            // Local: query for data.
                            trh = TheRecordHolder.QueryRecordHolder(id);
                        }
                    }
                }

                if (trh != null)
                {
                    utils.CreateJsonResponse <TheRecordHolder>(pRequest, trh);
                }
                else
                {
                    utils.CreateEmptyResponse(pRequest);
                }
            }
        }