-
Notifications
You must be signed in to change notification settings - Fork 24
/
ResourceManagementFacade.cs
189 lines (150 loc) · 6.11 KB
/
ResourceManagementFacade.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0
using System;
using System.Collections.Generic;
using Moryx.AbstractionLayer.Capabilities;
using Moryx.AbstractionLayer.Resources;
using Moryx.Runtime.Modules;
namespace Moryx.Resources.Management
{
internal class ResourceManagementFacade : IResourceManagement, IFacadeControl
{
#region Dependency Injection
public IResourceManager Manager { get; set; }
public IResourceGraph ResourceGraph { get; set; }
public IResourceTypeController TypeController { get; set; }
#endregion
#region IFacadeControl
/// <see cref="IFacadeControl.ValidateHealthState"/>
public Action ValidateHealthState { get; set; }
/// <seealso cref="IFacadeControl"/>
public void Activate()
{
Manager.ResourceAdded += OnResourceAdded;
Manager.CapabilitiesChanged += OnCapabilitiesChanged;
Manager.ResourceRemoved += OnResourceRemoved;
}
/// <seealso cref="IFacadeControl"/>
public void Deactivate()
{
Manager.ResourceAdded -= OnResourceAdded;
Manager.CapabilitiesChanged -= OnCapabilitiesChanged;
Manager.ResourceRemoved -= OnResourceRemoved;
}
private void OnCapabilitiesChanged(object sender, ICapabilities args)
{
CapabilitiesChanged?.Invoke(((IResource)sender).Proxify(TypeController), args);
}
private void OnResourceAdded(object sender, IResource publicResource)
{
ResourceAdded?.Invoke(this, publicResource.Proxify(TypeController));
}
private void OnResourceRemoved(object sender, IResource publicResource)
{
ResourceRemoved?.Invoke(this, publicResource.Proxify(TypeController));
}
#endregion
#region IResourceManagement
public TResource GetResource<TResource>() where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResource<TResource>().Proxify(TypeController);
}
public TResource GetResource<TResource>(long id)
where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResource<TResource>(id).Proxify(TypeController);
}
public TResource GetResource<TResource>(string name)
where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResource<TResource>(name).Proxify(TypeController);
}
public TResource GetResource<TResource>(ICapabilities requiredCapabilities)
where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResource<TResource>(r => requiredCapabilities.ProvidedBy(r.Capabilities)).Proxify(TypeController);
}
public TResource GetResource<TResource>(Func<TResource, bool> predicate)
where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResource(predicate).Proxify(TypeController);
}
public IEnumerable<TResource> GetResources<TResource>() where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResources<TResource>().Proxify(TypeController);
}
public IEnumerable<TResource> GetResources<TResource>(ICapabilities requiredCapabilities)
where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResources<TResource>(r => requiredCapabilities.ProvidedBy(r.Capabilities)).Proxify(TypeController);
}
public IEnumerable<TResource> GetResources<TResource>(Func<TResource, bool> predicate)
where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResources(predicate).Proxify(TypeController);
}
#endregion
#region IResourceModification
public long Create(Type resourceType, Action<Resource> initializer)
{
ValidateHealthState();
var resource = ResourceGraph.Instantiate(resourceType.ResourceType());
initializer(resource);
ResourceGraph.Save(resource);
return resource.Id;
}
/// <summary>
///
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="id"></param>
/// <param name="accessor"></param>
/// <returns>null, when a resource with this id doesn't exist</returns>
public TResult Read<TResult>(long id, Func<Resource, TResult> accessor)
{
ValidateHealthState();
var resource = ResourceGraph.Get(id);
var result = accessor(resource);
return result;
}
public void Modify(long id, Func<Resource, bool> modifier)
{
ValidateHealthState();
var resource = ResourceGraph.Get(id);
if (resource == null)
throw new KeyNotFoundException($"No resource with Id {id} found!");
var result = modifier(resource);
if (result)
ResourceGraph.Save(resource);
}
public bool Delete(long id)
{
ValidateHealthState();
var resource = ResourceGraph.Get(id);
if (resource == null)
return false;
return ResourceGraph.Destroy(resource);
}
public IEnumerable<TResource> GetAllResources<TResource>(Func<TResource, bool> predicate)
where TResource : class, IResource
{
ValidateHealthState();
return ResourceGraph.GetResources(predicate);
}
#endregion
/// <inheritdoc />
public event EventHandler<IResource> ResourceAdded;
/// <inheritdoc />
public event EventHandler<IResource> ResourceRemoved;
/// <inheritdoc />
public event EventHandler<ICapabilities> CapabilitiesChanged;
}
}