forked from indilib/indi-3rdparty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindi_starbook.cpp
446 lines (378 loc) · 11.4 KB
/
indi_starbook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <cmath>
/*
Starbook mount driver
Copyright (C) 2018 Norbert Szulc (not7cd)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "indi_starbook.h"
#include "config.h"
#include <indicom.h>
#include <cstring>
#include <regex>
static std::unique_ptr<StarbookDriver> starbook_driver(new StarbookDriver());
void log_exception(const char *dev, const char *what)
{
// NOTE: temporary solution to log driver failing and being silently restarted by server
INDI::Logger::getInstance().print(dev, INDI::Logger::DBG_ERROR, __FILE__, __LINE__, what);
}
StarbookDriver::StarbookDriver()
{
setVersion(STARBOOK_DRIVER_VERSION_MAJOR, STARBOOK_DRIVER_VERSION_MINOR);
SetTelescopeCapability(
TELESCOPE_CAN_PARK | TELESCOPE_CAN_GOTO | TELESCOPE_CAN_SYNC | TELESCOPE_CAN_ABORT | TELESCOPE_HAS_TIME |
TELESCOPE_HAS_LOCATION,
starbook::MAX_SPEED + 1);
// we are using custom Connection::Curl
setTelescopeConnection(CONNECTION_NONE);
}
StarbookDriver::~StarbookDriver() = default;
bool StarbookDriver::initProperties()
{
Telescope::initProperties();
IUFillText(&VersionT[0], "Version", "Version", "");
IUFillTextVector(&VersionTP, VersionT, 1, getDeviceName(), "Firmware", "Firmware", INFO_TAB, IP_RO, 0,
IPS_IDLE);
IUFillText(&StateT[0], "State", "State", "");
IUFillTextVector(&StateTP, StateT, 1, getDeviceName(), "Status", "Status", MAIN_CONTROL_TAB, IP_RO, 0,
IPS_IDLE);
IUFillSwitch(&StartS[0], "Initialize", "Initialize", ISS_OFF);
IUFillSwitchVector(&StartSP, StartS, 1, getDeviceName(), "Basic", "Basic control", MAIN_CONTROL_TAB,
IP_RW, ISR_ATMOST1, 60, IPS_IDLE);
curlConnection = new Connection::Curl(this);
curlConnection->registerHandshake([&]()
{
return callHandshake();
});
registerConnection(curlConnection);
curlConnection->setDefaultHost(DEFAULT_STARBOOK_ADDRESS);
curlConnection->setDefaultPort(DEFAULT_STARBOOK_PORT);
cmd_interface = std::unique_ptr<starbook::CommandInterface>(
new starbook::CommandInterface(curlConnection)
);
addDebugControl();
last_known_state = starbook::UNKNOWN;
cmd_interface->setDevice(getDeviceName());
return true;
}
bool StarbookDriver::updateProperties()
{
Telescope::updateProperties();
if (isConnected())
{
defineProperty(&VersionTP);
defineProperty(&StateTP);
defineProperty(&StartSP);
}
else
{
deleteProperty(VersionTP.name);
deleteProperty(StateTP.name);
deleteProperty(StartSP.name);
}
return true;
}
bool StarbookDriver::Connect()
{
failed_res = 0;
last_known_state = starbook::UNKNOWN;
bool rc = Telescope::Connect();
if (rc)
{
getFirmwareVersion();
// TODO: resolve this in less hacky way https://github.com/indilib/indi/issues/810
saveConfig(false, "DEVICE_ADDRESS");
}
else
{
LOG_ERROR("Connection failed");
}
return rc;
}
bool StarbookDriver::Disconnect()
{
if (isConnected())
{
bool rc = Telescope::Disconnect();
last_known_state = starbook::UNKNOWN;
// Disconnection is successful, set it IDLE and updateProperties.
if (rc)
{
setConnected(false, IPS_IDLE);
updateProperties();
}
else
setConnected(true, IPS_ALERT);
return rc;
}
else
{
return false;
}
}
const char *StarbookDriver::getDefaultName()
{
return "Starbook";
}
bool StarbookDriver::ReadScopeStatus()
{
LOG_DEBUG("Status! Sending GETSTATUS command");
starbook::StatusResponse res;
try
{
cmd_interface->GetStatus(res);
}
catch (std::exception &e)
{
StateTP.s = IPS_ALERT;
failed_res++;
LOG_ERROR(e.what());
if (failed_res > 3)
{
LOG_ERROR("Failed to keep connection, disconnecting");
StarbookDriver::Disconnect();
failed_res = 0;
}
return false;
}
last_known_state = res.state;
setTrackState(res);
setStarbookState(res.state);
NewRaDec(res.equ.ra / 15, res.equ.dec); // CONVERSION
failed_res = 0;
LOG_DEBUG("STATUS");
return true;
}
void StarbookDriver::setStarbookState(const starbook::StarbookState &state)
{
IUSaveText(&StateT[0], starbook::STATE_TO_STR.at(state).c_str());
StateTP.s = IPS_OK;
IDSetText(&StateTP, nullptr);
}
void StarbookDriver::setTrackState(const starbook::StatusResponse &res)
{
switch (res.state)
{
case starbook::INIT:
case starbook::USER:
TrackState = SCOPE_IDLE;
break;
case starbook::SCOPE:
case starbook::GUIDE:
case starbook::CHART:
case starbook::ALTAZ:
TrackState = res.executing_goto ? SCOPE_SLEWING : SCOPE_TRACKING;
break;
case starbook::UNKNOWN:
TrackState = SCOPE_IDLE;
break;
}
}
bool StarbookDriver::Goto(double ra, double dec)
{
ra = ra * 15; // CONVERSION
starbook::ResponseCode rc = cmd_interface->GotoRaDec(ra, dec);
LogResponse("GOTO", rc);
if (rc)
TrackState = SCOPE_SLEWING;
return rc == starbook::OK;
}
bool StarbookDriver::Sync(double ra, double dec)
{
ra = ra * 15; // CONVERSION
starbook::ResponseCode rc = cmd_interface->Align(ra, dec);
LogResponse("Sync", rc);
return rc == starbook::OK;
}
bool StarbookDriver::Abort()
{
starbook::ResponseCode rc = cmd_interface->Stop();
LogResponse("Aborting", rc);
return rc == starbook::OK;
}
bool StarbookDriver::Park()
{
starbook::ResponseCode rc = cmd_interface->Home();
LogResponse("Parking", rc);
return rc == starbook::OK;
}
bool StarbookDriver::UnPark()
{
return true;
}
bool StarbookDriver::MoveNS(INDI_DIR_NS dir, INDI::Telescope::TelescopeMotionCommand command)
{
starbook::ResponseCode rc = cmd_interface->Move(dir, command);
LogResponse("Move N-S", rc);
return rc == starbook::OK;
}
bool StarbookDriver::MoveWE(INDI_DIR_WE dir, INDI::Telescope::TelescopeMotionCommand command)
{
starbook::ResponseCode rc = cmd_interface->Move(dir, command);
LogResponse("Move W-E", rc);
return rc == starbook::OK;
}
bool StarbookDriver::SetSlewRate(int index)
{
starbook::ResponseCode rc = cmd_interface->SetSpeed(index);
LogResponse("Setting slew rate", rc);
return rc == starbook::OK;
}
bool StarbookDriver::updateTime(ln_date *utc, double utc_offset)
{
INDI_UNUSED(utc_offset);
if (last_known_state != starbook::INIT)
{
LOGF_WARN("Cannot update time in %s state", starbook::STATE_TO_STR.at(last_known_state).c_str());
return false;
}
starbook::ResponseCode rc;
utc->hours += floor(utc_offset); // starbook stores local time, go figure
rc = cmd_interface->SetTime(*utc);
LogResponse("Updating time", rc);
return rc == starbook::OK;
}
bool StarbookDriver::updateLocation(double latitude, double longitude, double elevation)
{
INDI_UNUSED(elevation);
if (last_known_state != starbook::INIT)
{
LOGF_WARN("Cannot update location in %s state", starbook::STATE_TO_STR.at(last_known_state).c_str());
return false;
}
if (TimeT[1].text == nullptr)
{
LOG_WARN("Cannot update location before time");
return false;
}
// INDI 0 to 360 longitude to -180 to 180 standard longitude
if (longitude > 180)
longitude -= 360;
auto utc_offset = static_cast<short>(std::floor(std::strtof(TimeT[1].text, nullptr)));
LOGF_WARN("UTC offset for location: %i", utc_offset);
starbook::LnLat posn(longitude, latitude);
starbook::ResponseCode rc = cmd_interface->SetPlace(posn, utc_offset);
LogResponse("Updating location", rc);
return rc == starbook::OK;
}
bool StarbookDriver::getFirmwareVersion()
{
starbook::VersionResponse res;
starbook::ResponseCode rc = cmd_interface->Version(res);
if (rc != starbook::OK)
{
LogResponse("Get version", rc);
return false;
}
if (res.major_minor < 2.7)
{
LOGF_WARN("Get version [OK]: %s (< 2.7) not well supported", res.full_str.c_str());
}
else
{
LOGF_INFO("Get version [OK]: %s", res.full_str.c_str());
}
IUSaveText(&VersionT[0], res.full_str.c_str());
IDSetText(&VersionTP, nullptr);
return true;
}
bool StarbookDriver::Handshake()
{
LOG_DEBUG("Handshake");
return Telescope::Handshake();
}
void StarbookDriver::LogResponse(const std::string &cmd, const starbook::ResponseCode &rc)
{
std::stringstream msg;
msg << cmd << " [";
switch (rc)
{
case starbook::OK:
msg << "OK";
break;
case starbook::ERROR_ILLEGAL_STATE:
msg << "ERROR_ILLEGAL_STATE";
break;
case starbook::ERROR_FORMAT:
msg << "ERROR_FORMAT";
break;
case starbook::ERROR_BELOW_HORIZON:
msg << "ERROR_BELOW_HORIZON";
break;
case starbook::ERROR_UNKNOWN:
msg << "ERROR_UNKNOWN";
break;
case starbook::ERROR_POINT:
msg << "ERROR_POINT";
break;
}
if (rc == starbook::ERROR_ILLEGAL_STATE)
{
msg << " (" << starbook::STATE_TO_STR.at(last_known_state) << ")";
}
msg << "]";
if (rc != starbook::OK)
{
msg << ": \"" << cmd_interface->getLastCmdUrl() << "\"";
msg << " \"" << cmd_interface->getLastResponse() << "\"";
LOG_ERROR(msg.str().c_str());
}
else
{
LOG_INFO(msg.str().c_str());
}
}
bool StarbookDriver::ISNewSwitch(const char *dev, const char *name, ISState *states, char **names, int n)
{
if (!strcmp(name, StartSP.name))
{
return performStart();
}
if (!strcmp(name, "CONNECTION_MODE"))
{
// TODO: resolve this in less hacky way https://github.com/indilib/indi/issues/810
loadConfig(false, "DEVICE_ADDRESS");
// we pass rest of the work to parent function
// hopefully, loading property before connection mode setups won't break anything
}
return Telescope::ISNewSwitch(dev, name, states, names, n);
}
bool StarbookDriver::performStart()
{
IUResetSwitch(&StartSP);
if (last_known_state == starbook::INIT)
{
if (cmd_interface->Start())
{
StartSP.s = IPS_OK;
}
}
else
{
LOGF_WARN("Cannot initialize in %s state, must be INIT", starbook::STATE_TO_STR.at(last_known_state).c_str());
StartSP.s = IPS_ALERT;
}
IDSetSwitch(&StartSP, nullptr);
return true;
}
void StarbookDriver::TimerHit()
{
try
{
Telescope::TimerHit();
}
catch (std::exception &e)
{
log_exception(getDeviceName(), e.what());
throw e;
}
}