From b83c069b0b78d0ad9d26a8213d451c7f082cc578 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Thu, 25 Apr 2019 12:20:54 +0100
Subject: [PATCH] Move power-btn and operable props into base class

AbstractInstrument now handles a generic power-btn and operable
property, and hence includes them in the serviceable-and-powered
check.
---
 src/Instrumentation/AbstractInstrument.cxx | 19 ++++++++++++++++-
 src/Instrumentation/AbstractInstrument.hxx |  2 ++
 src/Instrumentation/adf.cxx                | 13 ++----------
 src/Instrumentation/adf.hxx                |  2 --
 src/Instrumentation/navradio.cxx           | 24 ++--------------------
 src/Instrumentation/navradio.hxx           |  7 -------
 6 files changed, 24 insertions(+), 43 deletions(-)

diff --git a/src/Instrumentation/AbstractInstrument.cxx b/src/Instrumentation/AbstractInstrument.cxx
index 20b3c23e4..22bf28b25 100644
--- a/src/Instrumentation/AbstractInstrument.cxx
+++ b/src/Instrumentation/AbstractInstrument.cxx
@@ -48,12 +48,29 @@ void AbstractInstrument::initServicePowerProperties(SGPropertyNode* node)
     if (_serviceableNode->getType() == simgear::props::NONE)
         _serviceableNode->setBoolValue(true);
     
+    _powerButtonNode = node->getChild("power-btn", 0, true);
+    
+    // if the user didn't define a node, default to true
+    if (_powerButtonNode->getType() == simgear::props::NONE)
+        _powerButtonNode->setBoolValue(true);
+    
     _powerSupplyNode = fgGetNode(_powerSupplyPath, true);
+    
+     node->tie( "operable", SGRawValueMethods<AbstractInstrument,bool>
+               ( *this, &AbstractInstrument::isServiceableAndPowered ) );
+}
+
+void AbstractInstrument::unbind()
+{
+    auto nd = fgGetNode(nodePath());
+    if (nd) {
+        nd->untie("operable");
+    }
 }
 
 bool AbstractInstrument::isServiceableAndPowered() const
 {
-    if (!_serviceableNode->getBoolValue())
+    if (!_serviceableNode->getBoolValue() || !_powerButtonNode->getBoolValue())
         return false;
     
     if (_powerSupplyNode->getDoubleValue() < _minimumSupplyVolts)
diff --git a/src/Instrumentation/AbstractInstrument.hxx b/src/Instrumentation/AbstractInstrument.hxx
index b4233b7cb..788a06374 100644
--- a/src/Instrumentation/AbstractInstrument.hxx
+++ b/src/Instrumentation/AbstractInstrument.hxx
@@ -38,12 +38,14 @@ protected:
     std::string name() const { return _name; }
     int number() const { return _index; }
     
+    void unbind() override;
 private:
     std::string _name;
     int _index = 0;
     std::string _powerSupplyPath;
     
     SGPropertyNode_ptr _serviceableNode;
+    SGPropertyNode_ptr _powerButtonNode;
     double _minimumSupplyVolts;
     SGPropertyNode_ptr _powerSupplyNode;
 };
diff --git a/src/Instrumentation/adf.cxx b/src/Instrumentation/adf.cxx
index 0b531c315..60f4ff094 100644
--- a/src/Instrumentation/adf.cxx
+++ b/src/Instrumentation/adf.cxx
@@ -96,9 +96,7 @@ ADF::init ()
     _bearing_node       = node->getChild("indicated-bearing-deg", 0, true);
     _ident_node         = node->getChild("ident", 0, true);
     _ident_audible_node = node->getChild("ident-audible", 0, true);
-    _power_btn_node     = node->getChild("power-btn", 0, true);
-    _operable_node      = node->getChild("operable", 0, true);
-
+  
     // frequency properties
     SGPropertyNode *fnode = node->getChild("frequencies", 0, true);
     _frequency_node       = fnode->getChild("selected-khz", 0, true);
@@ -106,10 +104,6 @@ ADF::init ()
     // foreign simulator properties
     _heading_node       = fgGetNode("/orientation/heading-deg", true);
 
-    // backward compatibility check
-    if (_power_btn_node->getType() == simgear::props::NONE) 
-      _power_btn_node->setBoolValue(true); // front end didn't implement a power button
-
     // sound support (audible ident code)
     SGSoundMgr *smgr = globals->get_subsystem<SGSoundMgr>();
     _sgr = smgr->find("avionics", true);
