Instrument Neutral Distributed Interface INDI  1.6.0
tutorial_client.cpp
Go to the documentation of this file.
1 #if 0
2 Simple Client Tutorial
3 Demonstration of libindi v0.7 capabilities.
4 
5 Copyright (C) 2010 Jasem Mutlaq (mutlaqja@ikarustech.com)
6 
7 This library is free software;
8 you can redistribute it and / or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation;
11 either
12 version 2.1 of the License, or (at your option) any later version.
13 
14 This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY;
16 without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library;
22 if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 - 1301 USA
24 
25 #endif
26 
40 #include "tutorial_client.h"
41 
42 #include "indibase/basedevice.h"
43 
44 #include <cstring>
45 #include <fstream>
46 #include <iostream>
47 #include <memory>
48 
49 #define MYCCD "Simple CCD"
50 
51 /* Our client auto pointer */
52 std::unique_ptr<MyClient> camera_client(new MyClient());
53 
54 int main(int /*argc*/, char **/*argv*/)
55 {
56  camera_client->setServer("localhost", 7624);
57 
58  camera_client->watchDevice(MYCCD);
59 
60  camera_client->connectServer();
61 
62  camera_client->setBLOBMode(B_ALSO, MYCCD, nullptr);
63 
64  std::cout << "Press any key to terminate the client.\n";
65  std::string term;
66  std::cin >> term;
67 }
68 
69 /**************************************************************************************
70 **
71 ***************************************************************************************/
73 {
74  ccd_simulator = nullptr;
75 }
76 
77 /**************************************************************************************
78 **
79 ***************************************************************************************/
81 {
82  INumberVectorProperty *ccd_temperature = nullptr;
83 
84  ccd_temperature = ccd_simulator->getNumber("CCD_TEMPERATURE");
85 
86  if (ccd_temperature == nullptr)
87  {
88  IDLog("Error: unable to find CCD Simulator CCD_TEMPERATURE property...\n");
89  return;
90  }
91 
92  ccd_temperature->np[0].value = -20;
93  sendNewNumber(ccd_temperature);
94 }
95 
96 /**************************************************************************************
97 **
98 ***************************************************************************************/
100 {
101  INumberVectorProperty *ccd_exposure = nullptr;
102 
103  ccd_exposure = ccd_simulator->getNumber("CCD_EXPOSURE");
104 
105  if (ccd_exposure == nullptr)
106  {
107  IDLog("Error: unable to find CCD Simulator CCD_EXPOSURE property...\n");
108  return;
109  }
110 
111  // Take a 1 second exposure
112  IDLog("Taking a 1 second exposure.\n");
113  ccd_exposure->np[0].value = 1;
114  sendNewNumber(ccd_exposure);
115 }
116 
117 /**************************************************************************************
118 **
119 ***************************************************************************************/
121 {
122  if (strcmp(dp->getDeviceName(), MYCCD) == 0)
123  IDLog("Receiving %s Device...\n", dp->getDeviceName());
124 
125  ccd_simulator = dp;
126 }
127 
128 /**************************************************************************************
129 **
130 *************************************************************************************/
132 {
133  if (strcmp(property->getDeviceName(), MYCCD) == 0 && strcmp(property->getName(), "CONNECTION") == 0)
134  {
136  return;
137  }
138 
139  if (strcmp(property->getDeviceName(), MYCCD) == 0 && strcmp(property->getName(), "CCD_TEMPERATURE") == 0)
140  {
141  if (ccd_simulator->isConnected())
142  {
143  IDLog("CCD is connected. Setting temperature to -20 C.\n");
144  setTemperature();
145  }
146  return;
147  }
148 }
149 
150 /**************************************************************************************
151 **
152 ***************************************************************************************/
154 {
155  // Let's check if we get any new values for CCD_TEMPERATURE
156  if (strcmp(nvp->name, "CCD_TEMPERATURE") == 0)
157  {
158  IDLog("Receving new CCD Temperature: %g C\n", nvp->np[0].value);
159 
160  if (nvp->np[0].value == -20)
161  {
162  IDLog("CCD temperature reached desired value!\n");
163  takeExposure();
164  }
165  }
166 }
167 
168 /**************************************************************************************
169 **
170 ***************************************************************************************/
171 void MyClient::newMessage(INDI::BaseDevice *dp, int messageID)
172 {
173  if (strcmp(dp->getDeviceName(), MYCCD) != 0)
174  return;
175 
176  IDLog("Recveing message from Server:\n\n########################\n%s\n########################\n\n",
177  dp->messageQueue(messageID).c_str());
178 }
179 
180 /**************************************************************************************
181 **
182 ***************************************************************************************/
184 {
185  // Save FITS file to disk
186  std::ofstream myfile;
187 
188  myfile.open("ccd_simulator.fits", std::ios::out | std::ios::binary);
189 
190  myfile.write(static_cast<char *>(bp->blob), bp->bloblen);
191 
192  myfile.close();
193 
194  IDLog("Received image, saved as ccd_simulator.fits\n");
195 }
std::string messageQueue(int index) const
One Blob (Binary Large Object) descriptor.
Definition: indiapi.h:423
Provides generic container for INDI properties.
Definition: indiproperty.h:33
const char * getName() const
virtual void newProperty(INDI::Property *property)
Emmited when a new property is created for an INDI driver.
Class to provide basic INDI device functionality.
Definition: basedevice.h:44
INumberVectorProperty * getNumber(const char *name)
Definition: basedevice.cpp:72
char name[MAXINDINAME]
Definition: indiapi.h:304
int bloblen
Definition: indiapi.h:434
virtual void newNumber(INumberVectorProperty *nvp)
Emmited when a new number value arrives from INDI server.
virtual void newBLOB(IBLOB *bp)
Emmited when a new BLOB value arrives from INDI server.
void setTemperature()
virtual void newDevice(INDI::BaseDevice *dp)
Emmited when a new device is created from INDI server.
void * blob
Definition: indiapi.h:432
void takeExposure()
Number vector property descriptor.
Definition: indiapi.h:299
void sendNewNumber(INumberVectorProperty *pp)
Send new Number command to server.
Definition: baseclient.cpp:765
const char * getDeviceName()
#define MYCCD
virtual void newMessage(INDI::BaseDevice *dp, int messageID)
Emmited when a new message arrives from INDI server.
void IDLog(const char *msg,...)
Function Drivers call to log a message locally.
Definition: indicom.c:289
void connectDevice(const char *deviceName)
Connect to INDI driver.
Definition: baseclient.cpp:291
const char * getDeviceName() const
int main(int, char **)
std::unique_ptr< MyClient > camera_client(new MyClient())
Construct a basic INDI client that demonstrates INDI::Client capabilities. This client must be used w...
double value
Definition: indiapi.h:271