[r234]: / branches / aliazzz / mqttsparkplugb / SparkplugB / Function Blocks / FB_EoN_Node / FB_EoN_Node_1 / svnobj  Maximize  Restore  History

Download this file

66 lines (65 with data), 9.5 kB

 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
(*
    Edge of Network Node
    Binds together FB_MQTT_PubSub and FB_Sparkplug
*)
FUNCTION_BLOCK FB_EoN_node    
VAR_INPUT
    ServerUrl : STRING; // eg:'broker.hivemq.com';
    ServerPort : UINT; // eg: 1883
    KeepAlive : UINT; // eg: 60
    GroupId : WSTRING; // eg: "SparkplugB Device"
    NodeName : WSTRING; // eg: "Edge of Network Node 1"
    DeviceName : WSTRING; // eg: "Emulated Device"
    publishPeriod : UINT; // eg: 5000
    Username : WSTRING;
    Password : WSTRING;
END_VAR
VAR
    _Client : FB_MQTT_PubSub; // Handles all MQTT stuff
    _DeathPayload : FB_Payload; // Stores the most recent DeathPayload content, will update after each initialisation.
	_LastWillTopic : WSTRING(1024);
    _LastWillMessage : WSTRING(1024);
    _LastWillQos : MQTT.MQTT_QOS := MQTT.MQTT_QOS.QoS0;
    _LastWillRetain : BOOL := FALSE;
	_OnConnect : R_TRIG;
	_OnDisConnect : R_TRIG;
    _MessageType : SparkplugMessageTypes; 
	_RecvMesg : WSTRING(1024);
	_RecvTopic : WSTRING(1024);
	_Diag : STRING;
VAR CONSTANT
	// UTF-8 encoding only
	SPARKPLUG_B : WSTRING := "spBv1.0/";
	FORWARD_SLASH : WSTRING := "/";
	TOPIC_FILTER_POUND : WSTRING := "/#";
	TOPIC_FILTER_PLUS : WSTRING := "/+";
	NODE_DEATH_TOPIC : WSTRING := "/NDEATH/";
	NODE_BIRTH_TOPIC : WSTRING := "/NBIRTH/";
	DEVICE_DEATH_TOPIC : WSTRING := "/DDEATH/";
	DEVICE_BIRTH_TOPIC : WSTRING := "/DBIRTH/";
	NCMD : WSTRING := "/NCMD/";
	DCMD : WSTRING := "/DCMD/";
// Components
_Client();
_SparkplugB();
// Retry to reconnect until connected
IF NOT _Client.IsConnected() THEN
	// Set connection and update lastwill (new BdSeq)
	Initialise();
	// Connect
	_Client.Connect();
END_IF
_OnConnect( CLK := _Client.IsConnected() );
IF _OnConnect.Q THEN
	_RecvMesg := _Client.GetMessage1( MessageTopic => _RecvTopic );
	// Avoid copying around chunks of data
    SparkplugMessageTypes.NBIRTH : //Birth certificate for MQTT EoN nodes.
        ;
    SparkplugMessageTypes.DBIRTH : //Birth certificate forDevices.     
        PublishDeviceBirth();   
    SparkplugMessageTypes.STATE  ://Critical application state message  
END_CASE
// diagnose
_Client.ClientDiag();
_Client.PublisherDiag();
_Client.Subscriber1Diag();