@@ -124,15 +118,12 @@ void
 ADF::update (double delta_time_sec)
 {
     // If it's off, don't waste any time.
-    if (!isServiceableAndPowered() || !_power_btn_node->getBoolValue()) {
+    if (!isServiceableAndPowered()) {
         _in_range_node->setBoolValue(false);
-        _operable_node->setBoolValue(false);
         _ident_node->setStringValue("");
         return;
     }
 
-    _operable_node->setBoolValue(true);
-
     string mode = _mode_node->getStringValue();
     if (mode == "ant" || mode == "test") set_bearing(delta_time_sec, 90);
     if (mode != "bfo" && mode != "adf") {
diff --git a/src/Instrumentation/adf.hxx b/src/Instrumentation/adf.hxx
index 8f63d2d6d..56a491bcd 100644
--- a/src/Instrumentation/adf.hxx
+++ b/src/Instrumentation/adf.hxx
@@ -64,8 +64,6 @@ private:
     SGPropertyNode_ptr _ident_node;
     SGPropertyNode_ptr _ident_audible_node;
     SGPropertyNode_ptr _volume_node;
-    SGPropertyNode_ptr _power_btn_node;
-    SGPropertyNode_ptr _operable_node;
 
     double _time_before_search_sec;
 
diff --git a/src/Instrumentation/navradio.cxx b/src/Instrumentation/navradio.cxx
index 89e25ce92..20b77e393 100644
--- a/src/Instrumentation/navradio.cxx
+++ b/src/Instrumentation/navradio.cxx
@@ -103,7 +103,6 @@ static std::unique_ptr<SGInterpTable> static_terminalRangeInterp,
 
 // Constructor
 FGNavRadio::FGNavRadio(SGPropertyNode *node) :
-    _operable(false),
     play_count(0),
     _nav_search(true),
     _last_freq(0.0),
@@ -174,8 +173,6 @@ FGNavRadio::init ()
     
     // inputs
     is_valid_node = node->getChild("data-is-valid", 0, true);
-    power_btn_node = node->getChild("power-btn", 0, true);
-    power_btn_node->setBoolValue( true );
     vol_btn_node = node->getChild("volume", 0, true);
     ident_btn_node = node->getChild("ident", 0, true);
     ident_btn_node->setBoolValue( true );
@@ -278,20 +275,6 @@ FGNavRadio::reinit ()
     _time_before_search_sec = -1.0;
 }
 
-void
-FGNavRadio::bind ()
-{
-    _radio_node->tie( "operable", SGRawValueMethods<FGNavRadio,bool>( *this, &FGNavRadio::isOperable ) );
-}
-
-
-void
-FGNavRadio::unbind ()
-{
-    _radio_node->untie("operable");
-}
-
-
 // model standard VOR/DME/TACAN service volumes as per AIM 1-1-8
 double FGNavRadio::adjustNavRange( double stationElev, double aircraftElev,
                                  double nominalRange )
@@ -379,9 +362,8 @@ FGNavRadio::update(double dt)
     return; // paused
   }
     
-  if (!isServiceableAndPowered() || !power_btn_node->getBoolValue())
+  if (isServiceableAndPowered())
   {
-    _operable = true;
     updateReceiver(dt);
     updateCDI(dt);
   } else {
@@ -426,7 +408,6 @@ void FGNavRadio::clearOutputs()
   is_valid_node->setBoolValue(false);
   nav_id_node->setStringValue("");
   
-  _operable = false;
   _navaid = NULL;
 }
 
@@ -866,8 +847,7 @@ void FGNavRadio::updateAudio( double dt )
   
 	// play station ident via audio system if on + ident,
 	// otherwise turn it off
-  if (!power_btn_node->getBoolValue()
-      || !isServiceableAndPowered()
+  if (!isServiceableAndPowered()
       || !ident_btn_node->getBoolValue()
       || !audio_btn_node->getBoolValue() ) {
     _audioIdent->setIdent("", 0.0 );
diff --git a/src/Instrumentation/navradio.hxx b/src/Instrumentation/navradio.hxx
index edd01da94..4ee81c7ae 100644
--- a/src/Instrumentation/navradio.hxx
+++ b/src/Instrumentation/navradio.hxx
@@ -41,7 +41,6 @@ class FGNavRadio : public AbstractInstrument, public SGPropertyChangeListener
     // property inputs
     SGPropertyNode_ptr is_valid_node;   // is station data valid (may be way out
                                         // of range.)
-    SGPropertyNode_ptr power_btn_node;
     SGPropertyNode_ptr freq_node;       // primary freq
     SGPropertyNode_ptr alt_freq_node;   // standby freq
     SGPropertyNode_ptr is_loc_freq_node;// is the primary freq a loc/gs (paired) freq?
@@ -113,7 +112,6 @@ class FGNavRadio : public AbstractInstrument, public SGPropertyChangeListener
 
     // internal (private) values
 
-    bool _operable; ///< is the unit serviceable, on, powered, etc
     int play_count;
     bool _nav_search;
     double _last_freq;
@@ -167,9 +165,6 @@ class FGNavRadio : public AbstractInstrument, public SGPropertyChangeListener
 
     FGNavRecord* findPrimaryNavaid(const SGGeod& aPos, double aFreqMHz);
     
-    /// accessor for tied, read-only 'operable' property
-    bool isOperable() const
-      { return _operable; }
       
   // implement SGPropertyChangeListener
     virtual void valueChanged (SGPropertyNode * prop);
@@ -180,8 +175,6 @@ public:
 
     void init ();
     void reinit ();
-    void bind ();
-    void unbind ();
     void update (double dt);
 
     // Update nav/adf radios based on current postition