From daa87a3559defff4639babf0533cdcb8f41b29f2 Mon Sep 17 00:00:00 2001
From: Giulio Gaio <giulio.gaio@elettra.eu>
Date: Tue, 2 May 2023 22:34:41 +0200
Subject: [PATCH] Added states

---
 src/DataVampire.cpp             |  24 ++++--
 src/DataVampire.xmi             |  15 ++++
 src/DataVampireStateMachine.cpp | 139 +++++++++++++++++++++++++++-----
 3 files changed, 148 insertions(+), 30 deletions(-)

diff --git a/src/DataVampire.cpp b/src/DataVampire.cpp
index 1068df9..597532f 100644
--- a/src/DataVampire.cpp
+++ b/src/DataVampire.cpp
@@ -261,14 +261,22 @@ void DataVampire::init_device()
         	new_configuration.erase(pos-1, command_name.length()+1); // erase str2 from str1
     	}	
 	device_name = new_configuration;
-
-	device = new Tango::DeviceProxy(device_name);
-
-	Tango::CommandInfo cmd_info = device->command_query(command_name);
-	cmd_in_type = cmd_info.in_type;
-	cmd_out_type = cmd_info.out_type;
-	snprintf(*attr_CommandString_read, DATAVAMPIRE_MAX_STR_LEN, "%s", new_configuration.c_str());
-
+	
+	try {
+		device = new Tango::DeviceProxy(device_name);
+
+		Tango::CommandInfo cmd_info = device->command_query(command_name);
+		cmd_in_type = cmd_info.in_type;
+		cmd_out_type = cmd_info.out_type;
+		snprintf(*attr_CommandString_read, DATAVAMPIRE_MAX_STR_LEN, "%s", new_configuration.c_str());
+		set_state(Tango::ON);
+		set_status("Connected to device");
+	}	
+	catch(...) {
+		set_state(Tango::FAULT);
+		set_status("Failed to execute device command, check CommandString property and restart device");	
+	}	
+		
 	cache_buffer = 0;
 	allocate_cache_buffer();
 
diff --git a/src/DataVampire.xmi b/src/DataVampire.xmi
index 825420d..d931542 100644
--- a/src/DataVampire.xmi
+++ b/src/DataVampire.xmi
@@ -49,6 +49,7 @@
         <type xsi:type="pogoDsl:DoubleArrayType"/>
       </argout>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
+      <excludedStates>FAULT</excludedStates>
     </commands>
     <commands name="GetMean" description="" execMethod="get_mean" displayLevel="OPERATOR" polledPeriod="0" isDynamic="false">
       <argin description="mode,samples; mode,bunch start,bunch end">
@@ -58,6 +59,7 @@
         <type xsi:type="pogoDsl:DoubleArrayType"/>
       </argout>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
+      <excludedStates>FAULT</excludedStates>
     </commands>
     <commands name="GetHorPos" description="" execMethod="get_hor_pos" displayLevel="OPERATOR" polledPeriod="0" isDynamic="false">
       <argin description="mode,samples; mode,bunch start,bunch end">
@@ -67,6 +69,7 @@
         <type xsi:type="pogoDsl:DoubleArrayType"/>
       </argout>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
+      <excludedStates>FAULT</excludedStates>
     </commands>
     <commands name="GetVerPos" description="" execMethod="get_ver_pos" displayLevel="OPERATOR" polledPeriod="0" isDynamic="false">
       <argin description="mode,samples; mode,bunch start,bunch end">
@@ -76,6 +79,7 @@
         <type xsi:type="pogoDsl:DoubleArrayType"/>
       </argout>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
+      <excludedStates>FAULT</excludedStates>
     </commands>
     <commands name="GetHorSigma" description="" execMethod="get_hor_sigma" displayLevel="OPERATOR" polledPeriod="0" isDynamic="false">
       <argin description="mode,samples; mode,bunch start,bunch end">
@@ -85,6 +89,7 @@
         <type xsi:type="pogoDsl:DoubleArrayType"/>
       </argout>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
+      <excludedStates>FAULT</excludedStates>
     </commands>
     <commands name="GetVerSigma" description="" execMethod="get_ver_sigma" displayLevel="OPERATOR" polledPeriod="0" isDynamic="false">
       <argin description="mode,samples; mode,bunch start,bunch end">
