Exemple #1
0
        private async Task <IActionResult> RunScript(Endpoint endpoint, Dictionary <string, object> parameters = null, bool noSerialization = false)
        {
            try {
                var variables = new Dictionary <string, object> {
                    { "Request", Request },
                    { "Response", Response },
                    { "User", HttpContext?.User?.Identity?.Name },
                    { "MemoryCache", _memoryCache },
                    { "UDConnectionManager", _connectionManager }
                };

                try
                {
                    if (HttpContext.Request.Cookies.ContainsKey("location") == true)
                    {
                        var location = HttpContext.Request.Cookies["location"];
                        if (!string.IsNullOrEmpty(location))
                        {
                            location = Encoding.UTF8.GetString(Convert.FromBase64String(location));
                            var locationObject = JsonConvert.DeserializeObject <Location>(location);
                            variables.Add("Location", locationObject);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("RunScript() Exception processing geolocation. " + ex.Message);
                }

                ExecutionContext executionContext = new ExecutionContext(endpoint, variables, parameters, HttpContext?.User);
                executionContext.NoSerialization = noSerialization;

                if (HttpContext.Request.Headers.TryGetValue("UDConnectionId", out StringValues connectionId))
                {
                    executionContext.SessionId    = _connectionManager.GetSessionId(connectionId);
                    executionContext.ConnectionId = connectionId;
                }

                return(await Task.Run(() =>
                {
                    var result = _executionService.ExecuteEndpoint(executionContext, endpoint);
                    var actionResult = ConvertToActionResult(result);

                    return actionResult;
                }));
            }
            catch (Exception ex) {
                Log.Warn("RunScript() " + ex.Message + Environment.NewLine + ex.StackTrace);
                throw ex;
            }
        }
        public Task ClientEvent(string eventId, string eventName, string eventData, string location)
        {
            _logger.Debug($"ClientEvent {eventId} {eventName}");



            var variables = new Dictionary <string, object>();
            var userName  = Context.User?.Identity?.Name;

            if (!string.IsNullOrEmpty(userName))
            {
                variables.Add("user", userName);
            }

            if (!string.IsNullOrEmpty(location))
            {
                var locationObject = JsonConvert.DeserializeObject <Location>(location);
                variables.Add("Location", locationObject);
            }

            if (bool.TryParse(eventData, out bool data))
            {
                variables.Add("EventData", data);
            }
            else
            {
                variables.Add("EventData", eventData);
            }

            variables.Add("MemoryCache", _memoryCache);

            try
            {
                _memoryCache.TryGetValue(Context.ConnectionId, out string sessionId);

                var endpoint = _dashboardService.EndpointService.Get(eventId, sessionId);
                if (endpoint == null)
                {
                    _logger.Warn($"Endpoint {eventId} not found.");
                    throw new Exception($"Endpoint {eventId} not found.");
                }

                var executionContext = new ExecutionContext(endpoint, variables, new Dictionary <string, object>(), Context.User);
                executionContext.ConnectionId = Context.ConnectionId;
                executionContext.SessionId    = sessionId;

                return(Task.Run(() =>
                {
                    try
                    {
                        _executionService.ExecuteEndpoint(executionContext, endpoint);
                    }
                    catch (Exception ex)
                    {
                        _logger.Error("Failed to execute action. " + ex.Message);
                        throw;
                    }
                }));
            }
            catch (Exception ex)
            {
                _logger.Warn($"Failed to execute endpoint. " + ex.Message);
                throw;
            }
        }
        public Task ClientEvent(string eventId, string eventName, string eventData, string location)
        {
            _logger.Debug($"ClientEvent {eventId} {eventName}");

            var variables = new Dictionary <string, object>();
            var userName  = Context.User?.Identity?.Name;

            if (!string.IsNullOrEmpty(userName))
            {
                variables.Add("user", userName);
            }

            if (!string.IsNullOrEmpty(location))
            {
                location = Encoding.UTF8.GetString(Convert.FromBase64String(location));
                var locationObject = JsonConvert.DeserializeObject <Location>(location);
                variables.Add("Location", locationObject);
            }

            if (bool.TryParse(eventData, out bool data))
            {
                variables.Add("EventData", data);
            }
            else
            {
                variables.Add("EventData", eventData);
            }

            variables.Add("UDConnectionManager", _connectionManager);
            variables.Add("EventId", eventId);
            variables.Add("MemoryCache", _memoryCache);

            try
            {
                var sessionId = _connectionManager.GetSessionId(Context.ConnectionId);

                var endpoint = _dashboardService.EndpointService.Get(eventId, sessionId);
                if (endpoint == null)
                {
                    _logger.Warn($"Endpoint {eventId} not found.");
                    throw new Exception($"Endpoint {eventId} not found.");
                }

                var executionContext = new ExecutionContext(endpoint, variables, new Dictionary <string, object>(), Context.User);
                executionContext.ConnectionId = Context.ConnectionId;
                executionContext.SessionId    = sessionId;

                return(Task.Run(() =>
                {
                    try
                    {
                        dynamic result = _executionService.ExecuteEndpoint(executionContext, endpoint);
                        if (result.Error is Error error)
                        {
                            Clients.Client(Context.ConnectionId).SendAsync("showError", new { message = error.Message });
                        }
                    }
                    catch (RuntimeBinderException) {
                    }
                    catch (Exception ex)
                    {
                        _logger.Error("Failed to execute action. " + ex.Message);
                        throw;
                    }
                }));
            }
            catch (Exception ex)
            {
                _logger.Warn($"Failed to execute endpoint. " + ex.Message);
                throw;
            }
        }