22 #include "goby/common/logger.h" 23 #include "goby/common/logger/term_color.h" 24 #include "goby/common/zeromq_service.h" 26 #include "goby/acomms/connect.h" 27 #include "goby/acomms/modem_driver.h" 28 #include "goby/acomms/modemdriver/driver_exception.h" 29 #include "goby/acomms/modemdriver/iridium_driver.h" 30 #include "goby/acomms/modemdriver/iridium_shore_driver.h" 31 #include "goby/acomms/modemdriver/udp_driver.h" 33 #include "goby/pb/application.h" 34 #include "goby/pb/pb_modem_driver.h" 36 #include "goby/acomms/protobuf/modem_driver_status.pb.h" 37 #include "modemdriver_config.pb.h" 62 std::string modem_id_str() {
return goby::util::as<std::string>(cfg_.driver_cfg().modem_id()); }
68 boost::shared_ptr<goby::common::ZeroMQService> zeromq_service_;
71 boost::shared_ptr<boost::asio::io_service> asio_service_;
73 boost::shared_ptr<goby::acomms::ModemDriverBase> driver_;
75 bool data_response_received_;
78 bool initiate_transmit_pending_;
83 double last_status_time_;
89 int main(
int argc,
char* argv[])
92 goby::run<goby::acomms::ModemDriver>(argc, argv, &cfg);
99 initiate_transmit_pending_(
false), driver_started_(
false), last_status_time_(0)
101 glog.is(DEBUG1) &&
glog << cfg_.DebugString() << std::endl;
103 switch (cfg_.driver_type())
105 case goby::acomms::protobuf::DRIVER_WHOI_MICROMODEM:
109 case goby::acomms::protobuf::DRIVER_PB_STORE_SERVER:
114 case goby::acomms::protobuf::DRIVER_UDP:
115 asio_service_.reset(
new boost::asio::io_service);
119 case goby::acomms::protobuf::DRIVER_IRIDIUM:
124 case goby::acomms::protobuf::DRIVER_IRIDIUM_SHORE:
129 case goby::acomms::protobuf::DRIVER_NONE:
134 subscribe(&ModemDriver::handle_initiate_transmission,
this,
135 "Tx" + goby::util::as<std::string>(cfg_.driver_cfg().modem_id()));
137 subscribe(&ModemDriver::handle_data_response,
this,
138 "DataResponse" + goby::util::as<std::string>(cfg_.driver_cfg().modem_id()));
140 connect(&driver_->signal_receive,
this, &ModemDriver::handle_modem_receive);
142 connect(&driver_->signal_data_request,
this, &ModemDriver::handle_modem_data_request);
144 status_.set_src(cfg_.driver_cfg().modem_id());
145 status_.set_status(goby::acomms::protobuf::ModemDriverStatus::NOMINAL);
148 goby::acomms::ModemDriver::~ModemDriver()
154 void goby::acomms::ModemDriver::loop()
160 if (!driver_started_)
162 driver_->startup(cfg_.driver_cfg());
163 driver_started_ =
true;
164 status_.set_status(goby::acomms::protobuf::ModemDriverStatus::NOMINAL);
175 if (last_status_time_ + cfg_.status_period_s() <= now)
177 status_.set_time(now);
178 publish(status_,
"Status" + modem_id_str());
179 last_status_time_ = now;
182 if (initiate_transmit_pending_)
184 driver_->handle_initiate_transmission(initiate_transmission_);
185 initiate_transmit_pending_ =
false;
191 publish(*msg,
"DataRequest" + modem_id_str());
192 data_response_received_ =
false;
197 zeromq_service().poll(10000);
198 if (data_response_received_)
200 *msg = data_response_;
204 if (!data_response_received_)
205 glog.is(WARN) &&
glog <<
"Timeout waiting for response to data request" << std::endl;
210 publish(message,
"Rx" + modem_id_str());
215 data_response_received_ =
true;
216 data_response_ = message;
219 void goby::acomms::ModemDriver::handle_initiate_transmission(
223 initiate_transmit_pending_ =
true;
224 initiate_transmission_ = message;
229 status_.set_status(e.status());
230 status_.set_n_resets(status_.n_resets() + 1);
231 glog.is(WARN) &&
glog <<
"Exception: " << e.what() << std::endl;
232 const int restart_sec = 15;
233 glog.is(WARN) &&
glog <<
"Shutting down driver." << std::endl;
235 glog.is(WARN) &&
glog <<
"Attempting to restart driver in " << restart_sec <<
" seconds." 238 driver_started_ =
false;
Base class provided for users to generate applications that participate in the Goby publish/subscribe...
provides an API to the WHOI Micro-Modem driver
double goby_time< double >()
Returns current UTC time as seconds and fractional seconds since 1970-01-01 00:00:00.
void connect(Signal *signal, Slot slot)
connect a signal to a slot (e.g. function pointer)
common::FlexOstream glog
Access the Goby logger through this object.
The global namespace for the Goby project.
simple exception class for goby applications