DevRunEventListener.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using Coravel.Events.Interfaces;
  2. using InfluxData.Net.Common.Enums;
  3. using InfluxData.Net.InfluxDb;
  4. using InitQ.Cache;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Quartz.Impl.AdoJobStore.Common;
  7. using Ropin.Core.Common;
  8. using System;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace Ropin.Environmentally.WebScada.Subscribe
  12. {
  13. public class DevRunEventListener : IListener<DevEvent>
  14. {
  15. private readonly ICacheService _redisService;
  16. public DevRunEventListener(ICacheService redisService) {
  17. _redisService = redisService;
  18. }
  19. private InfluxDbClient clientDb;
  20. public async Task HandleAsync(DevEvent broadcasted)
  21. {
  22. var msg = broadcasted.Message;
  23. if(msg == "devrunstart")
  24. {
  25. Console.WriteLine($"WriteMessageToConsoleListener receive this message from DevEvent: ${msg}");
  26. IniInflux();
  27. }
  28. if (msg == "devrunend")
  29. {
  30. Console.WriteLine($"WriteMessageToConsoleListener receive this message from DevEvent: ${msg}");
  31. IniInflux();
  32. DateTime startTime = await _redisService.GetAsync<DateTime>("fanyibox_devStartRun_" + broadcasted.DevId);
  33. DateTime endTime = await _redisService.GetAsync<DateTime>("fanyibox_devEndRun_" + broadcasted.DevId);
  34. }
  35. await Task.CompletedTask;
  36. }
  37. private void IniInflux()
  38. {
  39. //连接InfluxDb的API地址、账号、密码
  40. var infuxUrl = "http://localhost:8086/";
  41. var infuxUser = "admin";
  42. var infuxPwd = "admin";
  43. //创建InfluxDbClient实例
  44. clientDb = new InfluxDbClient(infuxUrl, infuxUser, infuxPwd, InfluxDbVersion.Latest);
  45. }
  46. public async Task GetMeanData(string id,DateTime start, DateTime end)
  47. {
  48. //传入查询命令,支持多条
  49. var queries = new[]
  50. {
  51. "SELECT mean(Val) FROM fanyidev where (Id ='224873679711261516') and time >"+start+" and time < " + end
  52. };
  53. var dbName = "fanyidb";
  54. //从指定库中查询数据
  55. var response = await clientDb.Client.QueryAsync(queries, dbName);
  56. //得到Serie集合对象(返回执行多个查询的结果)
  57. var series = response.ToList();
  58. //取出第一条命令的查询结果,是一个集合
  59. var list = series[0].Values;
  60. //从集合中取出第一条数据
  61. var info_model = list.FirstOrDefault();
  62. }
  63. }
  64. }