)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Port (Ia.Ftn.Cl.Models.Data.Huawei)

Public general use code classes and xml files that we've compiled and used over the years:

Huawei's Port support class of Fixed Telecommunications Network (FTN) data model.

    1: using System;
    2: using System.Collections.Generic;
    3: using System.Data;
    4: using System.Linq;
    5: using System.Text;
    6:  
    7: namespace Ia.Ftn.Cl.Models.Data.Huawei
    8: {
    9:     ////////////////////////////////////////////////////////////////////////////
   10:  
   11:     /// <summary publish="true">
   12:     /// Huawei's Port support class of Fixed Telecommunications Network (FTN) data model.
   13:     /// </summary>
   14:     /// 
   15:     /// <remarks> 
   16:     /// Copyright © 2016-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   17:     ///
   18:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   19:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   20:     ///
   21:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   22:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   23:     /// 
   24:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   25:     /// 
   26:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   27:     /// </remarks> 
   28:     public class Port
   29:     {
   30:         /// <summary/>
   31:         public Port() { }
   32:  
   33:         ////////////////////////////////////////////////////////////////////////////
   34:  
   35:         /// <summary>
   36:         ///
   37:         /// </summary>
   38:         public static bool Update(Ia.Ftn.Cl.Models.Business.Huawei.Ems.Response response, out Ia.Cl.Models.Result result)
   39:         {
   40:             bool isUpdated;
   41:             int did, fn, sn, pn, queryFn, querySn, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
   42:             string id, queryCommand, queryDev;
   43:             DataColumnCollection columns;
   44:             DataRow columnDataRow;
   45:             Dictionary<string, int> devToDidDictionary;
   46:             Ia.Ftn.Cl.Models.Huawei.EmsPort port, newPort;
   47:             Ia.Ftn.Cl.Models.Huawei.EmsBoard board;
   48:             List<string> itemIdList;
   49:             List<Ia.Ftn.Cl.Models.Huawei.EmsPort> portList;
   50:  
   51:             isUpdated = false;
   52:             readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
   53:             result = new Ia.Cl.Models.Result();
   54:  
   55:             if (response.CompletionCode == "COMPLD")
   56:             {
   57:                 if (response.QueryDataTable != null)
   58:                 {
   59:                     queryCommand = response.CommandFromCorrelationTagDictionaryByCtag;
   60:  
   61:                     // LST-PORT::DEV=MDU-SAA-838-001,FN=0,SN=1:{ctag}::;
   62:                     queryDev = Ia.Cl.Models.Default.Match(queryCommand, @"DEV=(.+?),");
   63:                     queryFn = int.Parse(Ia.Cl.Models.Default.Match(queryCommand, @"FN=(\d+)"));
   64:                     querySn = int.TryParse(Ia.Cl.Models.Default.Match(queryCommand, @"SN=(\d+)"), out int i) ? i : 0;
   65:  
   66:                     columns = response.QueryDataTable.Columns;
   67:                     readItemCount = response.QueryDataTable.Rows.Count;
   68:  
   69:                     devToDidDictionary = Ia.Ftn.Cl.Models.Data.Huawei.Dev.DevToDidDictionary;
   70:  
   71:                     if (devToDidDictionary.ContainsKey(queryDev))
   72:                     {
   73:                         did = devToDidDictionary[queryDev];
   74:  
   75:                         board = Ia.Ftn.Cl.Models.Data.Huawei.Board.ReadByDidFnSn(did, queryFn, querySn);
   76:  
   77:                         if (board != null)
   78:                         {
   79:                             itemIdList = new List<string>(response.QueryDataTable.Rows.Count + 1);
   80:  
   81:                             using (var db = new Ia.Ftn.Cl.Db())
   82:                             {
   83:                                 portList = (from ep in db.EmsPorts where ep.EmsBoard.Id == board.Id select ep).ToList();
   84:                                 // or portList = Ia.Ftn.Cl.Model.Data.Huawei.Port.List(board.Id);
   85:  
   86:                                 existingItemCount = portList.Count;
   87:  
   88:                                 if (response.QueryDataTable.Rows.Count >= 1)
   89:                                 {
   90:                                     columnDataRow = response.QueryDataTable.Rows[0];
   91:  
   92:                                     foreach (DataRow dataRow in response.QueryDataTable.Rows)
   93:                                     {
   94:                                         did = int.Parse(dataRow[columns.IndexOf("DID")].ToString());
   95:                                         fn = int.Parse(dataRow[columns.IndexOf("FN")].ToString());
   96:                                         sn = int.Parse(dataRow[columns.IndexOf("SN")].ToString());
   97:                                         pn = int.Parse(dataRow[columns.IndexOf("PN")].ToString());
   98:  
   99:                                         id = Ia.Ftn.Cl.Models.Business.Huawei.Port.PortId(board.Id, pn);
  100:  
  101:                                         port = (from p in portList where p.Id == id select p).SingleOrDefault();
  102:  
  103:                                         newPort = new Ia.Ftn.Cl.Models.Huawei.EmsPort()
  104:                                         {
  105:                                             Id = id,
  106:                                             DID = did,
  107:                                             FN = fn,
  108:                                             SN = sn,
  109:                                             PN = pn,
  110:                                             ALIAS = columns.Contains("ALIAS") ? dataRow[columns.IndexOf("ALIAS")].ToString() : string.Empty,
  111:                                             PT = columns.Contains("PT") ? dataRow[columns.IndexOf("PT")].ToString() : string.Empty,
  112:                                             PSTAT = columns.Contains("PSTAT") ? dataRow[columns.IndexOf("PSTAT")].ToString() : string.Empty,
  113:                                             NAME = columns.Contains("NAME") ? dataRow[columns.IndexOf("NAME")].ToString() : string.Empty,
  114:                                             PSPEED = columns.Contains("PSPEED") ? dataRow[columns.IndexOf("PSPEED")].ToString() : string.Empty,
  115:                                             PDIRECT = columns.Contains("PDIRECT") ? dataRow[columns.IndexOf("PDIRECT")].ToString() : string.Empty,
  116:                                             LOOPSTAT = columns.Contains("LOOPSTAT") ? dataRow[columns.IndexOf("LOOPSTAT")].ToString() : string.Empty,
  117:                                             TESTSTAT = columns.Contains("TESTSTAT") ? dataRow[columns.IndexOf("TESTSTAT")].ToString() : string.Empty,
  118:                                             FEC = columns.Contains("FEC") ? dataRow[columns.IndexOf("FEC")].ToString() : string.Empty,
  119:                                             RN = columns.Contains("RN") ? dataRow[columns.IndexOf("RN")].ToString() : string.Empty,
  120:                                             AState = columns.Contains("AState") ? dataRow[columns.IndexOf("AState")].ToString() : string.Empty,
  121:                                             EmsBoard = (from eb in db.EmsBoards where eb.Id == board.Id select eb).SingleOrDefault()
  122:                                         };
  123:  
  124:                                         if (port == null)
  125:                                         {
  126:                                             newPort.Created = newPort.Updated = DateTime.UtcNow.AddHours(3);
  127:  
  128:                                             db.EmsPorts.Add(newPort);
  129:  
  130:                                             insertedItemCount++;
  131:                                         }
  132:                                         else // update
  133:                                         {
  134:                                             // below: copy values from newPort to port
  135:  
  136:                                             if (port.Update(newPort))
  137:                                             {
  138:                                                 db.EmsPorts.Attach(port);
  139:                                                 db.Entry(port).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
  140:  
  141:                                                 updatedItemCount++;
  142:                                             }
  143:                                         }
  144:  
  145:                                         itemIdList.Add(id); // keep at the end
  146:                                     }
  147:  
  148:                                     // below: this function will remove values that were not present in the reading
  149:                                     if (portList.Count > 0)
  150:                                     {
  151:                                         foreach (Ia.Ftn.Cl.Models.Huawei.EmsPort po in portList)
  152:                                         {
  153:                                             if (!itemIdList.Contains(po.Id))
  154:                                             {
  155:                                                 port = (from ep in db.EmsPorts where ep.Id == po.Id select ep).SingleOrDefault();
  156:  
  157:                                                 db.EmsPorts.Remove(port);
  158:  
  159:                                                 deletedItemCount++;
  160:                                             }
  161:                                         }
  162:                                     }
  163:                                 }
  164:                                 else
  165:                                 {
  166:                                     // below: remove all values since reading was empty
  167:  
  168:                                     if (portList.Count > 0)
  169:                                     {
  170:                                         foreach (Ia.Ftn.Cl.Models.Huawei.EmsPort po in portList)
  171:                                         {
  172:                                             port = (from ep in db.EmsPorts where ep.Id == po.Id select ep).SingleOrDefault();
  173:  
  174:                                             db.EmsPorts.Remove(port);
  175:  
  176:                                             deletedItemCount++;
  177:                                         }
  178:                                     }
  179:                                 }
  180:  
  181:                                 db.SaveChanges();
  182:                             }
  183:  
  184:                             if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
  185:                             else isUpdated = false;
  186:  
  187:                             result.AddSuccess("LST-PORT (" + response.Ctag + "): (" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
  188:                         }
  189:                         else
  190:                         {
  191:                             result.AddWarning("LST-PORT (" + response.Ctag + "): board is null");
  192:                         }
  193:                     }
  194:                     else
  195:                     {
  196:                         result.AddError("LST-PORT (" + response.Ctag + "): devToDidDictionary does not contain key: " + queryDev);
  197:                     }
  198:                 }
  199:                 else
  200:                 {
  201:                     result.AddWarning("LST-PORT (" + response.Ctag + "): QueryDataTable is null, (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ")");
  202:                 }
  203:             }
  204:             else
  205:             {
  206:                 result.AddWarning("LST-PORT (" + response.Ctag + "): (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ")");
  207:             }
  208:  
  209:             return isUpdated;
  210:         }
  211:  
  212:         ////////////////////////////////////////////////////////////////////////////
  213:  
  214:         /// <summary>
  215:         ///
  216:         /// </summary>
  217:         public static List<Ia.Ftn.Cl.Models.Huawei.EmsPort> List()
  218:         {
  219:             List<Ia.Ftn.Cl.Models.Huawei.EmsPort> list;
  220:  
  221:             using (var db = new Ia.Ftn.Cl.Db())
  222:             {
  223:                 list = (from p in db.EmsPorts select p).ToList();
  224:             }
  225:  
  226:             return list;
  227:         }
  228:  
  229:         ////////////////////////////////////////////////////////////////////////////
  230:  
  231:         /// <summary>
  232:         ///
  233:         /// </summary>
  234:         public static List<Ia.Ftn.Cl.Models.Huawei.EmsPort> List(string boardId)
  235:         {
  236:             List<Ia.Ftn.Cl.Models.Huawei.EmsPort> list;
  237:  
  238:             using (var db = new Ia.Ftn.Cl.Db())
  239:             {
  240:                 list = (from p in db.EmsPorts where p.EmsBoard.Id == boardId select p).ToList();
  241:             }
  242:  
  243:             return list;
  244:         }
  245:  
  246:         ////////////////////////////////////////////////////////////////////////////
  247:  
  248:         /// <summary>
  249:         ///
  250:         /// </summary>
  251:         public static List<Ia.Ftn.Cl.Models.Huawei.EmsPort> ListByDevId(int devId)
  252:         {
  253:             List<Ia.Ftn.Cl.Models.Huawei.EmsPort> list;
  254:  
  255:             using (var db = new Ia.Ftn.Cl.Db())
  256:             {
  257:                 list = (from p in db.EmsPorts where p.EmsBoard.EmsDev.Id == devId select p).ToList();
  258:             }
  259:  
  260:             return list;
  261:         }
  262:  
  263:         ////////////////////////////////////////////////////////////////////////////
  264:  
  265:         /// <summary>
  266:         ///
  267:         /// </summary>
  268:         public static List<Ia.Ftn.Cl.Models.Huawei.EmsPort> ListByDid(int did)
  269:         {
  270:             List<Ia.Ftn.Cl.Models.Huawei.EmsPort> list;
  271:  
  272:             using (var db = new Ia.Ftn.Cl.Db())
  273:             {
  274:                 list = (from p in db.EmsPorts where p.DID == did select p).ToList();
  275:             }
  276:  
  277:             return list;
  278:         }
  279:  
  280:         ////////////////////////////////////////////////////////////////////////////
  281:  
  282:         /// <summary>
  283:         ///
  284:         /// </summary>
  285:         public static Ia.Ftn.Cl.Models.Huawei.EmsPort ReadByDidFnSnPn(int did, int fn, int sn, int pn)
  286:         {
  287:             Ia.Ftn.Cl.Models.Huawei.EmsPort item;
  288:  
  289:             using (var db = new Ia.Ftn.Cl.Db())
  290:             {
  291:                 item = (from p in db.EmsPorts where p.DID == did && p.FN == fn && p.SN == sn && p.PN == pn select p).SingleOrDefault();
  292:             }
  293:  
  294:             return item;
  295:         }
  296:  
  297:         ////////////////////////////////////////////////////////////////////////////
  298:         ////////////////////////////////////////////////////////////////////////////
  299:  
  300:         /// <summary>
  301:         ///
  302:         /// </summary>
  303:         public static string ToSimpleTextString(Ia.Ftn.Cl.Models.Huawei.EmsPort emdPort)
  304:         {
  305:             StringBuilder sb;
  306:  
  307:             sb = new StringBuilder();
  308:  
  309:             sb.AppendLine("ALIAS: " + emdPort.ALIAS);
  310:             sb.AppendLine("DID: " + emdPort.DID);
  311:             sb.AppendLine("FN: " + emdPort.FN);
  312:             sb.AppendLine("SN: " + emdPort.SN);
  313:             sb.AppendLine("PN: " + emdPort.PN);
  314:             sb.AppendLine("PT: " + emdPort.PT);
  315:             sb.AppendLine("PSTAT: " + emdPort.PSTAT);
  316:  
  317:             return sb.ToString();
  318:         }
  319:  
  320:         ////////////////////////////////////////////////////////////////////////////
  321:         ////////////////////////////////////////////////////////////////////////////
  322:     }
  323:  
  324:     ////////////////////////////////////////////////////////////////////////////
  325:     ////////////////////////////////////////////////////////////////////////////
  326: }