123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using PhihongEv.Lib.Convert;
- using PhihongEv.Lib.Model;
- using System;
- using System.Text;
- namespace PhihongEv.Lib
- {
- public class ModelName
- {
- public MajorType Type { get; private set; }
- public InputConfig InputConfig { get; private set; }
- public SafetyRegulation SafetyReg { get; private set; }
- public decimal RatedPower { get; private set; }
- public ConnectorType[] ConnectorTypes { get; private set; }
- public NetworkOption Network { get; private set; }
- public Generation Generation { get; private set; }
- public string CustomCode { get; private set; }
- public MajorTypeCategory Type_Cat { get => Type.ToCategory(); }
- private string _modelNameString;
- public string modelNameString
- {
- get => _modelNameString;
- set
- {
- if (TryParse(value, out var result))
- {
- this.Type = result.Type;
- this.InputConfig = result.InputConfig;
- this.SafetyReg = result.SafetyReg;
- this.RatedPower = result.RatedPower;
- this.ConnectorTypes = result.ConnectorTypes;
- this.Network = result.Network;
- this.Generation = result.Generation;
- this.CustomCode = result.CustomCode;
- _modelNameString = value;
- }
- }
- }
- public static bool TryParse(byte[] modeNameByte, out ModelName modelName)
- {
- modelName = null;
- string modelNameString;
- try
- {
- modelNameString = Encoding.UTF8.GetString(modeNameByte,0,modeNameByte.Length);
- }
- catch
- {
- return false;
- }
- if (modelNameString.Length != 14)
- return false;
- return TryParse(modelNameString, out modelName);
- }
- public static bool TryParse(string modeNameString, out ModelName modelName)
- {
- modelName = null;
- ModelName model = new ModelName();
- if (modeNameString == null || modeNameString.Length != 14)
- return false;
- var typeString = modeNameString.Substring(0, 2);
- model.Type = typeString.ToMajorType();
- if (model.Type == null)
- return false;
- var inputConfigChr = modeNameString.Substring(2,1);
- model.InputConfig = inputConfigChr.ToInputConfig();
- if (model.InputConfig == null)
- return false;
- var safeRegChr = modeNameString.Substring(3,1);
- model.SafetyReg = safeRegChr.ToSafetyRegulation();
- if (model.SafetyReg == null)
- return false;
- var ratedPowerStr = modeNameString.Substring(4, 3);
- model.RatedPower = GetRatedPowerFromStr(ratedPowerStr);
- model.ConnectorTypes = new ConnectorType[3];
- var connectorListStr = modeNameString.Substring(7, 3);
- /*
- model.ConnectorTypes.Add(ConnectorType.CHadeMO);
- model.ConnectorTypes.Add(ConnectorType.CCS);
- model.ConnectorTypes.Add(ConnectorType.IEC);
- */
- for (int index = 0; index < 3; index++)
- {
- var connectorStr = connectorListStr.Substring(index,1);
- model.ConnectorTypes[index] = connectorStr.ToConnectorType();
- if (model.ConnectorTypes[index] == null)
- return false;
- }
- var networkOpChr = modeNameString.Substring(10,1);
- model.Network = networkOpChr.ToNetworkOption();
- if (model.Network == null)
- {
- return false;
- }
- var genStr = modeNameString.Substring(11, 1);
- model.Generation = genStr.ToGeneration();
- if (model.Generation == null)
- {
- return false;
- }
- model.CustomCode = modeNameString.Substring(12, 2);
- model._modelNameString = modeNameString;
- modelName = model;
- return true;
- }
- public override string ToString()
- {
- return modelNameString;
- }
- public int GetConnectorViewIndex(int arrayIndex)
- {
- if (arrayIndex < 0 || arrayIndex > 3)
- {
- throw new Exception("connector Array index out of range");
- }
- //assume same index from start
- int viewIndex = arrayIndex;
- while (arrayIndex >= 0)
- {
- arrayIndex--;
- //for those none connector before index, view index - 1
- if (
- arrayIndex >= 0 &&
- ConnectorTypes[arrayIndex] == ConnectorType.None)
- {
- viewIndex--;
- }
- }
- return viewIndex;
- }
- public int GetConnectorArrayIndex(int viewIndex)
- {
- if (viewIndex < 0 || viewIndex > 3)
- {
- throw new Exception("connector Array index out of range");
- }
- int arrayIndex = 0;
- while (viewIndex >= 0)
- {
- //find next not non connector
- while (ConnectorTypes[arrayIndex] == ConnectorType.None)
- {
- arrayIndex++;
- }
- //find connector, match and find next
- viewIndex--;
- }
- return arrayIndex;
- }
- private static decimal GetRatedPowerFromStr(string fromStr)
- {
- if (!int.TryParse(fromStr, out int ratedPowerNum) &&
- ratedPowerNum > 0)
- return -1;
- decimal realnum = (decimal)ratedPowerNum / 10;
- var exp = ratedPowerNum % 10;
- return (decimal)(realnum * (10 ^ exp));
- }
- }
- }
|