Beispiel #1
0
void lifDIODevice::turn_off(lifMsg msg) {
  std::string rtnMsg;

  //If there are no parameters, turn off all pins associated with this DIODevice
  if (msg.get_num_params() == 0) {
    rtnMsg = "Turned off pins:";
    for (std::vector<lifTSDIOPin*>::iterator it = pins.begin(); it != pins.end(); ++it) {
      (*it)->turn_off();
      rtnMsg = rtnMsg + (*it)->get_pin_name();
    }
    send_back_msg(rtnMsg);
  }    //If there are parameters, turn off pins listed in parameters
  else {
    for (int i = 0; i < msg.get_num_params(); i++) {
      try {
        pins.at(get_vector_position(msg.get_param_at(i)))->turn_off();
        rtnMsg = "Turned off pin: " + msg.get_param_at(i);
        lif_print(rtnMsg);
      } catch (std::exception& e) {
        rtnMsg = "Error turning off pin: " + msg.get_param_at(i) + ". Pin does not exist.";
        throw lifException(rtnMsg);
      }
    }
  }
}
Beispiel #2
0
void lifLEDArray::handle_msg() {
  std::string toDo;
  lifMsg nextMsg;
  runThread = true;
  bool sendStatus;
  std::string successMsg;
  while (runThread) {
    //Lock the msgMutex
    pthread_mutex_lock(&msgMutex);
    //Check if msgQueue is empty
    // - If it is empty, wait hear until you receive a signal that
    //   a message has been pushed onto mainMsgQueue
    if (msgQueue.empty()) {
      pthread_cond_wait(&msgQueuePushed, &msgMutex);
    }
    nextMsg = msgQueue.front();
    msgQueue.pop();
    pthread_mutex_unlock(&msgMutex);

    toDo = nextMsg.get_command();
    sendStatus = true;
    successMsg = "";
    try {
      if (toDo == "Initialize") {
        initialize();
      } else if (toDo == "TurnOnLED") {
        successMsg = turn_on_LED(to_num<unsigned int>(nextMsg.get_param_at(0)));
      } else if (toDo == "TurnOffLED") {
        turn_off_LED();
      } else if (toDo == "NextLED") {
        successMsg = turn_on_next_LED();
      } else if (toDo == "ListLEDs") {
        lifMsg LEDmsg;
        LEDmsg.set_target("MainComm");
        LEDmsg.set_source(devName);
        LEDmsg.set_command("Output");
        std::string LEDList;
        LEDList = "";
        std::string sp = " ";
        std::string sc = ";";
        for (int i = 0; i < LEDs.size(); i++)
          LEDList = LEDList + LEDs.at(i)->get_name() + sp + to_string(LEDs.at(i)->get_wavelength()) + sc;
        LEDmsg.add_params(LEDList);
        send_back_msg(LEDmsg);
      } else if (toDo == "Print") {

      } else if (toDo == "StatusReport") {
        sendStatus = false;
      } else {
        default_msg_handling(nextMsg);
      }
      if (sendStatus)
        status_report("ALL", toDo + "SUCCESS" + " " + successMsg);
    } catch (std::string errStr) {
      status_report("ALL", toDo + "ERROR " + errStr);
    }
  }
}
Beispiel #3
0
void lifDIODevice::get_pin_names(){
  std::vector<std::string> names;
  int i;
  lifMsg msg;
  msg.set_target("MainComm");
  msg.set_command("Output");
  for (std::vector<lifTSDIOPin*>::iterator it = pins.begin(); it != pins.end(); ++it) {
    names.push_back(pins.at(i)->get_pin_name());
    msg.add_param(names.back());
    i++;
  }
  send_back_msg(msg);
}
Beispiel #4
0
void lifMainComm::handle_msg() {
  std::string toDo;
  lifMsg nextMsg;
  runThread = true;

  while (runThread) {
    //Lock the msgMutex
    pthread_mutex_lock(&msgMutex);
    //Check if msgQueue is empty
    // - If it is empty, wait hear until you receive a signal that
    //   a message has been pushed onto mainMsgQueue
    if (msgQueue.empty()) {
      pthread_cond_wait(&msgQueuePushed, &msgMutex);
    }
    nextMsg = msgQueue.front();
    msgQueue.pop();
    pthread_mutex_unlock(&msgMutex);
    toDo = nextMsg.get_command();

    //This command is only issued once at the startup of iLedlif
    //It starts a thread specifically to handle command input
    if (toDo == "Run") {
      void * ptr = static_cast<void*> (this);
      pthread_attr_init(&attr);
      pthread_attr_setdetachstate(& attr, PTHREAD_CREATE_DETACHED);
      pthread_create(&msg_receive_thread, &attr, &run, ptr);
      //pthread_attr_destroy(&attr);
    } else if (toDo == "Stop") {
      pthread_cancel(msg_receive_thread);
      lifMsg rMsg("iLedlif Exited " + devName);
      rMsg.set_source(devName);
      send_back_msg(rMsg);
      runThread = false;
    } else if (toDo == "Output") {
      output(nextMsg);
    } else if (toDo == "StatusReport") {
      std::cout << "Status Report: ";
      output(nextMsg);
    } else {
      default_msg_handling(nextMsg);
    }
  }
}
Beispiel #5
0
std::string lifLEDArray::turn_on_LED(unsigned int LEDNumber) throw (std::string) {
  if (!initialized)
    throw ((std::string)(devName + " has not been initialized."));

  //Check if the LED exists
  if (LEDNumber > LEDs.size() | LEDNumber < 1)
    throw ((std::string)"LED out of range.");

  std::string LEDPwr = LEDs.at(LEDNumber - 1)->get_power_source();

  //If an LED power source is listed, send message to turn it on
  if (LEDPwr != "") {
    lifMsg msg;
    msg.set_target(LEDs.at(LEDNumber - 1)->get_power_source());
    msg.set_command("TurnOn");
    msg.set_source(devName);
    send_back_msg(msg);
    msg.set_source(LEDs.at(LEDNumber - 1)->get_power_source());
    msg.set_command("StatusReport");
    msg.clear_params();
    msg.add_param("TurnOnSUCCESS");
    try {
      wait_for_msg(msg, 5000);
      lif_print("Received successful message reply");
    } catch (std::string errStr) {
      throw (errStr);
    }
  }

  //Turn on the LED by writing the correct value to the MUX
  unsigned int regValue;
  disable_mux();
  regValue = valueMemAcc.readValue();
  valueMemAcc.writeValue((regValue & ~0xf) | (LEDNumber - 1));
  enable_mux(); //enable the mux to actually turn on the LED


  lif_print(((std::string)"Turned on LED #") + to_string(LEDNumber) + ((std::string)"."));
  //lifDevice::status_report("ALL", "Illumination LED " + to_string(LEDs.at(LEDNumber-1)->get_wavelength()));
  //lifDevice::status_report("ALL", "LEDon");
  return (std::string)("Illumination LED " + to_string(LEDs.at(LEDNumber - 1)->get_wavelength()));
}
Beispiel #6
0
//deal calculate para
bool tcp_deal::deal_cal_input(std::string input, calculate_thread * & ret_cal_thread){
    /*
     * input format: json
     * {"pwi_para":"", "dwi_para":"", "pid":""}
     *
     */
    QJsonParseError json_error;
    QJsonDocument parse_doucment = QJsonDocument::fromJson(input.c_str(), &json_error);
    std::map <QString, QString> map_para;
    if(json_error.error == QJsonParseError::NoError){
        if(parse_doucment.isObject()){
            QJsonObject obj = parse_doucment.object();
            for(auto itr = obj.begin(); itr != obj.end(); ++itr){
                map_para[itr.key()] = itr.value().toString();
            }
        }
    }

    QLOG_DEBUG() << "map" << map_para["pwi_para"] << " " << map_para["dwi_para"] << " " <<  map_para["pid"];
    PWIConstTable * pwi_para = new PWIConstTable();
    DWIConstTable * dwi_para = new DWIConstTable();
    pwi_para -> load(map_para["pwi_para"].toStdString());
    dwi_para -> load(map_para["dwi_para"].toStdString());
    calculate_thread * cal_thread = new calculate_thread(this);
    QString data_dir = Config::single() ->mp["pdata_dir"] + "/" + map_para["pid"];
    pid = map_para["pid"];
    version_guid = commutil::generate_uuid();
    QLOG_DEBUG() << "debug guid" << version_guid;
    if(!cal_thread -> set(dwi_para, pwi_para, data_dir, version_guid))
        return false;
    //manage thread
    connect(cal_thread, SIGNAL(finished()), this, SLOT(send_back_msg()));
    connect(cal_thread, SIGNAL(finished()), cal_thread, SLOT(deleteLater()));
    cal_thread -> start();
    //get return value
    ret_cal_thread = cal_thread;
    return true;
}