SimpleProxy(Options opt) { var snmpAgentEp = GetSnmpAgentEndPoint(opt.SnmpAgent); coapServer = new CoAP.Server.CoapServer(opt.CoapPort); coapServer.Add(new CoapMibResource("mg/mib/", opt.Community, snmpAgentEp)); }
public override async Task Run(IList <string> args) { CoAP.Log.LogManager.Level = CoAP.Log.LogLevel.Warning; // configure a oneM2M CoAP connection var connection = new CoapConnection(_connectionConfiguration); // start the POA CoAP server var coapServer = new CoAP.Server.CoapServer(); coapServer.AddEndPoint(new IPEndPoint(IPAddress.Any, _poaUrl.Port)); coapServer.Add(connection.CreateNotificationResource()); coapServer.Start(); // initialize the connection var ae = await Register(connection); _application = new Application(connection, ae.App_ID, ae.AE_ID, "./", _poaUrl); // create our containers await _application.EnsureContainerAsync(_MsPolicyPath); await _application.EnsureContainerAsync(_MsCommandsPath); // discover the in-ae var inAeUrl = (await _application.GetResponseAsync(new RequestPrimitive { Operation = Operation.Retrieve, To = "/PN_CSE/", FilterCriteria = new FilterCriteria { FilterUsage = FilterUsage.Discovery, ResourceType = new[] { ResourceType.AE }, Attribute = Connection <PrimitiveContent> .GetAttributes <AE>(_ => _.AppName == _AeAppName), } })).URIList.SingleOrDefault(); if (inAeUrl == null) { this.ShowError($"Unable to find in-AE {_AeAppName}"); } var tsReadInterval = TimeSpan.FromDays(15); var updatePolicySource = new CancellationTokenSource(); var lockPolicyUpdate = new object(); // fetch the most recent policy, initialize the read interval var policy = await _application.GetLatestContentInstanceAsync <global::Example.Types.Config.MeterReadPolicy>(_MsPolicyPath); if (policy != null) { tsReadInterval = TimeSpan.Parse(policy.ReadInterval); } // subscribe to the policy container var policyObservable = await _application.ObserveContentInstanceAsync <global::Example.Types.Config.MeterReadPolicy>(_MsPolicyPath, MeterReadSubscriptionName); using var eventSubscription = policyObservable.Subscribe(policy => { lock (lockPolicyUpdate) { tsReadInterval = TimeSpan.Parse(policy.ReadInterval); Console.WriteLine($"New Read Interval: {tsReadInterval}"); // var oldTokenSource = updatePolicySource; updatePolicySource = new CancellationTokenSource(); oldTokenSource.Cancel(); } }); while (true) { var timeStart = DateTimeOffset.UtcNow; await CreateMeterRead($"{inAeUrl}/{_ReadsContainerName}"); try { await Task.Delay(DateTimeOffset.UtcNow + tsReadInterval - timeStart, updatePolicySource.Token); } catch (TaskCanceledException) {} // ignore the wake-up exception } }
public override async Task Run(IList <string> args) { Task hostTask; object server; Connection <PrimitiveContent> connection; if (_connectionConfiguration.M2MUrl.Scheme.StartsWith("coap")) { CoAP.Log.LogManager.Level = CoAP.Log.LogLevel.Warning; // configure a oneM2M CoAP connection var coapConnection = new CoapConnection(_connectionConfiguration); connection = coapConnection; // start the POA CoAP server var coapServer = new CoAP.Server.CoapServer(); coapServer.AddEndPoint(new IPEndPoint(IPAddress.Any, _poaUrl.Port)); coapServer.Add(coapConnection.CreateNotificationResource()); coapServer.Start(); server = coapServer; hostTask = Task.Delay(Timeout.Infinite); // TODO: terminate } else { // configure a oneM2M HTTP connection var httpConnection = new HttpConnection(_connectionConfiguration); connection = httpConnection; // start the POA web server hostTask = WebHost.CreateDefaultBuilder() .UseUrls((_listenUrl ?? _poaUrl).ToString()) .Configure(app => app.Map("/notify", builder => builder.Run(httpConnection.HandleNotificationAsync))) .Build() .RunAsync(); } ////// // register the AE var ae = await Register(connection); Trace.TraceInformation($"Using AE, Id = {ae.AE_ID}"); ////// // configure the oneM2M applicaiton api _application = new Application(connection, ae.App_ID, ae.AE_ID, "./", _poaUrl); ////// // create a subscription await _application.EnsureContainerAsync(_MsReadsPath); Trace.TraceInformation("Invoking Create Subscription API"); var policyObservable = await _application.ObserveContentInstanceAsync <MeterRead>(_MsReadsPath, MeterReadSubscriptionName); using var eventSubscription = policyObservable.Subscribe(policy => { var data = policy.MeterSvcData; Trace.TraceInformation($"new meter read:"); Trace.TraceInformation($"\tpowerQuality: {data.PowerQuality.VoltageA}"); Trace.TraceInformation($"\treadTimeLocal: {data.ReadTimeLocal}"); }); ////// // create a meter read policy content instance await CreateMeterReadPolicy(); ////// // create a (fake) meter read content instance, triggering a notification await CreateMeterRead(); try { // continue running the POA server await hostTask; } finally { await DeleteSubscription(); await DeleteMeterReadPolicy(); await DeRegister(); } }