summaryrefslogtreecommitdiffstats
path: root/src/DetectorModule/DetectorModule.cpp
blob: d2c82988687138dd83e88f0f03ad8c3476040d31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright 2016 Tobias Frust
 *
 * DetectorModule.cpp
 *
 *  Created on: 29.06.2016
 *      Author: Tobias Frust
 */

#include "DetectorModule.h"
#include "../ConfigReader/ConfigReader.h"

#include <exception>
#include <fstream>

DetectorModule::DetectorModule(const int detectorID, const std::string& address, const std::string& configPath) :
   detectorID_{detectorID},
   numberOfDetectorsPerModule_{16},
   index_{0},
   client_{address, detectorID + 4000} {

   if (readConfig(configPath)) {
      throw std::runtime_error("DetectorModule: Configuration file could not be loaded successfully. Please check!");
   }

   //read the input data from the file corresponding to the detectorModuleID
   readInput();
}

auto DetectorModule::sendPeriodically(unsigned int timeIntervall) -> void {
   client_.send((char*)buffer_.data(), sizeof(unsigned short)*numberOfDetectorsPerModule_*numberOfProjections_);
}

auto DetectorModule::readInput() -> void {
   if(path_.back() != '/')
      path_.append("/");
   //open file
   std::ifstream input(path_ + fileName_ + std::to_string(detectorID_) + fileEnding_, std::ios::in | std::ios::binary);
   //allocate memory in vector
   std::streampos fileSize;
   input.seekg(0, std::ios::end);
   fileSize = input.tellg();
   input.seekg(0, std::ios::beg);
   buffer_.resize(fileSize / sizeof(unsigned short));
   input.read((char*) &buffer_[0], fileSize);
}

auto DetectorModule::readConfig(const std::string& configFile) -> bool {
     ConfigReader configReader = ConfigReader(configFile.data());
     int samplingRate, scanRate;
     if (configReader.lookupValue("numberOfFanDetectors", numberOfDetectors_)
           && configReader.lookupValue("dataInputPath", path_)
           && configReader.lookupValue("dataFileName", fileName_)
           && configReader.lookupValue("dataFileEnding", fileEnding_)
           && configReader.lookupValue("numberOfPlanes", numberOfPlanes_)
           && configReader.lookupValue("samplingRate", samplingRate)
           && configReader.lookupValue("scanRate", scanRate)
           && configReader.lookupValue("numberOfDataFrames", numberOfFrames_)) {
        numberOfProjections_ = samplingRate * 1000000 / scanRate;
        return EXIT_SUCCESS;
     }

     return EXIT_FAILURE;
  }