-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
358 lines (306 loc) · 12.1 KB
/
Program.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
using MQTTnet;
using MQTTnet.Client.Connecting;
using MQTTnet.Client.Disconnecting;
using MQTTnet.Client.Options;
using MQTTnet.Extensions.ManagedClient;
using MQTTnet.Formatter;
using MQTTnet.Protocol;
using MQTTnet.Server;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Timer = System.Timers.Timer;
/*
*
I could not find a good MQTT console app example the only one that work seemed to be one from a Windows form example combined with a managed client console application
that did not work.
https://dzone.com/articles/mqtt-publishing-and-subscribing-messages-to-mqtt-b
https://github.com/chkr1011/MQTTnet/wiki
*
*
*/
namespace Fanuc13319
{
class Program
{
// short _ret = 0; // Stores our return value
private IManagedMqttClient managedMqttClientPublisher;
string mqttServer = "10.1.1.83";
Node[] nodes;
MqttFactory mqttFactory;
/// <summary>
/// The managed publisher client.
/// </summary>
// static IManagedMqttClient managedMqttClientPublisher;
static void Main(string[] args)
{
// ushort handle = 0;
Program myObj = new Program();
SettingsReader sr = new SettingsReader();
myObj.nodes = (Node[])sr.LoadConfig(typeof(Node[]));
// myObj.Test();
// return;
myObj.MQTTConnect();
var timer = new Timer
{
AutoReset = true,
Enabled = true,
Interval = 60000
};
timer.Elapsed += myObj.TimerElapsed;
// myObj.GetCounter(526);
// System.Threading.Thread.Sleep( Timeout.InfiniteTimeSpan);
// System.Threading.Thread.Sleep(35000);
// Put in OnExit() cleanup handler
Console.ReadLine();
myObj.MQTTStop();
myObj.FanucCleanup();
// Focas1.cnc_freelibhndl(myObj.fanucHandle); // Not sure if I should free this handle indescriminately
}
private void Test()
{
DateTime localDate = DateTime.Now;
string transDate = localDate.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(transDate);
MQTTConnect();
foreach (Node node in nodes)
{
try
{
decimal value = GetVariable(node.ip, node.variable);
// Only publish value if it has changed.
if (value != node.value)
{
node.value = value;
node.transDate = transDate;
MQTTPublish(node);
}
}
catch (Exception e)
{
// no cleanup
continue;
}
}
MQTTStop();
}
private void FanucCleanup()
{
// Focas1.cnc_freelibhndl(myObj.fanucHandle); // Not sure if I should free this handle indescriminately
Console.WriteLine("All Fanuc Handles have been closed.");
}
private async void MQTTStop()
{
await managedMqttClientPublisher.StopAsync();
managedMqttClientPublisher = null;
Console.WriteLine("MQTT Connection has been stopped");
}
private async void MQTTConnect()
{
mqttFactory = new MqttFactory();
var tlsOptions = new MqttClientTlsOptions
{
UseTls = false,
IgnoreCertificateChainErrors = true,
IgnoreCertificateRevocationErrors = true,
AllowUntrustedCertificates = true
};
var options = new MqttClientOptions
{
ClientId = "ClientPublisher",
ProtocolVersion = MqttProtocolVersion.V311,
ChannelOptions = new MqttClientTcpOptions
{
Server = "10.1.1.83",
Port = 1883, // Test only
// Port = 1882, // Production
TlsOptions = tlsOptions
}
};
if (options.ChannelOptions == null)
{
throw new InvalidOperationException();
}
options.Credentials = new MqttClientCredentials
{
Username = "username",
Password = Encoding.UTF8.GetBytes("password")
};
options.CleanSession = true;
options.KeepAlivePeriod = TimeSpan.FromSeconds(5);
managedMqttClientPublisher = mqttFactory.CreateManagedMqttClient();
managedMqttClientPublisher.UseApplicationMessageReceivedHandler(HandleReceivedApplicationMessage);
managedMqttClientPublisher.ConnectedHandler = new MqttClientConnectedHandlerDelegate(OnPublisherConnected);
managedMqttClientPublisher.DisconnectedHandler = new MqttClientDisconnectedHandlerDelegate(OnPublisherDisconnected);
await managedMqttClientPublisher.StartAsync(
new ManagedMqttClientOptions
{
ClientOptions = options
});
}
/// <summary>
/// The method that handles the timer events.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The event args.</param>
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
DateTime localDate = DateTime.Now;
string transDate = localDate.ToString("yyyy-MM-dd HH:mm:ss");
Console.WriteLine(transDate);
foreach (Node node in nodes)
{
try
{
decimal value = GetVariable(node.ip, node.variable);
// Only publish value if it has changed.
if (value != node.value)
{
node.value = value;
node.transDate = transDate;
MQTTPublish(node);
}
}
catch (Exception ex)
{
// no cleanup
continue;
}
}
}
async void MQTTPublish(Node node)
{
try
{
string test = @"{
'updateId': 5,
'nodeId': 'ns=2;s=cnc362.cnc362.Cycle_Counter_Shift_SL',
'name':'Cycle_Counter_Shift_SL',
'plexus_Customer_No':'310507',
'pcn': 'Avilla',
'workcenter_Key': '61314',
'workcenter_Code': 'Honda Civic cnc 359 362',
'cnc': '362',
'value': 0,
'transDate': '2020-06-29 00:00:00'
}";
Console.WriteLine("node.nodeId {0}", node.nodeId);
Console.WriteLine("node.name {0}", node.name);
// https://stackoverflow.com/questions/7574606/left-function-in-c-sharp/7574645
// const transDate = moment(new Date()).format("YYYY-MM-DDTHH:mm:ss");
// yyyy-MM
string json2 = JsonConvert.SerializeObject(node);
Console.WriteLine("json2=> {0}", json2);
var payload = Encoding.UTF8.GetBytes(json2);
// var payload = Encoding.UTF8.GetBytes(partCounter);
var message = new MqttApplicationMessageBuilder().WithTopic("Fanuc13319").WithPayload(payload).WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtLeastOnce).WithRetainFlag().Build();
if (managedMqttClientPublisher != null)
{
await managedMqttClientPublisher.PublishAsync(message);
Console.WriteLine("Published Message=>{0}", message);
}
else
{
throw new Exception("MQTTPublish => Not connected to MQTT server");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw ex; //bubble up
}
}
/* number is variable number to be read.
* GetCounter(526);//cnc362 10.1.90.4
GetCounter(511);//cnc422 10.1.90.5
GetCounter(501);//cnc422 10.1.90.2
*
*/
decimal GetVariable(string ip,short variable)
{
short ret = -1;
decimal value= -1;
ushort fanucHandle = 0;
try
{
ret = Focas1.cnc_allclibhndl3(ip, 8193, 6, out fanucHandle);
if (ret == Focas1.EW_OK)
{
Console.WriteLine("We are connected!");
}
else
{
// Console.WriteLine("There was an error connecting. Return value: " + _ret);
throw new Exception("There was an error connecting. Return value: " + ret);
}
Focas1.ODBM macro = new Focas1.ODBM();
string strVal;
ret = Focas1.cnc_rdmacro(fanucHandle, variable, 10, macro);
if (ret == Focas1.EW_OK)
{
// mcr_val = 406000000
// dec_val = 6
// value = 406.000000
strVal = string.Format("{0:d9}", Math.Abs(macro.mcr_val));
if (0 < macro.dec_val) strVal = strVal.Insert(9 - macro.dec_val, ".");
if (macro.mcr_val < 0) strVal = "-" + strVal;
decimal decimalVal;
decimalVal = Convert.ToDecimal(strVal);
Console.WriteLine("String converted to decimal = {0} ", decimalVal);
value = decimalVal;
//partCounter = Convert.ToInt32(decimalVal);
// partCounter2 = macro.mcr_val;
}
else
{
throw new Exception("There was an error reading the macro variable. Return value: " + ret);
// Console.WriteLine("**********");
}
}
catch (Exception e)
{
Console.WriteLine("Exception in GetVariable => {0}", e);
throw e; // will bubble up after finally
}
finally
{
if (fanucHandle != 0)
{
Focas1.cnc_freelibhndl(fanucHandle);
Console.WriteLine("Handle has been freed");
}
}
return value;
}
/// <summary>
/// Handles the publisher connected event.
/// </summary>
/// <param name="x">The MQTT client connected event args.</param>
private static void OnPublisherConnected(MqttClientConnectedEventArgs x)
{
Console.WriteLine("Publisher Connected");
}
/// <summary>
/// Handles the publisher disconnected event.
/// </summary>
/// <param name="x">The MQTT client disconnected event args.</param>
private static void OnPublisherDisconnected(MqttClientDisconnectedEventArgs x)
{
Console.WriteLine("Publisher Disconnected");
}
/// <summary>
/// Handles the received application message event.
/// </summary>
/// <param name="x">The MQTT application message received event args.</param>
private void HandleReceivedApplicationMessage(MqttApplicationMessageReceivedEventArgs x)
{
var item = $"Timestamp: {DateTime.Now:O} | Topic: {x.ApplicationMessage.Topic} | Payload: {x.ApplicationMessage.ConvertPayloadToString()} | QoS: {x.ApplicationMessage.QualityOfServiceLevel}";
// this.BeginInvoke((MethodInvoker)delegate { this.TextBoxSubscriber.Text = item + Environment.NewLine + this.TextBoxSubscriber.Text; });
}
}
}