<commit_before>//#define TWO_CAM
//#define DISPLAY
#define DEBUG_NO_SENSORS true

#include <fstream>
#include "CameraLogger.h"
#include "GPSLogger.h"

#define ZMQ_CAMERALOGGER_BIND_PORT 5001 // this app will listen on this port
#define ZMQ_DIAGNOSTICS_SEND_PORT 5000 // this app will send to master on this port

#define CAMERA_DISPLAY_SKIP 3
#define NUMTHREAD_PER_BUFFER 10

SyncBuffer cam1_buff[NUMTHREAD_PER_BUFFER];
SyncBuffer cam2_buff[NUMTHREAD_PER_BUFFER];

zmq::context_t context(1);
zmq::socket_t my_commands(context, ZMQ_SUB);
zmq::socket_t send_diagnostics(context, ZMQ_PUB);

bool is_done_working = false; 
bool quit_via_user_input = false;

Time currentTime(boost::posix_time::microsec_clock::local_time());
Time lastTime(currentTime);

inline void ImageCallback(Image* pImage, const void* pCallbackData, 
    SyncBuffer* w_buff) { 
  if (!is_done_working) {
    Image* im = new Image();
    pImage->Convert(PIXEL_FORMAT_BGR, im);
    if (!w_buff->getBuffer()->pushBack(im)) {
      boost::mutex::scoped_lock io_lock ( *(w_buff->getMutex()) );
      cerr << "Warning! Buffer full, overwriting data!" << endl; 
    }
  }
}

inline void sendDiagnosticsMessage(string message) { 
  zmq::message_t diag_msg_t((void *) message.c_str(), message.length(), NULL);
  send_diagnostics.send(diag_msg_t, ZMQ_NOBLOCK);
}

void ctrlC (int)
{
#ifndef DISPLAY
  //printf("\nCtrl-C detected, exit condition set to true.\n");
  //is_done_working = true;
#endif
#ifdef DISPLAY
  printf("\nCtrl-C Disabled! Use 'q' to quit instead\n");
#endif
}

