Browse Source

switch ocpp2.0.1

Jessica Tseng 4 năm trước cách đây
mục cha
commit
78e691aa40

BIN
EVCB_OCPP.WSServer/DLL/EVCB_OCPP.Packet20.dll


BIN
EVCB_OCPP.WSServer/DLL/EVCB_OCPP20.Packet.dll


+ 4 - 2
EVCB_OCPP.WSServer/EVCB_OCPP.WSServer.csproj

@@ -50,8 +50,9 @@
       <SpecificVersion>False</SpecificVersion>
       <HintPath>DLL\EVCB_OCPP.Packet.dll</HintPath>
     </Reference>
-    <Reference Include="EVCB_OCPP.Packet20">
-      <HintPath>DLL\EVCB_OCPP.Packet20.dll</HintPath>
+    <Reference Include="EVCB_OCPP20.Packet, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>DLL\EVCB_OCPP20.Packet.dll</HintPath>
     </Reference>
     <Reference Include="log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
       <HintPath>..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
@@ -169,6 +170,7 @@
     <None Include="App.config">
       <SubType>Designer</SubType>
     </None>
+    <Content Include="DLL\EVCB_OCPP20.Packet.dll" />
     <Content Include="DLL\SuperSocket.Common.dll" />
     <Content Include="DLL\SuperSocket.SocketBase.dll" />
     <Content Include="DLL\SuperSocket.SocketEngine.dll" />

+ 9 - 0
EVCB_OCPP.WSServer/Helper/Convertor.cs

@@ -15,5 +15,14 @@ namespace EVCB_OCPP.WSServer.Helper
             Enum.TryParse<Actions>(action, out result);
             return result;
         }
+
+        public static EVCB_OCPP20.Packet.Features.Actions GetActionby20(string action)
+        {
+            EVCB_OCPP20.Packet.Features.Actions result = EVCB_OCPP20.Packet.Features.Actions.None;
+            Enum.TryParse<EVCB_OCPP20.Packet.Features.Actions>(action, out result);
+            return result;
+        }
+
+        
     }
 }

+ 4 - 4
EVCB_OCPP.WSServer/Message/BasicMessageHandler.cs

@@ -11,7 +11,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
-using Packet20 = EVCB_OCPP.Packet20;
+
 
 namespace EVCB_OCPP.WSServer.Message
 {
@@ -58,7 +58,7 @@ namespace EVCB_OCPP.WSServer.Message
         internal MessageResult AnalysisReceiveData(ClientData client, string data)
         {
             MessageResult result = null;
-            if (client.IsOCPP16)
+            if (!client.ISOCPP20)
             {
                 result = _ocpp16Handler.AnalysisReceiveData(client, data);
             }
@@ -93,7 +93,7 @@ namespace EVCB_OCPP.WSServer.Message
             }
             return msg;
         }
-        static internal string GenerateConfirmationofOCPP20(string uniqueId, Packet20.Messages.IConfirmation confirmation)
+        static internal string GenerateConfirmationofOCPP20(string uniqueId, EVCB_OCPP20.Packet.Messages.IConfirmation confirmation)
         {
             string msg = string.Empty;
             if (confirmation != null && confirmation.Validate())
@@ -123,7 +123,7 @@ namespace EVCB_OCPP.WSServer.Message
             return msg;
         }
 
