Repository navigation
platform sdk macros
A Platform SDK application can create, configure, start, and monitor macro entities. Calling Macro.Execute requests execution on a Security Center server; the Platform SDK application does not run the UserMacro code. For developing the UserMacro class, see the Macro SDK developer guide. The macro entity members are in Genetec.Sdk.dll; validating source code before setting it also uses Genetec.Sdk.Scripting.Compiler.dll and Genetec.Sdk.Scripting.Interfaces.dll.
Create the entity with CreateEntity and cast it to Macro. A new macro contains generated template source. The template's Execute method throws NotImplementedException, so replace the source before calling Execute. Passing a parent entity is not supported. The creation is committed immediately; to batch it with other changes, run it inside a transaction:
public Macro CreateMacro(Engine engine, string name)
{
return (Macro)engine.CreateEntity(
name, EntityType.Macro);
}SetSourceCode requires an administrator; other users get SdkException with SdkError.UnsufficientPrivilege and the message Only administrators can set source code of the macros. On a published macro, the source is compiled with the system's configured assembly references, and a compilation failure throws SdkException with SdkError.InvalidOperation and the message Source code contains compilation errors. Run compilation with MacroCompiler to get error details. On a draft macro, the source is stored without compiling. Read the stored source back through the get-only SourceCode property.
To obtain detailed compiler diagnostics before setting the source, compile it with the macro assembly references configured in Security Center:
public bool TrySetSource(
Engine engine, Macro macro, string source)
{
SystemConfiguration systemConfiguration =
engine.GetEntity<SystemConfiguration>(
SystemConfiguration.SystemConfigurationGuid);
IReadOnlyCollection<string> assemblyReferences =
systemConfiguration.GetMacroAssemblyReferences();
MacroCompiler compiler = new MacroCompiler();
ICompilationDetails details = compiler.Compile(
source,
assemblyReferences,
CodeLanguage.CsharpRoslyn);
if (!details.IsSuccess)
{
foreach (IError error in details.Errors)
{
Console.WriteLine(
$"{error.Line}:{error.Column} " +
error.Message);
}
return false;
}
macro.SetSourceCode(source);
return true;
}The macro's parameters are defined by its compiled source: every public property with a public setter whose type is int, uint, double, long, float, bool, string, Guid, or DateTime becomes a parameter. A property named PermissionSet is reserved and never becomes a parameter. Recompiling preserves the values of parameters that keep the same name and type.
The SetDefaultThreshold and RunWithThreshold examples assume that the compiled UserMacro class declares public int Threshold { get; set; }.
Read the values through DefaultParameters. The collection is read-only, so you cannot add or remove parameters, but each MacroParameter.Value is writable. Each read of DefaultParameters returns a new copy. Changing a value in that copy does not update the macro's saved defaults; pass the modified collection to SetDefaultParameters to save the changes. The string indexer throws KeyNotFoundException for an unknown name, and assigning a value of the wrong type throws InvalidCastException. SetDefaultParameters applies values whose names and types match the compiled parameters and ignores the rest. The change is committed immediately unless a transaction is active:
public void SetDefaultThreshold(Macro macro, int threshold)
{
Macro.ReadOnlyMacroParameterCollection parameters = macro.DefaultParameters;
parameters["Threshold"].Value = threshold;
macro.SetDefaultParameters(parameters);
}Execute() runs the macro with its default parameter values; Execute(parameters) runs it with the values you pass. Both submit the request without waiting for the macro to finish and return an execution identifier when Security Center accepts the request. If Security Center rejects the request without returning an execution identifier, the methods throw InvalidOperationException. This can occur, for example, when the logged-on user lacks the Execute macros privilege or the macro is a draft:
public Guid RunWithThreshold(Macro macro, int threshold)
{
Macro.ReadOnlyMacroParameterCollection parameters = macro.DefaultParameters;
parameters["Threshold"].Value = threshold;
return macro.Execute(parameters);
}Use the static Macro.MacroStarted, Macro.MacroCompleted, and Macro.MacroAborted events to monitor macro executions received by the Engine. To monitor a particular macro entity, use its Started, Completed, and Aborted instance events. Each event provides MacroEventArgs: MacroGuid identifies the macro, and InstanceGuid identifies the execution and matches the identifier returned by Execute.
Engine.GetMacroInstances() returns a snapshot of the running macro instances. Each MacroInstance provides Entity, ExecutionId, Instigator, and StartTime. AbortInstance starts an asynchronous abort request for one execution; it does not confirm that the execution has ended before returning. Handle the matching Aborted event or call GetMacroInstances() again to confirm that the execution identifier is no longer present:
public void RequestAbortForMacroInstances(Engine engine, Macro macro)
{
foreach (MacroInstance instance in engine.GetMacroInstances())
{
if (instance.Entity == macro.Guid)
{
macro.AbortInstance(instance.ExecutionId);
}
}
}Event-to-action rules run macros through the ExecuteMacroAction; see Event-to-actions.
IsDraft reports the state. Both SetMacroToDraft and PublishMacro require the logged-on user to be an administrator. SetMacroToDraft turns a macro into a draft, whose source is stored without compiling and which cannot be executed. PublishMacro recompiles the stored source; when publishing fails, the macro returns to draft. Both methods throw SdkException with SdkError.InvalidOperation and the message Feature is not yet available. when the system disables draft macros.
- Macro SDK overview: Config Tool procedures, execution contexts, and automation triggers.
- Macros in the Web SDK: Creating, starting, and stopping macros over the Web SDK.
- Overview
- Connecting to Security Center
- SDK certificates
- Referencing SDK assemblies
- ASP.NET hosting
- SDK compatibility
- Migrating to modern .NET
- Entities
- Entity cache
- Entity loading, cache, and query options
- Detecting entity changes
- Entity change event reference
- Transactions
- Security Center events
- Actions
- Security Desk
- Custom events
- Event-to-actions
- Managing macros
- ReportManager
- ReportManager query reference
- Access control raw events
- DownloadAllRelatedData and StrictResults
- Privileges
- Partitions
- Entity write restrictions
- About custom fields
- About video
- About cameras
- Enrolling a video unit
- Ingesting third-party video
- Archiver and auxiliary archiver roles
- Archive transfer
- About access control
- About cardholders and credentials
- About doors, areas, elevators, and access points
- About access rules and schedules
- About access control units and interface modules
- Enrolling an access control unit
- Door templates
- Visitors
- Mobile credentials
- About threat levels
- About alarms
- About incidents
- Maps
- Logging
- Overview
- Localization
- Certificates
- Lifecycle
- Threading
- State management
- Configuration
- Restricted configuration
- Events
- Queries
- Request manager
- Database
- Entity ownership
- Engine extensions
- Entity mappings
- Server management
- Custom privileges
- Custom entity types
- Resolving non-SDK assemblies
- Deploying plugins
- Modern .NET support
- Overview
- Localization
- Certificates
- Creating modules
- Tasks
- Pages
- Components
- Tile extensions
- Services
- Contextual actions
- Custom actions
- Options extensions
- Configuration pages
- Monitors
- Shared components
- Commands
- Extending events
- Map extensions
- Timeline providers
- Image extractors
- Credential encoders
- Credential readers
- Cardholder fields extractors
- Badge printers
- Content builders
- Dashboard widgets
- Incidents
- Logon providers
- Pinnable content builders
- Custom report pages
- Overview
- Getting started
- Media file formats
- MediaPlayer
- VideoSourceFilter
- MediaExporter
- MediaFile
- G64 converters
- FileCryptingManager
- PlaybackSequenceQuerier
- PlaybackStreamReader
- OverlayFactory
- PtzCoordinatesManager
- AudioTransmitter
- AudioRecorder
- AnalogMonitorController
- Camera blocking
- Overview
- Getting started
- Referencing entities
- Entity operations
- About access control in the Web SDK
- About video in the Web SDK
- Users and user groups
- Partitions
- Custom fields
- Custom card formats
- Actions
- Events and alarms
- Incidents
- Reports
- Tasks
- Macros
- Custom entity types
- System endpoints
- Performance guide
- Reference
- Endpoints
- Constants
- Enumerations
- Under the hood
- Troubleshooting