/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using Opc.Ua;
using Opc.Ua.Generators;
namespace Generators
{
///
/// What each generator-set method means, expressed against the simulation
/// rather than against address-space nodes.
///
///
///
/// The method nodes are plumbing; this is the behaviour. Keeping the two apart
/// means the semantics can be held to account by a test without standing up a
/// server, and it keeps the node manager's handlers down to one line each.
///
///
/// Every command routes through ,
/// so legality is decided in exactly one place. A caller cannot drive a machine
/// from Off straight to Loaded by picking the right method.
///
///
internal static class GeneratorCommands
{
///
/// Starts a set that is shut down or waiting.
///
/// The set's simulation.
/// when the set began starting.
///
/// From Off this is two declared transitions, not one: a set becomes
/// available before it cranks. Collapsing them would publish a machine that
/// never passed through Ready.
///
public static bool Start(GeneratorSimulation simulation)
{
return simulation.State switch
{
GeneratorRunState.Off =>
simulation.RequestState(GeneratorRunState.Ready)
&& simulation.RequestState(GeneratorRunState.Starting),
GeneratorRunState.Ready => simulation.RequestState(GeneratorRunState.Starting),
_ => false,
};
}
///
/// Stops a running set through its cooldown.
///
/// The set's simulation.
/// when the set began stopping.
///
/// A normal stop always goes through cooldown; dropping a hot engine
/// straight to rest is what EmergencyStop is for.
///
public static bool Stop(GeneratorSimulation simulation)
{
return simulation.RequestState(GeneratorRunState.Cooldown);
}
///
/// Trips a set on the emergency stop.
///
/// The set's simulation.
/// when the set was stopped.
///
/// The model declares an emergency stop only out of the three states in
/// which the engine is turning at speed, so this is refused from
/// Starting and Warmup. That is the specification's shape, not
/// this sample's choice, and it is left visible rather than papered over.
///
public static bool EmergencyStop(GeneratorSimulation simulation)
{
return simulation.RequestState(GeneratorRunState.EmergencyStopped);
}
///
/// Clears a latched shutdown.
///
/// The set's simulation.
/// in every case that is not an error.
///
/// Resetting a healthy set is a no-op rather than an error. An operator
/// pressing reset on a machine that is running has not done anything wrong,
/// and answering Bad would only train them to ignore the result.
///
public static bool ResetFaults(GeneratorSimulation simulation)
{
return simulation.State is GeneratorRunState.Fault
or GeneratorRunState.EmergencyStopped
? simulation.RequestState(GeneratorRunState.Off)
: true;
}
///
/// Starts a set for a scheduled exercise run.
///
/// The set's simulation.
/// when the set began starting.
///
/// The same move as ; the difference a client observes is
/// the operating mode the caller publishes alongside it.
///
public static bool StartTest(GeneratorSimulation simulation)
{
return Start(simulation);
}
///
/// Reads a selector position out of a method's input arguments.
///
/// The call's input arguments.
/// Receives the requested mode.
/// when the argument named a real mode.
public static bool TryReadOperatingMode(
ArrayOf arguments,
out GeneratorOperatingModeEnum mode)
{
mode = default;
if (arguments.Count < 1 || !arguments[0].TryGetValue(out int requested))
{
return false;
}
if (!Enum.IsDefined(typeof(GeneratorOperatingModeEnum), requested))
{
return false;
}
mode = (GeneratorOperatingModeEnum)requested;
return true;
}
}
}