Communication.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using ICommunication;
  2. using Shaker.Model;
  3. using Shaker.Models;
  4. using ShakerService.ViewModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace ShakerService
  12. {
  13. internal class Communication
  14. {
  15. public ICommunication.ICommunication Context { get; }
  16. private Communication()
  17. {
  18. Context = new LocalCommunication.Communication();
  19. string path = "ShakerConfig.db";
  20. if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  21. {
  22. path = Path.Combine(Environment.CurrentDirectory, path);
  23. }
  24. else if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  25. {
  26. path = Path.Combine("/etc/", path);
  27. }
  28. DbConnection = new SQLite.SQLiteConnection(path);
  29. }
  30. static Communication()
  31. {
  32. }
  33. public void StartService(string ip,int port)
  34. {
  35. Context.StartService(ip, port);
  36. Context.GetEvent<string>(Topic.SERVICERESULT)?.Subscrip((_, _) => Topic.SERVICERESULT);
  37. Context.Disconnected += (sender, args) => EventBus.EventBroker.Instance.GetEvent(Topic.STOP).Publish(this,null);
  38. }
  39. public SQLite.SQLiteConnection DbConnection { get; private set; }
  40. public static Communication Instance { get; } = new Communication();
  41. }
  42. }