Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

pseudo_control_point.cc

00001 /**********************************************************************/
00002 /*  Class PseudoControlPoint:- access a control point via an AT Dataset.    */
00003 /*                                           */
00004 /*                                                                    */
00005 /*                                          Simon Hoyle               */
00006 /*                      -2002                 */
00007 /**********************************************************************/
00008 
00009 
00010 #include <iostream>
00011 #ifdef SOLARIS
00012 #include <stdio.h>
00013 #endif
00014 #include <cstring>
00015 #include <assert.h>
00016 
00017 #include "pseudo_control_point.h"
00018 
00019 using namespace std;
00020 
00021 bool PseudoControlPoint::debugAll = false;
00022 
00023 
00024 PseudoControlPoint::PseudoControlPoint(const char* id, float* var) : 
00025                            ControlPoint(id, NULL), fvar(var)
00026 {
00027     commDevType = NONE;
00028     ivar = NULL;
00029     svar = NULL;
00030 }
00031 
00032 PseudoControlPoint::PseudoControlPoint(const char* id, int* var) : 
00033                            ControlPoint(id, NULL), ivar(var)
00034 {
00035     commDevType = NONE;
00036     fvar = NULL;
00037     svar = NULL;
00038 }
00039 
00040 PseudoControlPoint::PseudoControlPoint(const char* id, char* svar, int len) :
00041                            ControlPoint(id, NULL), svar(svar), len(len)
00042 {
00043     commDevType = NONE;
00044     ivar = NULL;
00045     fvar = NULL;
00046 }
00047 
00048 const char* PseudoControlPoint::write(const char* val)
00049 {
00050     updated = false;
00051     sprintf(timestamp, "%s", CommsDevice::getUT());
00052     if (fvar != NULL)
00053     {
00054         float fval;
00055         int n = sscanf(val, "%f", &fval);
00056         if (n < 1 )
00057             sprintf(response, "<ERR>\t%s\tInvalid cmd: %s", id, val);
00058         else
00059         {
00060             *fvar = fval;
00061             sprintf(response, "<OK>\t%s\t%s\t%s", id, val, timestamp);
00062             sprintf(update, "%s\t%s\t%s\n", id, val, timestamp); 
00063             updated = true;
00064         }
00065     }
00066     else if (ivar != NULL)
00067     {
00068         int ival;
00069         int n = sscanf(val, "%i", &ival);
00070         if (n < 1 )
00071             sprintf(response, "<ERR>\t%s\tInvalid cmd: %s", id, val);
00072         else
00073         {
00074             *ivar = cmdVal = ival;
00075             sprintf(response, "<OK>\t%s\t%s\t%s", id, val, timestamp);
00076             sprintf(update, "%s\t%s\t%s\n", id, val, timestamp); 
00077             updated = true;
00078         }
00079     }
00080     else
00081     {
00082         strncpy(svar, val, len);
00083         sprintf(response, "<OK>\t%s\t%s\t%s", id, svar, timestamp);
00084         sprintf(update, "%s\t%s\t%s\n", id, svar, timestamp); 
00085         updated = true;        
00086     }
00087     return response;
00088 }
00089 
00090 const char* PseudoControlPoint::write(const char* id, const char* val)
00091 {
00092     if (!hasID(id)) 
00093     {
00094         sprintf(response, "<ERR>\t%s is not %s", this->id, id);
00095         return response;
00096     }
00097     return write(val);
00098 }
00099 
00100 int PseudoControlPoint::write(int val)
00101 {
00102     assert(ivar != NULL);
00103     *ivar = cmdVal = val;
00104     return 0;    
00105 }
00106 
00107 int PseudoControlPoint::write(float val)
00108 {
00109     assert(fvar != NULL);
00110     *fvar = val;
00111     return 0;
00112 }
00113 
00114 const char* PseudoControlPoint::read()
00115 {
00116     time_t t = time(NULL);
00117     strftime(timestamp, 14, "%H:%M:%S", gmtime(&t));
00118     if (fvar != NULL)
00119         sprintf(response, "%s\t%.2f\t%s\t", this->id, *fvar, timestamp); 
00120     else if (ivar != NULL)
00121         sprintf(response, "%s\t%d\t%s\t", this->id, *ivar, timestamp);
00122     else
00123         sprintf(response, "%s\t%s\t%s\t", this->id, svar, timestamp);
00124 
00125     return response;
00126 }
00127 
00128 const char* PseudoControlPoint::read(const char* id)
00129 {
00130     if (!hasID(id)) 
00131     {
00132         sprintf(response, "<ERR>\t%s is not %s", this->id, id);
00133         return response;
00134     }
00135     time_t t = time(NULL);
00136     strftime(timestamp, 14, "%H:%M:%S", gmtime(&t));
00137     if (fvar != NULL)
00138         sprintf(response, "<OK>\t%s\t%.2f\t%s\t", this->id, *fvar, timestamp); 
00139     else if (ivar != NULL)
00140         sprintf(response, "<OK>\t%s\t%d\t%s\t", this->id, *ivar, timestamp);
00141     else
00142         sprintf(response, "<OK>\t%s\t%s\t%s\t", this->id, svar, timestamp);
00143 
00144     return response;   
00145 }
00146 
00147 const char* PseudoControlPoint::getOptions()
00148 {
00149     static char str[30] = { 0 };
00150 
00151     if (fvar != NULL)
00152         sprintf(str, "\t\t%s [float]", id);
00153     else if (ivar != NULL)
00154         sprintf(str, "\t\t%s [int]", id); 
00155     else
00156         sprintf(str, "\t\t%s [string[%d]]", id, len);   
00157     return str; 
00158 }
00159 
00160 

Generated on Mon Apr 30 13:32:38 2007 for Parkes M & C - PKMC C++ library API by  doxygen 1.4.4