nidas  v1.2-1520
CharacterSensor.h
Go to the documentation of this file.
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
2 // vim: set shiftwidth=4 softtabstop=4 expandtab:
3 /*
4  ********************************************************************
5  ** NIDAS: NCAR In-situ Data Acquistion Software
6  **
7  ** 2006, Copyright University Corporation for Atmospheric Research
8  **
9  ** This program is free software; you can redistribute it and/or modify
10  ** it under the terms of the GNU General Public License as published by
11  ** the Free Software Foundation; either version 2 of the License, or
12  ** (at your option) any later version.
13  **
14  ** This program is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  ** GNU General Public License for more details.
18  **
19  ** The LICENSE.txt file accompanying this software contains
20  ** a copy of the GNU General Public License. If it is not found,
21  ** write to the Free Software Foundation, Inc.,
22  ** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  **
24  ********************************************************************
25 */
26 
27 #ifndef NIDAS_CORE_CHARACTERSENSOR_H
28 #define NIDAS_CORE_CHARACTERSENSOR_H
29 
30 #include "DSMSensor.h"
31 #include "Prompt.h"
32 #include <nidas/util/util.h>
33 
34 namespace nidas { namespace core {
35 
36 class AsciiSscanf;
37 class Sample;
38 class TimetagAdjuster;
39 
46 class CharacterSensor: public DSMSensor {
47 
48 public:
49 
54 
55  virtual ~CharacterSensor();
56 
67 
77 
83  void open(int flags) throw(nidas::util::IOException,
85 
91 
99 
104  virtual void setMessageParameters(unsigned int length, const std::string& val, bool eom)
106 
111  const std::string& getMessageSeparator() const
112  {
113  return _messageSeparator;
114  }
115 
119  const std::string getBackslashedMessageSeparator() const
120  {
122  }
123 
125  {
126  return _separatorAtEOM;
127  }
128 
129  int getMessageLength() const
130  {
131  return _messageLength;
132  }
133 
140  virtual void addPrompt(const std::string& promptString, double promptRate,
141  double promptOffset)
142  {
143  Prompt prompt;
144  prompt.setString(promptString);
145  prompt.setRate(promptRate);
146  prompt.setOffset(promptOffset);
147 
148  _prompts.push_back(prompt);
149  _prompted = true;
150 //cerr<< "pushed back prompt. String = "<<promptString<<" rate= "<<promptRate;
151  }
152 
153  const std::list<Prompt>& getPrompts() const { return _prompts;}
154 
160  virtual bool isPrompted() const { return _prompted; }
161 
166  virtual bool isPrompting() const { return false; }
167 
168  virtual void startPrompting() throw(nidas::util::IOException)
169  {
171  "startPrompting","not supported");
172  }
173 
174  virtual void stopPrompting() throw(nidas::util::IOException)
175  {
177  "stopPrompting","not supported");
178  }
179 
180  virtual void togglePrompting() throw(nidas::util::IOException)
181  {
182  if (isPrompting()) stopPrompting();
183  else startPrompting();
184  }
185 
191  void setInitString(const std::string& val) { _initString = val; }
192 
193  const std::string& getInitString() const { return _initString; }
194 
195  virtual void sendInitString() throw(nidas::util::IOException);
196 
197  bool getNullTerminated() const
198  {
199  if (!getSampleScanner()) return false;
201  }
202 
208  int getNumScanfFailures() const { return _scanfFailures; }
209 
215  int getNumScanfPartials() const { return _scanfPartials; }
216 
221  const std::list<AsciiSscanf*>& getScanfers() const
222  {
223  return _sscanfers;
224  }
225 
231  int getMaxScanfFields() const { return _maxScanfFields; }
232 
239  virtual void validateSscanfs() throw(nidas::util::InvalidParameterException);
240 
246  virtual bool process(const Sample*,std::list<const Sample*>& result)
247  throw();
248 
249  void fromDOMElement(const xercesc::DOMElement*)
250  throw(nidas::util::InvalidParameterException);
251 
256  bool doesAsciiSscanfs();
257 
258 protected:
259 
265  void setPromptRate(const double val) {_promptRate = val;}
266 
267  double getPromptRate() const { return (_promptRate);}
268 
269  void setPromptOffset(const double val) {_promptOffset = val;}
270 
271  double getPromptOffset()const { return (_promptOffset);}
272 
278  void setPromptString(const std::string& val) { _promptString = val; }
279 
280  const std::string& getPromptString() const { return (_promptString);}
281 
282  virtual int scanSample(AsciiSscanf* sscanf, const char* inputstr,
283  float* data_ptr);
284 
301  searchSampleScanners(const Sample* samp, SampleTag** stag_out=0) throw();
302 
307  void
308  adjustTimeTag(SampleTag* stag, SampleT<float>* outs);
309 
311 
312 private:
313 
314  std::string _messageSeparator;
315 
317 
319 
320  std::list<Prompt> _prompts;
321 
322  std::string _promptString;
323 
324  double _promptRate;
325 
327 
328  std::list<AsciiSscanf*> _sscanfers;
329 
330  std::list<AsciiSscanf*>::const_iterator _nextSscanfer;
331 
333 
335 
337 
338  bool _prompted;
339 
343  std::string _initString;
344 
345  std::string _emptyString;
346 
349 
351  CharacterSensor& operator=(const CharacterSensor&);
352 
353 };
354 
355 }} // namespace nidas namespace core
356 
357 #endif
virtual bool getNullTerminate() const
Should the SampleScanner append a null character, &#39;\0&#39;, to the messages.
Definition: SampleScanner.h:127
std::string _promptString
Definition: CharacterSensor.h:322
virtual int scanSample(AsciiSscanf *sscanf, const char *inputstr, float *data_ptr)
Definition: CharacterSensor.cc:356
void open(int flags)
Open the sensor device port for real-time sampling.
Definition: CharacterSensor.cc:99
const std::string & getInitString() const
Definition: CharacterSensor.h:193
SampleScanner * getSampleScanner() const
Definition: DSMSensor.h:953
void setPromptString(const std::string &val)
Set the &lt;sensor&gt; prompt string for this sensor.
Definition: CharacterSensor.h:278
std::list< AsciiSscanf * >::const_iterator _nextSscanfer
Definition: CharacterSensor.h:330
virtual ~CharacterSensor()
Definition: CharacterSensor.cc:70
int getMaxScanfFields() const
The maximum number of fields in any of the AsciiSscanfs for this CharacterSensor. ...
Definition: CharacterSensor.h:231
const std::string & getMessageSeparator() const
Get message separator with backslash sequences replaced by their intended character.
Definition: CharacterSensor.h:111
bool _prompted
Definition: CharacterSensor.h:338
std::string _messageSeparator
Definition: CharacterSensor.h:314
Implementation of support for a sensor which generates character output.
Definition: CharacterSensor.h:46
virtual std::string getName() const
Return a name that should fully identify this sensor.
Definition: DSMSensor.h:183
void setOffset(const double val)
Set prompt offset in seconds.
Definition: Prompt.h:63
double getPromptRate() const
Definition: CharacterSensor.h:267
void validate()
Implementation of DSMSensor::validate for a Character Sensor.
Definition: CharacterSensor.cc:327
const std::list< AsciiSscanf * > & getScanfers() const
Return the list of AsciiSscanfs requested for this CharacterSensor.
Definition: CharacterSensor.h:221
Class providing sscanf functionality for parsing ASCII data.
Definition: AsciiSscanf.h:62
std::map< const SampleTag *, TimetagAdjuster * > _ttadjusters
Definition: CharacterSensor.h:310
virtual bool process(const Sample *, std::list< const Sample * > &result)
Process a raw sample, which in this case means do a sscanf on the character string contents...
Definition: CharacterSensor.cc:451
virtual void sendInitString()
Definition: CharacterSensor.cc:109
virtual void setMessageParameters(unsigned int length, const std::string &val, bool eom)
The messageSeparator is the string of bytes that sensor generates to separate messages.
Definition: CharacterSensor.cc:88
Class to contain prompt information - string and rate.
Definition: Prompt.h:38
IODevice * buildIODevice()
Creates an IODevice depending on the device name prefix: name prefix type of IODevice inet: TCPSocket...
Definition: CharacterSensor.cc:119
Adjust time tags of fixed delta-T time series to correct for irregular latency in the assignments of ...
Definition: TimetagAdjuster.h:45
bool getMessageSeparatorAtEOM() const
Definition: CharacterSensor.h:124
void setString(const std::string &val)
Definition: Prompt.h:43
void setInitString(const std::string &val)
Set the initialization string(s) for this sensor.
Definition: CharacterSensor.h:191
void fromDOMElement(const xercesc::DOMElement *)
Initialize myself from a xercesc::DOMElement.
Definition: CharacterSensor.cc:226
bool doesAsciiSscanfs()
Definition: CharacterSensor.cc:154
void setPromptRate(const double val)
Set the rate at which &lt;sensor&gt; prompts are sent to this sensor.
Definition: CharacterSensor.h:265
int getMessageLength() const
Definition: CharacterSensor.h:129
std::string _initString
String that is sent once after sensor is opened.
Definition: CharacterSensor.h:343
virtual void startPrompting()
Definition: CharacterSensor.h:168
void setRate(const double val)
Set rate of desired prompting, in Hz (sec^-1).
Definition: Prompt.h:52
int _scanfPartials
Definition: CharacterSensor.h:336
virtual void stopPrompting()
Definition: CharacterSensor.h:174
std::list< Prompt > _prompts
Definition: CharacterSensor.h:320
double _promptRate
Definition: CharacterSensor.h:324
virtual void validateSscanfs()
Virtual method to check that the Sscanfs for this CharacterSensor are OK.
Definition: CharacterSensor.cc:204
bool getNullTerminated() const
Definition: CharacterSensor.h:197
int getNumScanfPartials() const
How many samples have been partially scanned, because a character in the middle of a message conflict...
Definition: CharacterSensor.h:215
int _scanfFailures
Definition: CharacterSensor.h:334
std::string addBackslashSequences(const std::string &str)
Utility function for substituting backslash sequences back into a string.
Definition: util.cc:102
An interface to an IO device.
Definition: IODevice.h:41
SampleT< float > * searchSampleScanners(const Sample *samp, SampleTag **stag_out=0)
Search through the AsciiSscanf instances attached to this sensor, looking for the next scanner which ...
Definition: CharacterSensor.cc:365
void adjustTimeTag(SampleTag *stag, SampleT< float > *outs)
Apply TimetagAdjuster and lag adjustments to the timetag of the given sample.
Definition: CharacterSensor.cc:475
const std::string getBackslashedMessageSeparator() const
Get message separator with backslash sequences added back.
Definition: CharacterSensor.h:119
virtual void togglePrompting()
Definition: CharacterSensor.h:180
Interface to a data sample.
Definition: Sample.h:189
DSMSensor provides the basic support for reading, processing and distributing samples from a sensor a...
Definition: DSMSensor.h:87
void init()
Initialize the CharacterSensor instance for post-processing.
Definition: CharacterSensor.cc:164
Definition: IOException.h:37
double _promptOffset
Definition: CharacterSensor.h:326
const std::list< Prompt > & getPrompts() const
Definition: CharacterSensor.h:153
virtual void addPrompt(const std::string &promptString, double promptRate, double promptOffset)
Prompting Sensors can have multiple prompts and rates.
Definition: CharacterSensor.h:140
SampleScanner * buildSampleScanner()
Creates a SampleScanner for this DSMSensor depending on the device name prefix: name prefix type of S...
Definition: CharacterSensor.cc:134
Class describing a group of variables that are sampled and handled together.
Definition: SampleTag.h:87
std::string _emptyString
Definition: CharacterSensor.h:345
const std::string & getPromptString() const
Definition: CharacterSensor.h:280
bool _separatorAtEOM
Definition: CharacterSensor.h:316
A scanner of sample data.
Definition: SampleScanner.h:73
int getNumScanfFailures() const
How many samples have resulted in complete scanf failures - nothing parsed, because the sensor messag...
Definition: CharacterSensor.h:208
std::list< AsciiSscanf * > _sscanfers
Definition: CharacterSensor.h:328
int _maxScanfFields
Definition: CharacterSensor.h:332
double getPromptOffset() const
Definition: CharacterSensor.h:271
CharacterSensor()
No arg constructor.
Definition: CharacterSensor.cc:51
int _messageLength
Definition: CharacterSensor.h:318
virtual bool isPrompted() const
Is this a prompted sensor.
Definition: CharacterSensor.h:160
void setPromptOffset(const double val)
Definition: CharacterSensor.h:269
virtual bool isPrompting() const
Is prompting active, i.e.
Definition: CharacterSensor.h:166
Definition: InvalidParameterException.h:35