forked from IoTSharp/IoTSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CapPublisher.cs
87 lines (78 loc) · 3.13 KB
/
CapPublisher.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
using DotNetCore.CAP;
using DotNetCore.CAP.Messages;
using DotNetCore.CAP.Monitoring;
using IoTSharp.Contracts;
using IoTSharp.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace IoTSharp.EventBus
{
public class CapPublisher : IPublisher
{
private readonly ICapPublisher _queue;
private readonly DotNetCore.CAP.Persistence.IDataStorage _storage;
public CapPublisher(ICapPublisher queue, DotNetCore.CAP.Persistence.IDataStorage storage)
{
_queue = queue;
_storage = storage;
}
public async Task<EventBusMetrics> GetMetrics()
{
var _api = _storage.GetMonitoringApi();
var ps =await _api.HourlySucceededJobs(MessageType.Publish);
var pf =await _api.HourlyFailedJobs(MessageType.Publish);
var ss =await _api.HourlySucceededJobs(MessageType.Subscribe);
var sf =await _api.HourlyFailedJobs(MessageType.Subscribe);
var dayHour = ps.OrderBy(k => k.Key).Select(k => k.Key.ToString("MM-dd HH:00")).ToList();
var s = await _api.GetStatisticsAsync();
var result = new EventBusMetrics(
dayHour,
ps.OrderBy(k=>k.Key).Select(k=>k.Value).ToList(),
pf.OrderBy(k => k.Key).Select(k => k.Value).ToList(),
ss.OrderBy(k => k.Key).Select(k => k.Value).ToList(),
sf.OrderBy(k => k.Key).Select(k => k.Value).ToList()
)
{
Servers = s.Servers,
Subscribers = s.Subscribers,
PublishedSucceeded = s.PublishedSucceeded,
ReceivedSucceeded = s.ReceivedSucceeded,
PublishedFailed = s.PublishedFailed,
ReceivedFailed = s.ReceivedFailed
};
return result;
}
public async Task PublishAttributeData(PlayloadData msg)
{
await _queue.PublishAsync("iotsharp.services.datastream.attributedata", msg);
}
public async Task PublishTelemetryData( PlayloadData msg)
{
await _queue.PublishAsync("iotsharp.services.datastream.telemetrydata", msg);
}
public async Task PublishDeviceAlarm( CreateAlarmDto alarmDto)
{
await _queue.PublishAsync("iotsharp.services.datastream.alarm", alarmDto);
}
public async Task PublishCreateDevice(Guid devid)
{
await _queue.PublishAsync("iotsharp.services.platform.createdevice", devid);
}
public async Task PublishDeleteDevice(Guid devid)
{
await _queue.PublishAsync("iotsharp.services.platform.deleteDevice", devid);
}
public async Task PublishConnect(Guid devid, ConnectStatus devicestatus)
{
await _queue.PublishAsync("iotsharp.services.platform.connect", new DeviceConnectStatus(devid, devicestatus));
}
public async Task PublishActive(Guid devid, ActivityStatus activity)
{
await _queue.PublishAsync("iotsharp.services.platform.active", new DeviceActivityStatus(devid, activity));
}
}
}