コード例 #1
0
ファイル: MapHandler.cs プロジェクト: dxmcu/Mirle.Agv.Utmc
        public void ReadAddressCsv()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(AddressPath))
                {
                    mirleLogger.Log(new LogFormat("Error", "5", GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, "Device", "CarrierID"
                                                  , $"IsAddressPathNull={string.IsNullOrWhiteSpace(AddressPath)}"));
                    return;
                }
                Vehicle.MapInfo.addressMap.Clear();
                Vehicle.MapInfo.chargerAddressMap.Clear();

                string[] allRows = File.ReadAllLines(AddressPath);
                if (allRows == null || allRows.Length < 2)
                {
                    mirleLogger.Log(new LogFormat("Error", "5", GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, "Device", "CarrierID"
                                                  , $"There are no address in file"));
                    return;
                }

                string[] titleRow = allRows[0].Split(',');
                allRows = allRows.Skip(1).ToArray();

                int nRows    = allRows.Length;
                int nColumns = titleRow.Length;

                Dictionary <string, int> dicHeaderIndexes = new Dictionary <string, int>();
                for (int i = 0; i < nColumns; i++)
                {
                    var keyword = titleRow[i].Trim();
                    if (!string.IsNullOrWhiteSpace(keyword))
                    {
                        dicHeaderIndexes.Add(keyword, i);
                    }
                }

                for (int i = 0; i < nRows; i++)
                {
                    string[]         getThisRow = allRows[i].Split(',');
                    MapAddress       oneRow     = new MapAddress();
                    MapAddressOffset offset     = new MapAddressOffset();
                    try
                    {
                        oneRow.Id         = getThisRow[dicHeaderIndexes["Id"]];
                        oneRow.Position.X = double.Parse(getThisRow[dicHeaderIndexes["PositionX"]]);
                        oneRow.Position.Y = double.Parse(getThisRow[dicHeaderIndexes["PositionY"]]);
                        if (dicHeaderIndexes.ContainsKey("TransferPortDirection"))
                        {
                            oneRow.TransferPortDirection = oneRow.AddressDirectionParse(getThisRow[dicHeaderIndexes["TransferPortDirection"]]);
                        }
                        if (dicHeaderIndexes.ContainsKey("GateType"))
                        {
                            oneRow.GateType = getThisRow[dicHeaderIndexes["GateType"]];
                        }
                        if (dicHeaderIndexes.ContainsKey("ChargeDirection"))
                        {
                            oneRow.ChargeDirection = oneRow.AddressDirectionParse(getThisRow[dicHeaderIndexes["ChargeDirection"]]);
                        }
                        if (dicHeaderIndexes.ContainsKey("PioDirection"))
                        {
                            oneRow.PioDirection = oneRow.AddressDirectionParse(getThisRow[dicHeaderIndexes["PioDirection"]]);
                        }
                        //oneRow.CanSpin = bool.Parse(getThisRow[dicHeaderIndexes["CanSpin"]]);
                        //oneRow.IsTR50 = bool.Parse(getThisRow[dicHeaderIndexes["IsTR50"]]);
                        if (dicHeaderIndexes.ContainsKey("InsideSectionId"))
                        {
                            oneRow.InsideSectionId = FitZero(getThisRow[dicHeaderIndexes["InsideSectionId"]]);
                        }
                        if (dicHeaderIndexes.ContainsKey("OffsetX"))
                        {
                            offset.OffsetX     = double.Parse(getThisRow[dicHeaderIndexes["OffsetX"]]);
                            offset.OffsetY     = double.Parse(getThisRow[dicHeaderIndexes["OffsetY"]]);
                            offset.OffsetTheta = double.Parse(getThisRow[dicHeaderIndexes["OffsetTheta"]]);
                        }
                        oneRow.AddressOffset = offset;
                        if (dicHeaderIndexes.ContainsKey("VehicleHeadAngle"))
                        {
                            oneRow.VehicleHeadAngle = double.Parse(getThisRow[dicHeaderIndexes["VehicleHeadAngle"]]);
                        }
                    }
                    catch (Exception ex)
                    {
                        mirleLogger.Log(new LogFormat("Error", "5", GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, "Device", "CarrierID", $"LoadAddressCsv read oneRow : [lastReadAdrId={lastReadAdrId}]"));
                        LogException(GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, ex.Message);
                    }

                    lastReadAdrId = oneRow.Id;
                    Vehicle.MapInfo.addressMap.TryAdd(oneRow.Id, oneRow);
                    if (oneRow.IsCharger())
                    {
                        Vehicle.MapInfo.chargerAddressMap.Add(oneRow);
                    }
                    Vehicle.MapInfo.gateTypeMap.Add(oneRow.Id, oneRow.GateType);
                }

                LogDebug(GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, $"Load Address File Ok. [lastReadAdrId={lastReadAdrId}]");
            }
            catch (Exception ex)
            {
                LogException(GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, $"LoadAddressCsv : [lastReadAdrId={lastReadAdrId}]");
                LogException(GetType().Name + ":" + MethodBase.GetCurrentMethod().Name, ex.Message);
            }
        }