~vbursian/research-assistant/intervers

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
////////////////////////////////////////////////////////////////////////////////
/*! @file Device.cpp   Classes sDevice and sDeviceTag.
- Part of RANet - Research Assistant Net Library.
- Copyright(C) 2010-2014, Viktor E. Bursian, St.Petersburg, Russia.
                          Viktor.Bursian@mail.ioffe.ru
*///////////////////////////////////////////////////////////////////////////////
#include "Device.h"
#include "BasicNodes.h"
#include "attr.h"
namespace RA {
//------------------------------------------------------------------------------

//--------------------------------------------------------------- sDeviceTag ---

sDeviceTag::sDeviceTag (literal          classname
                       ,eInstrumentType  classtype
                       ,pfPreconfigure   preconf)
    :sInstrumentTag(classname,classtype)
    ,ThePreconfigureFunctionPtr(preconf)
{
  AvailableClassList[sString(classname)]=classtype;
}

//------------------------------------------------------------------ sDevice ---
/*! @class sDevice


@todo{Instruments} Add functionality: Иерархию классов по именам

*/


void  sDevice::Preconfigure (sNodePtr  DeviceCfg)
{
  if( ! DeviceCfg.IsNULL() ){
    DeviceCfg |= attr("Disabled",false);
    DeviceCfg |= attr("TraceStates",false);
  }
}


bool  sDevice::IsRunning (sString  DeviceId)
{
  map<sString,psDevice>::iterator    D = RunningDevicesByName
                                                            .find(DeviceId);
  return ( D != RunningDevicesByName.end() );
}


bool  sDevice::Matches (sString  ClassName
                       ,sString  /*BaseClassName*/)
{
  //! @patch{Instruments} Device genealogy should be based on Adam's genealogy
  return IsDriver(ClassName);
}


sString  sDevice::InstallNew (sString  ClassName)
{
  if( DeviceList.IsCorrect() ){
    sPtr<sFolderNode>              DeviceCfg;
    sString                     DeviceId;
    int                         i = 0;
    do{
      DeviceId = ClassName.Tail(1) + "-";
      DeviceId << (++i);
      DeviceCfg = DeviceList >> DeviceId;
    }while( DeviceCfg.IsCorrect() );
    DeviceList |= DeviceId;
    DeviceCfg = DeviceList >> DeviceId;
    if( DeviceCfg.IsCorrect() ){
      DeviceCfg << attr("Class",ClassName);
      sPtr<sTextNode>             ClassNameNP;
      ClassNameNP = DeviceCfg >> "Class";
      if( ClassNameNP.IsCorrect() ){
        pcsTag                      Tag = sTag::ByName(ClassName);
        if( Tag ){
          pcsDeviceTag         DTag = dynamic_cast<pcsDeviceTag>(Tag);
          if( DTag ){
            (*(DTag->ThePreconfigureFunctionPtr))(DeviceCfg);
          }else{
            throw xProgramError("sDevice::InstallNew(): wrong ClassName"
                               ,__FILE__,__LINE__);
          }
        }else{
          throw xProgramError("sDevice::InstallNew(): wrong ClassName"
                             ,__FILE__,__LINE__);
        }
        return DeviceId;
      }
    }
  }
  return sString();
}


sDevice::~sDevice ()
{
  int n = RunningDevicesByName.erase(DeviceIdentifier());
  ABORT_IF_NOT(n==1);
  DeviceLinks.SetToNULL();
  DeviceSettings.SetToNULL();
  DeviceConfig.SetToNULL();
}


sDevice::sDevice (sString   DeviceId
                 ,sNodePtr  DeviceCfg)
    :DeviceConfig(DeviceCfg)
    ,TheDeviceIdentifier(DeviceId)
    ,InvalidFlag(false)
    ,TraceStatesFlag(false)
{
  std::pair<map<sString,psDevice>::iterator,bool>  P;
  P=RunningDevicesByName.insert(
          std::pair<sString,psDevice>(DeviceIdentifier(),this)
                                          );
  ASSERT(P.second);
//  можно проще, поскольку здесь проверка не нужна:
//  CreatedDriverByName[DeviceIdentifier]=this;

  //!@todo{NetIO} Обращение к сети в конструкторе sDevice.  Вытащить?
  DeviceSettings = DeviceConfig >> "Settings";
  DeviceLinks = DeviceConfig >> "Links";
  get_from<sBooleanNode>(DeviceConfig>>"TraceStates",TraceStatesFlag);
}


void sDevice::SetInvalid (const QString &  reason)
{
  InvalidFlag=true;
  if( ! TheInvalidityReason.isEmpty() )
    TheInvalidityReason+='\n';
  TheInvalidityReason+=reason;
}

//------------------------------------------------------------------------------
} //namespace RA