Beispiel #1
0
        public Models.Response CreateDigitalTwinModel(Models.DigitalTwinModelRequest value, long organization)
        {
            Models.Response response = new Models.Response();

            try
            {
                //SQL Statement
                var sqlString = "INSERT INTO digital_twin_models (id, name, description, organization, version) " +
                                "VALUES (@id, @name, @description, @organization, @version)";

                //Create UNIX Timestamp
                var utcDateTime  = DateTime.UtcNow;
                var dto          = new DateTimeOffset(utcDateTime);
                var unixDateTime = dto.ToUnixTimeMilliseconds();

                using (var connection = new NpgsqlConnection(connectionString))
                {
                    connection.Open();

                    using (var command = new NpgsqlCommand(sqlString, connection))
                    {
                        command.Parameters.AddWithValue("@id", NpgsqlTypes.NpgsqlDbType.Bigint, unixDateTime);
                        command.Parameters.AddWithValue("@name", NpgsqlTypes.NpgsqlDbType.Varchar, value.Name);
                        command.Parameters.AddWithValue("@description", NpgsqlTypes.NpgsqlDbType.Varchar, value.Description);
                        command.Parameters.AddWithValue("@organization", NpgsqlTypes.NpgsqlDbType.Bigint, organization);
                        command.Parameters.AddWithValue("@version", NpgsqlTypes.NpgsqlDbType.Bigint, value.Version);
                        command.Prepare();
                        command.ExecuteNonQuery();

                        //Log Success
                        response.Status  = "success";
                        response.Message = "digital twin model created";
                        response.Id      = unixDateTime;
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log Exception
                //_logger.LogError(ex, "digital twin model creation failed");
                response.Status  = "error";
                response.Message = "digital twin model creation failed";
                response.Id      = 0;
                return(response);
            }
        }
Beispiel #2
0
        public Models.Response CreateDigitalTwinModel(Models.DigitalTwinModelRequest value, Guid organization)
        {
            Models.Response response = new Models.Response();

            try
            {
                //SQL Statement
                var sqlString = "INSERT INTO digital_twin_models (id, name, description, version, organization, created, created_by) " +
                                "VALUES (@id, @name, @description, @version, @organization, @created, @created_by)";

                //Create a new Model Id UUID
                Guid modelIdGuid = Guid.NewGuid();

                using (var connection = new NpgsqlConnection(connectionString))
                {
                    connection.Open();

                    using (var command = new NpgsqlCommand(sqlString, connection))
                    {
                        command.Parameters.AddWithValue("@id", NpgsqlTypes.NpgsqlDbType.Uuid, modelIdGuid);
                        command.Parameters.AddWithValue("@name", NpgsqlTypes.NpgsqlDbType.Text, value.Name);
                        command.Parameters.AddWithValue("@description", NpgsqlTypes.NpgsqlDbType.Text, value.Description);
                        command.Parameters.AddWithValue("@version", NpgsqlTypes.NpgsqlDbType.Bigint, value.Version);
                        command.Parameters.AddWithValue("@organization", NpgsqlTypes.NpgsqlDbType.Uuid, organization);
                        command.Parameters.AddWithValue("@created", NpgsqlTypes.NpgsqlDbType.TimestampTz, DateTime.UtcNow);
                        command.Parameters.AddWithValue("@created_by", NpgsqlTypes.NpgsqlDbType.Uuid, value.CreatedBy);
                        command.Prepare();
                        command.ExecuteNonQuery();

                        //Log Success
                        response.Status  = "success";
                        response.Message = "digital twin model created";
                        response.Id      = modelIdGuid;
                        return(response);
                    }
                }
            }
            catch (Exception ex)
            {
                //Log Exception
                _logger.LogError(ex, "digital twin model creation failed");
                response.Status  = "error";
                response.Message = "digital twin model creation failed";
                response.Id      = errorGuid;
                return(response);
            }
        }
        private async void btnCreate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text != "" && txtDescription.Text != "")
                {
                    //Capture Values
                    Models.DigitalTwinModelRequest digitalTwinModelRequest = new Models.DigitalTwinModelRequest();
                    digitalTwinModelRequest.Name        = txtName.Text.Trim();
                    digitalTwinModelRequest.Description = txtDescription.Text.Trim();
                    digitalTwinModelRequest.Version     = Convert.ToInt64(numericUpDownVersion.Value);
                    digitalTwinModelRequest.CreatedBy   = Program.identity;


                    //Create JSON Document
                    var jsonString = JsonConvert.SerializeObject(digitalTwinModelRequest);

                    //Clear Values
                    txtName.Clear();
                    txtDescription.Clear();
                    numericUpDownVersion.Value = 1;

                    string credentials = Program.identity.ToString() + "." + Program.securityToken.ToString();

                    //Send Data
                    ClientSDK clientSDK  = new ClientSDK();
                    string    uriString  = Program.serverURL + "/DigitalTwinModel";
                    var       jsonResult = await clientSDK.Create(uriString, jsonString, credentials);

                    var objectResult = JsonConvert.DeserializeObject <Models.Response>(jsonResult);

                    //Add to Group List
                    ListViewItem listViewItem = new ListViewItem(objectResult.Id.ToString());
                    listViewItem.SubItems.Add(digitalTwinModelRequest.Name);
                    listViewItem.SubItems.Add(digitalTwinModelRequest.Description);
                    listViewItem.SubItems.Add(digitalTwinModelRequest.Version.ToString());
                    listViewModels.Items.Add(listViewItem);
                }
                else
                {
                    MessageBox.Show("All fields must be properly filled-in.", "Information");
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "404")
                {
                    //No data returned
                }
                else if (ex.Message == "401")
                {
                    MessageBox.Show("The email address or password you entered is either incorrect or this user doesn't exist in the system", "Error");
                }
                else if (ex.Message == "An error occurred while sending the request.")
                {
                    MessageBox.Show("The Moab Platform is unreachable.", "Network Error");
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }
            }
            finally
            {
            }
        }