-        static internal string GenerateRequestofOCPP20(string uniqueId, string action, Packet20.Messages.IRequest request)
+        static internal string GenerateRequestofOCPP20(string uniqueId, string action, EVCB_OCPP20.Packet.Messages.IRequest request)
         {
             string msg = string.Empty;
             if (request != null && request.Validate())

+ 5 - 5
EVCB_OCPP.WSServer/Message/CoreProfileHandler.cs

@@ -454,7 +454,6 @@ namespace EVCB_OCPP.WSServer.Message
         internal MessageResult ExecuteCoreConfirm(Actions action, ClientData session, IConfirmation confirm, string requestId)
         {
             MessageResult result = new MessageResult() { Success = true };
-
             switch (action)
             {
                 case Actions.DataTransfer:
@@ -654,7 +653,7 @@ namespace EVCB_OCPP.WSServer.Message
                             if (_confirm.configurationKey != null)
                             {
                                 foreach (var item in _confirm.configurationKey)
-                                {
+                                {                                    
                                     string oldValue = string.Empty;
                                     var foundConfig = configure.Find(x => x.ConfigureName == item.key);
 
@@ -666,7 +665,7 @@ namespace EVCB_OCPP.WSServer.Message
                                         }
 
                                         foundConfig.ReadOnly = item.IsReadOnly;
-                                        foundConfig.ConfigureSetting = item.value;
+                                        foundConfig.ConfigureSetting = string.IsNullOrEmpty(item.value) ? string.Empty : item.value;
                                     }
                                     else
                                     {
@@ -675,14 +674,15 @@ namespace EVCB_OCPP.WSServer.Message
                                             ChargeBoxId = session.ChargeBoxId,
                                             ConfigureName = item.key,
                                             ReadOnly = item.IsReadOnly,
-                                            ConfigureSetting = item.value,
+                                            ConfigureSetting = string.IsNullOrEmpty(item.value) ? string.Empty : item.value,
                                             Exists = true
                                         });
                                     }
 
                                     if (foundConfig.ConfigureName == "SecurityProfile")
                                     {
-                                        if (oldValue != item.value)
+
+                                        if (foundConfig.ConfigureSetting != oldValue)
                                         {
                                             session.ResetSecurityProfile = true;
                                         }

+ 2 - 2
EVCB_OCPP.WSServer/Message/MessageResult.cs

@@ -48,9 +48,9 @@ namespace EVCB_OCPP.WSServer.Message
 
         internal IConfirmation Confirmation { set; get; }
 
-        internal EVCB_OCPP.Packet20.Messages.IRequest Request20 { set; get; }
+        internal EVCB_OCPP20.Packet.Messages.IRequest Request20 { set; get; }
 
-        internal EVCB_OCPP.Packet20.Messages.IConfirmation Confirmation20 { set; get; }
+        internal EVCB_OCPP20.Packet.Messages.IConfirmation Confirmation20 { set; get; }
 
         internal Exception Exception { get; set; }
     }

+ 42 - 81
EVCB_OCPP.WSServer/Message/OCPP20MessageHandler.cs

@@ -1,6 +1,4 @@
 using EVCB_OCPP.Packet.Messages.Basic;
-using EVCB_OCPP.Packet20.Features;
-using EVCB_OCPP.Packet20.Messages;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using NLog;
@@ -10,9 +8,10 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
-using I20Request = EVCB_OCPP.Packet20.Messages.IRequest;
-using I20Confirmation = EVCB_OCPP.Packet20.Messages.IConfirmation;
+using I20Request = EVCB_OCPP20.Packet.Messages.IRequest;
+using I20Confirmation = EVCB_OCPP20.Packet.Messages.IConfirmation;
 using EVCB_OCPP.Packet.Messages;
+using EVCB_OCPP20.Packet.Features;
 
 namespace EVCB_OCPP.WSServer.Message
 {
@@ -47,11 +46,7 @@ namespace EVCB_OCPP.WSServer.Message
         private const string DATE_FORMAT_WITH_MS = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
         #endregion
 
-        private List<Profile> profiles = new List<Profile>()
-        {
-             new CoreProfile(),
-
-        };
+       
 
 
         /// <summary>
@@ -116,39 +111,39 @@ namespace EVCB_OCPP.WSServer.Message
                             break;
                         case TYPENUMBER_CALLRESULT:
                             {
-                                //BasicMessageResult baseResult = UnPackPayloadbyCallResult(client.queue, msg.Id, msg.Payload.ToString());
+                                BasicMessageResult baseResult = UnPackPayloadbyCallResult(client.queue20, msg.Id, msg.Payload.ToString());
 
-                                //if (baseResult.Confirmation != null)
-                                //{
+                                if (baseResult.Confirmation != null)
+                                {
 
-                                //    if (baseResult.Confirmation.Validate())
-                                //    {
-                                //        result.Id = TYPENUMBER_CALLRESULT;
-                                //        result.Message = baseResult.Confirmation;
-                                //        result.Action = baseResult.Confirmation.GetRequest().Action;
-                                //        //return data
-                                //    }
-                                //    else
-                                //    {
-                                //        string replyMsg = BasicMessageHandler.GenerateCallError(msg.Id, OCPPErrorCodes.OccurenceConstraintViolation.ToString(),
-                                //      OCPPErrorDescription.OccurenceConstraintViolation);
-                                //        result.Id = TYPENUMBER_CALLRESULT;
-                                //        result.Message = baseResult.Confirmation;
-                                //        result.Success = false;
-                                //        result.CallErrorMsg = replyMsg;
-                                //        result.Exception = new Exception("Validate Failed");
-                                //    }
-                                //}
-                                //else
-                                //{
-                                //    string replyMsg = BasicMessageHandler.GenerateCallError(msg.Id, OCPPErrorCodes.OccurenceConstraintViolation.ToString(),
-                                //   OCPPErrorDescription.OccurenceConstraintViolation);
-                                //    result.Id = TYPENUMBER_CALLRESULT;
-                                //    result.Message = baseResult.Confirmation;
-                                //    result.Success = false;
-                                //    result.CallErrorMsg = replyMsg;
-                                //    result.Exception = baseResult.Exception;
-                                //}
+                                    if (baseResult.Confirmation.Validate())
+                                    {
+                                        result.Id = TYPENUMBER_CALLRESULT;
+                                        result.Message = baseResult.Confirmation;
+                                        result.Action = baseResult.Confirmation.GetRequest().Action;
+                                        //return data
+                                    }
+                                    else
+                                    {
+                                        string replyMsg = BasicMessageHandler.GenerateCallError(msg.Id, OCPPErrorCodes.OccurenceConstraintViolation.ToString(),
+                                      OCPPErrorDescription.OccurenceConstraintViolation);
+                                        result.Id = TYPENUMBER_CALLRESULT;
+                                        result.Message = baseResult.Confirmation;
+                                        result.Success = false;
+                                        result.CallErrorMsg = replyMsg;
+                                        result.Exception = new Exception("Validate Failed");
+                                    }
+                                }
+                                else
+                                {
+                                    string replyMsg = BasicMessageHandler.GenerateCallError(msg.Id, OCPPErrorCodes.OccurenceConstraintViolation.ToString(),
+                                   OCPPErrorDescription.OccurenceConstraintViolation);
+                                    result.Id = TYPENUMBER_CALLRESULT;
+                                    result.Message = baseResult.Confirmation;
+                                    result.Success = false;
+                                    result.CallErrorMsg = replyMsg;
+                                    result.Exception = baseResult.Exception;
+                                }
 
                             }
                             break;
@@ -255,25 +250,12 @@ namespace EVCB_OCPP.WSServer.Message
 
         private BasicMessageResult UnPackPayloadbyCall(string action, string payload)
         {
+           
             BasicMessageResult result = new BasicMessageResult();
             try
             {
-                Feature feature = null;
-                foreach (var profile in profiles)
-                {
-                    feature = profile.GetFeaturebyAction(action);
-                    if (feature == null)
-                    {
-                        continue;
-                    }
-                    else
-                    {
-                        break;
-                    }
-                }
-                string tt = "{  \"connectorId\": 1,  \"evseId\": 1,  \"connectorStatus\": \"Occupied\",  \"timestamp\": \"2020-10-28T08:28:47Z\"}";
-                var gg = JsonConvert.DeserializeObject(tt, typeof(StatusNotificationRequest));
-                result.Request20 = JsonConvert.DeserializeObject(payload, feature.GetRequestType()) as I20Request;
+
+                result.Request20 = (I20Request)(JsonConvert.DeserializeObject(payload, Type.GetType("EVCB_OCPP20.Packet.Messages." + action + "Request,EVCB_OCPP20.Packet")));
 
             }
             catch (Exception ex)
@@ -284,30 +266,17 @@ namespace EVCB_OCPP.WSServer.Message
             }
 
             return result;
+
+           
         }
 
-        private BasicMessageResult UnPackPayloadbyCallResult(EVCB_OCPP.Packet20.Messages.Basic.Queue requestQueue, string uniqueId, string payload)
+        private BasicMessageResult UnPackPayloadbyCallResult(EVCB_OCPP20.Packet.Messages.Basic.Queue requestQueue, string uniqueId, string payload)
         {
             BasicMessageResult result = new BasicMessageResult();
             try
             {
                 I20Request request = requestQueue.RestoreRequest(uniqueId);
-                Feature feature = null;
-                foreach (var profile in profiles)
-                {
-                    feature = profile.GetFeaturebyType(request.GetType());
-                    if (feature == null)
-                    {
-                        continue;
-                    }
-                    else
-                    {
-                        break;
-                    }
-                }
-
-
-                I20Confirmation confrim = JsonConvert.DeserializeObject(payload, feature.GetConfirmationType()) as I20Confirmation;
+                I20Confirmation confrim = JsonConvert.DeserializeObject(payload, Type.GetType("EVCB_OCPP20.Packet.Messages." + request.Action + "Response,EVCB_OCPP20.Packet")) as I20Confirmation;
                 confrim.SetRequest(request);
                 result.Confirmation20 = confrim;
 
@@ -322,14 +291,6 @@ namespace EVCB_OCPP.WSServer.Message
 
         }
 
-        private I20Request UnPackPayloadbyCallError(EVCB_OCPP.Packet20.Messages.Basic.Queue requestQueue, string uniqueId)
-        {
-            I20Request sentMsg = requestQueue.RestoreRequest(uniqueId);
-
-            return sentMsg;
-
-        }
-
 
 
         #endregion

+ 2 - 4
EVCB_OCPP.WSServer/Program.cs

@@ -1,6 +1,6 @@
 using EVCB_OCPP.Packet.Features;
 using EVCB_OCPP.Packet.Messages.Basic;
-using EVCB_OCPP.Packet20.Messages;
+
 using EVCB_OCPP.WSServer.Message;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
@@ -16,9 +16,7 @@ namespace EVCB_OCPP.WSServer
     {
         static void Main(string[] args)
         {
-            string tt = "{  \"connectorId\": 1,  \"evseId\": 1,  \"connectorStatus\": \"Occupied\",  \"timestamp\": \"2020-10-28T08:28:47Z\"}";
-
-          var gg=  JsonConvert.DeserializeObject<StatusNotificationRequest>(tt);
+           
 
             ProtalServer s = new ProtalServer();
             Console.WriteLine("Starting Server...");

+ 130 - 86
EVCB_OCPP.WSServer/ProtalServer.cs

@@ -35,7 +35,7 @@ using EVCB_OCPP.Packet.Messages.SmartCharging;
 using System.Security.Authentication;
 using NLog.Fluent;
 using System.Diagnostics;
-using Packet20 = EVCB_OCPP.Packet20;
+
 
 namespace EVCB_OCPP.WSServer
 {
@@ -100,7 +100,7 @@ namespace EVCB_OCPP.WSServer
         };
         private CancellationTokenSource _cts = new CancellationTokenSource();
         private CancellationToken _ct;
-
+        private string _ocpp20NetworkSetting = "";
 
         internal ProtalServer()
         {
@@ -134,6 +134,22 @@ namespace EVCB_OCPP.WSServer
             Task serverHealthTask = new Task(HealthCheckTrigger, _ct);
             serverHealthTask.Start();
 
+            var ocpp20NetworkSetting = new EVCB_OCPP20.Packet.Messages.SetNetworkProfileRequest()
+            {
+                ConfigurationSlot = 0,
+                ConnectionData = new EVCB_OCPP20.Packet.DataTypes.NetworkConnectionProfileType()
+                {
+                    OcppVersion = EVCB_OCPP20.Packet.DataTypes.EnumTypes.OCPPVersionEnumType.OCPP20,
+                    OcppTransport = EVCB_OCPP20.Packet.DataTypes.EnumTypes.OCPPTransportEnumType.JSON,
+                    MessageTimeout = 30,
+                    OcppCsmsUrl = "ws://ocpp.phihong.com.tw:5004",
+                    OcppInterface = EVCB_OCPP20.Packet.DataTypes.EnumTypes.OCPPInterfaceEnumType.Wired0
+                }
+
+            };
+
+            _ocpp20NetworkSetting = JsonConvert.SerializeObject(ocpp20NetworkSetting, Formatting.None);
+
             while (true)
             {
                 var input = Console.ReadLine();
@@ -302,13 +318,13 @@ namespace EVCB_OCPP.WSServer
             }
         }
 
-        
+
 
         private void OpenNetwork()
         {
 
             //載入OCPP Protocol
-            var appServer = new OCPPWSServer(new List<OCPPSubProtocol>() { new OCPPSubProtocol(), new OCPPSubProtocol(" ocpp1.6")});
+            var appServer = new OCPPWSServer(new List<OCPPSubProtocol>() { new OCPPSubProtocol(), new OCPPSubProtocol(" ocpp1.6"), new OCPPSubProtocol("ocpp2.0") });
 
             List<IListenerConfig> llistener = new List<IListenerConfig>();
             //System.Net.IPAddress.Any.ToString()
@@ -373,7 +389,7 @@ namespace EVCB_OCPP.WSServer
             {
                 lock (_lockClientDic)
                 {
-                    bool isNotSupported = session.SecWebSocketProtocol.Contains("ocpp1.6") ?false : session.SecWebSocketProtocol.Contains("ocpp2.0") ? false : true;
+                    bool isNotSupported = session.SecWebSocketProtocol.Contains("ocpp1.6") ? false : session.SecWebSocketProtocol.Contains("ocpp2.0") ? false : true;
                     if (isNotSupported)
                     {
                         //logger.Debug(string.Format("ChargeBoxId:{0} SecWebSocketProtocol:{1} NotSupported", session.ChargeBoxId, session.SecWebSocketProtocol));
@@ -430,10 +446,10 @@ namespace EVCB_OCPP.WSServer
                 WriteMachineLog(session, rawdata,
                      string.Format("{0} {1}", string.IsNullOrEmpty(analysisResult.Action) ? "unknown" : analysisResult.Action, analysisResult.Id == 2 ? "Request" : (analysisResult.Id == 3 ? "Confirmation" : "Error")), analysisResult.Exception == null ? "" : analysisResult.Exception.Message);
 
-                if(session.ResetSecurityProfile)
+                if (session.ResetSecurityProfile)
                 {
                     logger.Error(string.Format("[{0}] ChargeBoxId:{1} ResetSecurityProfile", DateTime.Now, session.ChargeBoxId));
-                    RemoveClient(session);                   
+                    RemoveClient(session);
                     return;
                 }
 
@@ -478,107 +494,59 @@ namespace EVCB_OCPP.WSServer
                 else
                 {
 
-                    Actions action = Convertor.GetAction(analysisResult.Action);
 
-                    if (!session.IsOCPP16 && !(action != Actions.BootNotification || action != Actions.Heartbeat))
-                    {
-                        Send(session, "Backend doesn't support this message.", string.Format("{0} {1}", analysisResult.Action, "Error"));
-                        return;
-                    }
+
+                    //if (session.ISOCPP20 && !(analysisResult.Action != Actions.BootNotification.ToString() || analysisResult.Action != Actions.Heartbeat.ToString()))
+                    //{
+                    //    Send(session, "Backend doesn't support this message.", string.Format("{0} {1}", analysisResult.Action, "Error"));
+                    //    return;
+                    //}
                     switch (analysisResult.Id)
                     {
                         case BasicMessageHandler.TYPENUMBER_CALL:
                             {
-                                if (session.IsOCPP16)
+                                if (!session.ISOCPP20)
                                 {
+                                    Actions action = Convertor.GetAction(analysisResult.Action);
                                     ProcessRequestMessage(analysisResult, session, action);
                                 }
                                 else
                                 {
+                                    EVCB_OCPP20.Packet.Features.Actions action = Convertor.GetActionby20(analysisResult.Action);
                                     MessageResult result = new MessageResult() { Success = true };
                                     //ocpp20 處理
                                     switch (action)
                                     {
-                                        case Actions.Heartbeat:
-                                            {
-                                                var confirm = new Packet20.Messages.HeartbeatResponse() { CurrentTime = DateTime.Now };
-                                                result.Message = confirm;
-                                                result.Success = true;
 
-                                                string response = BasicMessageHandler.GenerateConfirmationofOCPP20(analysisResult.UUID, (Packet20.Messages.IConfirmation)result.Message);
-                                                Send(session, response, string.Format("{0} {1}", analysisResult.Action, "Response"), result.Exception == null ? string.Empty : result.Exception.ToString());
-                                            }
-                                            break;
-                                        case Actions.BootNotification:
+                                        case EVCB_OCPP20.Packet.Features.Actions.BootNotification:
                                             {
-                                                Packet20.Messages.BootNotificationRequest _request = (Packet20.Messages.IRequest)analysisResult.Message as Packet20.Messages.BootNotificationRequest;
-                                                int heartbeat_interval = GlobalConfig.GetHEARTBEAT_INTERVAL();
-                                                //using (var db = new MainDBContext())
-                                                //{
-                                                //    var _machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId).FirstOrDefault();
-                                                //    _machine.ChargeBoxSerialNumber = string.IsNullOrEmpty(_request.chargeBoxSerialNumber) ? string.Empty : _request.chargeBoxSerialNumber;
-                                                //    _machine.ChargePointModel = string.IsNullOrEmpty(_request.chargePointModel) ? string.Empty : _request.chargePointModel;
-                                                //    _machine.ChargePointVendor = string.IsNullOrEmpty(_request.chargePointVendor) ? string.Empty : _request.chargePointVendor;
-                                                //    _machine.FW_CurrentVersion = string.IsNullOrEmpty(_request.firmwareVersion) ? string.Empty : _request.firmwareVersion;
-                                                //    _machine.Iccid = string.IsNullOrEmpty(_request.iccid) ? string.Empty : _request.iccid;
-                                                //    _machine.Imsi = string.IsNullOrEmpty(_request.imsi) ? string.Empty : _request.imsi;
-                                                //    _machine.MeterSerialNumber = string.IsNullOrEmpty(_request.meterSerialNumber) ? string.Empty : _request.meterSerialNumber;
-                                                //    _machine.MeterType = string.IsNullOrEmpty(_request.meterType) ? string.Empty : _request.meterType;
-
-                                                //    db.SaveChanges();
-
-                                                //    var configVaule = db.MachineConfiguration.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.HeartbeatInterval)
-                                                //        .Select(x => x.ConfigureSetting).FirstOrDefault();
-
-                                                //    if (configVaule != null)
-                                                //    {
-                                                //        int.TryParse(configVaule, out heartbeat_interval);
-                                                //    }
-                                                //}
-                                                var confirm = new Packet20.Messages.BootNotificationResponse() { CurrentTime = DateTime.Now, Interval = heartbeat_interval, Status = Packet20.DataTypes.EnumTypes.RegistrationStatusEnumType.Accepted };
+                                                EVCB_OCPP20.Packet.Messages.BootNotificationRequest _request = (EVCB_OCPP20.Packet.Messages.IRequest)analysisResult.Message as EVCB_OCPP20.Packet.Messages.BootNotificationRequest;
+
+                                                var confirm = new EVCB_OCPP20.Packet.Messages.BootNotificationResponse() { CurrentTime = DateTime.Now, Interval = 180, Status = EVCB_OCPP20.Packet.DataTypes.EnumTypes.RegistrationStatusEnumType.Pending };
 
                                                 result.Message = confirm;
                                                 result.Success = true;
 
-                                                string response = BasicMessageHandler.GenerateConfirmationofOCPP20(analysisResult.UUID, (Packet20.Messages.IConfirmation)result.Message);
+                                                string response = BasicMessageHandler.GenerateConfirmationofOCPP20(analysisResult.UUID, (EVCB_OCPP20.Packet.Messages.IConfirmation)result.Message);
                                                 Send(session, response, string.Format("{0} {1}", analysisResult.Action, "Response"), result.Exception == null ? string.Empty : result.Exception.ToString());
 
-                                            }
-                                            break;
-                                        case Actions.StatusNotification:
-                                            {
-                                                Packet20.Messages.StatusNotificationRequest _request = (Packet20.Messages.IRequest)analysisResult.Message as Packet20.Messages.StatusNotificationRequest;
-                                               // int heartbeat_interval = GlobalConfig.GetHEARTBEAT_INTERVAL();
-                                                //using (var db = new MainDBContext())
-                                                //{
-                                                //    var _machine = db.Machine.Where(x => x.ChargeBoxId == session.ChargeBoxId).FirstOrDefault();
-                                                //    _machine.ChargeBoxSerialNumber = string.IsNullOrEmpty(_request.chargeBoxSerialNumber) ? string.Empty : _request.chargeBoxSerialNumber;
-                                                //    _machine.ChargePointModel = string.IsNullOrEmpty(_request.chargePointModel) ? string.Empty : _request.chargePointModel;
-                                                //    _machine.ChargePointVendor = string.IsNullOrEmpty(_request.chargePointVendor) ? string.Empty : _request.chargePointVendor;
-                                                //    _machine.FW_CurrentVersion = string.IsNullOrEmpty(_request.firmwareVersion) ? string.Empty : _request.firmwareVersion;
-                                                //    _machine.Iccid = string.IsNullOrEmpty(_request.iccid) ? string.Empty : _request.iccid;
-                                                //    _machine.Imsi = string.IsNullOrEmpty(_request.imsi) ? string.Empty : _request.imsi;
-                                                //    _machine.MeterSerialNumber = string.IsNullOrEmpty(_request.meterSerialNumber) ? string.Empty : _request.meterSerialNumber;
-                                                //    _machine.MeterType = string.IsNullOrEmpty(_request.meterType) ? string.Empty : _request.meterType;
-
-                                                //    db.SaveChanges();
-
-                                                //    var configVaule = db.MachineConfiguration.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.HeartbeatInterval)
-                                                //        .Select(x => x.ConfigureSetting).FirstOrDefault();
-
-                                                //    if (configVaule != null)
-                                                //    {
-                                                //        int.TryParse(configVaule, out heartbeat_interval);
-                                                //    }
-                                                //}
-                                                var confirm = new Packet20.Messages.StatusNotificationResponse() {   };
 
-                                                result.Message = confirm;
-                                                result.Success = true;
+                                                string changeServerRequest = BasicMessageHandler.GenerateRequestofOCPP20(Guid.NewGuid().ToString(), "SetNetworkProfile", new EVCB_OCPP20.Packet.Messages.SetNetworkProfileRequest()
+                                                {
+                                                    ConfigurationSlot = 0,
+                                                    ConnectionData = new EVCB_OCPP20.Packet.DataTypes.NetworkConnectionProfileType()
+                                                    {
+                                                        OcppVersion = EVCB_OCPP20.Packet.DataTypes.EnumTypes.OCPPVersionEnumType.OCPP20,
+                                                        OcppTransport = EVCB_OCPP20.Packet.DataTypes.EnumTypes.OCPPTransportEnumType.JSON,
+                                                        MessageTimeout = 30,
+                                                        OcppCsmsUrl = "ws://ocpp.phihong.com.tw:5004",
+                                                        OcppInterface = EVCB_OCPP20.Packet.DataTypes.EnumTypes.OCPPInterfaceEnumType.Wired0
+                                                    }
 
-                                                string response = BasicMessageHandler.GenerateConfirmationofOCPP20(analysisResult.UUID, (Packet20.Messages.IConfirmation)result.Message);
-                                                Send(session, response, string.Format("{0} {1}", analysisResult.Action, "Response"), result.Exception == null ? string.Empty : result.Exception.ToString());
+                                                }
+                                                 );
 
+                                                Send(session, changeServerRequest, "SetNetworkProfile");
                                             }
                                             break;
                                         default:
@@ -595,8 +563,82 @@ namespace EVCB_OCPP.WSServer
                             break;
                         case BasicMessageHandler.TYPENUMBER_CALLRESULT:
                             {
+                                if (!session.ISOCPP20)
+                                {
+                                    Actions action = Convertor.GetAction(analysisResult.Action);
+                                    ProcessConfirmationMessage(analysisResult, session, action);
+                                }
+                                else
+                                {
+                                    EVCB_OCPP20.Packet.Features.Actions action = Convertor.GetActionby20(analysisResult.Action);
+                                    MessageResult result = new MessageResult() { Success = true };
+                                    //ocpp20 處理
+                                    switch (action)
+                                    {
+
+                                        case EVCB_OCPP20.Packet.Features.Actions.SetNetworkProfile:
+                                            {
+                                                EVCB_OCPP20.Packet.Messages.SetNetworkProfileResponse response = (EVCB_OCPP20.Packet.Messages.IConfirmation)analysisResult.Message as EVCB_OCPP20.Packet.Messages.SetNetworkProfileResponse;
+
+                                                if (response.Status == EVCB_OCPP20.Packet.DataTypes.EnumTypes.SetNetworkProfileStatusEnumType.Accepted)
+                                                {
+                                                    var request = new EVCB_OCPP20.Packet.Messages.SetVariablesRequest()
+                                                    {
+                                                        SetVariableData = new List<EVCB_OCPP20.Packet.DataTypes.SetVariableDataType>()
+                                                         {
+                                                              new EVCB_OCPP20.Packet.DataTypes.SetVariableDataType()
+                                                              {
+                                                                    Component=new EVCB_OCPP20.Packet.DataTypes.ComponentType()
+                                                                    {
+                                                                         Name="OCPPCommCtrlr",
+
+                                                                    },
+                                                                     AttributeType= EVCB_OCPP20.Packet.DataTypes.EnumTypes.AttributeEnumType.Actual,
+                                                                     AttributeValue= _ocpp20NetworkSetting,
+                                                                     Variable=new EVCB_OCPP20.Packet.DataTypes.VariableType()
+                                                                    {
+                                                                            Name="NetworkConfigurationPriority",
+
+                                                                    }
+
+
+                                                              }
+                                                         }
+
+                                                    };
+                                                    var uuid = session.queue20.store(request);
+                                                    string requestText = BasicMessageHandler.GenerateRequestofOCPP20(uuid, "SetVariables", request);                                         
+                                                    Send(session, requestText, "SetVariables");
+                                                }
+
+                                            }
+                                            break;
+                                        case EVCB_OCPP20.Packet.Features.Actions.SetVariables:
+                                            {
+                                                EVCB_OCPP20.Packet.Messages.SetVariablesResponse response = (EVCB_OCPP20.Packet.Messages.IConfirmation)analysisResult.Message as EVCB_OCPP20.Packet.Messages.SetVariablesResponse;
+
+                                                if (response.SetVariableResult[0].AttributeStatus ==  EVCB_OCPP20.Packet.DataTypes.EnumTypes.SetVariableStatusEnumType.RebootRequired )
+                                                {
+                                                    var request = new EVCB_OCPP20.Packet.Messages.ResetRequest()
+                                                    {
+                                                        Type = EVCB_OCPP20.Packet.DataTypes.EnumTypes.ResetEnumType.OnIdle
+
+                                                    };
+                                                    var uuid = session.queue20.store(request);
+                                                    string requestText = BasicMessageHandler.GenerateRequestofOCPP20(uuid, "Reset", request);
+                                                    Send(session, requestText, "Reset");
+                                                 
+                                                }
+                                            }
+                                            break;
+                                        default:
+                                            {
+                                                logger.Error(string.Format("We don't implement messagetype:{0} of raw data :{1} by {2}", analysisResult.Id, rawdata, session.ChargeBoxId));
+                                            }
+                                            break;
+                                    }
+                                }
 
-                                ProcessConfirmationMessage(analysisResult, session, action);
                             }
                             break;
                         case BasicMessageHandler.TYPENUMBER_CALLERROR:
@@ -604,6 +646,7 @@ namespace EVCB_OCPP.WSServer
                                 //只處理 丟出Request 收到Error的訊息                              
                                 if (analysisResult.Success && analysisResult.Message != null)
                                 {
+                                    Actions action = Convertor.GetAction(analysisResult.Action);
                                     ProcessErrorMessage(analysisResult, session, action);
                                 }
 
@@ -815,6 +858,7 @@ namespace EVCB_OCPP.WSServer
 
         private void ProcessConfirmationMessage(MessageResult analysisResult, ClientData session, Actions action)
         {
+
             BasicMessageHandler msgAnalyser = new BasicMessageHandler();
             if (ReConfirmMessage(analysisResult))
             {
@@ -1003,7 +1047,7 @@ namespace EVCB_OCPP.WSServer
                                     string requestId = Guid.NewGuid().ToString();
                                     // using (var db = new MainDBContext())
 
-                                    if (session.IsCheckIn && session.IsOCPP16)
+                                    if (session.IsCheckIn && !session.ISOCPP20)
                                     {
 
                                         var _request = new TriggerMessageRequest()
@@ -1126,7 +1170,7 @@ namespace EVCB_OCPP.WSServer
                             {
                                 Console.WriteLine(string.Format("charger_SN:{0} startDt:{1} CreatedOn:{2}", charger_SN, startDt.ToString("yyyy/MM/dd HH:mm:ss"), DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
 
-                                if (session.IsCheckIn && session.IsOCPP16)
+                                if (session.IsCheckIn && !session.ISOCPP20)
                                 {
                                     var cmdList = commandList.Where(c => c.ChargeBoxId == charger_SN).ToList();
 

+ 3 - 1
EVCB_OCPP.WSServer/SuperSocket.Protocol/ClientData.cs

@@ -13,6 +13,8 @@ namespace OCPPServer.Protocol
       /// </summary>
         public Queue queue = new Queue();
 
+        public EVCB_OCPP20.Packet.Messages.Basic.Queue queue20 = new EVCB_OCPP20.Packet.Messages.Basic.Queue();
+
         public bool IsCheckIn { set; get; }
 
         public string ChargeBoxId { set; get; }
@@ -21,7 +23,7 @@ namespace OCPPServer.Protocol
 
         public string MachineId { set; get; }
 
-        public bool IsOCPP16 { set; get; }
+        public bool ISOCPP20 { set; get; }
 
         public bool ResetSecurityProfile { set; get; }
 

+ 8 - 12
EVCB_OCPP.WSServer/SuperSocket.Protocol/OCPPWSServer.cs

@@ -62,7 +62,7 @@ namespace OCPPServer.Protocol
 
         protected override bool ValidateHandshake(ClientData session, string origin)
         {
-            session.IsOCPP16 = session.SecWebSocketProtocol.ToLower().Contains("ocpp2.0") ? false : true;
+            session.ISOCPP20 = session.SecWebSocketProtocol.ToLower().Contains("ocpp2.0");
 
             int securityProfile = 0;
             string authorizationKey = string.Empty;
@@ -85,18 +85,14 @@ namespace OCPPServer.Protocol
                 session.MachineId = machine == null ? String.Empty : machine.Id;
                 isExistedSN = machine == null ? false : true;
 
-                if (session.IsOCPP16)
-                {
-                    var configVaule = db.MachineConfiguration.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.SecurityProfile)
-                                   .Select(x => x.ConfigureSetting).FirstOrDefault();
-                    int.TryParse(configVaule, out securityProfile);
-
-                    if (securityProfile == 1 || securityProfile == 2)
-                    {
-                        authorizationKey = db.MachineConfiguration.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.AuthorizationKey)
+                var configVaule = db.MachineConfiguration.Where(x => x.ChargeBoxId == session.ChargeBoxId && x.ConfigureName == StandardConfiguration.SecurityProfile)
                                   .Select(x => x.ConfigureSetting).FirstOrDefault();
+                int.TryParse(configVaule, out securityProfile);
 
-                    }
+                if (session.ISOCPP20)
+                {
+                    // 1.6 server only support change server  function
+                    securityProfile = 0;
                 }
             }
             if (securityProfile == 3 && session.UriScheme == "ws")
@@ -138,7 +134,7 @@ namespace OCPPServer.Protocol
                 else
                 {
                     authorizated = true;
-                  
+
                 }