runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
CamManager.cpp
Go to the documentation of this file.
1 #include <CamManager.h>
2 
3 #include <ZmqPubExtractor.h>
4 #include <FileStorageExtractor.h>
5 #include <StatsExtractor.h>
6 #include <FFMpegExtractor.h>
7 #include <stringtricks.h>
8 #include <config.h>
9 
10 #include <iostream>
11 
12 using namespace std;
13 
14 CamManager::CamManager(zmq::context_t & _ctx,
15  const std::string & _brokerEndpoint,
16  const std::string & _serviceName,
17  const PubSubEndpoint & _cameraSource,
18  StorageManager & _storageManager):
19  RocServer(_ctx, _brokerEndpoint, _serviceName),
20  cameraSource(_cameraSource),
21  logger(ctx, "inproc://log", "camserver", SSTR("camManager.", cameraSource.topic)) {
22  storageManager = &_storageManager;
23 }
24 
26  unique_lock<mutex> _(extractorsMutex);
27  for(auto extractorP: extractors) {
28  extractorP->stopServing();
29  delete extractorP;
30  }
31 }
32 
33 Json::Value CamManager::call(const std::string & name, const Json::Value & arguments) {
34  if(name == "addExtractor") {
35  return addExtractor(arguments[0].asString(), arguments[1]);
36  } else if(name == "listExtractors") {
37  return listExtractors();
38  } else if(name == "removeExtractor") {
39  return removeExtractor(arguments[0].asString(), arguments[1].asString());
40  }
41  throw RocNotImplementedError();
42 }
43 
44 
45 Json::Value CamManager::addExtractor(const std::string & name, const Json::Value & arguments) {
46  logger.info(SSTR("add Extractor: ", name, " (", arguments, ") "));
47  auto identifier = arguments[0].asString();
48  {
49  unique_lock<mutex> _(extractorsMutex);
50  for(auto extractorP: extractors) {
51  if(extractorP->type() == name && extractorP->identifier() == identifier) {
52  /* already registered! */
53  logger.debug("already registered!");
54  return "already registered!";
55  }
56  }
57  }
58  Extractor * extractor;
59  if(name == "zmqPub") {
60  extractor = new ZmqPubExtractor(ctx,
61  cameraSource,
62  {"inproc://output", identifier},
63  {arguments[1][0].asUInt(),
64  arguments[1][1].asUInt(),
65  arguments[1][2].asUInt(),
66  arguments[1][3].asUInt()});
67  } else if(name == "fileStorage") {
68  int maxFrames = -1; /* infinite */
69  if(arguments.size() > 1) {
70  maxFrames = arguments[1].asInt();
71  }
72  string metaData = "{}";
73  if(arguments.size() > 2) {
74  metaData = arguments[2].asString();
75  }
76  int fileCycling = -1;
77  if(arguments.size() > 3) {
78  fileCycling = arguments[3].asInt();
79  }
80  bool cumulative = false;
81  if(arguments.size() > 4) {
82  cumulative = arguments[4].asBool();
83  }
84  bool binning = false;
85  if(arguments.size() > 5) {
86  if(cameraSource.topic.compare(0, 4, "vnir") == 0)
87  binning = arguments[5].asBool();
88  }
89  double wantedFPS = 0;
90  if(arguments.size() > 6) {
91  wantedFPS = arguments[6].asDouble();
92  }
93  extractor = new FileStorageExtractor(ctx, cameraSource, identifier, *storageManager, maxFrames, metaData, fileCycling, cumulative, binning, wantedFPS);
94  } else if(name == "Stats") {
95  extractor = new StatsExtractor(ctx,
96  cameraSource,
97  {"inproc://log", identifier},
98  identifier);
99  } else if(name == "FFMpeg") {
100  auto encode_to = arguments[1];
101  extractor = new FFMpegExtractor(ctx,
102  cameraSource,
103  identifier,
104  encode_to.asString());
105 
106  } else {
107  throw RocNotImplementedError();
108  }
109  extractor->startServing();
110  unique_lock<mutex> _(extractorsMutex);
111  extractors.push_back(extractor);
112  return Json::Value::null;
113 }
114 
116  /* cout << "list Extractors" << endl; */
117  Json::Value ret(Json::ValueType::arrayValue);
118  unique_lock<mutex> _(extractorsMutex);
119  for(auto extractorP: extractors) {
120  Json::Value exId;
121  exId["type"] = extractorP->type();
122  exId["identifier"] = extractorP->identifier();
123  Json::Value params;
124  for(auto param: extractorP->parameters()) {
125  params.append(param);
126  }
127  exId["parameters"] = params;
128  ret.append(exId);
129  }
130  return ret;
131 }
132 
133 
134 Json::Value CamManager::removeExtractor(const std::string & name, const std::string & identifier) {
135  bool removedSomething = false;
136  logger.info(SSTR("removing ", name, " / ", identifier, " extractor."));
137  {
138  unique_lock<mutex> _(extractorsMutex);
139  for(auto it = extractors.begin(); it != extractors.end();) {
140  if((*it)->type() == name && (*it)->identifier() == identifier) {
141  (*it)->stopServing();
142  delete (*it);
143  removedSomething = true;
144  it = extractors.erase(it);
145  } else {
146  ++it;
147  }
148  }
149  }
150  cout << "done removing." << endl;
151  if(removedSomething) {
152  return Json::Value::null;
153  } else {
154  return "not found";
155  }
156 }
Base class for an image extractor.
Definition: Extractor.h:17
STL namespace.
void debug(const std::string &_msg)
Definition: RocLogger.cpp:24
Handles storage locations.
std::string SSTR(Args &&...components)
Creates a temporary string stream for string concatenation.
Definition: stringtricks.h:21
zmq::context_t & ctx
Definition: RocServer.h:77
Json::Value listExtractors()
Definition: CamManager.cpp:115
Json::Value removeExtractor(const std::string &name, const std::string &identifier)
Definition: CamManager.cpp:134
Json::Value addExtractor(const std::string &name, const Json::Value &arguments)
Definition: CamManager.cpp:45
std::string topic
Definition: utils.h:16
void startServing()
Definition: Extractor.cpp:20
CamManager(zmq::context_t &_ctx, const std::string &_brokerEndpoint, const std::string &_serviceName, const PubSubEndpoint &_cameraSource, StorageManager &_storageManager)
Definition: CamManager.cpp:14
void info(const std::string &_msg)
Definition: RocLogger.cpp:28