Instrument Neutral Distributed Interface INDI  1.9.2
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 static std::unique_ptr<MyClient> camera_client(new MyClient());
52 
53 int main(int /*argc*/, char **/*argv*/)
54 {
55  camera_client->setServer("localhost", 7624);
56 
57  camera_client->watchDevice(MYCCD);
58 
59  camera_client->connectServer();
60 
61  camera_client->setBLOBMode(B_ALSO, MYCCD, nullptr);
62 
63  std::cout << "Press any key to terminate the client.\n";
64  std::string term;
65  std::cin >> term;
66 }
67 
68 /**************************************************************************************
69 **
70 ***************************************************************************************/
72 {
73  ccd_simulator = nullptr;
74 }
75 
76 /**************************************************************************************
77 **
78 ***************************************************************************************/
80 {
81  INumberVectorProperty *ccd_temperature = nullptr;
82 
83  ccd_temperature = ccd_simulator->getNumber("CCD_TEMPERATURE");
84 
85  if (ccd_temperature == nullptr)
86  {
87  IDLog("Error: unable to find CCD Simulator CCD_TEMPERATURE property...\n");
88  return;
89  }
90 
91  ccd_temperature->np[0].value = -20;
92  sendNewNumber(ccd_temperature);
93 }
94 
95 /**************************************************************************************
96 **
97 ***************************************************************************************/
99 {
100  INumberVectorProperty *ccd_exposure = nullptr;
101 
102  ccd_exposure = ccd_simulator->getNumber("CCD_EXPOSURE");
103 
104  if (ccd_exposure == nullptr)
105  {
106  IDLog("Error: unable to find CCD Simulator CCD_EXPOSURE property...\n");
107  return;
108  }
109 
110  // Take a 1 second exposure
111  IDLog("Taking a 1 second exposure.\n");
112  ccd_exposure->np[0].value = 1;
113  sendNewNumber(ccd_exposure);
114 }
115 
116 /**************************************************************************************
117 **
118 ***************************************************************************************/
120 {
121  if (strcmp(dp->getDeviceName(), MYCCD) == 0)
122  IDLog("Receiving %s Device...\n", dp->getDeviceName());
123 
124  ccd_simulator = dp;
125 }
126 
127 /**************************************************************************************
128 **
129 *************************************************************************************/
131 {
132  if (strcmp(property->getDeviceName(), MYCCD) == 0 && strcmp(property->getName(), "CONNECTION") == 0)
133  {
135  return;
136  }
137 
138  if (strcmp(property->getDeviceName(), MYCCD) == 0 && strcmp(property->getName(), "CCD_TEMPERATURE") == 0)
139  {
140  if (ccd_simulator->isConnected())
141  {
142  IDLog("CCD is connected. Setting temperature to -20 C.\n");
143  setTemperature();
144  }
145  return;
146  }
147 }
148 
149 /**************************************************************************************
150 **
151 ***************************************************************************************/
153 {
154  // Let's check if we get any new values for CCD_TEMPERATURE
155  if (strcmp(nvp->name, "CCD_TEMPERATURE") == 0)
156  {
157  IDLog("Receving new CCD Temperature: %g C\n", nvp->np[0].value);
158 
159  if (nvp->np[0].value == -20)
160  {
161  IDLog("CCD temperature reached desired value!\n");
162  takeExposure();
163  }
164  }
165 }
166 
167 /**************************************************************************************
168 **
169 ***************************************************************************************/
170 void MyClient::newMessage(INDI::BaseDevice *dp, int messageID)
171 {
172  if (strcmp(dp->getDeviceName(), MYCCD) != 0)
173  return;
174 
175  IDLog("Recveing message from Server:\n\n########################\n%s\n########################\n\n",
176  dp->messageQueue(messageID).c_str());
177 }
178 
179 /**************************************************************************************
180 **
181 ***************************************************************************************/
183 {
184  // Save FITS file to disk
185  std::ofstream myfile;
186 
187  myfile.open("ccd_simulator.fits", std::ios::out | std::ios::binary);
188 
189  myfile.write(static_cast<char *>(bp->blob), bp->bloblen);
190 
191  myfile.close();
192 
193  IDLog("Received image, saved as ccd_simulator.fits\n");
194 }
MYCCD
#define MYCCD
Definition: tutorial_client.cpp:49
INDI::BaseClient::connectDevice
void connectDevice(const char *deviceName)
Connect to INDI driver.
Definition: baseclient.cpp:900
basedevice.h
MyClient::newNumber
void newNumber(INumberVectorProperty *nvp) override
Emmited when a new number value arrives from INDI server.
Definition: tutorial_client.cpp:152
Aux::ANY
@ ANY
Definition: celestronauxpacket.h:86
INDI::Property::getName
const char * getName() const
Definition: indiproperty.cpp:289
INDI::Property::getDeviceName
const char * getDeviceName() const
Definition: indiproperty.cpp:310
INDI::BaseDevice::getNumber
INDI::PropertyView< INumber > * getNumber(const char *name) const
Definition: basedevice.cpp:99
INDI::BaseDevice::messageQueue
const std::string & messageQueue(size_t index) const
Definition: basedevice.cpp:867
MyClient::newProperty
void newProperty(INDI::Property *property) override
Emmited when a new property is created for an INDI driver.
Definition: tutorial_client.cpp:130
INDI::BaseDevice::getDeviceName
const char * getDeviceName() const
Definition: basedevice.cpp:799
IDLog
void void void void void IDLog(const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(1
Function Drivers call to log a message locally.
INDI::Property
Provides generic container for INDI properties.
Definition: indiproperty.h:43
INDI::BaseClient::sendNewNumber
void sendNewNumber(INumberVectorProperty *pp)
Send new Number command to server.
Definition: baseclient.cpp:976
_INumberVectorProperty
Number vector property descriptor.
Definition: indiapi.h:317
MyClient
Definition: tutorial_client.h:39
MyClient::newBLOB
void newBLOB(IBLOB *bp) override
Emmited when a new BLOB value arrives from INDI server.
Definition: tutorial_client.cpp:182
B_ALSO
@ B_ALSO
Definition: indidevapi.h:271
_INumberVectorProperty::np
INumber * np
Definition: indiapi.h:334
_INumberVectorProperty::name
char name[MAXINDINAME]
Definition: indiapi.h:322
MyClient::takeExposure
void takeExposure()
Definition: tutorial_client.cpp:98
INDI::BaseDevice::isConnected
bool isConnected() const
Definition: basedevice.cpp:518
MyClient::setTemperature
void setTemperature()
Definition: tutorial_client.cpp:79
tutorial_client.h
Construct a basic INDI client that demonstrates INDI::Client capabilities. This client must be used w...
main
int main(int, char **)
Definition: tutorial_client.cpp:53
MyClient::MyClient
MyClient()
Definition: tutorial_client.cpp:71
MyClient::newDevice
void newDevice(INDI::BaseDevice *dp) override
Emmited when a new device is created from INDI server.
Definition: tutorial_client.cpp:119
MyClient::newMessage
void newMessage(INDI::BaseDevice *dp, int messageID) override
Emmited when a new message arrives from INDI server.
Definition: tutorial_client.cpp:170
INDI::BaseDevice
Class to provide basic INDI device functionality.
Definition: basedevice.h:45
IBLOB
One Blob (Binary Large Object) descriptor.