int main(int argc, char** argv)
{
  using namespace boost::program_options;
  options_description desc("Allowed options");
  desc.add_options()
    ("help", "produce help message")
    ("serial,s", value<string>(), "the serial location for the gps unit")
    ("maxframes,m", value<uint64_t>(), "maximum number of frames to capture")
    ("output,o", value<string>(), "the filename for data logging");

  signal (SIGINT, ctrlC);

  string diagnostics_address = "tcp://localhost:"+boost::to_string(ZMQ_DIAGNOSTICS_SEND_PORT);
  send_diagnostics.connect(diagnostics_address.c_str());
  sleep(1);
  sendDiagnosticsMessage("WARN:Connecting to Cameras...");

  variables_map vm;
  store(parse_command_line(argc, argv, desc), vm);
  notify(vm);
  if (vm.count("help")) { 
    cout << desc << endl;
    return 1;
  }
  string fname1 = "";
  string fname2 = "";
  string fnamegps = "";
  if (vm.count("output")) { 
    fname1 = vm["output"].as<string>() + "1.avi";
    fname2 = vm["output"].as<string>() + "2.avi";
    fnamegps = vm["output"].as<string>() + "_gps.out";
  }
  else {
    cout << desc << endl;
    return 1;
  };

  uint64_t maxframes = -1; 
  if (vm.count("maxframes")) {
    maxframes = vm["maxframes"].as<uint64_t>(); 
  }

  bool useGPS = true & !DEBUG_NO_SENSORS;
  GPSLogger gpsLogger;
  ofstream gps_output;
  if (useGPS) {
    gps_output.open(fnamegps.c_str());
    gpsLogger.Connect(vm["serial"].as<string>());
  }
  int imageWidth = 1280;
  int imageHeight = 960;

  cout << "Capturing for maximum of " << maxframes << " frames" << endl; 
  cout  << "Filenames: " << fname1 << " " << fname2 << endl; 

  for (int thread_num = 0; thread_num < NUMTHREAD_PER_BUFFER; thread_num++)
  {
    cam1_buff[thread_num].getBuffer()->setCapacity(1000);
    cam2_buff[thread_num].getBuffer()->setCapacity(1000);
  }

#ifndef DEBUG_NO_SENSORS
  Camera* cam1 = ConnectCamera(0); // 0 indexing
  assert(cam1 != NULL);
  RunCamera(cam1);

  Consumer<Image>* cam1_consumer[NUMTHREAD_PER_BUFFER];
  for (int thread_num = 0; thread_num < NUMTHREAD_PER_BUFFER; thread_num++) {
    stringstream sstm;
    sstm << "split_" << thread_num << "_" << fname1; 
    string thread_fname = sstm.str();
    cam1_consumer[thread_num] = new Consumer<Image>(
        cam1_buff[thread_num].getBuffer(),
        thread_fname, cam1_buff[thread_num].getMutex(), 50.0f,
        imageWidth, imageHeight);
  }
#endif

#ifdef DISPLAY
  cvNamedWindow("cam1", CV_WINDOW_AUTOSIZE);
#endif

#ifdef TWO_CAM
  Camera* cam2 = ConnectCamera(1); // 0 indexing 
  assert(cam2 != NULL);
  RunCamera(cam2); 
  Consumer<Image>* cam2_consumer[NUMTHREAD_PER_BUFFER];
  for (int thread_num = 0; thread_num < NUMTHREAD_PER_BUFFER; thread_num++) {
    stringstream sstm;
    sstm << "split_" << thread_num << "_" << fname2; 
    string thread_fname = sstm.str();
    cam2_consumer[thread_num] = new Consumer<Image>(
        cam2_buff[thread_num].getBuffer(),
        thread_fname, cam2_buff[thread_num].getMutex(), 50.0f,
        imageWidth, imageHeight);
  }
#ifdef DISPLAY
  cvNamedWindow("cam2", CV_WINDOW_AUTOSIZE);
#endif //DISPLAY
#endif //TWO_CAM

#ifdef DISPLAY
  IplImage* img = cvCreateImage(cvSize(imageWidth, imageHeight), IPL_DEPTH_8U, 3);
  int counter = 0;
#endif

  //// setup ZMQ communication
  string bind_address = "tcp://*:"+boost::to_string(ZMQ_CAMERALOGGER_BIND_PORT);
  cout << "binding to " << bind_address << endl; 
  my_commands.bind("tcp://*:5001");
  my_commands.setsockopt(ZMQ_SUBSCRIBE, NULL, 0);
  cout << "binding successful" << endl; 
  
  // start GPS Trigger
  if (useGPS) gpsLogger.Run();
  
  uint64_t numframes = 0;
  uint64_t lastframes = 0; 
  ///////// main loop
  while (!is_done_working) {
    numframes++;
    if (numframes > maxframes) { 
      is_done_working = true;
    }
    try {
      zmq::message_t command_msg; 
      if (my_commands.recv(&command_msg, ZMQ_NOBLOCK) == true) { 
        string command((const char*)command_msg.data(), command_msg.size());
        if (command.compare("TERMINATE") == 0) {
          // properly close the application 
          is_done_working = true; 
        } 
        else if (command.compare("LASTCAMERADATA") == 0) { 
          // transmit the camera data over
        }
        else if (command.compare("LASTGPSDATA") == 0) { 
          // transmit the last known GPS log over
        }
      }
    } catch (const zmq::error_t&e) {}

#ifndef DEBUG_NO_SENSORS
    Image image; 
    cam1->RetrieveBuffer(&image);
    ImageCallback(&image, NULL, &cam1_buff[numframes % NUMTHREAD_PER_BUFFER]);

#ifdef DISPLAY
    counter = (counter + 1) % CAMERA_DISPLAY_SKIP;
    Image cimage; 
    if (counter == 0) {
      image.Convert(PIXEL_FORMAT_BGR, &cimage);
      show(&cimage, img, "cam1");
    }
#endif //DISPLAY
#endif //DEBUG_NO_SENSORS
#ifdef TWO_CAM
    cam2->RetrieveBuffer(&image);<commit_msg>adding handling of terminate<commit_after>//#define TWO_CAM
//#define DISPLAY
#define DEBUG_NO_SENSORS true

#include <fstream>
#include "CameraLogger.h"
#include "GPSLogger.h"

#define ZMQ_CAMERALOGGER_BIND_PORT 5001 // this app will listen on this port
#define ZMQ_DIAGNOSTICS_SEND_PORT 5000 // this app will send to master on this port

#define CAMERA_DISPLAY_SKIP 3
#define NUMTHREAD_PER_BUFFER 10

SyncBuffer cam1_buff[NUMTHREAD_PER_BUFFER];
SyncBuffer cam2_buff[NUMTHREAD_PER_BUFFER];

zmq::context_t context(1);
zmq::socket_t my_commands(context, ZMQ_SUB);
zmq::socket_t send_diagnostics(context, ZMQ_PUB);

bool is_done_working = false; 
bool quit_via_user_input = false;

Time currentTime(boost::posix_time::microsec_clock::local_time());
Time lastTime(currentTime);

inline void ImageCallback(Image* pImage, const void* pCallbackData, 
    SyncBuffer* w_buff) { 
  if (!is_done_working) {
    Image* im = new Image();
    pImage->Convert(PIXEL_FORMAT_BGR, im);
    if (!w_buff->getBuffer()->pushBack(im)) {
      boost::mutex::scoped_lock io_lock ( *(w_buff->getMutex()) );
      cerr << "Warning! Buffer full, overwriting data!" << endl; 
    }
  }
}

inline void sendDiagnosticsMessage(string message) { 
  zmq::message_t diag_msg_t((void *) message.c_str(), message.length(), NULL);
  send_diagnostics.send(diag_msg_t, ZMQ_NOBLOCK);
}

void ctrlC (int)
{
#ifndef DISPLAY
  //printf("\nCtrl-C detected, exit condition set to true.\n");
  //is_done_working = true;
#endif
#ifdef DISPLAY
  printf("\nCtrl-C Disabled! Use 'q' to quit instead\n");
#endif
}

int main(int argc, char** argv)
{
  using namespace boost::program_options;
  options_description desc("Allowed options");
  desc.add_options()
    ("help", "produce help message")
    ("serial,s", value<string>(), "the serial location for the gps unit")
    ("maxframes,m", value<uint64_t>(), "maximum number of frames to capture")
    ("output,o", value<string>(), "the filename for data logging");

  signal (SIGINT, ctrlC);

  string diagnostics_address = "tcp://localhost:"+boost::to_string(ZMQ_DIAGNOSTICS_SEND_PORT);
  send_diagnostics.connect(diagnostics_address.c_str());
  sleep(1);
  sendDiagnosticsMessage("WARN:Connecting to Cameras...");

  variables_map vm;
  store(parse_command_line(argc, argv, desc), vm);
  notify(vm);
  if (vm.count("help")) { 
    cout << desc << endl;
    return 1;
  }
  string fname1 = "";
  string fname2 = "";
  string fnamegps = "";
  if (vm.count("output")) { 
    fname1 = vm["output"].as<string>() + "1.avi";
    fname2 = vm["output"].as<string>() + "2.avi";
    fnamegps = vm["output"].as<string>() + "_gps.out";
  }
  else {
    cout << desc << endl;
    return 1;
  };

  uint64_t maxframes = -1; 
  if (vm.count("maxframes")) {
    maxframes = vm["maxframes"].as<uint64_t>(); 
  }

  bool useGPS = true & !DEBUG_NO_SENSORS;
  GPSLogger gpsLogger;
  ofstream gps_output;
  if (useGPS) {
    gps_output.open(fnamegps.c_str());
    gpsLogger.Connect(vm["serial"].as<string>());
  }
  int imageWidth = 1280;
  int imageHeight = 960;

  cout << "Capturing for maximum of " << maxframes << " frames" << endl; 
  cout  << "Filenames: " << fname1 << " " << fname2 << endl; 

  for (int thread_num = 0; thread_num < NUMTHREAD_PER_BUFFER; thread_num++)
  {
    cam1_buff[thread_num].getBuffer()->setCapacity(1000);
    cam2_buff[thread_num].getBuffer()->setCapacity(1000);
  }

#ifndef DEBUG_NO_SENSORS
  Camera* cam1 = ConnectCamera(0); // 0 indexing
  assert(cam1 != NULL);
  RunCamera(cam1);

  Consumer<Image>* cam1_consumer[NUMTHREAD_PER_BUFFER];
  for (int thread_num = 0; thread_num < NUMTHREAD_PER_BUFFER; thread_num++) {
    stringstream sstm;
    sstm << "split_" << thread_num << "_" << fname1; 
    string thread_fname = sstm.str();
    cam1_consumer[thread_num] = new Consumer<Image>(
        cam1_buff[thread_num].getBuffer(),
        thread_fname, cam1_buff[thread_num].getMutex(), 50.0f,
        imageWidth, imageHeight);
  }
#endif

#ifdef DISPLAY
  cvNamedWindow("cam1", CV_WINDOW_AUTOSIZE);
#endif

#ifdef TWO_CAM
  Camera* cam2 = ConnectCamera(1); // 0 indexing 
  assert(cam2 != NULL);
  RunCamera(cam2); 
  Consumer<Image>* cam2_consumer[NUMTHREAD_PER_BUFFER];
  for (int thread_num = 0; thread_num < NUMTHREAD_PER_BUFFER; thread_num++) {
    stringstream sstm;
    sstm << "split_" << thread_num << "_" << fname2; 
    string thread_fname = sstm.str();
    cam2_consumer[thread_num] = new Consumer<Image>(
        cam2_buff[thread_num].getBuffer(),
        thread_fname, cam2_buff[thread_num].getMutex(), 50.0f,
        imageWidth, imageHeight);
  }
#ifdef DISPLAY
  cvNamedWindow("cam2", CV_WINDOW_AUTOSIZE);
#endif //DISPLAY
#endif //TWO_CAM

#ifdef DISPLAY
  IplImage* img = cvCreateImage(cvSize(imageWidth, imageHeight), IPL_DEPTH_8U, 3);
  int counter = 0;
#endif

  //// setup ZMQ communication
  string bind_address = "tcp://*:"+boost::to_string(ZMQ_CAMERALOGGER_BIND_PORT);
  cout << "binding to " << bind_address << endl; 
  my_commands.bind("tcp://*:5001");
  my_commands.setsockopt(ZMQ_SUBSCRIBE, NULL, 0);
  cout << "binding successful" << endl; 
  
  // start GPS Trigger
  if (useGPS) gpsLogger.Run();
  
  uint64_t numframes = 0;
  uint64_t lastframes = 0; 
  ///////// main loop
  while (!is_done_working) {
    numframes++;
    if (numframes > maxframes) { 
      is_done_working = true;
    }
    try {
      zmq::message_t command_msg; 
      if (my_commands.recv(&command_msg, ZMQ_NOBLOCK) == true) { 
        string command((const char*)command_msg.data(), command_msg.size());
        if (command.compare("TERMINATE") == 0) {
          // properly close the application 
          is_done_working = true; 
          quit_via_user_input = true;
        } 
        else if (command.compare("LASTCAMERADATA") == 0) { 
          // transmit the camera data over
        }
        else if (command.compare("LASTGPSDATA") == 0) { 
          // transmit the last known GPS log over
        }
      }
    } catch (const zmq::error_t&e) {}

#ifndef DEBUG_NO_SENSORS
    Image image; 
    cam1->RetrieveBuffer(&image);
    ImageCallback(&image, NULL, &cam1_buff[numframes % NUMTHREAD_PER_BUFFER]);

#ifdef DISPLAY
    counter = (counter + 1) % CAMERA_DISPLAY_SKIP;
    Image cimage; 
    if (counter == 0) {
      image.Convert(PIXEL_FORMAT_BGR, &cimage);
      show(&cimage, img, "cam1");
    }
#endif //DISPLAY
#endif //DEBUG_NO_SENSORS
#ifdef TWO_CAM
    cam2->RetrieveBuffer(&image);
{'fraction_non_alphanumeric': 0.14353768059351815, 'fraction_numerical': 0.016087465833658728, 'mean_word_length': 3.904634239754883, 'pattern_counts': {'":': 0, '<': 147, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5298125', 'n_tokens_mistral': 4715, 'n_tokens_neox': 4487, 'n_words': 962}