Instrument Neutral Distributed Interface INDI  2.0.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 <basedevice.h>
43 
44 #include <cstring>
45 #include <fstream>
46 #include <iostream>
47 #include <memory>
48 
49 
50 int main(int, char *[])
51 {
52  MyClient myClient;
53  myClient.setServer("localhost", 7624);
54 
55  myClient.connectServer();
56 
57  myClient.setBLOBMode(B_ALSO, "Simple CCD", nullptr);
58 
59  myClient.enableDirectBlobAccess("Simple CCD", nullptr);
60 
61  std::cout << "Press Enter key to terminate the client.\n";
62  std::cin.ignore();
63 }
64 
65 /**************************************************************************************
66 **
67 ***************************************************************************************/
69 {
70  // wait for the availability of the device
71  watchDevice("Simple CCD", [this](INDI::BaseDevice device)
72  {
73  mSimpleCCD = device; // save device
74 
75  // wait for the availability of the "CONNECTION" property
76  device.watchProperty("CONNECTION", [this](INDI::Property)
77  {
78  IDLog("Connecting to INDI Driver...\n");
79  connectDevice("Simple CCD");
81 
82  // wait for the availability of the "CCD_TEMPERATURE" property
83  device.watchProperty("CCD_TEMPERATURE", [this](INDI::PropertyNumber property)
84  {
85  if (mSimpleCCD.isConnected())
86  {
87  IDLog("CCD is connected.\n");
88  setTemperature(-20);
89  }
90 
91  // call lambda function if property changed
92  property.onUpdate([property, this]()
93  {
94  IDLog("Receving new CCD Temperature: %g C\n", property[0].getValue());
95  if (property[0].getValue() == -20)
96  {
97  IDLog("CCD temperature reached desired value!\n");
98  takeExposure(1);
99  }
100  });
102 
103  // call if updated of the "CCD1" property - simplified way
104  device.watchProperty("CCD1", [](INDI::PropertyBlob property)
105  {
106  // Save FITS file to disk
107  std::ofstream myfile;
108 
109  myfile.open("ccd_simulator.fits", std::ios::out | std::ios::binary);
110  myfile.write(static_cast<char *>(property[0].getBlob()), property[0].getBlobLen());
111  myfile.close();
112 
113  IDLog("Received image, saved as ccd_simulator.fits\n");
115  });
116 }
117 
118 /**************************************************************************************
119 **
120 ***************************************************************************************/
121 void MyClient::setTemperature(double value)
122 {
123  INDI::PropertyNumber ccdTemperature = mSimpleCCD.getProperty("CCD_TEMPERATURE");
124 
125  if (!ccdTemperature.isValid())
126  {
127  IDLog("Error: unable to find Simple CCD, CCD_TEMPERATURE property...\n");
128  return;
129  }
130 
131  IDLog("Setting temperature to %g C.\n", value);
132  ccdTemperature[0].setValue(value);
133  sendNewProperty(ccdTemperature);
134 }
135 
136 /**************************************************************************************
137 **
138 ***************************************************************************************/
139 void MyClient::takeExposure(double seconds)
140 {
141  INDI::PropertyNumber ccdExposure = mSimpleCCD.getProperty("CCD_EXPOSURE");
142 
143  if (!ccdExposure.isValid())
144  {
145  IDLog("Error: unable to find CCD Simulator CCD_EXPOSURE property...\n");
146  return;
147  }
148 
149  // Take a 1 second exposure
150  IDLog("Taking a %g second exposure.\n", seconds);
151  ccdExposure[0].setValue(seconds);
152  sendNewProperty(ccdExposure);
153 }
154 
155 /**************************************************************************************
156 **
157 ***************************************************************************************/
158 void MyClient::newMessage(INDI::BaseDevice baseDevice, int messageID)
159 {
160  if (!baseDevice.isDeviceNameMatch("Simple CCD"))
161  return;
162 
163  IDLog("Recveing message from Server:\n"
164  " %s\n\n",
165  baseDevice.messageQueue(messageID).c_str());
166 }
hid_device * device
void setServer(const char *hostname, unsigned int port)
Set the server host name and port.
void setBLOBMode(BLOBHandling blobH, const char *dev, const char *prop=nullptr)
Set Binary Large Object policy mode.
void sendNewProperty(INDI::Property pp)
Send new Property command to server.
void connectDevice(const char *deviceName)
Disconnect INDI driver.
void watchDevice(const char *deviceName)
Add a device to the watch list.
void enableDirectBlobAccess(const char *dev=nullptr, const char *prop=nullptr)
activate zero-copy delivering of the blob content. When enabled, all blob copy will be avoided when p...
Definition: baseclient.cpp:360
bool connectServer() override
Connect to INDI server.
Definition: baseclient.cpp:308
Class to provide basic INDI device functionality.
Definition: basedevice.h:52
bool isConnected() const
Definition: basedevice.cpp:520
const std::string & messageQueue(size_t index) const
Definition: basedevice.cpp:888
Property getProperty(const char *name, INDI_PROPERTY_TYPE type=INDI_UNKNOWN) const
Return a property and its type given its name.
Definition: basedevice.cpp:138
bool isDeviceNameMatch(const char *otherName) const
Check that the device name matches the argument.
Definition: basedevice.cpp:827
Provides generic container for INDI properties.
Definition: indiproperty.h:48
bool isValid() const
void setTemperature(double value)
void newMessage(INDI::BaseDevice baseDevice, int messageID) override
Emmited when a new message arrives from INDI server.
void takeExposure(double seconds)
void IDLog(const char *fmt,...)
Definition: indicom.c:316
@ B_ALSO
Definition: indidevapi.h:268
int main(int, char *[])
Construct a basic INDI client that demonstrates INDI::Client capabilities. This client must be used w...