11/*
22 INDI alpaca Focuser Driver
3-
3+
44 Copyright (C) 2024 Gord Tulloch
55
66 This library is free software; you can redistribute it and/or
@@ -29,13 +29,11 @@ AlpacaFocuser::AlpacaFocuser()
2929{
3030 setVersion (1 , 0 );
3131 FI::SetCapability (FOCUSER_CAN_ABS_MOVE | FOCUSER_CAN_ABORT);
32-
33- tcpConnection = new Connection::TCP (this );
32+ setSupportedConnections (CONNECTION_TCP);
3433}
3534
3635AlpacaFocuser::~AlpacaFocuser ()
3736{
38- delete tcpConnection;
3937}
4038
4139const char *AlpacaFocuser::getDefaultName ()
@@ -48,11 +46,12 @@ bool AlpacaFocuser::initProperties()
4846 INDI::Focuser::initProperties ();
4947
5048 // Use built-in TCP connection with default alpaca.local:32323
51- setActiveConnection (tcpConnection);
5249 tcpConnection->setDefaultHost (" alpaca.local" );
5350 tcpConnection->setDefaultPort (32323 );
54- tcpConnection->registerHandshake ([&]() { return Handshake (); });
55- registerConnection (tcpConnection);
51+ tcpConnection->registerHandshake ([&]()
52+ {
53+ return Handshake ();
54+ });
5655
5756 // Device information
5857 DeviceInfoTP[DESCRIPTION].fill (" DESCRIPTION" , " Description" , " " );
@@ -241,7 +240,7 @@ IPState AlpacaFocuser::MoveAbsFocuser(uint32_t targetTicks)
241240 // Validate range
242241 if (targetTicks > FocusMaxPosNP[0 ].getValue ())
243242 {
244- LOGF_ERROR (" Target position %u exceeds maximum %u" , targetTicks,
243+ LOGF_ERROR (" Target position %u exceeds maximum %u" , targetTicks,
245244 static_cast <uint32_t >(FocusMaxPosNP[0 ].getValue ()));
246245 return IPS_ALERT;
247246 }
@@ -250,7 +249,7 @@ IPState AlpacaFocuser::MoveAbsFocuser(uint32_t targetTicks)
250249
251250 nlohmann::json response;
252251 std::string data = " Position=" + std::to_string (targetTicks);
253-
252+
254253 if (!sendAlpacaPUT (" /move" , data, response))
255254 {
256255 LOGF_ERROR (" Failed to move to position %u" , targetTicks);
@@ -263,16 +262,16 @@ IPState AlpacaFocuser::MoveAbsFocuser(uint32_t targetTicks)
263262 int errorNum = response[" ErrorNumber" ].get <int >();
264263 if (errorNum != 0 )
265264 {
266- std::string errorMsg = response.contains (" ErrorMessage" ) ?
267- response[" ErrorMessage" ].get <std::string>() : " Unknown error" ;
265+ std::string errorMsg = response.contains (" ErrorMessage" ) ?
266+ response[" ErrorMessage" ].get <std::string>() : " Unknown error" ;
268267 LOGF_ERROR (" Error moving focuser: %d - %s" , errorNum, errorMsg.c_str ());
269268 return IPS_ALERT;
270269 }
271270 }
272271
273272 m_TargetPosition = targetTicks;
274273 m_Moving = true ;
275-
274+
276275 return IPS_BUSY;
277276}
278277
@@ -281,7 +280,7 @@ bool AlpacaFocuser::AbortFocuser()
281280 LOG_INFO (" Aborting focuser movement" );
282281
283282 nlohmann::json response;
284-
283+
285284 if (!sendAlpacaPUT (" /halt" , " " , response))
286285 {
287286 LOG_ERROR (" Failed to halt focuser" );
@@ -291,7 +290,7 @@ bool AlpacaFocuser::AbortFocuser()
291290 m_Moving = false ;
292291 FocusAbsPosNP.setState (IPS_IDLE);
293292 FocusAbsPosNP.apply ();
294-
293+
295294 LOG_INFO (" Focuser movement halted" );
296295 return true ;
297296}
@@ -317,20 +316,20 @@ void AlpacaFocuser::TimerHit()
317316 if (m_Moving)
318317 {
319318 bool moving = isMoving ();
320-
319+
321320 if (!moving)
322321 {
323322 // Movement completed
324323 m_Moving = false ;
325-
324+
326325 // Update current position
327326 int currentPos = getPosition ();
328327 if (currentPos >= 0 )
329328 {
330329 FocusAbsPosNP[0 ].setValue (currentPos);
331330 FocusAbsPosNP.setState (IPS_OK);
332331 FocusAbsPosNP.apply ();
333-
332+
334333 LOGF_INFO (" Focuser reached position: %d" , currentPos);
335334 }
336335 }
@@ -352,7 +351,7 @@ void AlpacaFocuser::TimerHit()
352351bool AlpacaFocuser::isMoving ()
353352{
354353 nlohmann::json response;
355-
354+
356355 if (!sendAlpacaGET (" /ismoving" , response))
357356 {
358357 return false ;
@@ -369,7 +368,7 @@ bool AlpacaFocuser::isMoving()
369368int AlpacaFocuser::getPosition ()
370369{
371370 nlohmann::json response;
372-
371+
373372 if (!sendAlpacaGET (" /position" , response))
374373 {
375374 return -1 ;
@@ -399,11 +398,11 @@ bool AlpacaFocuser::sendAlpacaGET(const std::string &endpoint, nlohmann::json &r
399398 return false ;
400399
401400 std::string url = " /api/v1/focuser/" + std::to_string (m_DeviceNumber) + endpoint;
402-
401+
403402 LOGF_DEBUG (" GET %s" , url.c_str ());
404-
403+
405404 auto res = m_AlpacaClient->Get (url.c_str ());
406-
405+
407406 if (!res)
408407 {
409408 LOGF_ERROR (" HTTP GET failed for %s" , url.c_str ());
@@ -419,20 +418,20 @@ bool AlpacaFocuser::sendAlpacaGET(const std::string &endpoint, nlohmann::json &r
419418 try
420419 {
421420 response = nlohmann::json::parse (res->body );
422-
421+
423422 // Check for Alpaca errors
424423 if (response.contains (" ErrorNumber" ))
425424 {
426425 int errorNum = response[" ErrorNumber" ].get <int >();
427426 if (errorNum != 0 )
428427 {
429- std::string errorMsg = response.contains (" ErrorMessage" ) ?
430- response[" ErrorMessage" ].get <std::string>() : " Unknown error" ;
428+ std::string errorMsg = response.contains (" ErrorMessage" ) ?
429+ response[" ErrorMessage" ].get <std::string>() : " Unknown error" ;
431430 LOGF_WARN (" Alpaca error %d: %s" , errorNum, errorMsg.c_str ());
432431 return false ;
433432 }
434433 }
435-
434+
436435 return true ;
437436 }
438437 catch (const nlohmann::json::exception &e)
@@ -448,18 +447,18 @@ bool AlpacaFocuser::sendAlpacaPUT(const std::string &endpoint, const std::string
448447 return false ;
449448
450449 std::string url = " /api/v1/focuser/" + std::to_string (m_DeviceNumber) + endpoint;
451-
450+
452451 // Add client info to data
453452 std::string fullData = data;
454453 if (!fullData.empty ())
455454 fullData += " &" ;
456455 fullData += " ClientID=" + std::to_string (m_ClientID);
457456 fullData += " &ClientTransactionID=" + std::to_string (++m_TransactionID);
458-
457+
459458 LOGF_DEBUG (" PUT %s: %s" , url.c_str (), fullData.c_str ());
460-
459+
461460 auto res = m_AlpacaClient->Put (url.c_str (), fullData, " application/x-www-form-urlencoded" );
462-
461+
463462 if (!res)
464463 {
465464 LOGF_ERROR (" HTTP PUT failed for %s" , url.c_str ());
@@ -475,20 +474,20 @@ bool AlpacaFocuser::sendAlpacaPUT(const std::string &endpoint, const std::string
475474 try
476475 {
477476 response = nlohmann::json::parse (res->body );
478-
477+
479478 // Check for Alpaca errors
480479 if (response.contains (" ErrorNumber" ))
481480 {
482481 int errorNum = response[" ErrorNumber" ].get <int >();
483482 if (errorNum != 0 )
484483 {
485- std::string errorMsg = response.contains (" ErrorMessage" ) ?
486- response[" ErrorMessage" ].get <std::string>() : " Unknown error" ;
484+ std::string errorMsg = response.contains (" ErrorMessage" ) ?
485+ response[" ErrorMessage" ].get <std::string>() : " Unknown error" ;
487486 LOGF_WARN (" Alpaca error %d: %s" , errorNum, errorMsg.c_str ());
488487 return false ;
489488 }
490489 }
491-
490+
492491 return true ;
493492 }
494493 catch (const nlohmann::json::exception &e)
0 commit comments