Exemple #1
0
        /// <summary>
        /// Saves a single <see cref="IShipment"/> object
        /// </summary>
        /// <param name="shipment">The <see cref="IShipment"/> to save</param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        public void Save(IShipment shipment, bool raiseEvents = true)
        {
            if (!((Shipment)shipment).HasIdentity && shipment.ShipmentNumber <= 0)
            {
                // We have to generate a new 'unique' invoice number off the configurable value
                ((Shipment)shipment).ShipmentNumber = _storeSettingService.GetNextShipmentNumber();
            }

            var includesStatusChange = ((Shipment)shipment).IsPropertyDirty("ShipmentStatus") &&
                                       shipment.HasIdentity;

            if (raiseEvents)
            {
                if (Saving.IsRaisedEventCancelled(new SaveEventArgs <IShipment>(shipment), this))
                {
                    ((Shipment)shipment).WasCancelled = true;
                    return;
                }

                if (includesStatusChange)
                {
                    StatusChanging.RaiseEvent(new StatusChangeEventArgs <IShipment>(shipment), this);
                }
            }


            if (raiseEvents)
            {
                if (Saving.IsRaisedEventCancelled(new SaveEventArgs <IShipment>(shipment), this))
                {
                    ((Shipment)shipment).WasCancelled = true;
                    return;
                }
            }

            using (new WriteLock(Locker))
            {
                var uow = _uowProvider.GetUnitOfWork();
                using (var repository = _repositoryFactory.CreateShipmentRepository(uow))
                {
                    repository.AddOrUpdate(shipment);
                    uow.Commit();
                }
            }

            if (!raiseEvents)
            {
                return;
            }

            Saved.RaiseEvent(new SaveEventArgs <IShipment>(shipment), this);
            if (includesStatusChange)
            {
                StatusChanged.RaiseEvent(new StatusChangeEventArgs <IShipment>(shipment), this);
            }
        }
        /// <summary>
        /// Saves a collection of <see cref="IOrder"/>
        /// </summary>
        /// <param name="orders">The collection of <see cref="IOrder"/></param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        public void Save(IEnumerable <IOrder> orders, bool raiseEvents = true)
        {
            // Generate Order Number for new Orders in the collection
            var ordersArray   = orders as IOrder[] ?? orders.ToArray();
            var newOrderCount = ordersArray.Count(x => x.OrderNumber <= 0 && !((Order)x).HasIdentity);

            if (newOrderCount > 0)
            {
                var lastOrderNumber =
                    _storeSettingService.GetNextOrderNumber(newOrderCount);
                foreach (var newOrder in ordersArray.Where(x => x.OrderNumber <= 0 && !((Order)x).HasIdentity))
                {
                    ((Order)newOrder).OrderNumber = lastOrderNumber;
                    lastOrderNumber = lastOrderNumber - 1;
                }
            }

            var existingOrdersWithStatusChanges =
                ordersArray.Where(
                    x => ((Order)x).HasIdentity == false && ((Order)x).IsPropertyDirty("OrderStatus"))
                .ToArray();

            if (raiseEvents)
            {
                Saving.RaiseEvent(new SaveEventArgs <IOrder>(ordersArray), this);
                if (existingOrdersWithStatusChanges.Any())
                {
                    StatusChanging.RaiseEvent(new StatusChangeEventArgs <IOrder>(existingOrdersWithStatusChanges),
                                              this);
                }
            }

            using (new WriteLock(Locker))
            {
                var uow = UowProvider.GetUnitOfWork();
                using (var repository = RepositoryFactory.CreateOrderRepository(uow))
                {
                    foreach (var order in ordersArray)
                    {
                        repository.AddOrUpdate(order);
                    }
                    uow.Commit();
                }
            }

            if (raiseEvents)
            {
                Saved.RaiseEvent(new SaveEventArgs <IOrder>(ordersArray), this);
                if (existingOrdersWithStatusChanges.Any())
                {
                    StatusChanged.RaiseEvent(new StatusChangeEventArgs <IOrder>(existingOrdersWithStatusChanges),
                                             this);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Saves a collection of <see cref="IInvoice"/>
        /// </summary>
        /// <param name="invoices">The collection of <see cref="IInvoice"/></param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        public void Save(IEnumerable <IInvoice> invoices, bool raiseEvents = true)
        {
            // Generate Invoice Number for new Invoices in the collection
            var invoicesArray   = invoices as IInvoice[] ?? invoices.ToArray();
            var newInvoiceCount = invoicesArray.Count(x => x.InvoiceNumber <= 0 && !((Invoice)x).HasIdentity);

            if (newInvoiceCount > 0)
            {
                var lastInvoiceNumber =
                    _storeSettingService.GetNextInvoiceNumber(newInvoiceCount);
                foreach (var newInvoice in invoicesArray.Where(x => x.InvoiceNumber <= 0 && !((Invoice)x).HasIdentity))
                {
                    ((Invoice)newInvoice).InvoiceNumber = lastInvoiceNumber;
                    lastInvoiceNumber = lastInvoiceNumber - 1;
                }
            }

            var existingInvoicesWithStatusChanges =
                invoicesArray.Where(
                    x => ((Invoice)x).HasIdentity == false && ((Invoice)x).IsPropertyDirty("InvoiceStatusKey"))
                .ToArray();

            if (raiseEvents)
            {
                Saving.RaiseEvent(new SaveEventArgs <IInvoice>(invoicesArray), this);
                if (existingInvoicesWithStatusChanges.Any())
                {
                    StatusChanging.RaiseEvent(
                        new StatusChangeEventArgs <IInvoice>(existingInvoicesWithStatusChanges),
                        this);
                }
            }

            using (new WriteLock(Locker))
            {
                var uow = _uowProvider.GetUnitOfWork();
                using (var repository = _repositoryFactory.CreateInvoiceRepository(uow))
                {
                    foreach (var invoice in invoicesArray)
                    {
                        repository.AddOrUpdate(invoice);
                    }
                    uow.Commit();
                }
            }

            if (raiseEvents)
            {
                Saved.RaiseEvent(new SaveEventArgs <IInvoice>(invoicesArray), this);
                if (existingInvoicesWithStatusChanges.Any())
                {
                    StatusChanged.RaiseEvent(new StatusChangeEventArgs <IInvoice>(existingInvoicesWithStatusChanges), this);
                }
            }
        }
        /// <summary>
        /// Saves a single <see cref="IOrder"/>
        /// </summary>
        /// <param name="order">The <see cref="IOrder"/> to save</param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        public void Save(IOrder order, bool raiseEvents = true)
        {
            if (!((Order)order).HasIdentity && order.OrderNumber <= 0)
            {
                // We have to generate a new 'unique' order number off the configurable value
                ((Order)order).OrderNumber = _storeSettingService.GetNextOrderNumber();
            }

            var includesStatusChange = ((Order)order).IsPropertyDirty("OrderStatus") &&
                                       ((Order)order).HasIdentity;

            if (raiseEvents)
            {
                if (Saving.IsRaisedEventCancelled(new SaveEventArgs <IOrder>(order), this))
                {
                    ((Order)order).WasCancelled = true;
                    return;
                }

                if (includesStatusChange)
                {
                    StatusChanging.RaiseEvent(new StatusChangeEventArgs <IOrder>(order), this);
                }
            }

            using (new WriteLock(Locker))
            {
                var uow = UowProvider.GetUnitOfWork();
                using (var repository = RepositoryFactory.CreateOrderRepository(uow))
                {
                    repository.AddOrUpdate(order);
                    uow.Commit();
                }
            }

            if (!raiseEvents)
            {
                return;
            }

            Saved.RaiseEvent(new SaveEventArgs <IOrder>(order), this);
            if (includesStatusChange)
            {
                StatusChanged.RaiseEvent(new StatusChangeEventArgs <IOrder>(order), this);
            }
        }
Exemple #5
0
        /// <summary>
        /// Saves a collection of <see cref="IShipment"/> objects
        /// </summary>
        /// <param name="shipmentList">Collection of <see cref="IShipment"/> to save</param>
        /// <param name="raiseEvents">Optional boolean indicating whether or not to raise events</param>
        public void Save(IEnumerable <IShipment> shipmentList, bool raiseEvents = true)
        {
            var shipmentsArray = shipmentList as IShipment[] ?? shipmentList.ToArray();

            var newShipmentCount = shipmentsArray.Count(x => x.ShipmentNumber <= 0 && !((Shipment)x).HasIdentity);


            if (newShipmentCount > 0)
            {
                var lastShipmentumber =
                    _storeSettingService.GetNextShipmentNumber(newShipmentCount);
                foreach (var newShipment in shipmentsArray.Where(x => x.ShipmentNumber <= 0 && !((Shipment)x).HasIdentity))
                {
                    ((Shipment)newShipment).ShipmentNumber = lastShipmentumber;
                    lastShipmentumber = lastShipmentumber - 1;
                }
            }

            var existingShipmentsWithStatusChanges =
                shipmentsArray.Where(
                    x => ((Shipment)x).HasIdentity && ((Shipment)x).IsPropertyDirty("ShipmentStatus"))
                .ToArray();

            if (raiseEvents)
            {
                Saving.RaiseEvent(new SaveEventArgs <IShipment>(shipmentsArray), this);
                if (existingShipmentsWithStatusChanges.Any())
                {
                    StatusChanging.RaiseEvent(
                        new StatusChangeEventArgs <IShipment>(existingShipmentsWithStatusChanges),
                        this);
                }
            }

            if (raiseEvents)
            {
                Saving.RaiseEvent(new SaveEventArgs <IShipment>(shipmentsArray), this);
            }

            using (new WriteLock(Locker))
            {
                var uow = _uowProvider.GetUnitOfWork();
                using (var repository = _repositoryFactory.CreateShipmentRepository(uow))
                {
                    foreach (var shipment in shipmentsArray)
                    {
                        repository.AddOrUpdate(shipment);
                    }
                    uow.Commit();
                }
            }

            if (!raiseEvents)
            {
                return;
            }
            Saved.RaiseEvent(new SaveEventArgs <IShipment>(shipmentsArray), this);
            if (existingShipmentsWithStatusChanges.Any())
            {
                StatusChanged.RaiseEvent(new StatusChangeEventArgs <IShipment>(existingShipmentsWithStatusChanges), this);
            }
        }
        public void Start()
        {
            try
            {
                Logger.WriteDebug(MethodBase.GetCurrentMethod(), "Starting");
                var _stopWatch = new Stopwatch();
                _stopWatch.Start();

                IsRunning = false;

                //Timeout between messages
                _delayTimer = new Timer
                {
                    Interval  = 2500,
                    AutoReset = false
                };
                _delayTimer.Elapsed += delayTimer_Elapsed;
                _delayTimer.Start();

                if (Settings.Default.Decoder1_Mode != DecoderMode.OFF)
                {
                    //Create DecoderInstance
                    switch (Settings.Default.Decoder1_Mode)
                    {
                    case DecoderMode.FMS32:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[1]FMS32");
                        _decoderClient1 = new DecoderTcpClient("DecoderService[1]",
                                                               Settings.Default.Decoder1_Server, Settings.Default.Decoder1_Port, ETX_FMS32,
                                                               Encoding.ASCII);
                        break;

                    case DecoderMode.MONITORD:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[1]MONITORD");
                        _decoderClient1 = new DecoderTcpClient("DecoderService[1]",
                                                               Settings.Default.Decoder1_Server, Settings.Default.Decoder1_Port, ETX_MONITORD,
                                                               Encoding.ASCII);
                        break;

                    case DecoderMode.SANDAN:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[1]SANDAN");
                        _decoderClient1 = new DecoderTcpClient("DecoderService[1]",
                                                               Settings.Default.Decoder1_Server, Settings.Default.Decoder1_Port, ETX_SANDAN,
                                                               Encoding.ASCII);
                        _decoderClient1.EnableLoginString("GET NidanConnect!34");
                        break;

                    case DecoderMode.OPERATOR2:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[1]OPERATOR2");
                        _decoderClient1 = new DecoderTcpClient("DecoderService[1]",
                                                               Settings.Default.Decoder1_Server, Settings.Default.Decoder1_Port, ETX_OPERATOR2,
                                                               Encoding.ASCII);
                        _decoderClient1.EnableLoginString("OPERATOR2RIS");
                        break;

                    case DecoderMode.SYSLOG:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[1]SYSLOG");
                        _decoderClient1 = new DecoderTcpServer("DecoderService[1]",
                                                               Settings.Default.Decoder1_Server, Settings.Default.Decoder1_Port, ETX_SYSLOG);
                        break;

                    case DecoderMode.LARDIS:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[1]LARDIS");
                        _decoderClient1 = new DecoderTcpClient("DecoderService[1]",
                                                               Settings.Default.Decoder1_Server, Settings.Default.Decoder1_Port, ETX_LARDIS,
                                                               Encoding.GetEncoding(1252));
                        break;

                    case DecoderMode.TETRACONTROL:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[1]TETRACONTROL");
                        _decoderClient1 =
                            new DecoderHttpServer("DecoderService[1]", Settings.Default.Decoder1_Port);
                        break;
                    }

                    //Register for DecoderClient1 events
                    _decoderClient1.Connecting += (sender, e) =>
                    {
                        var _statusEventArgs = new StatusEventArgs
                        {
                            Number = 1,
                            Status = "Verbinde..."
                        };
                        StatusChanged.RaiseEvent(this, _statusEventArgs);
                    };
                    _decoderClient1.Connected += (sender, e) =>
                    {
                        var _statusEventArgs = new StatusEventArgs
                        {
                            Number = 1,
                            Status = "Verbunden"
                        };
                        StatusChanged.RaiseEvent(this, _statusEventArgs);
                    };
                    _decoderClient1.Disconnected += (sender, e) =>
                    {
                        var _statusEventArgs = new StatusEventArgs
                        {
                            Number = 1,
                            Status = "Getrennt"
                        };
                        StatusChanged.RaiseEvent(this, _statusEventArgs);
                    };
                    _decoderClient1.MessageReceived += (sender, e) =>
                    {
                        switch (Settings.Default.Decoder1_Mode)
                        {
                        case DecoderMode.FMS32:
                            decoderClient_MessageReceived_FMS32(e);
                            break;

                        case DecoderMode.MONITORD:
                            decoderClient_MessageReceived_MONITORD(e);
                            break;

                        case DecoderMode.SANDAN:
                            decoderClient_MessageReceived_SANDAN(e);
                            break;

                        case DecoderMode.OPERATOR2:
                            decoderClient_MessageReceived_OPERATOR2(e);
                            break;

                        case DecoderMode.SYSLOG:
                            decoderClient_MessageReceived_SYSLOG(e);
                            break;

                        case DecoderMode.LARDIS:
                            decoderClient_MessageReceived_LARDIS(e);
                            break;

                        case DecoderMode.TETRACONTROL:
                            decoderClient_MessageReceived_TETRACONTROL(e);
                            break;
                        }
                    };

                    //Start decoder
                    _decoderClient1.Start();

                    //Send message after connecting
                    switch (Settings.Default.Decoder1_Mode)
                    {
                    case DecoderMode.SANDAN:
                        _decoderClient1.EnablePingTimer("Ping", 15000);
                        break;
                    }
                }
                else
                {
                    var _statusEventArgs = new StatusEventArgs
                    {
                        Number = 1,
                        Status = "Deaktiviert"
                    };
                    StatusChanged.RaiseEvent(this, _statusEventArgs);
                }


                if (Settings.Default.Decoder2_Mode != DecoderMode.OFF)
                {
                    //Create DecoderClient
                    switch (Settings.Default.Decoder2_Mode)
                    {
                    case DecoderMode.FMS32:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[2]FMS32");
                        _decoderClient2 = new DecoderTcpClient("DecoderService[2]",
                                                               Settings.Default.Decoder2_Server, Settings.Default.Decoder2_Port, ETX_FMS32,
                                                               Encoding.ASCII);
                        break;

                    case DecoderMode.MONITORD:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[2]MONITORD");
                        _decoderClient2 = new DecoderTcpClient("DecoderService[2]",
                                                               Settings.Default.Decoder2_Server, Settings.Default.Decoder2_Port, ETX_MONITORD,
                                                               Encoding.ASCII);
                        break;

                    case DecoderMode.SANDAN:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[2]SANDAN");
                        _decoderClient2 = new DecoderTcpClient("DecoderService[2]",
                                                               Settings.Default.Decoder2_Server, Settings.Default.Decoder2_Port, ETX_SANDAN,
                                                               Encoding.ASCII);
                        _decoderClient2.EnableLoginString("GET NidanConnect!34");
                        break;

                    case DecoderMode.OPERATOR2:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[2]OPERATOR2");
                        _decoderClient2 = new DecoderTcpClient("DecoderService[2]",
                                                               Settings.Default.Decoder2_Server, Settings.Default.Decoder2_Port, ETX_OPERATOR2,
                                                               Encoding.ASCII);
                        _decoderClient2.EnableLoginString("OPERATOR2RIS");
                        break;

                    case DecoderMode.SYSLOG:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[2]SYSLOG");
                        _decoderClient2 = new DecoderTcpServer("DecoderService[2]",
                                                               Settings.Default.Decoder2_Server, Settings.Default.Decoder2_Port, ETX_SYSLOG);
                        break;

                    case DecoderMode.LARDIS:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[2]LARDIS");
                        _decoderClient2 = new DecoderTcpClient("DecoderService[2]",
                                                               Settings.Default.Decoder2_Server, Settings.Default.Decoder2_Port, ETX_LARDIS,
                                                               Encoding.GetEncoding(1252));
                        break;

                    case DecoderMode.TETRACONTROL:
                        Logger.WriteDebug(MethodBase.GetCurrentMethod(), "[2]TETRACONTROL");
                        _decoderClient2 =
                            new DecoderHttpServer("DecoderService[2]", Settings.Default.Decoder2_Port);
                        break;
                    }

                    //Register for DecoderClient2 events
                    _decoderClient2.Connecting += (sender, e) =>
                    {
                        var _statusEventArgs = new StatusEventArgs
                        {
                            Number = 2,
                            Status = "Verbinde..."
                        };
                        StatusChanged.RaiseEvent(this, _statusEventArgs);
                    };
                    _decoderClient2.Connected += (sender, e) =>
                    {
                        var _statusEventArgs = new StatusEventArgs
                        {
                            Number = 2,
                            Status = "Verbunden"
                        };
                        StatusChanged.RaiseEvent(this, _statusEventArgs);
                    };
                    _decoderClient2.Disconnected += (sender, e) =>
                    {
                        var _statusEventArgs = new StatusEventArgs
                        {
                            Number = 2,
                            Status = "Getrennt"
                        };
                        StatusChanged.RaiseEvent(this, _statusEventArgs);
                    };
                    _decoderClient2.MessageReceived += (sender, e) =>
                    {
                        switch (Settings.Default.Decoder2_Mode)
                        {
                        case DecoderMode.FMS32:
                            decoderClient_MessageReceived_FMS32(e);
                            break;

                        case DecoderMode.MONITORD:
                            decoderClient_MessageReceived_MONITORD(e);
                            break;

                        case DecoderMode.SANDAN:
                            decoderClient_MessageReceived_SANDAN(e);
                            break;

                        case DecoderMode.OPERATOR2:
                            decoderClient_MessageReceived_OPERATOR2(e);
                            break;

                        case DecoderMode.SYSLOG:
                            decoderClient_MessageReceived_SYSLOG(e);
                            break;

                        case DecoderMode.LARDIS:
                            decoderClient_MessageReceived_LARDIS(e);
                            break;

                        case DecoderMode.TETRACONTROL:
                            decoderClient_MessageReceived_TETRACONTROL(e);
                            break;
                        }
                    };

                    //Start Decoder
                    _decoderClient2.Start();

                    //Send message after connecting
                    switch (Settings.Default.Decoder2_Mode)
                    {
                    case DecoderMode.SANDAN:
                        _decoderClient2.EnablePingTimer("Ping", 15000);
                        break;
                    }
                }
                else
                {
                    var _statusEventArgs = new StatusEventArgs
                    {
                        Number = 2,
                        Status = "Deaktiviert"
                    };
                    StatusChanged.RaiseEvent(this, _statusEventArgs);
                }

                IsRunning = true;

                _stopWatch.Stop();
                Logger.WriteDebug(MethodBase.GetCurrentMethod(), $"Started -> {_stopWatch.Elapsed}");
            }
            catch (Exception ex)
            {
                ExceptionOccured.RaiseEvent(this, new ExceptionEventArgs
                {
                    Methode = MethodBase.GetCurrentMethod(),
                    Error   = ex
                });
            }
        }