@@ -94,6 +99,7 @@
         <type xsi:type="pogoDsl:DoubleArrayType"/>
       </argout>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
+      <excludedStates>FAULT</excludedStates>
     </commands>
     <commands name="GetValidValues" description="" execMethod="get_valid_values" displayLevel="OPERATOR" polledPeriod="0" isDynamic="false">
       <argin description="mode,samples; mode,bunch start,bunch end">
@@ -103,6 +109,7 @@
         <type xsi:type="pogoDsl:DoubleArrayType"/>
       </argout>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
+      <excludedStates>FAULT</excludedStates>
     </commands>
     <commands name="SetBackground" description="" execMethod="set_background" displayLevel="OPERATOR" polledPeriod="0" isDynamic="false">
       <argin description="">
@@ -112,6 +119,7 @@
         <type xsi:type="pogoDsl:VoidType"/>
       </argout>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
+      <excludedStates>FAULT</excludedStates>
     </commands>
     <attributes name="MinThreshold" attType="Scalar" rwType="READ_WRITE" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" memorized="true" memorizedAtInit="true" allocReadMember="true" isDynamic="false">
       <dataType xsi:type="pogoDsl:DoubleType"/>
@@ -160,6 +168,7 @@
       <dataReadyEvent fire="false" libCheckCriteria="true"/>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
       <properties description="" label="" unit="" standardUnit="" displayUnit="" format="%.5f" maxValue="9999999999" minValue="-9999999999" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
+      <readExcludedStates>FAULT</readExcludedStates>
     </attributes>
     <attributes name="Mean" attType="Scalar" rwType="READ" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" allocReadMember="true" isDynamic="false">
       <dataType xsi:type="pogoDsl:DoubleType"/>
@@ -168,6 +177,7 @@
       <dataReadyEvent fire="false" libCheckCriteria="true"/>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
       <properties description="" label="" unit="" standardUnit="" displayUnit="" format="%.5f" maxValue="9999999999" minValue="-9999999999" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
+      <readExcludedStates>FAULT</readExcludedStates>
     </attributes>
     <attributes name="HorPos" attType="Scalar" rwType="READ" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" allocReadMember="true" isDynamic="false">
       <dataType xsi:type="pogoDsl:DoubleType"/>
@@ -176,6 +186,7 @@
       <dataReadyEvent fire="false" libCheckCriteria="true"/>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
       <properties description="" label="" unit="" standardUnit="" displayUnit="" format="%.5f" maxValue="9999999999" minValue="-9999999999" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
+      <readExcludedStates>FAULT</readExcludedStates>
     </attributes>
     <attributes name="VerPos" attType="Scalar" rwType="READ" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" allocReadMember="true" isDynamic="false">
       <dataType xsi:type="pogoDsl:DoubleType"/>
@@ -184,6 +195,7 @@
       <dataReadyEvent fire="false" libCheckCriteria="true"/>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
       <properties description="" label="" unit="" standardUnit="" displayUnit="" format="%.5f" maxValue="9999999999" minValue="-9999999999" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
+      <readExcludedStates>FAULT</readExcludedStates>
     </attributes>
     <attributes name="HorSigma" attType="Scalar" rwType="READ" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" allocReadMember="true" isDynamic="false">
       <dataType xsi:type="pogoDsl:DoubleType"/>
@@ -192,6 +204,7 @@
       <dataReadyEvent fire="false" libCheckCriteria="true"/>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
       <properties description="" label="" unit="" standardUnit="" displayUnit="" format="%.5f" maxValue="9999999999" minValue="-9999999999" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
+      <readExcludedStates>FAULT</readExcludedStates>
     </attributes>
     <attributes name="VerSigma" attType="Scalar" rwType="READ" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" allocReadMember="true" isDynamic="false">
       <dataType xsi:type="pogoDsl:DoubleType"/>
@@ -200,6 +213,7 @@
       <dataReadyEvent fire="false" libCheckCriteria="true"/>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
       <properties description="" label="" unit="" standardUnit="" displayUnit="" format="%.5f" maxValue="9999999999" minValue="-9999999999" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
