Instrument Neutral Distributed Interface INDI  2.0.2
sqm_simulator.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2017 Ralph Rogge. All rights reserved.
3 
4  INDI Sky Quality Meter Simulator
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the Free
8  Software Foundation; either version 2 of the License, or (at your option)
9  any later version.
10 
11  This program is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 
21  The full GNU General Public License is included in this distribution in the
22  file called LICENSE.
23 *******************************************************************************/
24 
25 #include "sqm_simulator.h"
26 
27 #include <cerrno>
28 #include <memory>
29 #include <cstring>
30 #include <unistd.h>
31 
32 std::unique_ptr<SQMSimulator> sqmSimulator(new SQMSimulator());
33 
34 #define UNIT_TAB "Unit"
35 
36 enum
37 {
43 };
44 
45 enum
46 {
51 };
52 
54 {
55  setVersion(1, 0);
56 }
57 
59 {
60  readingProperties.s = getReading() ? IPS_OK : IPS_ALERT;
61  IDSetNumber(&readingProperties, nullptr);
62 
63  unitProperties.s = getUnit() ? IPS_OK : IPS_ALERT;
64  IDSetNumber(&unitProperties, nullptr);
65 
66  return true;
67 }
68 
70 {
71  return true;
72 }
73 
75 {
77 
78  // Readings
79  IUFillNumber(&readingValues[READING_BRIGHTNESS_INDEX], "SKY_BRIGHTNESS", "Quality (mag/arcsec^2)", "%6.2f", -20, 30, 0, 0);
80  IUFillNumber(&readingValues[READING_FREQUENCY_INDEX], "SENSOR_FREQUENCY", "Freq (Hz)", "%6.2f", 0, 1000000, 0, 0);
81  IUFillNumber(&readingValues[READING_COUNTER_INDEX], "SENSOR_COUNTS", "Period (counts)", "%6.2f", 0, 1000000, 0, 0);
82  IUFillNumber(&readingValues[READING_TIME_INDEX], "SENSOR_PERIOD", "Period (s)", "%6.2f", 0, 1000000, 0, 0);
83  IUFillNumber(&readingValues[READING_TEMPERATURE_INDEX], "SKY_TEMPERATURE", "Temperature (C)", "%6.2f", -50, 80, 0, 0);
84  IUFillNumberVector(&readingProperties, readingValues, READING_NUMBER_OF_VALUES, getDeviceName(), "SKY_QUALITY", "Readings",
86 
87  // Unit Info
88  IUFillNumber(&unitValues[UNIT_PROTOCOL_INDEX], "Protocol", "", "%.f", 0, 1000000, 0, 0);
89  IUFillNumber(&unitValues[UNIT_MODEL_INDEX], "Model", "", "%.f", 0, 1000000, 0, 0);
90  IUFillNumber(&unitValues[UNIT_FEATURE_INDEX], "Feature", "", "%.f", 0, 1000000, 0, 0);
91  IUFillNumber(&unitValues[UNIT_SERIAL_INDEX], "Serial", "", "%.f", 0, 1000000, 0, 0);
92  IUFillNumberVector(&unitProperties, unitValues, UNIT_NUMBER_OF_VALUES, getDeviceName(), "Unit Info", "", UNIT_TAB, IP_RW, 0,
93  IPS_IDLE);
94 
96 
97  return true;
98 }
99 
101 {
102  return (const char *) "SQM Simulator";
103 }
104 
105 bool SQMSimulator::getReading()
106 {
107  readingValues[READING_BRIGHTNESS_INDEX].value = 16.9;
108  readingValues[READING_FREQUENCY_INDEX].value = 15.0;
109  readingValues[READING_COUNTER_INDEX].value = 28856;
110  readingValues[READING_TIME_INDEX].value = 0.063;
111  readingValues[READING_TEMPERATURE_INDEX].value = 21.5;
112 
113  return true;
114 }
115 
116 bool SQMSimulator::getUnit()
117 {
118  unitValues[UNIT_PROTOCOL_INDEX].value = 4;
119  unitValues[UNIT_MODEL_INDEX].value = 3;
120  unitValues[UNIT_FEATURE_INDEX].value = 49;
121  unitValues[UNIT_SERIAL_INDEX].value = 1234;
122 
123  return true;
124 }
125 
126 bool SQMSimulator::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
127 {
128  if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
129  {
130  if (strcmp(name, readingProperties.name) == 0)
131  {
132  IUUpdateNumber(&readingProperties, values, names, n);
133  readingProperties.s = IPS_OK;
134  IDSetNumber(&readingProperties, nullptr);
135  return true;
136  }
137 
138  if (strcmp(name, unitProperties.name) == 0)
139  {
140  IUUpdateNumber(&unitProperties, values, names, n);
141  unitProperties.s = IPS_OK;
142  IDSetNumber(&unitProperties, nullptr);
143  return true;
144  }
145  }
146 
147  return INDI::DefaultDevice::ISNewNumber(dev, name, values, names, n);
148 }
149 
151 {
153 
154  if (isConnected())
155  {
156  defineProperty(&readingProperties);
157  defineProperty(&unitProperties);
158  }
159  else
160  {
161  deleteProperty(readingProperties.name);
162  deleteProperty(unitProperties.name);
163  }
164 
165  return true;
166 }
bool isConnected() const
Definition: basedevice.cpp:520
const char * getDeviceName() const
Definition: basedevice.cpp:821
virtual bool updateProperties()
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
void setVersion(uint16_t vMajor, uint16_t vMinor)
Set driver version information to be defined in DRIVER_INFO property as vMajor.vMinor.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
virtual bool initProperties()
Initilize properties initial state and value. The child class must implement this function.
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Process the client newNumber command.
void addDebugControl()
Add Debug control to the driver.
virtual bool updateProperties() override
updateProperties is called whenever there is a change in the CONNECTION status of the driver....
virtual bool initProperties() override
Initilize properties initial state and value. The child class must implement this function.
const char * getDefaultName() override
virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n) override
Process the client newNumber command.
bool Connect() override
Connect to the device. INDI::DefaultDevice implementation connects to appropriate connection interfac...
bool Disconnect() override
Disconnect from device.
const char * MAIN_CONTROL_TAB
MAIN_CONTROL_TAB Where all the primary controls for the device are located.
@ IP_RW
Definition: indiapi.h:186
@ IPS_ALERT
Definition: indiapi.h:164
@ IPS_IDLE
Definition: indiapi.h:161
@ IPS_OK
Definition: indiapi.h:162
void IUFillNumberVector(INumberVectorProperty *nvp, INumber *np, int nnp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a number vector property. The vector's auxiliary elements will be set to NULL.
Definition: indidevapi.c:272
void IUFillNumber(INumber *np, const char *name, const char *label, const char *format, double min, double max, double step, double value)
Assign attributes for a number property. The number's auxiliary elements will be set to NULL.
Definition: indidevapi.c:180
void IDSetNumber(const INumberVectorProperty *nvp, const char *fmt,...)
Definition: indidriver.c:1211
int IUUpdateNumber(INumberVectorProperty *nvp, double values[], char *names[], int n)
Update all numbers in a number vector property.
Definition: indidriver.c:1362
@ UNIT_FEATURE_INDEX
@ UNIT_PROTOCOL_INDEX
@ UNIT_SERIAL_INDEX
@ UNIT_MODEL_INDEX
#define UNIT_TAB
std::unique_ptr< SQMSimulator > sqmSimulator(new SQMSimulator())
@ READING_TEMPERATURE_INDEX
@ READING_TIME_INDEX
@ READING_COUNTER_INDEX
@ READING_FREQUENCY_INDEX
@ READING_BRIGHTNESS_INDEX
char name[MAXINDINAME]
Definition: indiapi.h:323