using System; using System.Collections.Generic; using Microsoft.Extensions.Logging; namespace MQTTnet.Server.Scripting.DataSharing { public class DataSharingService { private readonly Dictionary _storage = new Dictionary(); private readonly PythonScriptHostService _pythonScriptHostService; private readonly ILogger _logger; public DataSharingService(PythonScriptHostService pythonScriptHostService, ILogger logger) { _pythonScriptHostService = pythonScriptHostService ?? throw new ArgumentNullException(nameof(pythonScriptHostService)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public void Configure() { _pythonScriptHostService.RegisterProxyObject("write_shared_data", new Action(Write)); _pythonScriptHostService.RegisterProxyObject("read_shared_data", new Func(Read)); } public void Write(string key, object value) { lock (_storage) { _storage[key] = value; _logger.LogInformation($"Shared data with key '{key}' updated."); } } public object Read(string key, object defaultValue) { lock (_storage) { if (!_storage.TryGetValue(key, out var value)) { return defaultValue; } return value; } } } }