+      <readExcludedStates>FAULT</readExcludedStates>
     </attributes>
     <attributes name="ValidValues" attType="Scalar" rwType="READ" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" allocReadMember="true" isDynamic="false">
       <dataType xsi:type="pogoDsl:IntType"/>
@@ -208,6 +222,7 @@
       <dataReadyEvent fire="false" libCheckCriteria="true"/>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
       <properties description="" label="" unit="" standardUnit="" displayUnit="" format="%d" maxValue="99999999" minValue="0" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
+      <readExcludedStates>FAULT</readExcludedStates>
     </attributes>
     <attributes name="Enable" attType="Scalar" rwType="READ_WRITE" displayLevel="OPERATOR" polledPeriod="0" maxX="" maxY="" memorized="true" memorizedAtInit="true" allocReadMember="true" isDynamic="false">
       <dataType xsi:type="pogoDsl:BooleanType"/>
diff --git a/src/DataVampireStateMachine.cpp b/src/DataVampireStateMachine.cpp
index ae46882..bd72954 100644
--- a/src/DataVampireStateMachine.cpp
+++ b/src/DataVampireStateMachine.cpp
@@ -167,11 +167,20 @@ bool DataVampire::is_PollingSamples_allowed(TANGO_UNUSED(Tango::AttReqType type)
 bool DataVampire::is_Sum_allowed(TANGO_UNUSED(Tango::AttReqType type))
 {
 
-	//	Not any excluded states for Sum attribute in read access.
-	/*----- PROTECTED REGION ID(DataVampire::SumStateAllowed_READ) ENABLED START -----*/
+	//	Check access type.
+	if ( type==Tango::READ_REQ )
+	{
+		//	Compare device state with not allowed states for READ 
+		if (get_state()==Tango::FAULT)
+		{
+		/*----- PROTECTED REGION ID(DataVampire::SumStateAllowed_READ) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::SumStateAllowed_READ
+			return false;
+		}
+		return true;
+	}
 	return true;
 }
 
@@ -184,11 +193,20 @@ bool DataVampire::is_Sum_allowed(TANGO_UNUSED(Tango::AttReqType type))
 bool DataVampire::is_Mean_allowed(TANGO_UNUSED(Tango::AttReqType type))
 {
 
-	//	Not any excluded states for Mean attribute in read access.
-	/*----- PROTECTED REGION ID(DataVampire::MeanStateAllowed_READ) ENABLED START -----*/
+	//	Check access type.
+	if ( type==Tango::READ_REQ )
+	{
+		//	Compare device state with not allowed states for READ 
+		if (get_state()==Tango::FAULT)
+		{
+		/*----- PROTECTED REGION ID(DataVampire::MeanStateAllowed_READ) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::MeanStateAllowed_READ
+			return false;
+		}
+		return true;
+	}
 	return true;
 }
 
@@ -201,11 +219,20 @@ bool DataVampire::is_Mean_allowed(TANGO_UNUSED(Tango::AttReqType type))
 bool DataVampire::is_HorPos_allowed(TANGO_UNUSED(Tango::AttReqType type))
 {
 
-	//	Not any excluded states for HorPos attribute in read access.
-	/*----- PROTECTED REGION ID(DataVampire::HorPosStateAllowed_READ) ENABLED START -----*/
+	//	Check access type.
+	if ( type==Tango::READ_REQ )
+	{
+		//	Compare device state with not allowed states for READ 
+		if (get_state()==Tango::FAULT)
+		{
+		/*----- PROTECTED REGION ID(DataVampire::HorPosStateAllowed_READ) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::HorPosStateAllowed_READ
+			return false;
+		}
+		return true;
+	}
 	return true;
 }
 
@@ -218,11 +245,20 @@ bool DataVampire::is_HorPos_allowed(TANGO_UNUSED(Tango::AttReqType type))
 bool DataVampire::is_VerPos_allowed(TANGO_UNUSED(Tango::AttReqType type))
 {
 
-	//	Not any excluded states for VerPos attribute in read access.
-	/*----- PROTECTED REGION ID(DataVampire::VerPosStateAllowed_READ) ENABLED START -----*/
+	//	Check access type.
+	if ( type==Tango::READ_REQ )
+	{
+		//	Compare device state with not allowed states for READ 
+		if (get_state()==Tango::FAULT)
+		{
+		/*----- PROTECTED REGION ID(DataVampire::VerPosStateAllowed_READ) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::VerPosStateAllowed_READ
+			return false;
+		}
+		return true;
+	}
 	return true;
 }
 
@@ -235,11 +271,20 @@ bool DataVampire::is_VerPos_allowed(TANGO_UNUSED(Tango::AttReqType type))
 bool DataVampire::is_HorSigma_allowed(TANGO_UNUSED(Tango::AttReqType type))
 {
 
-	//	Not any excluded states for HorSigma attribute in read access.
-	/*----- PROTECTED REGION ID(DataVampire::HorSigmaStateAllowed_READ) ENABLED START -----*/
+	//	Check access type.
+	if ( type==Tango::READ_REQ )
+	{
+		//	Compare device state with not allowed states for READ 
+		if (get_state()==Tango::FAULT)
+		{
+		/*----- PROTECTED REGION ID(DataVampire::HorSigmaStateAllowed_READ) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::HorSigmaStateAllowed_READ
+			return false;
+		}
+		return true;
+	}
 	return true;
 }
 
@@ -252,11 +297,20 @@ bool DataVampire::is_HorSigma_allowed(TANGO_UNUSED(Tango::AttReqType type))
 bool DataVampire::is_VerSigma_allowed(TANGO_UNUSED(Tango::AttReqType type))
 {
 
-	//	Not any excluded states for VerSigma attribute in read access.
-	/*----- PROTECTED REGION ID(DataVampire::VerSigmaStateAllowed_READ) ENABLED START -----*/
+	//	Check access type.
+	if ( type==Tango::READ_REQ )
+	{
+		//	Compare device state with not allowed states for READ 
+		if (get_state()==Tango::FAULT)
+		{
+		/*----- PROTECTED REGION ID(DataVampire::VerSigmaStateAllowed_READ) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::VerSigmaStateAllowed_READ
+			return false;
+		}
+		return true;
+	}
 	return true;
 }
 
@@ -269,11 +323,20 @@ bool DataVampire::is_VerSigma_allowed(TANGO_UNUSED(Tango::AttReqType type))
 bool DataVampire::is_ValidValues_allowed(TANGO_UNUSED(Tango::AttReqType type))
 {
 
-	//	Not any excluded states for ValidValues attribute in read access.
-	/*----- PROTECTED REGION ID(DataVampire::ValidValuesStateAllowed_READ) ENABLED START -----*/
+	//	Check access type.
+	if ( type==Tango::READ_REQ )
+	{
+		//	Compare device state with not allowed states for READ 
+		if (get_state()==Tango::FAULT)
+		{
+		/*----- PROTECTED REGION ID(DataVampire::ValidValuesStateAllowed_READ) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::ValidValuesStateAllowed_READ
+			return false;
+		}
+		return true;
+	}
 	return true;
 }
 
@@ -508,11 +571,15 @@ bool DataVampire::is_Image_allowed(TANGO_UNUSED(Tango::AttReqType type))
 //--------------------------------------------------------
 bool DataVampire::is_GetSum_allowed(TANGO_UNUSED(const CORBA::Any &any))
 {
-	//	Not any excluded states for GetSum command.
+	//	Compare device state with not allowed states.
+	if (get_state()==Tango::FAULT)
+	{
 	/*----- PROTECTED REGION ID(DataVampire::GetSumStateAllowed) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::GetSumStateAllowed
+		return false;
+	}
 	return true;
 }
 
@@ -524,11 +591,15 @@ bool DataVampire::is_GetSum_allowed(TANGO_UNUSED(const CORBA::Any &any))
 //--------------------------------------------------------
 bool DataVampire::is_GetMean_allowed(TANGO_UNUSED(const CORBA::Any &any))
 {
-	//	Not any excluded states for GetMean command.
+	//	Compare device state with not allowed states.
+	if (get_state()==Tango::FAULT)
+	{
 	/*----- PROTECTED REGION ID(DataVampire::GetMeanStateAllowed) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::GetMeanStateAllowed
+		return false;
+	}
 	return true;
 }
 
@@ -540,11 +611,15 @@ bool DataVampire::is_GetMean_allowed(TANGO_UNUSED(const CORBA::Any &any))
 //--------------------------------------------------------
 bool DataVampire::is_GetHorPos_allowed(TANGO_UNUSED(const CORBA::Any &any))
 {
-	//	Not any excluded states for GetHorPos command.
+	//	Compare device state with not allowed states.
+	if (get_state()==Tango::FAULT)
+	{
 	/*----- PROTECTED REGION ID(DataVampire::GetHorPosStateAllowed) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::GetHorPosStateAllowed
+		return false;
+	}
 	return true;
 }
 
@@ -556,11 +631,15 @@ bool DataVampire::is_GetHorPos_allowed(TANGO_UNUSED(const CORBA::Any &any))
 //--------------------------------------------------------
 bool DataVampire::is_GetVerPos_allowed(TANGO_UNUSED(const CORBA::Any &any))
 {
-	//	Not any excluded states for GetVerPos command.
+	//	Compare device state with not allowed states.
+	if (get_state()==Tango::FAULT)
+	{
 	/*----- PROTECTED REGION ID(DataVampire::GetVerPosStateAllowed) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::GetVerPosStateAllowed
+		return false;
+	}
 	return true;
 }
 
@@ -572,11 +651,15 @@ bool DataVampire::is_GetVerPos_allowed(TANGO_UNUSED(const CORBA::Any &any))
 //--------------------------------------------------------
 bool DataVampire::is_GetHorSigma_allowed(TANGO_UNUSED(const CORBA::Any &any))
 {
-	//	Not any excluded states for GetHorSigma command.
+	//	Compare device state with not allowed states.
+	if (get_state()==Tango::FAULT)
+	{
 	/*----- PROTECTED REGION ID(DataVampire::GetHorSigmaStateAllowed) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::GetHorSigmaStateAllowed
+		return false;
+	}
 	return true;
 }
 
@@ -588,11 +671,15 @@ bool DataVampire::is_GetHorSigma_allowed(TANGO_UNUSED(const CORBA::Any &any))
 //--------------------------------------------------------
 bool DataVampire::is_GetVerSigma_allowed(TANGO_UNUSED(const CORBA::Any &any))
 {
-	//	Not any excluded states for GetVerSigma command.
+	//	Compare device state with not allowed states.
+	if (get_state()==Tango::FAULT)
+	{
 	/*----- PROTECTED REGION ID(DataVampire::GetVerSigmaStateAllowed) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::GetVerSigmaStateAllowed
+		return false;
+	}
 	return true;
 }
 
@@ -604,11 +691,15 @@ bool DataVampire::is_GetVerSigma_allowed(TANGO_UNUSED(const CORBA::Any &any))
 //--------------------------------------------------------
 bool DataVampire::is_GetValidValues_allowed(TANGO_UNUSED(const CORBA::Any &any))
 {
-	//	Not any excluded states for GetValidValues command.
+	//	Compare device state with not allowed states.
+	if (get_state()==Tango::FAULT)
+	{
 	/*----- PROTECTED REGION ID(DataVampire::GetValidValuesStateAllowed) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::GetValidValuesStateAllowed
+		return false;
+	}
 	return true;
 }
 
@@ -620,11 +711,15 @@ bool DataVampire::is_GetValidValues_allowed(TANGO_UNUSED(const CORBA::Any &any))
 //--------------------------------------------------------
 bool DataVampire::is_SetBackground_allowed(TANGO_UNUSED(const CORBA::Any &any))
 {
-	//	Not any excluded states for SetBackground command.
+	//	Compare device state with not allowed states.
+	if (get_state()==Tango::FAULT)
+	{
 	/*----- PROTECTED REGION ID(DataVampire::SetBackgroundStateAllowed) ENABLED START -----*/
 	/* clang-format on */
 	/* clang-format off */
 	/*----- PROTECTED REGION END -----*/	//	DataVampire::SetBackgroundStateAllowed
+		return false;
+	}
 	return true;
 }
 
-- 
GitLab