Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2020 Robert Bosch GmbH |
| 3 | * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| 4 | * |
| 5 | * This program and the accompanying materials are made available under the |
| 6 | * terms of the Eclipse Public License 2.0 which is available at |
| 7 | * http://www.eclipse.org/legal/epl-2.0 |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | *******************************************************************************/ |
| 11 | using BaSyx.API.AssetAdministrationShell; |
| 12 | using BaSyx.Models.Core.AssetAdministrationShell.Generics; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 13 | using BaSyx.Utils.ResultHandling; |
| 14 | using BaSyx.Utils.Client; |
| 15 | using System.Collections.Generic; |
| 16 | using System; |
| 17 | using Newtonsoft.Json; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 18 | using System.Linq; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 19 | using BaSyx.Models.Connectivity.Descriptors; |
| 20 | using BaSyx.Models.Core.Common; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 21 | using BaSyx.Models.Communication; |
| 22 | using System.Threading.Tasks; |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 23 | using NLog; |
| 24 | using System.Threading; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 25 | |
| 26 | namespace BaSyx.API.Components |
| 27 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 28 | /// <summary> |
| 29 | /// Reference implementation of ISubmodelServiceProvider interface |
| 30 | /// </summary> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 31 | public class SubmodelServiceProvider : ISubmodelServiceProvider |
| 32 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 33 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
| 34 | |
| 35 | private ISubmodel _submodel; |
| 36 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 37 | public ISubmodelDescriptor ServiceDescriptor { get; internal set; } |
| 38 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 39 | private readonly Dictionary<string, MethodCalledHandler> methodCalledHandler; |
| 40 | private readonly Dictionary<string, SubmodelElementHandler> submodelElementHandler; |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 41 | private readonly Dictionary<string, Action<IValue>> updateFunctions; |
| 42 | private readonly Dictionary<string, EventDelegate> eventDelegates; |
| 43 | private readonly Dictionary<string, InvocationResponse> invocationResults; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 44 | |
| 45 | private IMessageClient messageClient; |
| 46 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 47 | /// <summary> |
| 48 | /// Constructor for SubmodelServiceProvider |
| 49 | /// </summary> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 50 | public SubmodelServiceProvider() |
| 51 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 52 | methodCalledHandler = new Dictionary<string, MethodCalledHandler>(); |
| 53 | submodelElementHandler = new Dictionary<string, SubmodelElementHandler>(); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 54 | updateFunctions = new Dictionary<string, Action<IValue>>(); |
| 55 | eventDelegates = new Dictionary<string, EventDelegate>(); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 56 | invocationResults = new Dictionary<string, InvocationResponse>(); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 57 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 58 | /// <summary> |
| 59 | /// Contructor for SubmodelServiceProvider with a Submodel object to bind to |
| 60 | /// </summary> |
| 61 | /// <param name="submodel">Submodel object</param> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 62 | public SubmodelServiceProvider(ISubmodel submodel) : this() |
| 63 | { |
| 64 | BindTo(submodel); |
| 65 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 66 | /// <summary> |
| 67 | /// Contructor for SubmodelServiceProvider with a Submodel object to bind to and a SubmodelDescriptor as ServiceDescriptor |
| 68 | /// </summary> |
| 69 | /// <param name="submodel">Submodel object</param> |
| 70 | /// <param name="submodelDescriptor">SubmodelDescriptor object</param> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 71 | public SubmodelServiceProvider(ISubmodel submodel, ISubmodelDescriptor submodelDescriptor) : this() |
| 72 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 73 | _submodel = submodel; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 74 | ServiceDescriptor = submodelDescriptor; |
| 75 | } |
| 76 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 77 | /// <summary> |
| 78 | /// Bind this SubmodelServiceProvider to a specific Submodel |
| 79 | /// </summary> |
| 80 | /// <param name="submodel">Submodel object</param> |
| 81 | public void BindTo(ISubmodel submodel) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 82 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 83 | _submodel = submodel; |
| 84 | ServiceDescriptor = new SubmodelDescriptor(submodel, null); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 85 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 86 | |
| 87 | /// <summary> |
| 88 | /// Returns the model binding of this SubmodelServiceProvider |
| 89 | /// </summary> |
| 90 | /// <returns>Submodel object</returns> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 91 | public ISubmodel GetBinding() |
| 92 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 93 | return _submodel; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 96 | public void UseInMemorySubmodelElementHandler() |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 97 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 98 | UseInMemorySubmodelElementHandlerInternal(_submodel.SubmodelElements); |
| 99 | } |
| 100 | private void UseInMemorySubmodelElementHandlerInternal(IElementContainer<ISubmodelElement> submodelElements) |
| 101 | { |
| 102 | if (submodelElements.HasChildren()) |
| 103 | { |
| 104 | foreach (var child in submodelElements.Children) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 105 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 106 | UseInMemorySubmodelElementHandlerInternal(child); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 107 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 108 | } |
| 109 | if(submodelElements.Value != null) |
| 110 | { |
| 111 | if (submodelElements.Value.ModelType != ModelType.Operation) |
| 112 | RegisterSubmodelElementHandler(submodelElements.Path, new SubmodelElementHandler(submodelElements.Value.Get, submodelElements.Value.Set)); |
| 113 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 116 | /// <summary> |
| 117 | /// Use as specific SubmodelElementHandler for all SubmodelElements |
| 118 | /// </summary> |
| 119 | /// <param name="elementHandler">SubmodelElementHandler</param> |
| 120 | public void UseSubmodelElementHandler(SubmodelElementHandler elementHandler) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 121 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 122 | UseSubmodelElementHandlerInternal(_submodel.SubmodelElements, elementHandler, null); |
| 123 | } |
| 124 | /// <summary> |
| 125 | /// Use a specific SubmodelElementHandler for all SubmodelElements of a specific ModelType (e.g. Property) except Operations |
| 126 | /// </summary> |
| 127 | /// <param name="elementHandler">SubmodelElementHandler</param> |
| 128 | /// <param name="modelType">ModelType</param> |
| 129 | public void UseSubmodelElementHandlerForModelType(SubmodelElementHandler elementHandler, ModelType modelType) |
| 130 | { |
| 131 | UseSubmodelElementHandlerInternal(_submodel.SubmodelElements, elementHandler, modelType); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 134 | private void UseSubmodelElementHandlerInternal(IElementContainer<ISubmodelElement> submodelElements, SubmodelElementHandler elementHandler, ModelType modelType = null) |
| 135 | { |
| 136 | if (submodelElements.HasChildren()) |
| 137 | { |
| 138 | foreach (var child in submodelElements.Children) |
| 139 | { |
| 140 | UseSubmodelElementHandlerInternal(child, elementHandler, modelType); |
| 141 | } |
| 142 | } |
| 143 | if (submodelElements.Value != null) |
| 144 | { |
| 145 | if (modelType == null) |
| 146 | RegisterSubmodelElementHandler(submodelElements.Path, elementHandler); |
| 147 | else if (submodelElements.Value.ModelType == modelType) |
| 148 | RegisterSubmodelElementHandler(submodelElements.Path, elementHandler); |
| 149 | else |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | /// <summary> |
| 154 | /// Use a specific MethodCalledHandler for all Operations |
| 155 | /// </summary> |
| 156 | /// <param name="methodCalledHandler"></param> |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 157 | public void UseOperationHandler(MethodCalledHandler methodCalledHandler) |
| 158 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 159 | UseOperationHandlerInternal(_submodel.SubmodelElements, methodCalledHandler); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 162 | private void UseOperationHandlerInternal(IElementContainer<ISubmodelElement> submodelElements, MethodCalledHandler methodCalledHandler) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 163 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 164 | if (submodelElements.HasChildren()) |
| 165 | { |
| 166 | foreach (var child in submodelElements.Children) |
| 167 | { |
| 168 | UseOperationHandlerInternal(child, methodCalledHandler); |
| 169 | } |
| 170 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 171 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 172 | { |
| 173 | if (submodelElements.Value is IOperation) |
| 174 | RegisterMethodCalledHandler(submodelElements.Path, methodCalledHandler); |
| 175 | else |
| 176 | return; |
| 177 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 178 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 179 | |
| 180 | public MethodCalledHandler RetrieveMethodCalledHandler(string pathToOperation) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 181 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 182 | if (methodCalledHandler.TryGetValue(pathToOperation, out MethodCalledHandler handler)) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 183 | return handler; |
| 184 | else |
| 185 | return null; |
| 186 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 187 | |
| 188 | public SubmodelElementHandler RetrieveSubmodelElementHandler(string pathToElement) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 189 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 190 | if (submodelElementHandler.TryGetValue(pathToElement, out SubmodelElementHandler elementHandler)) |
| 191 | return elementHandler; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 192 | else |
| 193 | return null; |
| 194 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 195 | |
| 196 | public void RegisterSubmodelElementHandler(string pathToElement, SubmodelElementHandler elementHandler) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 197 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 198 | if (!submodelElementHandler.ContainsKey(pathToElement)) |
| 199 | submodelElementHandler.Add(pathToElement, elementHandler); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 200 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 201 | submodelElementHandler[pathToElement] = elementHandler; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 202 | } |
Constantin Ziesche | 09dcb6b | 2020-10-07 13:47:39 +0200 | [diff] [blame^] | 203 | |
| 204 | public void UnregisterSubmodelElementHandler(string pathToElement) |
| 205 | { |
| 206 | if (submodelElementHandler.ContainsKey(pathToElement)) |
| 207 | submodelElementHandler.Remove(pathToElement); |
| 208 | } |
| 209 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 210 | public void RegisterMethodCalledHandler(string pathToOperation, MethodCalledHandler handler) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 211 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 212 | if (!methodCalledHandler.ContainsKey(pathToOperation)) |
| 213 | methodCalledHandler.Add(pathToOperation, handler); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 214 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 215 | methodCalledHandler[pathToOperation] = handler; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 216 | } |
Constantin Ziesche | 09dcb6b | 2020-10-07 13:47:39 +0200 | [diff] [blame^] | 217 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 218 | public void RegisterEventDelegate(string pathToEvent, EventDelegate eventDelegate) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 219 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 220 | if (!eventDelegates.ContainsKey(pathToEvent)) |
| 221 | eventDelegates.Add(pathToEvent, eventDelegate); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 222 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 223 | eventDelegates[pathToEvent] = eventDelegate; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 226 | public IResult<InvocationResponse> InvokeOperation(string pathToOperation, InvocationRequest invocationRequest) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 227 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 228 | if (_submodel == null) |
| 229 | return new Result<InvocationResponse>(false, new NotFoundMessage("Submodel")); |
| 230 | |
| 231 | var operation_Retrieved = _submodel.SubmodelElements.Retrieve<IOperation>(pathToOperation); |
| 232 | if (operation_Retrieved.Success && operation_Retrieved.Entity != null) |
| 233 | { |
| 234 | MethodCalledHandler methodHandler; |
| 235 | if (operation_Retrieved.Entity.OnMethodCalled != null) |
| 236 | methodHandler = operation_Retrieved.Entity.OnMethodCalled; |
| 237 | else if (methodCalledHandler.TryGetValue(pathToOperation, out MethodCalledHandler handler)) |
| 238 | methodHandler = handler; |
| 239 | else |
| 240 | return new Result<InvocationResponse>(false, new NotFoundMessage($"MethodHandler for {pathToOperation}")); |
| 241 | |
| 242 | InvocationResponse invocationResponse = new InvocationResponse(invocationRequest.RequestId); |
| 243 | invocationResponse.InOutputArguments = invocationRequest.InOutputArguments; |
| 244 | |
| 245 | using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) |
| 246 | { |
| 247 | Task<OperationResult> runner = Task.Run(async () => |
| 248 | { |
| 249 | try |
| 250 | { |
| 251 | invocationResponse.ExecutionState = ExecutionState.Running; |
| 252 | var result = await methodHandler.Invoke(operation_Retrieved.Entity, invocationRequest.InputArguments, invocationResponse.InOutputArguments, invocationResponse.OutputArguments, cancellationTokenSource.Token); |
| 253 | invocationResponse.ExecutionState = ExecutionState.Completed; |
| 254 | return result; |
| 255 | } |
| 256 | catch (Exception e) |
| 257 | { |
| 258 | invocationResponse.ExecutionState = ExecutionState.Failed; |
| 259 | return new OperationResult(e); |
| 260 | } |
| 261 | |
| 262 | }, cancellationTokenSource.Token); |
| 263 | |
| 264 | if (Task.WhenAny(runner, Task.Delay(invocationRequest.Timeout.Value, cancellationTokenSource.Token)).Result == runner) |
| 265 | { |
| 266 | cancellationTokenSource.Cancel(); |
| 267 | invocationResponse.OperationResult = runner.Result; |
| 268 | return new Result<InvocationResponse>(true, invocationResponse); |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | cancellationTokenSource.Cancel(); |
| 273 | invocationResponse.OperationResult = new OperationResult(false, new TimeoutMessage()); |
| 274 | invocationResponse.ExecutionState = ExecutionState.Timeout; |
| 275 | return new Result<InvocationResponse>(false, invocationResponse); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | return new Result<InvocationResponse>(operation_Retrieved); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 280 | } |
| 281 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 282 | public IResult<CallbackResponse> InvokeOperationAsync(string pathToOperation, InvocationRequest invocationRequest) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 283 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 284 | if (_submodel == null) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 285 | return new Result<CallbackResponse>(false, new NotFoundMessage("Submodel")); |
| 286 | if (invocationRequest == null) |
| 287 | return new Result<CallbackResponse>(new ArgumentNullException(nameof(invocationRequest))); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 288 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 289 | var operation_Retrieved = _submodel.SubmodelElements.Retrieve<IOperation>(pathToOperation); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 290 | if (operation_Retrieved.Success && operation_Retrieved.Entity != null) |
| 291 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 292 | MethodCalledHandler methodHandler; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 293 | if (operation_Retrieved.Entity.OnMethodCalled != null) |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 294 | methodHandler = operation_Retrieved.Entity.OnMethodCalled; |
| 295 | else if (methodCalledHandler.TryGetValue(pathToOperation, out MethodCalledHandler handler)) |
| 296 | methodHandler = handler; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 297 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 298 | return new Result<CallbackResponse>(false, new NotFoundMessage($"MethodHandler for {pathToOperation}")); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 299 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 300 | Task invocationTask = Task.Run(async() => |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 301 | { |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 302 | InvocationResponse invocationResponse = new InvocationResponse(invocationRequest.RequestId); |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 303 | invocationResponse.InOutputArguments = invocationRequest.InOutputArguments; |
| 304 | SetInvocationResult(pathToOperation, invocationRequest.RequestId, ref invocationResponse); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 305 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 306 | using (var cancellationTokenSource = new CancellationTokenSource()) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 307 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 308 | Task<OperationResult> runner = Task.Run(async () => |
| 309 | { |
| 310 | try |
| 311 | { |
| 312 | invocationResponse.ExecutionState = ExecutionState.Running; |
| 313 | var result = await methodHandler.Invoke(operation_Retrieved.Entity, invocationRequest.InputArguments, invocationResponse.InOutputArguments, invocationResponse.OutputArguments, cancellationTokenSource.Token); |
| 314 | invocationResponse.ExecutionState = ExecutionState.Completed; |
| 315 | return result; |
| 316 | } |
| 317 | catch (Exception e) |
| 318 | { |
| 319 | invocationResponse.ExecutionState = ExecutionState.Failed; |
| 320 | return new OperationResult(e); |
| 321 | } |
| 322 | }, cancellationTokenSource.Token); |
| 323 | |
| 324 | if (await Task.WhenAny(runner, Task.Delay(invocationRequest.Timeout.Value, cancellationTokenSource.Token)) == runner) |
| 325 | { |
| 326 | cancellationTokenSource.Cancel(); |
| 327 | invocationResponse.OperationResult = runner.Result; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | cancellationTokenSource.Cancel(); |
| 332 | invocationResponse.OperationResult = new OperationResult(false, new TimeoutMessage()); |
| 333 | invocationResponse.ExecutionState = ExecutionState.Timeout; |
| 334 | } |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 335 | } |
| 336 | }); |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 337 | |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 338 | string endpoint = ServiceDescriptor?.Endpoints?.FirstOrDefault()?.Address; |
| 339 | CallbackResponse callbackResponse = new CallbackResponse(invocationRequest.RequestId); |
| 340 | if (string.IsNullOrEmpty(endpoint)) |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 341 | callbackResponse.CallbackUrl = new Uri($"/submodelElements/{pathToOperation}/invocationList/{invocationRequest.RequestId}", UriKind.Relative); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 342 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 343 | callbackResponse.CallbackUrl = new Uri($"{endpoint}/submodelElements/{pathToOperation}/invocationList/{invocationRequest.RequestId}", UriKind.Absolute); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 344 | return new Result<CallbackResponse>(true, callbackResponse); |
| 345 | } |
| 346 | return new Result<CallbackResponse>(operation_Retrieved); |
| 347 | } |
| 348 | |
| 349 | private void SetInvocationResult(string operationId, string requestId, ref InvocationResponse invocationResponse) |
| 350 | { |
| 351 | string key = string.Join("_", operationId, requestId); |
| 352 | if (invocationResults.ContainsKey(key)) |
| 353 | { |
| 354 | invocationResults[key] = invocationResponse; |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | invocationResults.Add(key, invocationResponse); |
| 359 | } |
| 360 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 361 | |
| 362 | public IResult<InvocationResponse> GetInvocationResult(string pathToOperation, string requestId) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 363 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 364 | string key = string.Join("_", pathToOperation, requestId); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 365 | if (invocationResults.ContainsKey(key)) |
| 366 | { |
| 367 | return new Result<InvocationResponse>(true, invocationResults[key]); |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | return new Result<InvocationResponse>(false, new NotFoundMessage($"Request with id {requestId}")); |
| 372 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 375 | public IResult ThrowEvent(IPublishableEvent publishableEvent, string topic = "/", Action<IMessagePublishedEventArgs> MessagePublished = null, byte qosLevel = 2, bool retain = false) |
| 376 | { |
| 377 | if (messageClient == null || !messageClient.IsConnected) |
| 378 | return new Result(false, new Message(MessageType.Warning, "MessageClient is not initialized or not connected")); |
| 379 | |
| 380 | if (publishableEvent == null) |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 381 | return new Result(new ArgumentNullException(nameof(publishableEvent))); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 382 | |
| 383 | if (eventDelegates.TryGetValue(publishableEvent.Name, out EventDelegate eventDelegate)) |
| 384 | eventDelegate.Invoke(this, publishableEvent); |
| 385 | |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 386 | string message = JsonConvert.SerializeObject(publishableEvent, Formatting.Indented); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 387 | return messageClient.Publish(topic, message, MessagePublished, qosLevel, retain); |
| 388 | } |
| 389 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 390 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 391 | public virtual void ConfigureEventHandler(IMessageClient messageClient) |
| 392 | { |
| 393 | this.messageClient = messageClient; |
| 394 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 395 | |
| 396 | public virtual void SubscribeUpdates(string pathToSubmodelElement, Action<IValue> updateFunction) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 397 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 398 | if (!updateFunctions.ContainsKey(pathToSubmodelElement)) |
| 399 | updateFunctions.Add(pathToSubmodelElement, updateFunction); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 400 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 401 | updateFunctions[pathToSubmodelElement] = updateFunction; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 402 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 403 | |
| 404 | public virtual void PublishUpdate(string pathToSubmodelElement, IValue value) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 405 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 406 | if (updateFunctions.TryGetValue(pathToSubmodelElement, out Action<IValue> updateFunction)) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 407 | updateFunction.Invoke(value); |
| 408 | |
| 409 | } |
| 410 | |
| 411 | public IResult<ISubmodel> RetrieveSubmodel() |
| 412 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 413 | return new Result<ISubmodel>(_submodel != null, _submodel); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Constantin Ziesche | 09dcb6b | 2020-10-07 13:47:39 +0200 | [diff] [blame^] | 416 | public IResult<ISubmodelElement> CreateOrUpdateSubmodelElement(string pathToSubmodelElement, ISubmodelElement submodelElement) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 417 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 418 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 419 | return new Result<ISubmodelElement>(false, new NotFoundMessage("Submodel")); |
| 420 | |
Constantin Ziesche | 09dcb6b | 2020-10-07 13:47:39 +0200 | [diff] [blame^] | 421 | var created = _submodel.SubmodelElements.CreateOrUpdate(pathToSubmodelElement, submodelElement); |
| 422 | if(created.Success && created.Entity != null) |
| 423 | RegisterSubmodelElementHandler(pathToSubmodelElement, new SubmodelElementHandler(submodelElement.Get, submodelElement.Set)); |
| 424 | return created; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | public IResult<IElementContainer<ISubmodelElement>> RetrieveSubmodelElements() |
| 428 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 429 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 430 | return new Result<ElementContainer<ISubmodelElement>>(false, new NotFoundMessage("Submodel")); |
| 431 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 432 | if (_submodel.SubmodelElements == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 433 | return new Result<ElementContainer<ISubmodelElement>>(false, new NotFoundMessage("SubmodelElements")); |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 434 | return _submodel.SubmodelElements.RetrieveAll(); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | public IResult<ISubmodelElement> RetrieveSubmodelElement(string submodelElementId) |
| 438 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 439 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 440 | return new Result<ISubmodelElement>(false, new NotFoundMessage("Submodel")); |
| 441 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 442 | if (_submodel.SubmodelElements == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 443 | return new Result<ISubmodelElement>(false, new NotFoundMessage(submodelElementId)); |
Constantin Ziesche | 8b4a64d | 2020-06-25 11:52:09 +0200 | [diff] [blame] | 444 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 445 | return _submodel.SubmodelElements.Retrieve(submodelElementId); |
Constantin Ziesche | 8b4a64d | 2020-06-25 11:52:09 +0200 | [diff] [blame] | 446 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 447 | public IResult<IValue> RetrieveSubmodelElementValue(string submodelElementId) |
| 448 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 449 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 450 | return new Result<IValue>(false, new NotFoundMessage("Submodel")); |
| 451 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 452 | if (submodelElementHandler.TryGetValue(submodelElementId, out SubmodelElementHandler elementHandler) && elementHandler.GetValueHandler != null) |
| 453 | { |
| 454 | var submodelElement = _submodel.SubmodelElements.Retrieve<ISubmodelElement>(submodelElementId); |
| 455 | if (submodelElement.Success && submodelElement.Entity != null) |
| 456 | return new Result<IValue>(true, elementHandler.GetValueHandler.Invoke(submodelElement.Entity)); |
| 457 | else |
| 458 | return new Result<IValue>(false, new Message(MessageType.Error, "SubmodelElement not found")); |
Constantin Ziesche | 09dcb6b | 2020-10-07 13:47:39 +0200 | [diff] [blame^] | 459 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 460 | else |
| 461 | return new Result<IValue>(false, new Message(MessageType.Error, "SubmodelElementHandler not found")); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 464 | public IResult UpdateSubmodelElementValue(string submodelElementId, IValue value) |
| 465 | { |
| 466 | if (_submodel == null) |
| 467 | return new Result(false, new NotFoundMessage("Submodel")); |
| 468 | |
| 469 | if (submodelElementHandler.TryGetValue(submodelElementId, out SubmodelElementHandler elementHandler) && elementHandler.SetValueHandler != null) |
| 470 | { |
| 471 | var submodelElement = _submodel.SubmodelElements.Retrieve<ISubmodelElement>(submodelElementId); |
| 472 | if (submodelElement.Success && submodelElement.Entity != null) |
| 473 | { |
| 474 | elementHandler.SetValueHandler.Invoke(submodelElement.Entity, value); |
| 475 | return new Result(true); |
| 476 | } |
| 477 | else |
| 478 | return new Result<IValue>(false, new Message(MessageType.Error, "property not found")); |
| 479 | } |
| 480 | else |
| 481 | return new Result<IValue>(false, new Message(MessageType.Error, "SubmodelElementHandler not found")); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | public IResult DeleteSubmodelElement(string submodelElementId) |
| 485 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 486 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 487 | return new Result(false, new NotFoundMessage("Submodel")); |
| 488 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 489 | if (_submodel.SubmodelElements == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 490 | return new Result(false, new NotFoundMessage(submodelElementId)); |
| 491 | |
Constantin Ziesche | 09dcb6b | 2020-10-07 13:47:39 +0200 | [diff] [blame^] | 492 | var deleted = _submodel.SubmodelElements.Delete(submodelElementId); |
| 493 | if (deleted.Success) |
| 494 | UnregisterSubmodelElementHandler(submodelElementId); |
| 495 | return deleted; |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 496 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 497 | } |
| 498 | } |