示例#1
0
        public IActionResult Index(IotModel model)
        {
            var cu = ControllerUtils.From(this);

            cu.PersistInput("HostName", model, IotModel.Default.HostName);
            cu.PersistInput("DeviceId", model, IotModel.Default.DeviceId);
            cu.PersistInput("DeviceKey", model, IotModel.Default.DeviceKey);

            if (model.Message.StartsWith("IoT Test Message at "))
            {
                model.Message = $"IoT Test Message at {DateTime.UtcNow.ToString(Tono.TimeUtil.FormatYMDHMS)}";
            }

            // Send Message to IoT Hub
            try
            {
                var client = DeviceClient.Create(model.HostName, new DeviceAuthenticationWithRegistrySymmetricKey(model.DeviceId, model.DeviceKey), model.TransportType);
                var mes    = new Message(Encoding.UTF8.GetBytes(model.Message));
                client.SendEventAsync(
                    message: mes,
                    cancellationToken: new CancellationTokenSource(TimeSpan.FromMilliseconds(5000)).Token
                    ).ConfigureAwait(false).GetAwaiter().GetResult();

                model.Result = $"Successfully sent the message '{model.Message}' from Device [{model.DeviceId}]";
            }
            catch (Exception ex)
            {
                model.ErrorMessage = ex.Message;
            }

            return(View(model));
        }
示例#2
0
        public IActionResult Index(StorageModel model)
        {
            // Apply input history from cookie
            var cu = ControllerUtils.From(this);

            cu.PersistInput("StrageAccountName", model, StorageModel.Default.StrageAccountName);
            cu.PersistInput("Key", model, StorageModel.Default.Key);
            cu.PersistInput("Page", model, StorageModel.Default.Page);
            cu.PersistInput("BlobContainerName", model, StorageModel.Default.BlobContainerName);
            cu.PersistInput("BlobName", model, StorageModel.Default.BlobName);
            cu.PersistInput("FileShareName", model, StorageModel.Default.FileShareName);
            cu.PersistInput("FileName", model, StorageModel.Default.FileName);
            cu.PersistInput("TableName", model, StorageModel.Default.TableName);
            cu.PersistInput("TablePartition", model, StorageModel.Default.TablePartition);
            cu.PersistInput("TableKey", model, StorageModel.Default.TableKey);
            cu.PersistInput("QueueName", model, StorageModel.Default.QueueName);

            if (!model.Pages.Contains(model.Page))
            {
                model.Page = "Blob";
            }
            switch (model.Page)
            {
            case "Blob": return(Blob(model));

            case "File": return(File(model));

            case "Table": return(Table(model));

            case "Queue": return(Queue(model));

            default: return(NotFound($"Page {model.Page} not found."));
            }
        }
