Skip to content

Commit

Permalink
ControlledUnits in Units.razor returns all items that are derived fro…
Browse files Browse the repository at this point in the history
…m UnitContainerBase (#18)

* ControlledUnits in Units.razor returns all items that are derived from UnitContainerBase

* simplification of getting all CUs in Units.razor

* create unit services automatically

* Added generation of the view of all cotrolled unit

* Default view generation call added

* Global modes button added
  • Loading branch information
TomKovac authored Oct 1, 2024
1 parent 35a4871 commit 6bcacda
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,99 @@
@page "/Context/"
@using AXOpen.VisualComposer;
@using AXSharp.Connector
@using AXOpen.Core;
@using AXOpen.Core.Blazor.AxoAlertDialog;
@using AXOpen.Base.Dialogs
@using axosimple.BaseUnit
@inject IAlertService _alerts

<div class="text-center p-2 fixed-top" style="z-index: 0;">
<button type="button" class="btn btn-primary" @onclick="Automat">@Localizer["AUTOMAT"]</button>
<button type="button" class="btn btn-primary" @onclick="Ground">@Localizer["GROUND"]</button>
<button type="button" class="btn btn-primary" @onclick="Service">@Localizer["SERVICE"]</button>
</div>

<VisualComposerContainer
Objects="new ITwinObject []{ Entry.Plc.Context, Entry.Plc.Inputs, Entry.Plc.Outputs }"
Id="ContextView"/>

@code {

}
@code
{
private async Task Automat()
{
_alerts.AddAlertDialog(eAlertType.Info, Localizer["Mode change"], $"{Localizer[$"Requesting automatic mode of selected units"]}.", 1);

var notGroundedStations = ControlledUnits.Where(p => p.GroundStatus != eAxoTaskState.Done);
var ungroundedStationsList = string.Join(",", notGroundedStations.Select(p => p.AttributeName));

if (notGroundedStations.Count() > 0)
{
_alerts.AddAlertDialog(eAlertType.Warning, Localizer["Mode change"],
$"{Localizer[$"Cannot start automatic mode on unit(s)"]} `{ungroundedStationsList}`. Ground must be executed successfully first.", 5);
return;
}

foreach (var a in ControlledUnits)
{
await a.AutomatTask.ExecuteAsync();
}

_alerts.AddAlertDialog(eAlertType.Success, Localizer["Mode change"], $"{Localizer[$"Automatic mode of selected units"]} has been started.", 3);
}

private async Task Ground()
{
_alerts.AddAlertDialog(eAlertType.Info, Localizer["Mode change"], $"{Localizer[$"Requesting ground mode of selected units"]}.", 1);
int expectedTimeToExecute = 20000;
ControlledUnits.ToList().ForEach(async a => await a.GroundTask.ExecuteAsync());

var sw = new System.Diagnostics.Stopwatch();
sw.Start();
await Task.Run(async () =>
{
foreach (var a in ControlledUnits)
{
while (a.GroundStatus != eAxoTaskState.Done
&& sw.ElapsedMilliseconds <= expectedTimeToExecute)
{
await Task.Delay(25);
}
}

});

if (sw.ElapsedMilliseconds > expectedTimeToExecute)
{
var pp = string.Join(",\n", ControlledUnits
.Where(p => p.GroundStatus != eAxoTaskState.Done)
.Select(p => p.AttributeName));

_alerts.AddAlertDialog(eAlertType.Danger,
Localizer["Mode change"],
$"{Localizer[$"Ground mode did not succeed on unit"]} " +
$"{pp}", 5);

return;
}

_alerts.AddAlertDialog(eAlertType.Success,
Localizer["Mode change"],
$"{Localizer[$"Ground mode of selected unit(s)"]} was successfully executed.", 3);

this.StateHasChanged();
}

private void Service()
{
ControlledUnits.ToList().ForEach(async a => await a.ServiceTask.ExecuteAsync());
this.StateHasChanged();
}

private UnitContainerBase[] ControlledUnits
{
get
{
return (Entry.Plc.Context.GetChildren().Where(o => o is UnitContainerBase)).Cast<UnitContainerBase>().ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,9 @@

private UnitContainerBase[] ControlledUnits
{
get
get
{
return new UnitContainerBase[]
{
//Entry.Plc.Context.UnitTemplate,
};
return (Entry.Plc.Context.GetChildren().Where(o => o is UnitContainerBase)).Cast<UnitContainerBase>().ToArray();
}
}

}
65 changes: 64 additions & 1 deletion axopen.template.simple/axpansion/server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
using System.Diagnostics;
using System.Runtime.InteropServices.JavaScript;
using AXOpen.VisualComposer;

using axosimple.BaseUnit;
using System.Net;
using AXOpen.VisualComposer.Types;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -66,6 +68,9 @@
// Register the unit services
CreateUnitServices();

// Default view generation
CreateDefaultUnitsView();

// Clean Temp directory
IAxoDataExchange.CleanUp();

Expand Down Expand Up @@ -120,8 +125,66 @@ static void CreateUnitServices()
{
var contextService = ContextService.Instance;
// axosimple.UnitTemplate.UnitServices.Create(ContextService.Instance);

Assembly? assembly = Assembly.GetAssembly(typeof(axosimpleTwinController));


if (contextService != null && assembly != null)
{
IEnumerable<Type> unitServices = assembly.GetTypes().Where(t => typeof(IUnitServices).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);

foreach (Type unitService in unitServices)
{
if (unitService.FullName != null)
{
Type classType = assembly.GetType(unitService.FullName);
if (classType != null)
{
MethodInfo? createMethod = classType.GetMethod("Create", BindingFlags.Public | BindingFlags.Static);
if (createMethod != null)
{
createMethod.Invoke(null, new object[] { contextService });
}
}
}
}
}
}

static async void CreateDefaultUnitsView()
{
List<ITwinObject> CUs = Entry.Plc.Context.GetChildren().Where(o => o is UnitContainerBase).ToList();
if (CUs != null && CUs.Count() > 0)
{
VisualComposerContainer VisualComposerContainer = new VisualComposerContainer();
VisualComposerContainer.Id = "ContextView";
VisualComposerContainer.FileName = "_generated";
VisualComposerContainer.CurrentView = "_generated";
VisualComposerContainer.BackgroundWidth = 100;
VisualComposerContainer.BackgroundHeight = 200;
VisualComposerContainer.ImgSrc = null;
VisualComposerContainer.BackgroundColor = "#027dfb";
VisualComposerContainer.BackgroundSVGInput = "";
double left = 1;
double top = 1;
TransformType transform = TransformType.TopLeft;

foreach (ITwinObject CU in CUs)
{
VisualComposerContainer.AddChildren(CU as ITwinElement, left, top, TransformType.TopLeft, "Spot", -1, -1, 10, 1, "", null, true, "#FFFFFF");
left = left + 25;
if (left > 76)
{
left = 1;
top = top + 13;
}

}
await VisualComposerContainer.SaveAsync();
}
}


static (IRepository<User>, IRepository<Group>) SetUpUserRepositories()
{
var MongoConnectionString = "mongodb://localhost:27017";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"Views": [
"A"
"A",
"_generated"
],
"DefaultView": "A"
"DefaultView": "_generated"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"BackgroundWidth": 100,
"BackgroundHeight": 200,
"ImgSrc": null,
"BackgroundColor": "#027dfb",
"BackgroundSVGInput": "",
"Items": [
{
"Id": "f122746ffacbbf3a0633a9695c646677467d58ed708ea07d46a16dea4f178979",
"Left": 42.71396103914338,
"Top": 6.913580246913581,
"Transform": "TopCenter",
"Presentation": "Spot",
"Width": -1,
"Height": -1,
"ZIndex": 10,
"Scale": 1,
"Roles": "",
"PresentationTemplate": null,
"Background": false,
"BackgroundColor": "#FFFFFF"
}
],
"Theme": "text-dark",
"Scale": 1,
"TranslateX": 0,
"TranslateY": 0,
"AllowZoomingAndPanning": false
}

0 comments on commit 6bcacda

Please sign in to comment.