Skip to content

Commit

Permalink
Added CLI test command 'reset' for OCPP v1.6/v2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ahzf committed Oct 29, 2024
1 parent b351551 commit 3903df4
Show file tree
Hide file tree
Showing 8 changed files with 567 additions and 6 deletions.
12 changes: 9 additions & 3 deletions WWCP_OCPPv1.6/DataStructures/PredefinedStrings/ResetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,27 @@ public static Boolean IsNotNullOrEmpty(this ResetType? ResetType)
/// <summary>
/// Indicates whether this reset type is null or empty.
/// </summary>
public readonly Boolean IsNullOrEmpty
public readonly Boolean IsNullOrEmpty
=> InternalId.IsNullOrEmpty();

/// <summary>
/// Indicates whether this reset type is NOT null or empty.
/// </summary>
public readonly Boolean IsNotNullOrEmpty
public readonly Boolean IsNotNullOrEmpty
=> InternalId.IsNotNullOrEmpty();

/// <summary>
/// The length of the reset type.
/// </summary>
public readonly UInt64 Length
public readonly UInt64 Length
=> (UInt64) (InternalId?.Length ?? 0);

/// <summary>
/// All registered reset types.
/// </summary>
public static IEnumerable<ResetType> All
=> lookup.Values;

#endregion

#region Constructor(s)
Expand Down
28 changes: 28 additions & 0 deletions WWCP_OCPPv1.6_CentralSystem/CLI/CLICommands/DefaultStrings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2014-2024 GraphDefined GmbH <achim.friedland@graphdefined.com>
* This file is part of WWCP OCPP <https://github.com/OpenChargingCloud/WWCP_OCPP>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace cloud.charging.open.protocols.OCPPv1_6.CentralSystem.CommandLine
{

public static class DefaultStrings
{

public const String OCPPv1_6 = "OCPPv1.6";

}

}
147 changes: 147 additions & 0 deletions WWCP_OCPPv1.6_CentralSystem/CLI/CLICommands/ResetCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright (c) 2014-2024 GraphDefined GmbH <achim.friedland@graphdefined.com>
* This file is part of WWCP OCPP <https://github.com/OpenChargingCloud/WWCP_OCPP>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#region Usings

using org.GraphDefined.Vanaheimr.CLI;
using org.GraphDefined.Vanaheimr.Illias;

using cloud.charging.open.protocols.OCPPv1_6.CS;

#endregion

namespace cloud.charging.open.protocols.OCPPv1_6.CentralSystem.CommandLine
{

/// <summary>
/// Reset the current networking node
/// </summary>
/// <param name="CLI">The command line interface</param>
//[CLIContext([ DefaultStrings.OCPPv1_6 ])]
public class ResetCommand(ICentralSystemCLI CLI) : ACLICommand<ICentralSystemCLI>(CLI),
ICLICommand
{

#region Data

public static readonly String CommandName = nameof(ResetCommand)[..^7].ToLowerFirstChar();

#endregion

#region Suggest(Arguments)

public override IEnumerable<SuggestionResponse> Suggest(String[] Arguments)
{

// No suggestions without a defined RemoteSystemId and matching OCPP version!
if (!cli.RemoteSystemIdIsSet() ||
cli.GetRemoteSystemOCPPVersion() != DefaultStrings.OCPPv1_6)
{
return [];
}


if (Arguments.Length > 2 &&
CommandName.Equals(Arguments[0], StringComparison.OrdinalIgnoreCase))
{
Arguments = Arguments.Take(2).ToArray();
}


if (Arguments.Length == 2 &&
CommandName.Equals(Arguments[0], StringComparison.OrdinalIgnoreCase))
{

foreach (var resetType in ResetType.All)
{

if (resetType.ToString().Equals (Arguments[1], StringComparison.OrdinalIgnoreCase))
return [ SuggestionResponse.ParameterCompleted($"{Arguments[0]} {resetType.ToString().ToLower()}") ];

if (resetType.ToString().StartsWith(Arguments[1], StringComparison.OrdinalIgnoreCase))
return [ SuggestionResponse.ParameterPrefix ($"{Arguments[0]} {resetType.ToString().ToLower()}") ];

}

return [ SuggestionResponse.CommandCompleted(CommandName) ];

}


if (Arguments.Length == 1)
{

if (CommandName.Equals (Arguments[0], StringComparison.OrdinalIgnoreCase))
return [ SuggestionResponse.CommandHelp(Help()) ];

if (CommandName.StartsWith(Arguments[0], StringComparison.OrdinalIgnoreCase))
return [ SuggestionResponse.CommandCompleted(CommandName) ];

}

return [];

}

#endregion

#region Execute(Arguments, CancellationToken)

public override async Task<String[]> Execute(String[] Arguments,
CancellationToken CancellationToken)
{

// No execution without a defined RemoteSystemId!
var sourceRoute = cli.GetRemoteSystemSourceRoute();
if (sourceRoute is null)
return [];


if (Arguments.Length == 2 &&
ResetType.TryParse(Arguments[1], out var resetType))
{

var response = await cli.OCPP.OUT.Reset(
new ResetRequest(
Destination: sourceRoute,
ResetType: resetType
)
);

return [
$"{Arguments.AggregateWith(" ")} => {response.Runtime.TotalMilliseconds} ms",
response.ToJSON().ToString(Newtonsoft.Json.Formatting.Indented)
];

}

return [ $"Usage: {CommandName} <{ResetType.All.Select(_ => _.ToString()).AggregateWith("|")}>" ];

}

#endregion

#region Help()

public override String Help()
=> $"{CommandName} <{ResetType.All.Select(_ => _.ToString()).AggregateWith("|")}> - Reset the current networking node";

#endregion

}

}
100 changes: 100 additions & 0 deletions WWCP_OCPPv1.6_CentralSystem/CLI/ICentralSystemCLI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2014-2024 GraphDefined GmbH <achim.friedland@graphdefined.com>
* This file is part of WWCP OCPP <https://github.com/OpenChargingCloud/WWCP_OCPP>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#region Usings