示例#3
0
        public IActionResult Index(KeyVaultModel model)
        {
            Request.Headers.Add("X-WI-ApplicationID", model.ApplicationID);
            Request.Headers.Add("X-WI-ClientSecret", model.ClientSecret);

            // Apply input history from cookie
            var cu = ControllerUtils.From(this);

            cu.PersistInput("Url", model, KeyVaultModel.Default.Url);
            cu.PersistInput("ApplicationID", model, KeyVaultModel.Default.ApplicationID);
            cu.PersistInput("ClientSecret", model, KeyVaultModel.Default.ClientSecret);
            cu.PersistInput("Key", model, KeyVaultModel.Default.Key);

            if (!model.SkipRequest)
            {
                try
                {
                    model.Value = GetSecretAsync(model).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (Exception ex)
                {
                    model.ErrorMessage = ex.Message;
                }
            }
            model.SkipRequest = false;

            return(View(model));
        }
示例#4
0
        public IActionResult Index(RedisModel model)
        {
            if (model.HostName?.EndsWith(".redis.cache.windows.net") ?? false)
            {
                model.HostName = StrUtil.LeftBefore(model.HostName, "\\.");
            }
            var cu = ControllerUtils.From(this);
            cu.PersistInput("HostName", model, RedisModel.Default.HostName);
            cu.PersistInput("AccessKey", model, RedisModel.Default.AccessKey);
            cu.PersistInput("Key", model, RedisModel.Default.Key);

            try
            {
                switch (model.Mode)
                {
                    case RedisModel.Modes.Ping:
                        var ret = GetRedis(model).Ping();
                        model.Result = $"PONG {ret.TotalMilliseconds}[ms]";
                        break;
                    case RedisModel.Modes.Get:
                        var rv = GetRedis(model).StringGet(new RedisKey(model.Key));
                        if (rv.HasValue)
                        {
                            model.Result = rv.ToString();
                        }
                        else
                        {
                            model.ErrorMessage = $"Key '{model.Key}' has not a value";
                        }
                        break;
                    case RedisModel.Modes.Set:
                        var sr = GetRedis(model).StringSet(new RedisKey(model.Key), new RedisValue(model.Value));
                        if (sr)
                        {
                            model.Result = $"OK : Set '{model.Key}' = '{model.Value}'";
                        }
                        else
                        {
                            model.ErrorMessage = $"Error : Set '{model.Key}' = '{model.Value}'";
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                model.ErrorMessage = ex.Message;
            }
            return View(model);
        }
示例#5
0
        public IActionResult Index(CallModel model)
        {
            // Apply input history from cookie
            var cu = ControllerUtils.From(this);

            cu.PersistInput("Uri", model, CallModel.Default.Uri);
            cu.PersistInput("Method", model, CallModel.Default.Method);
            cu.PersistInput("Body", model, CallModel.Default.Body);

            model.SampleUri = $"{Request.Scheme}://{Request.Host}/api/Dummy?a=11&b=22";

            if (!model.SkipCall)
            {
                Task <HttpResponseMessage> task = null;
                switch (model.Method)
                {
                case "(unknown)":
                case "GET":
                    task         = HTTP.GetAsync(model.Uri);
                    model.Method = "GET";
                    break;

                case "POST":
                    task = HTTP.PostAsync(model.Uri, new StringContent(model.Body));
                    break;

                default:
                    var req = new HttpRequestMessage
                    {
                        Method     = GetMethod(model.Method),
                        RequestUri = new Uri(model.Uri),
                        Content    = new StringContent(model.Body),
                    };
                    task = HTTP.SendAsync(req);
                    break;
                }
                model.Set(task);
            }
            model.SkipCall = false;
            return(View(model));
        }
示例#6
0
        public IActionResult Index(SqlServerModel model)
        {
            var cu = ControllerUtils.From(this);

            cu.PersistInput("ConnectionString", model, SqlServerModel.Default.ConnectionString);
            cu.PersistInput("Sql", model, SqlServerModel.Default.Sql);

            if (!model.SkipSql)
            {
                try
                {
                    var sql = model.Sql.Trim();
                    if (Regex.IsMatch(sql.ToUpper(), "DROP\\s+TABLE\\s+"))
                    {
                        throw new NotSupportedException("Cannot SQL contains drop table command.");
                    }
                    if (Regex.IsMatch(sql.ToUpper(), "DELETE\\s+FROM\\s+"))
                    {
                        throw new NotSupportedException("Cannot SQL contains delete command.");
                    }
                    if (Regex.IsMatch(sql.ToUpper(), "UPDATE\\s+.+SET\\s.+"))
                    {
                        throw new NotSupportedException("Cannot SQL contains update command.");
                    }
                    if (Regex.IsMatch(sql.ToUpper(), "INSERT\\s+INTO\\s+"))
                    {
                        throw new NotSupportedException("Cannot SQL contains insert command.");
                    }

                    if (sql.StartsWith("SELECT ", StringComparison.CurrentCultureIgnoreCase))
                    {
                        // remove input TOP(*) word
                        var mstr = Regex.Match(sql, "\\s+[Tt][Oo][Pp]\\s*\\(\\d+\\)\\s+");
                        if (mstr.Success)
                        {
                            sql = sql.Replace(mstr.Value, " ");
                        }
                        sql = "SELECT TOP(20) " + StrUtil.MidSkip(sql, "SELECT\\s+");
                    }
                    model.LatestSql = sql;

                    using var con = new SqlConnection(model.ConnectionString);
                    con.Open();
                    using var sqlcommand = new SqlCommand(sql, con);
                    using var row        = sqlcommand.ExecuteReader();

                    var cs = row.GetColumnSchema();
                    model.Table = new List <List <string> >();
                    model.Table.Add(cs.Select(a => a.ColumnName).ToList());
                    model.Table.Add(cs.Select(a => $"<{a.DataTypeName}>").ToList());

                    while (row.Read())
                    {
                        var r = new List <string>();
                        for (var col = 0; col < row.FieldCount; col++)
                        {
                            r.Add(Tono.DbUtil.ToString(row[col], "(null)"));
                        }
                        model.Table.Add(r);
                        if (model.Table.Count > 22)
                        {
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    model.ErrorMessage = ex.Message;
                }
            }

            if (model.Table == null)
            {
                model.Table = new List <List <string> >();
            }
            while (model.Table.Count < 2)
            {
                model.Table.Add(new List <string>());
            }

            model.SkipSql = false;
            return(View(model));
        }
示例#7
0
        public IActionResult Index(EventHubModel model)
        {
            // Apply input history from cookie
            var cu = ControllerUtils.From(this);

            cu.PersistInput("ConnectionString", model, EventHubModel.Default.ConnectionString);
            cu.PersistInput("EventHubName", model, EventHubModel.Default.EventHubName);
            cu.PersistInput("ConsumerGroupName", model, EventHubModel.Default.ConsumerGroupName);
            cu.PersistInput("StorageConnectionString", model, EventHubModel.Default.StorageConnectionString);
            cu.PersistInput("StorageContainerName", model, EventHubModel.Default.StorageContainerName);

            var receiveTimeout = model.ListeningTime - TimeSpan.FromMilliseconds(200 * 2);

            if (model.ConsumerGroupName == EventHubModel.Default.ConsumerGroupName)
            {
                model.ConsumerGroupName = PartitionReceiver.DefaultConsumerGroupName;
            }

            try
            {
                var cs = new EventHubsConnectionStringBuilder(model.ConnectionString)
                {
                    EntityPath = model.EventHubName,
                };
                var ec = EventHubClient.CreateFromConnectionString(cs.ToString()); // WARNING : Max # of TCP socket because of each instance created by Http Request


                if (model.ReceiveRequested)
                {
                    var eph = new EventProcessorHost(
                        model.EventHubName,
                        model.ConsumerGroupName,
                        model.ConnectionString,
                        model.StorageConnectionString,
                        model.StorageContainerName);

                    eph.RegisterEventProcessorFactoryAsync(this, new EventProcessorOptions
                    {
                        InvokeProcessorAfterReceiveTimeout = true,
                        ReceiveTimeout = receiveTimeout,    // It's necessary to set long time here. (1.6sec was not enough at 2020.7.30)
                    }
                                                           ).ConfigureAwait(false).GetAwaiter().GetResult();

                    Task.Delay(receiveTimeout + TimeSpan.FromMilliseconds(200)).ConfigureAwait(false).GetAwaiter().GetResult();    // wait message received

                    eph.UnregisterEventProcessorAsync().ConfigureAwait(false).GetAwaiter().GetResult();

                    model.ActionMessage = "";
                    var err = ErrorMessages.ToString();
                    if (!string.IsNullOrEmpty(err))
                    {
                        model.ActionMessage = $"{err}{Environment.NewLine}{Environment.NewLine}";
                    }
                    model.ActionMessage += $"Received {ReceivedMessages.Count} messages.{Environment.NewLine}{Environment.NewLine}{string.Join(Environment.NewLine, ReceivedMessages.Where(a => a != null))}";
                    if (ReceivedMessages.Count > 5)
                    {
                        model.ActionMessage += $"{Environment.NewLine}...more";
                    }

                    Task.Delay(TimeSpan.FromMilliseconds(200)).ConfigureAwait(false).GetAwaiter().GetResult();    // wait message received
                }
                else
                if (!model.SkipSend)
                {
                    ec.SendAsync(new EventData(Encoding.UTF8.GetBytes(model.Message))).ConfigureAwait(false).GetAwaiter().GetResult();
                    model.ActionMessage = $"OK : Sent '{model.Message}' to {model.EventHubName}";
                }

                // Collect Partition Information
                var isNetError  = false;
                var runtimeInfo = ec.GetRuntimeInformationAsync().ConfigureAwait(false).GetAwaiter().GetResult();
                foreach (var pid in runtimeInfo.PartitionIds)
                {
                    if (isNetError == false)
                    {
                        try
                        {
                            var pi = ec.GetPartitionRuntimeInformationAsync(pid).ConfigureAwait(false).GetAwaiter().GetResult();
                            if (model.PartitionInfo == null)
                            {
                                model.PartitionInfo = new Dictionary <string, EventHubPartitionRuntimeInformation>();
                            }
                            model.PartitionInfo[pid] = pi;
                        }
                        catch (SocketException ex)
                        {
                            throw ex;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                for (var exx = ex; exx != null; exx = ex.InnerException)
                {
                    model.ActionMessage = $"ERROR : {exx.Message}{Environment.NewLine}";
                }
            }

            model.ReceiveRequested = false;
            model.SkipSend         = false;

            return(View(model));
        }