Skip to content
Snippets Groups Projects
Commit 69615be4 authored by Stéphane LOS's avatar Stéphane LOS
Browse files

demo/142 a4a_k0_S7 : MQTT logging / hardening

parent d097d55b
Branches
No related tags found
No related merge requests found
Pipeline #577451628 passed
......@@ -58,6 +58,16 @@ package body A4A.MQTT_Client is
end if;
end Get_Name;
function Got_Ping_Response (Client : Instance) return Boolean is
begin
return Client.Ping_Response;
end Got_Ping_Response;
procedure Reset_Ping_Response (Client : in out Instance) is
begin
Client.Ping_Response := False;
end Reset_Ping_Response;
overriding
procedure On_Connect_Accepted
(Peer : in out Instance;
......@@ -84,8 +94,10 @@ package body A4A.MQTT_Client is
overriding
procedure On_Ping_Response (Peer : in out Instance) is
pragma Unreferenced (Peer);
-- pragma Unreferenced (Peer);
begin
Peer.Ping_Response := True;
A4A.Log.Logger.Put (Who => My_Ident & ".On_Ping_Response",
What => "Ping response",
Log_Level => Level_Info);
......
......@@ -36,6 +36,9 @@ package A4A.MQTT_Client is
function Get_Name (Client : Instance) return String;
function Got_Ping_Response (Client : Instance) return Boolean;
procedure Reset_Ping_Response (Client : in out Instance);
overriding
procedure On_Connect_Accepted
(Peer : in out Instance;
......@@ -76,8 +79,9 @@ private
type Instance is new MQTT_Peer with
record
Name : String_Ptr;
Action : Event; -- Connected/subscribed
Name : String_Ptr;
Action : Event; -- Connected/subscribed
Ping_Response : Boolean;
end record;
end A4A.MQTT_Client;
......@@ -22,6 +22,8 @@
-- --
------------------------------------------------------------------------------
with Ada.Exceptions; use Ada.Exceptions;
with A4A.Log;
with A4A.Logger; use A4A.Logger;
......@@ -47,7 +49,11 @@ package body A4A.User_Functions_MQTT is
-- MQTT tests
--------------------------------------------------------------------
Server_Address : constant String := "test.mosquitto.org";
-- Server_Address : constant String := "test.mosquitto.org";
Server_Address : constant String := "127.0.0.1";
Is_Connected : Boolean := False;
Ping_Sent : Boolean := False;
procedure MQTTClient_On_Start is
begin
......@@ -83,26 +89,41 @@ package body A4A.User_Functions_MQTT is
Server_Address,
GNAT.Sockets.MQTT.MQTT_Port);
while not Client.Is_Connected
loop -- Busy waiting
delay 0.1;
end loop;
delay 2.0;
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_On_Start",
What => "MQTT client connected to " & Server_Address,
Log_Level => Level_Info);
if Client.Is_Connected then
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_On_Start",
What => "Send_Connect",
Log_Level => Level_Info);
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_On_Start",
What => "MQTT client connected to " & Server_Address,
Log_Level => Level_Info);
Client.Send_Connect ("A4AMQTTclient", Keep_Alive => 60.0);
delay 1.0;
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_On_Start",
What => "Send_Connect",
Log_Level => Level_Info);
Client.Send_Connect ("A4AMQTTclient", Keep_Alive => 60.0);
delay 1.0;
Is_Connected := True;
else
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_On_Start",
What =>
"MQTT client could not connect to " & Server_Address,
Log_Level => Level_Info);
end if;
end;
exception
when Error : others =>
A4A.Log.Logger.Put (Who => My_Ident & ".MQTTClient_On_Start",
What => "Unexpected exception: " & CRLF
& Exception_Information (Error));
end MQTTClient_On_Start;
procedure MQTTClient_On_Stop is
......@@ -114,19 +135,28 @@ package body A4A.User_Functions_MQTT is
begin
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_On_Stop",
What => "Send_Disconnect",
Log_Level => Level_Info);
if Is_Connected then
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_On_Stop",
What => "Send_Disconnect",
Log_Level => Level_Info);
Client.Send_Disconnect;
delay 1.0;
Client.Send_Disconnect;
delay 1.0;
end if;
GNAT.Sockets.Server.Handles.Set
(A4A.User_Objects_MQTT.Reference, null);
A4A.User_Objects_MQTT.Server.Finalize;
exception
when Error : others =>
A4A.Log.Logger.Put (Who => My_Ident & ".MQTTClient_On_Stop",
What => "Unexpected exception: " & CRLF
& Exception_Information (Error));
end MQTTClient_On_Stop;
procedure MQTTClient_On_Error is
......@@ -143,27 +173,52 @@ package body A4A.User_Functions_MQTT is
begin
if A4A.User_Objects_MQTT.MW0 /= A4A.User_Objects.MW0 then
A4A.User_Objects_MQTT.MW0 := A4A.User_Objects.MW0;
if Ping_Sent and not Client.Got_Ping_Response then
Is_Connected := False;
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_Test",
What => "Send_Publish",
Log_Level => Level_Verbose);
-- Suscribe using :
-- mosquitto_sub -h test.mosquitto.org -t a4a/s7-315-2dp/mw0 -v
Client.Send_Publish
(Topic => "a4a/s7-315-2dp/mw0",
Message => Integer'Image
(Integer (Word_To_Int (A4A.User_Objects_MQTT.MW0))),
Packet => (QoS => GNAT.Sockets.MQTT.At_Most_Once),
Duplicate => False,
Retain => False);
What => "Got no Ping Response",
Log_Level => Level_Error);
end if;
Ping_Sent := False;
if Is_Connected then
Client.Reset_Ping_Response;
Client.Send_Ping;
Ping_Sent := True;
if A4A.User_Objects_MQTT.MW0 /= A4A.User_Objects.MW0 then
A4A.User_Objects_MQTT.MW0 := A4A.User_Objects.MW0;
A4A.Log.Logger.Put
(Who => My_Ident & ".MQTTClient_Test",
What => "Send_Publish",
Log_Level => Level_Verbose);
-- Suscribe using :
-- mosquitto_sub -h test.mosquitto.org -t a4a/s7-315-2dp/mw0 -v
Client.Send_Publish
(Topic => "a4a/s7-315-2dp/mw0",
Message => Integer'Image
(Integer (Word_To_Int (A4A.User_Objects_MQTT.MW0))),
Packet => (QoS => GNAT.Sockets.MQTT.At_Most_Once),
Duplicate => False,
Retain => False);
end if;
end if;
exception
when Error : others =>
A4A.Log.Logger.Put (Who => My_Ident & ".MQTTClient_Test",
What => "Unexpected exception: " & CRLF
& Exception_Information (Error));
end MQTTClient_Test;
end A4A.User_Functions_MQTT;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment