|
1 | | -using Coreflux.API.DataModels; |
2 | | -using CorefluxCSharpAPI.API.Services; |
3 | | -using CorefluxCSharpAPI.API.Services.Interfaces; |
| 1 | +using System; |
| 2 | +using System.Net; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Text; |
| 5 | +using System.IO; |
4 | 6 | using Newtonsoft.Json; |
5 | | -using System; |
| 7 | +using MQTTnet; |
| 8 | +using MQTTnet.Client; |
| 9 | +using MQTTnet.Protocol; |
| 10 | +using MQTTnet.Client.Options; |
6 | 11 |
|
7 | 12 | namespace Coreflux.API |
8 | 13 | { |
9 | | - public class Client |
| 14 | + |
| 15 | + public class Central |
10 | 16 | { |
11 | | - private readonly string _ip; |
12 | | - private readonly ICommunicationService _communicationService; |
| 17 | + |
| 18 | + private IMqttClient client; |
| 19 | + private string serverAddress = "127.0.0.1"; // replace with your Coreflux server address |
| 20 | + private string username = "root"; |
| 21 | + private string pass = "coreflux"; |
| 22 | + private string cloudCommand = "$SYS/Coreflux/Cloud/Command"; |
| 23 | + private string cloudCommandOutput = "$SYS/Coreflux/Cloud/Command/Output"; |
| 24 | + public Central(string Host, string user, string password) |
| 25 | + { |
| 26 | + var factory = new MqttFactory(); |
| 27 | + serverAddress = Host; |
| 28 | + username = user; |
| 29 | + pass = password; |
| 30 | + client = factory.CreateMqttClient(); |
| 31 | + } |
13 | 32 |
|
14 | 33 | /// <summary> |
15 | | - /// This constructor is recommend for unit testing. |
16 | | - /// This constructor let's you pass a mock object of ICommunicationService. |
| 34 | + /// Connects to the Coreflux Central. |
17 | 35 | /// </summary> |
18 | | - /// <param name="ip"></param> |
19 | | - /// <param name="communicationService"></param> |
20 | | - public Client(string ip, ICommunicationService communicationService = null) |
| 36 | + public async void Connect() |
21 | 37 | { |
22 | | - _ip = ip; |
23 | | - _communicationService = communicationService ?? new CommunicationService(); |
| 38 | + var options = new MqttClientOptionsBuilder() |
| 39 | + .WithClientId(Guid.NewGuid().ToString()) |
| 40 | + .WithTcpServer(serverAddress) |
| 41 | + .WithCredentials(username, pass) |
| 42 | + .Build(); |
| 43 | + |
| 44 | + await client.ConnectAsync(options); |
| 45 | + } |
| 46 | + |
| 47 | + private async void SendCommand(string command) |
| 48 | + { |
| 49 | + var message = new MqttApplicationMessageBuilder() |
| 50 | + .WithTopic(cloudCommand) |
| 51 | + .WithPayload(command) |
| 52 | + .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce) |
| 53 | + .WithRetainFlag() |
| 54 | + .Build(); |
| 55 | + |
| 56 | + await client.PublishAsync(message); |
24 | 57 | } |
25 | 58 |
|
26 | 59 | /// <summary> |
27 | | - /// This constructor is the recommend for production application's |
| 60 | + /// Subscribes to the given MQTT topic to receive messages from the Coreflux server. |
28 | 61 | /// </summary> |
29 | | - /// <param name="ip"></param> |
30 | | - public Client(string ip) |
| 62 | + /// <param name="responseTopic">The MQTT topic to subscribe to.</param> |
| 63 | + public async void SubscribeToCommandOutput(string responseTopic) |
31 | 64 | { |
32 | | - _ip = ip; |
33 | | - _communicationService = new CommunicationService(); |
| 65 | + await client.SubscribeAsync(new TopicFilterBuilder() |
| 66 | + .WithTopic(responseTopic) |
| 67 | + .WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce) |
| 68 | + .Build()); |
| 69 | + |
| 70 | + client.UseApplicationMessageReceivedHandler(e => |
| 71 | + { |
| 72 | + Console.WriteLine("### RECEIVED APPLICATION MESSAGE ###"); |
| 73 | + Console.WriteLine($"+ Topic = {e.ApplicationMessage.Topic}"); |
| 74 | + Console.WriteLine($"+ Payload = {Encoding.UTF8.GetString(e.ApplicationMessage.Payload)}"); |
| 75 | + Console.WriteLine($"+ QoS = {e.ApplicationMessage.QualityOfServiceLevel}"); |
| 76 | + Console.WriteLine($"+ Retain = {e.ApplicationMessage.Retain}"); |
| 77 | + Console.WriteLine(); |
| 78 | + }); |
34 | 79 | } |
35 | 80 |
|
| 81 | + |
36 | 82 | /// <summary> |
37 | | - /// If null is returned, something failed on obtaining the instances. |
| 83 | + /// Sends a login command to the Coreflux Cloud. |
38 | 84 | /// </summary> |
39 | | - /// <returns></returns> |
40 | | - public AppInstance[] GetInstances() |
| 85 | + /// <param name="account">The account email.</param> |
| 86 | + /// <param name="pass">The password.</param> |
| 87 | + public void CloudLogin(string account, string pass) |
41 | 88 | { |
42 | | - string response = _communicationService.GetInformation(string.Format("https://{0}:9501/CorefluxCentral/instances", _ip)); |
43 | | - return string.IsNullOrEmpty(response) ? null : JsonConvert.DeserializeObject<AppInstance[]>(response); |
| 89 | + SendCommand($"-L {account} {pass}"); |
44 | 90 | } |
45 | 91 |
|
46 | 92 | /// <summary> |
47 | | - /// This method start's the execution of a Instance |
| 93 | + /// Sends a command to install an asset on the Coreflux server. |
48 | 94 | /// </summary> |
49 | | - /// <param name="id"></param> |
50 | | - /// <returns></returns> |
51 | | - /// <exception cref="Exception"></exception> |
52 | | - public bool StartInstance(string id) |
| 95 | + /// <param name="assetName">The name of the asset to install.</param> |
| 96 | + public void InstallAsset(string assetName) |
53 | 97 | { |
54 | | - if (FindAppInInstances(id)) |
55 | | - { |
56 | | - // VERBS: start, stop, run, install, uninstall |
57 | | - string response = _communicationService.PostInformation(string.Format("https://{0}:9501/CorefluxCentral/instances/{1}/start", _ip, id)); |
58 | | - return string.IsNullOrEmpty(response) ? true : throw new Exception(response); |
59 | | - } |
60 | | - return false; |
61 | | - } |
| 98 | + SendCommand($"-I {assetName}"); |
62 | 99 |
|
| 100 | + } |
63 | 101 | /// <summary> |
64 | | - /// This method stop's the execution of a Instance |
| 102 | + /// Sends a command to list all assets on the Coreflux server. |
65 | 103 | /// </summary> |
66 | | - /// <param name="id"></param> |
67 | | - /// <returns></returns> |
68 | | - /// <exception cref="Exception"></exception> |
69 | | - public bool StopInstance(string id) |
| 104 | + public void ListAssets() |
70 | 105 | { |
71 | | - if (FindAppInInstances(id)) |
72 | | - { |
73 | | - var response = _communicationService.PostInformation(string.Format("https://{0}:9501/CorefluxCentral/instances/{1}/stop", _ip, id)); |
74 | | - return string.IsNullOrEmpty(response) ? true : throw new Exception(response); |
75 | | - } |
76 | | - return false; |
| 106 | + SendCommand("-l"); |
77 | 107 | } |
78 | 108 |
|
79 | | - private bool FindAppInInstances(string instance) |
| 109 | + /// <summary> |
| 110 | + /// Sends a command to uninstall an asset on the Coreflux server. |
| 111 | + /// </summary> |
| 112 | + /// <param name="assetGuid">The GUID of the asset to uninstall.</param> |
| 113 | + public void UninstallAsset(string assetInstanceGuid) |
80 | 114 | { |
81 | | - AppInstance[] apps = GetInstances(); |
82 | | - if (apps == null) |
83 | | - return false; |
| 115 | + SendCommand($"-U {assetInstanceGuid}"); |
| 116 | + } |
84 | 117 |
|
85 | | - foreach (AppInstance app in apps) |
86 | | - if (app.code.Equals(instance)) |
87 | | - return true; |
| 118 | + /// <summary> |
| 119 | + /// Sends a command to stop an asset instance on the Coreflux server. |
| 120 | + /// </summary> |
| 121 | + /// <param name="assetInstanceGuid">The GUID of the asset instance to stop.</param> |
| 122 | + public void StopInstance(string assetInstanceGuid) |
| 123 | + { |
| 124 | + SendCommand($"-S {assetInstanceGuid}"); |
| 125 | + } |
88 | 126 |
|
89 | | - return false; |
| 127 | + /// <summary> |
| 128 | + /// Sends a command to start an asset instance on the Coreflux server. |
| 129 | + /// </summary> |
| 130 | + /// <param name="assetInstanceGuid">The GUID of the asset instance to start.</param> |
| 131 | + public void StartAsset(string assetInstanceGuid) |
| 132 | + { |
| 133 | + SendCommand($"-R {assetInstanceGuid}"); |
90 | 134 | } |
| 135 | + |
91 | 136 | } |
| 137 | + |
| 138 | + |
| 139 | + |
92 | 140 | } |
0 commit comments