using org.GraphDefined.Vanaheimr.CLI;

using cloud.charging.open.protocols.WWCP.NetworkingNode;
using cloud.charging.open.protocols.OCPPv1_6.NetworkingNode;

#endregion

namespace cloud.charging.open.protocols.OCPPv1_6.CentralSystem.CommandLine
{

public static class ICentralSystemCLIExtensions
{

public static Boolean RemoteSystemIdIsSet(this ICentralSystemCLI CLI)
{

if (CLI.Environment.TryGetValue(EnvironmentKey.RemoteSystemId, out var values) &&
values.Count > 0)
{
return true;
}

return false;

}

public static NetworkingNode_Id? GetRemoteSystemId(this ICentralSystemCLI CLI)
{

if (CLI.Environment.TryGetValue(EnvironmentKey.RemoteSystemId, out var values) &&
values.Count > 0 &&
NetworkingNode_Id.TryParse(values.First(), out var remoteSystemId))
{
return remoteSystemId;
}

return null;

}

public static SourceRouting? GetRemoteSystemSourceRoute(this ICentralSystemCLI CLI)
{

if (CLI.Environment.TryGetValue(EnvironmentKey.RemoteSystemId, out var values) &&
values.Count > 0 &&
NetworkingNode_Id.TryParse(values.First(), out var remoteSystemId))
{
return SourceRouting.To(remoteSystemId);
}

return null;

}

public static String? GetRemoteSystemOCPPVersion(this ICentralSystemCLI CLI)
{

if (CLI.Environment.TryGetValue(EnvironmentKey.RemoteSystemOCPPVersion, out var values) &&
values.Count > 0)
{
return values.First();
}

return null;

}

}


public interface ICentralSystemCLI : ICLI
{

OCPPAdapter OCPP { get; }

IEnumerable<NetworkingNode_Id> ConnectedNetworkingNodeIds { get; }


}

}
12 changes: 9 additions & 3 deletions WWCP_OCPPv2.1/DataStructures/PredefinedStrings/ResetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,27 @@ public static Boolean IsNotNullOrEmpty(this ResetType? ResetType)
/// <summary>
/// Indicates whether this reset type is null or empty.
/// </summary>
public readonly Boolean IsNullOrEmpty
public readonly Boolean IsNullOrEmpty
=> InternalId.IsNullOrEmpty();

/// <summary>
/// Indicates whether this reset type is NOT null or empty.
/// </summary>
public readonly Boolean IsNotNullOrEmpty
public readonly Boolean IsNotNullOrEmpty
=> InternalId.IsNotNullOrEmpty();

/// <summary>
/// The length of the reset type.
/// </summary>
public readonly UInt64 Length
public readonly UInt64 Length
=> (UInt64) (InternalId?.Length ?? 0);

/// <summary>
/// All registered reset types.
/// </summary>
public static IEnumerable<ResetType> All
=> lookup.Values;

#endregion

#region Constructor(s)
Expand Down
29 changes: 29 additions & 0 deletions WWCP_OCPPv2.1_CSMS/CLI/CLICommands/DefaultStrings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2014-2024 GraphDefined GmbH <achim.friedland@graphdefined.com>
* This file is part of WWCP OCPP <https://github.com/OpenChargingCloud/WWCP_OCPP>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace cloud.charging.open.protocols.OCPPv2_1.CSMS.CommandLine
{

public static class DefaultStrings
{

public const String OCPPv2_0_1 = "OCPPv2.0.1";
public const String OCPPv2_1 = "OCPPv2.1";

}

}
Loading

0 comments on commit 3903df4

Please sign in to comment.