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 | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 203 | |
| 204 | public void RegisterMethodCalledHandler(string pathToOperation, MethodCalledHandler handler) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 205 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 206 | if (!methodCalledHandler.ContainsKey(pathToOperation)) |
| 207 | methodCalledHandler.Add(pathToOperation, handler); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 208 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 209 | methodCalledHandler[pathToOperation] = handler; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 210 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 211 | |
| 212 | public void RegisterEventDelegate(string pathToEvent, EventDelegate eventDelegate) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 213 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 214 | if (!eventDelegates.ContainsKey(pathToEvent)) |
| 215 | eventDelegates.Add(pathToEvent, eventDelegate); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 216 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 217 | eventDelegates[pathToEvent] = eventDelegate; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 220 | public IResult<InvocationResponse> InvokeOperation(string pathToOperation, InvocationRequest invocationRequest) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 221 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 222 | if (_submodel == null) |
| 223 | return new Result<InvocationResponse>(false, new NotFoundMessage("Submodel")); |
| 224 | |
| 225 | var operation_Retrieved = _submodel.SubmodelElements.Retrieve<IOperation>(pathToOperation); |
| 226 | if (operation_Retrieved.Success && operation_Retrieved.Entity != null) |
| 227 | { |
| 228 | MethodCalledHandler methodHandler; |
| 229 | if (operation_Retrieved.Entity.OnMethodCalled != null) |
| 230 | methodHandler = operation_Retrieved.Entity.OnMethodCalled; |
| 231 | else if (methodCalledHandler.TryGetValue(pathToOperation, out MethodCalledHandler handler)) |
| 232 | methodHandler = handler; |
| 233 | else |
| 234 | return new Result<InvocationResponse>(false, new NotFoundMessage($"MethodHandler for {pathToOperation}")); |
| 235 | |
| 236 | InvocationResponse invocationResponse = new InvocationResponse(invocationRequest.RequestId); |
| 237 | invocationResponse.InOutputArguments = invocationRequest.InOutputArguments; |
| 238 | |
| 239 | using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource()) |
| 240 | { |
| 241 | Task<OperationResult> runner = Task.Run(async () => |
| 242 | { |
| 243 | try |
| 244 | { |
| 245 | invocationResponse.ExecutionState = ExecutionState.Running; |
| 246 | var result = await methodHandler.Invoke(operation_Retrieved.Entity, invocationRequest.InputArguments, invocationResponse.InOutputArguments, invocationResponse.OutputArguments, cancellationTokenSource.Token); |
| 247 | invocationResponse.ExecutionState = ExecutionState.Completed; |
| 248 | return result; |
| 249 | } |
| 250 | catch (Exception e) |
| 251 | { |
| 252 | invocationResponse.ExecutionState = ExecutionState.Failed; |
| 253 | return new OperationResult(e); |
| 254 | } |
| 255 | |
| 256 | }, cancellationTokenSource.Token); |
| 257 | |
| 258 | if (Task.WhenAny(runner, Task.Delay(invocationRequest.Timeout.Value, cancellationTokenSource.Token)).Result == runner) |
| 259 | { |
| 260 | cancellationTokenSource.Cancel(); |
| 261 | invocationResponse.OperationResult = runner.Result; |
| 262 | return new Result<InvocationResponse>(true, invocationResponse); |
| 263 | } |
| 264 | else |
| 265 | { |
| 266 | cancellationTokenSource.Cancel(); |
| 267 | invocationResponse.OperationResult = new OperationResult(false, new TimeoutMessage()); |
| 268 | invocationResponse.ExecutionState = ExecutionState.Timeout; |
| 269 | return new Result<InvocationResponse>(false, invocationResponse); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | return new Result<InvocationResponse>(operation_Retrieved); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 276 | public IResult<CallbackResponse> InvokeOperationAsync(string pathToOperation, InvocationRequest invocationRequest) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 277 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 278 | if (_submodel == null) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 279 | return new Result<CallbackResponse>(false, new NotFoundMessage("Submodel")); |
| 280 | if (invocationRequest == null) |
| 281 | return new Result<CallbackResponse>(new ArgumentNullException(nameof(invocationRequest))); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 282 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 283 | var operation_Retrieved = _submodel.SubmodelElements.Retrieve<IOperation>(pathToOperation); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 284 | if (operation_Retrieved.Success && operation_Retrieved.Entity != null) |
| 285 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 286 | MethodCalledHandler methodHandler; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 287 | if (operation_Retrieved.Entity.OnMethodCalled != null) |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 288 | methodHandler = operation_Retrieved.Entity.OnMethodCalled; |
| 289 | else if (methodCalledHandler.TryGetValue(pathToOperation, out MethodCalledHandler handler)) |
| 290 | methodHandler = handler; |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 291 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 292 | return new Result<CallbackResponse>(false, new NotFoundMessage($"MethodHandler for {pathToOperation}")); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 293 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 294 | Task invocationTask = Task.Run(async() => |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 295 | { |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 296 | InvocationResponse invocationResponse = new InvocationResponse(invocationRequest.RequestId); |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 297 | invocationResponse.InOutputArguments = invocationRequest.InOutputArguments; |
| 298 | SetInvocationResult(pathToOperation, invocationRequest.RequestId, ref invocationResponse); |
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 | using (var cancellationTokenSource = new CancellationTokenSource()) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 301 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 302 | Task<OperationResult> runner = Task.Run(async () => |
| 303 | { |
| 304 | try |
| 305 | { |
| 306 | invocationResponse.ExecutionState = ExecutionState.Running; |
| 307 | var result = await methodHandler.Invoke(operation_Retrieved.Entity, invocationRequest.InputArguments, invocationResponse.InOutputArguments, invocationResponse.OutputArguments, cancellationTokenSource.Token); |
| 308 | invocationResponse.ExecutionState = ExecutionState.Completed; |
| 309 | return result; |
| 310 | } |
| 311 | catch (Exception e) |
| 312 | { |
| 313 | invocationResponse.ExecutionState = ExecutionState.Failed; |
| 314 | return new OperationResult(e); |
| 315 | } |
| 316 | }, cancellationTokenSource.Token); |
| 317 | |
| 318 | if (await Task.WhenAny(runner, Task.Delay(invocationRequest.Timeout.Value, cancellationTokenSource.Token)) == runner) |
| 319 | { |
| 320 | cancellationTokenSource.Cancel(); |
| 321 | invocationResponse.OperationResult = runner.Result; |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | cancellationTokenSource.Cancel(); |
| 326 | invocationResponse.OperationResult = new OperationResult(false, new TimeoutMessage()); |
| 327 | invocationResponse.ExecutionState = ExecutionState.Timeout; |
| 328 | } |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 329 | } |
| 330 | }); |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 331 | |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 332 | string endpoint = ServiceDescriptor?.Endpoints?.FirstOrDefault()?.Address; |
| 333 | CallbackResponse callbackResponse = new CallbackResponse(invocationRequest.RequestId); |
| 334 | if (string.IsNullOrEmpty(endpoint)) |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 335 | callbackResponse.CallbackUrl = new Uri($"/submodelElements/{pathToOperation}/invocationList/{invocationRequest.RequestId}", UriKind.Relative); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 336 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 337 | callbackResponse.CallbackUrl = new Uri($"{endpoint}/submodelElements/{pathToOperation}/invocationList/{invocationRequest.RequestId}", UriKind.Absolute); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 338 | return new Result<CallbackResponse>(true, callbackResponse); |
| 339 | } |
| 340 | return new Result<CallbackResponse>(operation_Retrieved); |
| 341 | } |
| 342 | |
| 343 | private void SetInvocationResult(string operationId, string requestId, ref InvocationResponse invocationResponse) |
| 344 | { |
| 345 | string key = string.Join("_", operationId, requestId); |
| 346 | if (invocationResults.ContainsKey(key)) |
| 347 | { |
| 348 | invocationResults[key] = invocationResponse; |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | invocationResults.Add(key, invocationResponse); |
| 353 | } |
| 354 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 355 | |
| 356 | public IResult<InvocationResponse> GetInvocationResult(string pathToOperation, string requestId) |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 357 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 358 | string key = string.Join("_", pathToOperation, requestId); |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 359 | if (invocationResults.ContainsKey(key)) |
| 360 | { |
| 361 | return new Result<InvocationResponse>(true, invocationResults[key]); |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | return new Result<InvocationResponse>(false, new NotFoundMessage($"Request with id {requestId}")); |
| 366 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 369 | public IResult ThrowEvent(IPublishableEvent publishableEvent, string topic = "/", Action<IMessagePublishedEventArgs> MessagePublished = null, byte qosLevel = 2, bool retain = false) |
| 370 | { |
| 371 | if (messageClient == null || !messageClient.IsConnected) |
| 372 | return new Result(false, new Message(MessageType.Warning, "MessageClient is not initialized or not connected")); |
| 373 | |
| 374 | if (publishableEvent == null) |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 375 | return new Result(new ArgumentNullException(nameof(publishableEvent))); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 376 | |
| 377 | if (eventDelegates.TryGetValue(publishableEvent.Name, out EventDelegate eventDelegate)) |
| 378 | eventDelegate.Invoke(this, publishableEvent); |
| 379 | |
Constantin Ziesche | fa61208 | 2020-04-03 09:54:56 +0200 | [diff] [blame] | 380 | string message = JsonConvert.SerializeObject(publishableEvent, Formatting.Indented); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 381 | return messageClient.Publish(topic, message, MessagePublished, qosLevel, retain); |
| 382 | } |
| 383 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 384 | |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 385 | public virtual void ConfigureEventHandler(IMessageClient messageClient) |
| 386 | { |
| 387 | this.messageClient = messageClient; |
| 388 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 389 | |
| 390 | public virtual void SubscribeUpdates(string pathToSubmodelElement, Action<IValue> updateFunction) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 391 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 392 | if (!updateFunctions.ContainsKey(pathToSubmodelElement)) |
| 393 | updateFunctions.Add(pathToSubmodelElement, updateFunction); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 394 | else |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 395 | updateFunctions[pathToSubmodelElement] = updateFunction; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 396 | } |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 397 | |
| 398 | public virtual void PublishUpdate(string pathToSubmodelElement, IValue value) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 399 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 400 | if (updateFunctions.TryGetValue(pathToSubmodelElement, out Action<IValue> updateFunction)) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 401 | updateFunction.Invoke(value); |
| 402 | |
| 403 | } |
| 404 | |
| 405 | public IResult<ISubmodel> RetrieveSubmodel() |
| 406 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 407 | return new Result<ISubmodel>(_submodel != null, _submodel); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 410 | public IResult<ISubmodelElement> CreateSubmodelElement(string pathToSubmodelElement, ISubmodelElement submodelElement) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 411 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 412 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 413 | return new Result<ISubmodelElement>(false, new NotFoundMessage("Submodel")); |
| 414 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 415 | return _submodel.SubmodelElements.Create(submodelElement); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | public IResult<IElementContainer<ISubmodelElement>> RetrieveSubmodelElements() |
| 419 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 420 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 421 | return new Result<ElementContainer<ISubmodelElement>>(false, new NotFoundMessage("Submodel")); |
| 422 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 423 | if (_submodel.SubmodelElements == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 424 | return new Result<ElementContainer<ISubmodelElement>>(false, new NotFoundMessage("SubmodelElements")); |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 425 | return _submodel.SubmodelElements.RetrieveAll(); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | public IResult<ISubmodelElement> RetrieveSubmodelElement(string submodelElementId) |
| 429 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 430 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 431 | return new Result<ISubmodelElement>(false, new NotFoundMessage("Submodel")); |
| 432 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 433 | if (_submodel.SubmodelElements == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 434 | return new Result<ISubmodelElement>(false, new NotFoundMessage(submodelElementId)); |
Constantin Ziesche | 8b4a64d | 2020-06-25 11:52:09 +0200 | [diff] [blame] | 435 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 436 | return _submodel.SubmodelElements.Retrieve(submodelElementId); |
Constantin Ziesche | 8b4a64d | 2020-06-25 11:52:09 +0200 | [diff] [blame] | 437 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 438 | public IResult<IValue> RetrieveSubmodelElementValue(string submodelElementId) |
| 439 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 440 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 441 | return new Result<IValue>(false, new NotFoundMessage("Submodel")); |
| 442 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 443 | if (submodelElementHandler.TryGetValue(submodelElementId, out SubmodelElementHandler elementHandler) && elementHandler.GetValueHandler != null) |
| 444 | { |
| 445 | var submodelElement = _submodel.SubmodelElements.Retrieve<ISubmodelElement>(submodelElementId); |
| 446 | if (submodelElement.Success && submodelElement.Entity != null) |
| 447 | return new Result<IValue>(true, elementHandler.GetValueHandler.Invoke(submodelElement.Entity)); |
| 448 | else |
| 449 | return new Result<IValue>(false, new Message(MessageType.Error, "SubmodelElement not found")); |
| 450 | } |
| 451 | else |
| 452 | return new Result<IValue>(false, new Message(MessageType.Error, "SubmodelElementHandler not found")); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | public IResult UpdateSubmodelElement(string submodelElementId, ISubmodelElement submodelElement) |
| 456 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 457 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 458 | return new Result(false, new NotFoundMessage("Submodel")); |
| 459 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 460 | if (_submodel.SubmodelElements == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 461 | return new Result(false, new NotFoundMessage(submodelElementId)); |
| 462 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 463 | return _submodel.SubmodelElements.Update(submodelElementId, submodelElement); |
| 464 | } |
| 465 | |
| 466 | public IResult UpdateSubmodelElementValue(string submodelElementId, IValue value) |
| 467 | { |
| 468 | if (_submodel == null) |
| 469 | return new Result(false, new NotFoundMessage("Submodel")); |
| 470 | |
| 471 | if (submodelElementHandler.TryGetValue(submodelElementId, out SubmodelElementHandler elementHandler) && elementHandler.SetValueHandler != null) |
| 472 | { |
| 473 | var submodelElement = _submodel.SubmodelElements.Retrieve<ISubmodelElement>(submodelElementId); |
| 474 | if (submodelElement.Success && submodelElement.Entity != null) |
| 475 | { |
| 476 | elementHandler.SetValueHandler.Invoke(submodelElement.Entity, value); |
| 477 | return new Result(true); |
| 478 | } |
| 479 | else |
| 480 | return new Result<IValue>(false, new Message(MessageType.Error, "property not found")); |
| 481 | } |
| 482 | else |
| 483 | return new Result<IValue>(false, new Message(MessageType.Error, "SubmodelElementHandler not found")); |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | public IResult DeleteSubmodelElement(string submodelElementId) |
| 487 | { |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 488 | if (_submodel == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 489 | return new Result(false, new NotFoundMessage("Submodel")); |
| 490 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 491 | if (_submodel.SubmodelElements == null) |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 492 | return new Result(false, new NotFoundMessage(submodelElementId)); |
| 493 | |
Constantin Ziesche | 0821550 | 2020-09-21 19:08:32 +0200 | [diff] [blame] | 494 | return _submodel.SubmodelElements.Delete(submodelElementId); |
Constantin Ziesche | e837f99 | 2020-08-19 12:04:32 +0200 | [diff] [blame] | 495 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 496 | } |
| 497 | } |