Skip to content

Commit

Permalink
Add parsing for commands, response structs, position and time commands
Browse files Browse the repository at this point in the history
  • Loading branch information
not7cd committed Mar 25, 2019
1 parent 5984b14 commit 6a906c8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
38 changes: 37 additions & 1 deletion indi-starbook/command_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <exception>
#include <regex>
#include <inditelescope.h>
#include <iomanip>
#include "command_interface.h"


Expand Down Expand Up @@ -181,6 +182,16 @@ namespace starbook {
return SendOkCommand(cmd.str());
}

ResponseCode CommandInterface::SetPlace(LnLat posn, int tz) {
std::ostringstream cmd;
if (tz > 24 || tz < -24) throw std::runtime_error("bad tz");
cmd << "SETPLACE?" << posn
<< "&timezone="
<< std::setfill('0') << std::setw(2) << tz;
return SendOkCommand(cmd.str());
}


ResponseCode CommandInterface::SetSpeed(int speed) {
if (speed < MIN_SPEED || speed > MAX_SPEED)
return ERROR_FORMAT;
Expand All @@ -200,7 +211,6 @@ namespace starbook {
return SendOkCommand(cmd.str());
}


ResponseCode CommandInterface::Move(INDI_DIR_WE dir, INDI::Telescope::TelescopeMotionCommand command) {
std::ostringstream cmd;
cmd << "MOVE?WEST="
Expand Down Expand Up @@ -276,4 +286,30 @@ namespace starbook {

return ERROR_UNKNOWN;
}

PlaceResponse CommandInterface::ParsePlaceResponse(const CommandResponse &response) {
if (!response.status) throw std::runtime_error("can't parse");
return {{0, 0}, 0}; // TODO
}

ln_date CommandInterface::ParseTimeResponse(const CommandResponse &response) {
if (!response.status) throw std::runtime_error("can't parse");
std::stringstream ss{response.payload.at("time")};
UTC time(0, 0, 0, 0, 0, 0);
ss >> time;
return time;
}

XYResponse CommandInterface::ParseXYResponse(const CommandResponse &response) {
if (!response.status) throw std::runtime_error("can't parse");
return {
.x = std::stod(response.payload.at("X")),
.y = std::stod(response.payload.at("Y"))
};
}

long int CommandInterface::ParseRoundResponse(const CommandResponse &response) {
if (!response.status) throw std::runtime_error("can't parse");
return std::stol(response.payload.at("ROUND"));
}
}
11 changes: 8 additions & 3 deletions indi-starbook/command_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ namespace starbook {
} VersionResponse;

typedef struct {
LnLat posn;
int t;
} PlaceResponse;

typedef struct {
double x;
double y;
} XYResponse;

const int MIN_SPEED = 0;
const int MAX_SPEED = 7;
constexpr int MIN_SPEED = 0;
constexpr int MAX_SPEED = 7;

class CommandInterface {
public:
Expand Down Expand Up @@ -93,7 +98,7 @@ namespace starbook {

ResponseCode SetSpeed(int speed);

ResponseCode SetPlace();
ResponseCode SetPlace(LnLat posn, int tz);

ResponseCode SetTime(ln_date &utc);

Expand Down
31 changes: 24 additions & 7 deletions indi-starbook/starbook_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,34 @@ ostream &starbook::operator<<(ostream &os, const starbook::Equ &equ) {
return os;
}

ostream &starbook::operator<<(ostream &os, const starbook::UTC &utc) {
constexpr char sep = '+';

ostream &starbook::operator<<(ostream &os, const starbook::UTC &obj) {
os << setfill('0') << std::fixed << setprecision(0)
<< utc.years << "+"
<< setw(2) << utc.months << "+"
<< setw(2) << utc.days << "+"
<< setw(2) << utc.hours << "+"
<< setw(2) << utc.minutes << "+"
<< setw(2) << floor(utc.seconds);
<< obj.years << sep
<< setw(2) << obj.months << sep
<< setw(2) << obj.days << sep
<< setw(2) << obj.hours << sep
<< setw(2) << obj.minutes << sep
<< setw(2) << floor(obj.seconds);
return os;
}

std::istream &starbook::operator>>(std::istream &is, starbook::UTC &utc) {
int Y, M, D, h, m, s;
std::array<char, 5> ch = {};
is >> Y >> ch[0] >> M >> ch[1] >> D >> ch[2] >> h >> ch[3] >> m >> ch[4] >> s;

if (!is) return is;
for (char i : ch)
if (i != sep) {
is.clear(ios_base::failbit);
return is;
}
utc = UTC(Y, M, D, h, m, static_cast<double>(s));
return is;
}

starbook::CommandResponse::CommandResponse(const std::string &url_like) : status{OK}, raw{url_like} {
if (url_like.empty()) throw runtime_error("parsing error, no payload");
if (url_like.rfind("OK", 0) == 0 || url_like.rfind("ERROR", 0) == 0) {
Expand Down
23 changes: 21 additions & 2 deletions indi-starbook/starbook_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,20 @@ namespace starbook {
UTC(int years, int months, int days, int hours, int minutes, double seconds)
: ln_date{years, months, days, hours, minutes, seconds} {};

friend std::ostream &operator<<(std::ostream &os, const UTC &utc);
friend std::ostream &operator<<(std::ostream &os, const UTC &obj);

friend std::istream &operator>>(std::istream &is, UTC &obj);

};

struct LnLat : ln_lnlat_posn {
// I regret my life decisions
LnLat(double ln, double lat)
: ln_lnlat_posn{ln, lat} {};

friend std::ostream &operator<<(std::ostream &os, const LnLat &obj);

friend std::istream &operator>>(std::istream &is, LnLat &obj);
};

std::ostream &operator<<(std::ostream &os, const DMS &dms);
Expand All @@ -58,7 +71,13 @@ namespace starbook {

std::ostream &operator<<(std::ostream &os, const Equ &equ);

std::ostream &operator<<(std::ostream &os, const UTC &utc);
std::ostream &operator<<(std::ostream &os, const UTC &obj);

std::istream &operator>>(std::istream &is, UTC &obj);

std::ostream &operator<<(std::ostream &os, const LnLat &obj);

std::istream &operator>>(std::istream &is, LnLat &obj);

enum StarbookState {
INIT, /* Initial state after boot */
Expand Down

0 comments on commit 6a906c8

Please sign in to comment.