FACT++  1.0
int StateMachineEventServer::Execute ( )
inlineprivatevirtual

Is called continously to execute actions in the current state.

This is what the state machine is doing in a certain state continously. In an idle state this might just be doing nothing.

In the tracking state of the drive system this might be sending new command values to the drive based on its current position.

The current state of the state machine can be accessed by GetCurrentState()

Returns
Usually it should just return the current state. However, sometimes execution might lead to a new state, e.g. when a hardware error is detected. In this case a new state can be returned to put the state machine into a different state. Note, that the function is responsible of doing all actions connected with the state change itself. If not overwritten it returns the current status.

Reimplemented from StateMachineImp.

Definition at line 58 of file evtserver.cc.

References dev, Error(), Tools::Form(), fits::GetNextRow(), Nova::GetSolarRst(), i, izstream::is_open(), Time::JavaDate(), Time::JD(), EventServer::State::kIdle, EventServer::State::kRunning, StateMachineImp::kSM_Error, StateMachineImp::kSM_Ready, EventServer::State::kStandby, Time::NightAsInt(), fits::SetRefAddress(), fits::SetVecAddress(), and DimState::state().

59  {
62 
63  // Nothing to do if not started
66 
67  if (fDimFadControl.state()==FAD::State::kRunInProgress)
69 
70  // Run only once in 5s
71  const Time now;
72  if (now<fTimer+boost::posix_time::milliseconds(fInterval))
73  return GetCurrentState();
74  fTimer = now;
75 
76  // Running is only allowed when sun is up
77  const Nova::RstTime rst = Nova::GetSolarRst(now.JD()-0.5);
78  const bool isUp =
79  (rst.rise<rst.set && (now.JD()>rst.rise && now.JD()<rst.set)) ||
80  (rst.rise>rst.set && (now.JD()<rst.set || now.JD()>rst.rise));
81 
82  // FIXME: What about errors?
83  if (!isUp)
85 
86  // Check if file has to be changed
87  const uint32_t night = fStartDate>0 ? fStartDate : Time().NightAsInt()-1;
88 
89  if (fNight!=night)
90  {
91  fNight = night;
92 
93  delete fIn;
94 
95  const string name = fAuxPath + Tools::Form("/%04d/%02d/%02d/%08d.FAD_CONTROL_EVENT_DATA.fits", night/10000, (night/100)%100, night%100, night);
96 
97  fIn = new fits(name);
98  if (!fIn->is_open())
99  {
100  Error(string("Failed to open "+name+": ")+strerror(errno));
102  }
103 
104  // Requiered columns
105  try
106  {
107  fIn->SetRefAddress("QoS", fQoS);
108  fIn->SetRefAddress("Time", fTime);
109  fIn->SetVecAddress("max", fMax);
110  //fIn->SetVecAddress(fRms);
111  //fIn->SetVecAddress(fMax);
112  //fIn->SetVecAddress(fPos);
113  }
114  catch (const runtime_error &e)
115  {
116  delete fIn;
117  fIn = 0;
118 
119  Error("Failed to open "+name+": "+e.what());
121  }
122 
123  // Non requiered columns
124  fRun = 0;
125  fEvt = 0;
126  try
127  {
128  fIn->SetRefAddress("run", fRun);
129  fIn->SetRefAddress("evt", fEvt);
130  }
131  catch (const runtime_error &e) { }
132 
133  Info("File "+name+" open.");
134  }
135 
138 
139  // Get next data event
140  vector<float> sorted;
141  while (1)
142  {
143  if (!fIn->GetNextRow())
144  {
145  fNight = 0;
147  }
148 
149  // Select a
150  if (fQoS & FAD::EventHeader::kAll)
151  continue;
152 
153  for (int i=0; i<1440; i++)
154  fMax[i] /= 1000;
155 
156  for (int i=8; i<1440; i+=9)
157  fMax[i] = fMax[i-2];
158 
159  // construct output
160  sorted = fMax;
161  sort(sorted.begin(), sorted.end());
162 
163  const double med = sorted[719];
164 
165  vector<float> dev(1440);
166  for (int i=0; i<1440; i++)
167  dev[i] = fabs(sorted[i]-med);
168  sort(dev.begin(), dev.end());
169 
170  const double deviation = dev[uint32_t(0.682689477208650697*1440)];
171 
172  // In a typical shower or muon ring, the first
173  // few pixels will have comparable brightness,
174  // in a NSB event not. Therefore, the 4th brightest
175  // pixel is a good reference.
176  if (sorted[1439-3]>med+deviation*5)
177  break;
178  }
179 
180  const double scale = max(0.25f, sorted[1436]);
181 
182  ostringstream out;
183  out << Time(fTime+40587).JavaDate() << '\n';
184  out << "0\n";
185  out << scale << '\n';
186  out << setprecision(3);
187  if (fRun>0)
188  //out << "DEMO [" << fEvt << "]\nDEMO [" << fRun << "]\nDEMO\x7f";
189  out << fEvt << '\n' << fRun << "\nDEMO\x7f";
190  else
191  out << "DEMO\nDEMO\nDEMO\x7f";
192 
193  //out << sorted[1439] << '\n';
194  //out << sorted[719] << '\n';
195  //out << sorted[0] << '\x7f';
196 
197  // The valid range is from 1 to 127
198  // \0 is used to seperate different curves
199  vector<uint8_t> val(1440);
200  for (uint64_t i=0; i<1440; i++)
201  {
202  float range = nearbyint(126*fMax[i]/scale); // [-2V; 2V]
203  if (range>126)
204  range=126;
205  if (range<0)
206  range=0;
207  val[i] = (uint8_t)range;
208  }
209 
210  const char *ptr = reinterpret_cast<char*>(val.data());
211  out.write(ptr, val.size()*sizeof(uint8_t));
212  out << '\x7f';
213 
214  if (fOutPath=="-")
215  Out() << out.str();
216  else
217  ofstream(fOutPath+"/cam-fadcontrol-eventdata.bin") << out.str();
218 
220  }
int is_open()
Definition: izstream.h:65
Mainloop running, state machine in operation.
int GetCurrentState() const
return the current state of the machine
uint64_t JavaDate() const
Definition: Time.h:111
int i
Definition: db_dim_client.c:21
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
uint32_t NightAsInt() const
Definition: Time.cc:397
const int32_t & state() const
Definition: DimState.h:80
bool SetVecAddress(const std::string &name, std::vector< T > &vec)
Definition: fits.h:956
std::ostream & Out() const
Definition: MessageImp.h:73
double JD() const
Definition: Time.h:87
bool GetNextRow(bool check=true)
Definition: fits.h:851
Definition: fits.h:54
vector< float > fMax
Definition: evtserver.cc:51
int Error(const std::string &str)
Definition: MessageImp.h:49
DimDescribedState fDimFadControl
Definition: evtserver.cc:35
std::string Form(const char *fmt,...)
Definition: tools.cc:45
RstTime GetSolarRst(double jd, const LnLatPosn &obs, double hrz=LN_SOLAR_STANDART_HORIZON)
Definition: nova.h:97
Error states should be between 0x100 and 0xffff.
Return to feeserver c CVS log Up to[MAIN] dcscvs FeeServer feeserver src Wed FeeServer_v0 v0 dev
Definition: feeserver.c:5
int Info(const std::string &str)
Definition: MessageImp.h:47
ln_rst_time RstTime
Definition: nova.h:13
bool SetRefAddress(const std::string &name, T &ptr)
Definition: fits.h:950

+ Here is the call graph for this function: