Ejemplo n.º 1
0
 /// <summary>
 /// Called in case an exception has been thrown
 /// </summary>
 private void CallbackExceptionThrown(Exception e, object caller)
 {
     if (OnExceptionThrown != null)
     {
         OnExceptionThrown.Invoke(e, caller);
     }
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Exception event raised, and exception thrown
 /// </summary>
 /// <param name="e">The exception thrown</param>
 protected void RaiseExceptionEvent(Exception e)
 {
     if (OnExceptionThrown != null)
     {
         OnExceptionThrown.Invoke(e, this);
     }
 }
Ejemplo n.º 3
0
        public async Task ServerMainAction()
        {
            while (stream == null)
            {
                await Task.Delay(200);
            }
            client.ReceiveTimeout = Timeout.Infinite;

            int bufferSize  = 0;
            int messageSize = 0;

            Methods.DataKind currentKind = 0;
            int current = 0;

            byte[] headBuffer    = new byte[4];
            byte[] messageBuffer = new byte[1024];

            CancellationToken cancelToken = Canceller.Token;

            while (true)
            {
                try
                {
                    while (true)
                    {
                        bufferSize = await stream.ReadAsync(headBuffer, 0, 4, cancelToken);

                        if (Canceller.IsCancellationRequested)
                        {
                            return;
                        }
                        if (bufferSize == 4)
                        {
                            messageSize = BitConverter.ToInt32(headBuffer, 0); // little endian
                            current     = 0;
                            goto data_kind_read_start;
                        }
                    }
data_kind_read_start:
                    while (true)
                    {
                        bufferSize = await stream.ReadAsync(headBuffer, 0, 4, cancelToken);

                        if (Canceller.IsCancellationRequested)
                        {
                            return;
                        }
                        if (bufferSize == 4)
                        {
                            currentKind = (Methods.DataKind)BitConverter.ToInt32(headBuffer, 0); // little endian
                            goto message_read_start;
                        }
                    }
message_read_start:

                    byte[] currentBuffer = messageBuffer.Length < messageSize ? new byte[messageSize] : messageBuffer;
                    while (current < messageSize)
                    {
                        bufferSize = await stream.ReadAsync(currentBuffer, current, messageSize, cancelToken);

                        current += bufferSize;
                        if (Canceller.IsCancellationRequested)
                        {
                            return;
                        }
                    }
                    if (isClient)
                    {
                        switch (currentKind)
                        {
                        case Methods.DataKind.GameInit:
                            clientReader.OnGameInit(MessagePackSerializer.Deserialize <Methods.GameInit>(currentBuffer));
                            break;

                        case Methods.DataKind.TurnStart:
                            clientReader.OnTurnStart(MessagePackSerializer.Deserialize <Methods.TurnStart>(currentBuffer));
                            break;

                        case Methods.DataKind.TurnEnd:
                            clientReader.OnTurnEnd(MessagePackSerializer.Deserialize <Methods.TurnEnd>(currentBuffer));
                            break;

                        case Methods.DataKind.GameEnd:
                            clientReader.OnGameEnd(MessagePackSerializer.Deserialize <Methods.GameEnd>(currentBuffer));
                            break;

                        case Methods.DataKind.Pause:
                            clientReader.OnPause(MessagePackSerializer.Deserialize <Methods.Pause>(currentBuffer));
                            break;

                        case Methods.DataKind.Interrupt:
                            clientReader.OnInterrupt(MessagePackSerializer.Deserialize <Methods.Interrupt>(currentBuffer));
                            break;

                        case Methods.DataKind.RebaseByUser:
                            clientReader.OnRebaseByUser(MessagePackSerializer.Deserialize <Methods.RebaseByUser>(currentBuffer));
                            break;

                        default:
                            throw new FormatException();
                        }
                    }
                    else
                    {
                        switch (currentKind)
                        {
                        case Methods.DataKind.Connect:
                            var Data = MessagePackSerializer.Deserialize <Methods.Connect>(currentBuffer);
                            AIProcess = Process.GetProcessById(Data.ProcessId);
                            AIProcess.EnableRaisingEvents = true;
                            AIProcess.Exited += __onAIProcessExited;
                            serverReader.OnConnect(Data);
                            break;

                        case Methods.DataKind.Decided:
                            serverReader.OnDecided(MessagePackSerializer.Deserialize <Methods.Decided>(currentBuffer));
                            break;

                        case Methods.DataKind.Interrupt:
                            serverReader.OnInterrupt(MessagePackSerializer.Deserialize <Methods.Interrupt>(currentBuffer));
                            break;

                        default:
                            throw new FormatException();
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (ex is TimeoutException)
                    {
                        continue;
                    }
                    if (ex is ObjectDisposedException)
                    {
                        return;
                    }
                    if (ex.InnerException is SocketException && ((SocketException)(ex.InnerException)).ErrorCode == 10060)
                    {
                        continue;
                    }
                    OnExceptionThrown?.Invoke(ex);
                    if (ex is IOException)
                    {
                        return;
                    }
                }
            }
        }
Ejemplo n.º 4
0
        /* A1        B2       C3       D4       E5       F6       G7       H8
         * group    select   desc   partNum    Unit   quantity   Manuf    Origin
         *
         * I9       F10      K11     12         13          14      15          16       17             18
         * char    macro   symbol    voltage voltagetype current  tripCurrent  Cpoint switchCap MaxPowerDissipation
         */
        public EplPartProperties GetPartProperty(int row, bool selected, ExcelWorksheet sheet)
        {
            try
            {
                EplPartProperties eplPartProperties = new EplPartProperties();
                if (selected)
                {
                    string select = sheet.GetCellText(2, row).ToLower();
                    if (select != "x")
                    {
                        return(null);
                    }
                }

                eplPartProperties.PartNumber     = sheet.GetCellText(4, row);
                eplPartProperties.PartGroup      = sheet.Name;
                eplPartProperties.Description    = sheet.GetCellText(3, row);
                eplPartProperties.Unit           = sheet.GetCellText(5, row);
                eplPartProperties.Quantity       = sheet.GetCellValue <int>(6, row);
                eplPartProperties.Manufacturer   = sheet.GetCellText(7, row);
                eplPartProperties.Supplier       = sheet.GetCellText(8, row);
                eplPartProperties.Characteristic = sheet.GetCellText(9, row);
                eplPartProperties.Symbol         = sheet.GetCellText(10, row);
                eplPartProperties.Macro          = sheet.GetCellText(11, row);

                eplPartProperties.Voltage                     = sheet.GetCellText(12, row);
                eplPartProperties.VoltageType                 = sheet.GetCellText(13, row);
                eplPartProperties.Current                     = sheet.GetCellText(14, row);
                eplPartProperties.TrippingCurrent             = sheet.GetCellText(15, row);
                eplPartProperties.ConnectionPointCrossSection = sheet.GetCellText(16, row);
                eplPartProperties.SwitchingCapacity           = sheet.GetCellText(17, row);
                eplPartProperties.MaxPowerDissipation         = sheet.GetCellText(18, row);

                if (string.IsNullOrEmpty(eplPartProperties.PartNumber) ||
                    string.IsNullOrEmpty(eplPartProperties.Description))
                {
                    return(null);
                }

                for (int i = 0; i < 10; i++)
                {
                    int    col = i * 3 + 19;
                    string des = sheet.GetCellText(col, row);
                    if (string.IsNullOrEmpty(des))
                    {
                        break;
                    }

                    eplPartProperties.PartFreeProperties.Add(new PartFreeProperties
                    {
                        Description = sheet.GetCellText(col, row),
                        Value       = sheet.GetCellText(col + 1, row),
                        Unit        = sheet.GetCellText(col + 2, row),
                    });

                    eplPartProperties.TotalFreeProperty = i + 1;
                }

                ExcelGetPartItemDone?.Invoke(eplPartProperties);
                return(eplPartProperties);
            }
            catch (Exception ex)
            {
                OnExceptionThrown?.Invoke($"Get excel part properties failed at {row}");
                Logger.WriteLine($"Get excel part properties failed at {row}", ex);
                return(null);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Project"></param>
        /// <param name="eplPartProperties"></param>
        /// <returns>true if successed</returns>
        public bool InsertPart(EplPartProperties eplPartProperties, PartAction action)
        {
            try
            {
                MDPart part = GetPart(eplPartProperties.PartNumber);

                if (part != null)
                {
                    switch (action)
                    {
                    case PartAction.OVERRIDE:
                        PartsDataBase.RemovePart(part);
                        part = AddPart(eplPartProperties.PartNumber);
                        break;

                    case PartAction.UPDATE:
                        break;

                    case PartAction.SKIP:
                        return(true);
                    }
                }
                else
                {
                    part = AddPart(eplPartProperties.PartNumber);
                }

                if (part == null)
                {
                    Logger.WriteLine("Add new part failed.");
                    return(false);
                }

                part.GenericProductGroup = MDPartsDatabaseItem.Enums.ProductTopGroup.Electric;
                part.ProductGroup        = MDPartsDatabaseItem.Enums.ProductGroup.Common;
                part.ProductSubGroup     = MDPartsDatabaseItem.Enums.ProductSubGroup.Common;

                part.Properties.ARTICLE_SUPPLIER          = eplPartProperties.Supplier;
                part.Properties.ARTICLE_NOTE              = eplPartProperties.Description;
                part.Properties.ARTICLE_MANUFACTURER      = eplPartProperties.Manufacturer;
                part.Properties.ARTICLE_ORDERNR           = eplPartProperties.OrderNumber;
                part.Properties.ARTICLE_QUANTITYUNIT      = eplPartProperties.Unit;
                part.Properties.ARTICLE_PACKAGINGQUANTITY = eplPartProperties.Quantity;
                part.Properties.ARTICLE_MACRO             = eplPartProperties.Macro;
                part.Properties.ARTICLE_CHARACTERISTICS   = eplPartProperties.Characteristic;
                part.Properties.ARTICLE_PACKAGINGPRICE_1  = eplPartProperties.Price;
                part.Properties.ARTICLE_REPORT_SYMBOL[1]  = eplPartProperties.Symbol;
                part.Properties.ARTICLE_GROUPNUMBER       = eplPartProperties.PartGroup;

                part.Properties.ARTICLE_VOLTAGE                = eplPartProperties.Voltage;
                part.Properties.ARTICLE_VOLTAGETYPE            = eplPartProperties.VoltageType;
                part.Properties.ARTICLE_ELECTRICALCURRENT      = eplPartProperties.Current;
                part.Properties.ARTICLE_TRIGGERCURRENT         = eplPartProperties.TrippingCurrent;
                part.Properties.ARTICLE_CONNECTIONCROSSSECTION = eplPartProperties.ConnectionPointCrossSection;
                part.Properties.ARTICLE_ELECTRICALPOWER        = eplPartProperties.SwitchingCapacity;
                part.Properties.ARTICLE_POWERDISSIPATION       = eplPartProperties.MaxPowerDissipation;

                if (eplPartProperties.PartFreeProperties != null)
                {
                    for (int i = 0; i < eplPartProperties.PartFreeProperties.Count; i++)
                    {
                        part.Properties.ARTICLE_FREE_DATA_DESCRIPTION[i + 1] = eplPartProperties.PartFreeProperties[i].Description;
                        part.Properties.ARTICLE_FREE_DATA_VALUE[i + 1]       = eplPartProperties.PartFreeProperties[i].Value;
                        part.Properties.ARTICLE_FREE_DATA_UNIT[i + 1]        = eplPartProperties.PartFreeProperties[i].Unit;
                    }
                }

                Logger.WriteLine($"Create part: {eplPartProperties.PartNumber} successfully.");
                return(true);
            }
            catch (Exception e)
            {
                OnExceptionThrown?.Invoke($"Insert Part {eplPartProperties.PartNumber} error.");
                Logger.WriteLine("InsertPart error.", e);
                return(false);
            }
        }