예제 #1
0
        public static void SaveSpisInstanceSettings(string dbName, SpisInstanceSettings settings)
        {
            try
            {
                using (var stmDbContext = new StmDbContext())
                {
                    stmDbContext.init(dbName);

                    var current = stmDbContext.SpisInstanceSettings.FirstOrDefault();
                    if (current == null)
                    {
                        stmDbContext.SpisInstanceSettings.Add(settings);
                    }
                    else
                    {
                        current.ClientCertificate = settings.ClientCertificate;
                        current.Password          = settings.Password;
                        current.ServiceId         = settings.ServiceId;
                        current.ServiceName       = settings.ServiceName;
                        current.StmModuleUrl      = settings.StmModuleUrl;
                        current.IMO                   = settings.IMO;
                        current.MMSI                  = settings.MMSI;
                        current.ApiKey                = settings.ApiKey;
                        current.ApplicationId         = settings.ApplicationId;
                        current.UseHMACAuthentication = settings.UseHMACAuthentication;
                    }

                    stmDbContext.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void SetupVisInstanceSettings()
        {
            var instanceDbName       = "stm";
            var instanceName         = "stm";
            var instanceId           = "urn:mrn:stm:service:instance:sma:stm";
            var stmModuleUrl         = "";
            var password             = "******";
            var encruptionPassphrase = "A_vErry secret paSsw0rd#that is not easy to Re2MeMbeR!";
            var encryptedPassword    = Encryption.EncryptString(password, encruptionPassphrase);

            var dlg = new System.Windows.Forms.OpenFileDialog();

            dlg.Filter = "Certifikat | *.p12";

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _dbContext.init(instanceDbName);
                byte[] certBytes = File.ReadAllBytes(dlg.FileName);

                var settings = _dbContext.VisInstanceSettings.FirstOrDefault();
                if (settings == null)
                {
                    settings                   = new VisInstanceSettings();
                    settings.ServiceName       = instanceName;
                    settings.ServiceId         = instanceId;
                    settings.StmModuleUrl      = stmModuleUrl;
                    settings.ClientCertificate = certBytes;
                    settings.Password          = encryptedPassword;

                    _dbContext.VisInstanceSettings.Add(settings);
                }
                else
                {
                    settings.ServiceName       = instanceName;
                    settings.ServiceId         = instanceId;
                    settings.StmModuleUrl      = stmModuleUrl;
                    settings.ClientCertificate = certBytes;
                    settings.Password          = encryptedPassword;
                }

                _dbContext.SaveChanges();
            }
        }
예제 #3
0
        private void SubmitMessageToVIS(byte[] msg)
        {
            PublishedRtzMessageService service = new PublishedRtzMessageService(_dbContext, _logContext);

            var messageType = _dbContext.MessageType.Where(m => m.Name == "RTZ").FirstOrDefault();
            //var parsedMsg = Serialization.Deserialize<Route>(Serialization.ByteArrayToString(msg));

            var         parser      = RtzParserFactory.Create(Serialization.ByteArrayToString(msg));
            string      routeStatus = parser.RouteStatus;
            RouteStatus status;

            if (string.IsNullOrEmpty(routeStatus))
            {
                status = RouteStatus.Unknown;
            }
            else
            {
                status = (RouteStatus)Enum.Parse(typeof(RouteStatus), routeStatus);
            }
            var entityToInsert = new PublishedRtzMessage
            {
                Message               = msg,
                MessageID             = "urn:mrn:stm:voyage:id:vis1:0001",
                MessageLastUpdateTime = DateTime.UtcNow,
                MessageStatus         = status,
                MessageType           = messageType,
                MessageValidFrom      = DateTime.UtcNow.AddDays(-3),
                MessageValidTo        = DateTime.UtcNow.AddDays(3),
                PublishTime           = DateTime.UtcNow.AddHours(-3)
            };

            try
            {
                service.Insert(entityToInsert);
                _dbContext.SaveChanges();
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="notification"></param>
        /// <returns></returns>
        public bool Notify(Notification notification)
        {
            notification.ReceivedAt            = DateTime.UtcNow;
            notification.NotificationCreatedAt = DateTime.UtcNow;

            var dbNotification = new DataAccess.Entities.Notification
            {
                Body                  = notification.Body ?? "Empty",
                FetchedByShip         = false,
                FetchTime             = null,
                FromOrgId             = notification.FromOrgId,
                FromOrgName           = notification.FromOrgName,
                FromServiceId         = notification.FromServiceId,
                NotificationCreatedAt = notification.NotificationCreatedAt,
                NotificationType      = (DataAccess.Entities.NotificationType)notification.NotificationType,
                NotificationSource    = (DataAccess.Entities.NotificationSource)notification.NotificationSource,
                ReceivedAt            = notification.ReceivedAt,
                Subject               = notification.Subject
            };

            var nrOfNotifications = _notificationService.Get(XmlParsers => XmlParsers.FetchedByShip == false).Count();

            // Write notification to database
            _notificationService.Insert(dbNotification);
            _context.SaveChanges();

            try
            {
                // Try to push notification to client
                if (!string.IsNullOrEmpty(InstanceContext.StmModuleUrl))
                {
                    log.Debug("Trying to send notification to STM module on url: " + InstanceContext.StmModuleUrl);

                    notification.MessageWaiting = nrOfNotifications;

                    WebHeaderCollection h = new WebHeaderCollection();
                    h.Add("content-type", "application/json; charset=utf-8");

                    var response = WebRequestHelper.Post(InstanceContext.StmModuleUrl, notification.ToJson(), h);
                    if (response.HttpStatusCode == HttpStatusCode.OK)
                    {
                        dbNotification.FetchedByShip = true;
                        dbNotification.FetchTime     = DateTime.UtcNow;
                        _notificationService.Update(dbNotification);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    log.Debug("No push to STM Module becouse no url is configured for the instance");
                    return(false);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                return(false);
            }

            return(true);
        }