PageRenderTime 3ms CodeModel.GetById 1ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/docs/source/sdk/csharp/csharp-overview.rst

https://bitbucket.org/Altronix/linq-documentation
ReStructuredText | 260 lines | 195 code | 65 blank | 0 comment | 0 complexity | 99916e79c0f9bed5e9ee099f9373499e MD5 | raw file
  1Overview
  2========
  3
  4Dependencies
  5------------
  6
  7  * Built for .NET 4.5.2 and up.
  8  * Newtonsoft.Json
  9
 10Installation
 11------------
 12
 13Once a project has been setup, to add C# SDK please follow the steps below:
 14
 15  1.  In the Solution Explorer, right-click the References section found under [Project name] -> References
 16
 17      .. image:: setup_1.jpg
 18        :align: center
 19
 20  2.  Click ‘Add Reference...’
 21
 22      .. image:: setup_2.jpg
 23        :align: center
 24
 25  3.  In the Reference Manager go to ‘Browse’ subsection
 26
 27      .. image:: setup_3.jpg
 28        :align: center
 29
 30  4.  Click the ‘Browse...’button
 31
 32      .. image:: setup_4.jpg
 33        :align: center
 34
 35  5.  Locate the AltronixAlerts.dll file on your local computer
 36
 37      .. image:: setup_5.jpg
 38        :align: center
 39
 40  6.  Once located, select the file and then click ‘Add’
 41
 42      .. image:: setup_6.jpg
 43        :align: center
 44
 45  7.  You will return to the Reference Manager where you can check to confirm that the library has been added and click ‘OK’
 46
 47      .. image:: setup_7.jpg
 48        :align: center
 49
 50  8.  Under References you should now see ‘AltronixAlerts.dll’ listed
 51
 52      .. image:: setup_8.jpg
 53        :align: center
 54
 55  9.  To use the library add the following line to the top of a file: ‘using Altronix.LINQ;’
 56
 57      .. image:: setup_9.jpg
 58        :align: center
 59
 60Import
 61------
 62
 63.. code-block:: csharp
 64
 65  using Altronix.LINQ;
 66
 67Setup for listening to devices
 68------------------------------
 69
 70
 71.. code-block:: csharp
 72
 73  // User Callback that triggers OnAlerts events
 74  public void UserCallback(List<Alert> list)
 75  {
 76      list.ForEach(alert =>
 77      {
 78          Console.WriteLine(
 79            "New Alert:\n"
 80            + "Site ID: " + alert.siteid + "\n"
 81            + "Message: " + alert.message + "\n"
 82            + "Date: " + alert.date + "\n"
 83            + "Address: " + alert.ip + ":" + alert.port + "\n"
 84            );
 85      });
 86  }
 87
 88  // User Callback that triggers OnError events
 89  public void UserErrorCallback(Device device)
 90  {
 91      switch (device.error)
 92      {
 93          case Error.SUCCESS:
 94              Console.WriteLine("Success connecting to " + device.ip + ":" + device.port + "\n");
 95              break;
 96          case Error.CONNECTING:
 97              Console.WriteLine("Error connecting to " + device.ip + ":" + device.port + "\n");
 98              break;
 99          case Error.AUTHENTICATE:
100              Console.WriteLine("Error authenticating on " + device.ip + ":" + device.port + "\n");
101              break;
102          case Error.FORBIDDEN:
103              Console.WriteLine("Error access forbidden on " + device.ip + ":" + device.port + "\n");
104              break;
105      }
106  }
107
108  // Async function creates a listener for one device
109  public async Task ListenToNewDevice(Device device)
110  {
111      // Create Listener Class instance
112      Listener atx = new Listener();
113
114      // Set user's callback for OnAlerts Event
115      atx.OnAlerts(UserCallback);
116
117      // Set user's callback for OnError Event
118      atx.OnError(UserErrorCallback);
119
120      // Start Listening on device
121      atx.Listen(device);
122  }
123
124  // Async function creates a listener for a list of devices
125  public async Task ListenToAllDevices(List<Device> devices)
126  {
127      // Create Listener Class instance
128      Listener atx = new Listener();
129
130      // Set user's callback for OnAlerts Event
131      atx.OnAlerts(UserCallback);
132
133      // Set user's callback for OnError Event
134      atx.OnError(UserErrorCallback);
135
136      // Start Listening to device
137      atx.ListenAll(devices);
138  }
139
140Listen for events on one device
141-------------------------------
142
143.. code-block:: csharp
144
145  Device device = new Device("10.10.10.10", "80", false, "username", "password");
146
147  await ListenToNewDevice(device); // Creates Listener for one new device
148
149Listen for events on list of devices
150------------------------------------
151
152.. code-block:: csharp
153
154  List<Device> devices = DeviceStorage.getDevices(); // Get or create list of devices you want to listen to
155
156  await ListenToAllDevices(devices); // Creates Listeners for all devices in list
157
158Example
159----------------
160
161.. code-block:: csharp
162
163  using System;
164  using System.Collections.Generic;
165  using System.ComponentModel;
166  using System.Data;
167  using System.Drawing;
168  using System.Linq;
169  using System.Text;
170  using System.Threading.Tasks;
171  using System.Windows.Forms;
172  using Altronix.LINQ;
173
174  namespace AlertsNugetTest
175  {
176    public partial class Form1 : Form
177    {
178        public Form1()
179        {
180            InitializeComponent();
181        }
182
183        private async void ListenToSingleDevice_Click(object sender, EventArgs e)
184        {
185            // Create instance of Altronix.LINQ.Device
186            Device device = new Device("10.10.10.188",  // ip addresss
187                                        "443",          // port number
188                                        true,           // TLS/SSL = true
189                                        "admin",        // username
190                                        "admin");       // password
191
192            // Create Altronix.LINQ.Listener Class instance
193            Listener atx = new Listener();
194
195            // Set user's callback for OnAlerts Event
196            atx.OnAlerts(UserCallback);
197
198            // Set user's callback for OnError Event
199            atx.OnError(UserErrorCallback);
200
201            // Start Listening on device
202            await atx.Listen(device);
203
204        }
205
206        // User Callback that triggers OnAlerts events
207        public void UserCallback(List<Alert> list)
208        {
209            list.ForEach(alert => {
210                var str = "[ " + alert.date + " ]  [ " + alert.siteid + alert.ip + ":" + alert.port + " ] ===> " +
211                            alert.message + "\n";
212                // Print to console
213                Console.WriteLine(str);
214            });
215        }
216
217        public void UserErrorCallback(Device device)
218        {
219            switch (device.error)
220            {
221                case Error.CONNECTING:
222                    /* Handle this error here */
223                    break;
224                case Error.AUTHENTICATE:
225                    /* Handle this error here */
226                    break;
227                case Error.FORBIDDEN:
228                    /* Handle this error here */
229                    break;
230            }
231        }
232
233        private void ListenToSeveralDevices2_Click(object sender, EventArgs e)
234        {
235            // Create List<Altronix.LINQ.Device> of devices
236            List<Device> devices = new List<Device>
237            {
238                new Device(){ ip="10.10.10.183",port="80",ssl=false,username="admin",password="admin"},
239                new Device(){ ip="10.10.10.185",port="443",ssl=true,username="admin",password="admin"},
240                new Device(){ ip="10.10.10.182",port="443",ssl=true,username="admin",password="admin"},
241                new Device(){ ip="10.10.10.186",port="443",ssl=true,username="admin",password="123"},
242                new Device(){ ip="10.10.10.188",port="443",ssl=true,username="admin",password="admin"}
243            };
244
245            // Create Altronix.LINQ.Listener Class instance
246            Listener atx = new Listener();
247
248            // Set user's callback for OnAlerts Event
249            atx.OnAlerts(UserCallback);
250
251            // Set user's callback for OnError Event
252            atx.OnError(UserErrorCallback);
253
254            // Start Listening on list of devices
255            atx.ListenAll(devices);
256
257        }
258    }
259
260  }