factory.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 2018 Simon Rasmussen (refactor)
3  *
4  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <cstdlib>
29 
30 static const int UR_PRIMARY_PORT = 30001;
31 
33 {
34 private:
37 
38  uint8_t major_version_;
39  uint8_t minor_version_;
40 
42  {
43  LOG_INFO("Got VersionMessage:");
44  LOG_INFO("project name: %s", vm.project_name.c_str());
45  LOG_INFO("version: %u.%u.%d", vm.major_version, vm.minor_version, vm.svn_version);
46  LOG_INFO("build date: %s", vm.build_date.c_str());
47 
48  major_version_ = vm.major_version;
49  minor_version_ = vm.minor_version;
50 
51  return true;
52  }
53 
55  {
56  }
58  {
59  }
60  void stopConsumer()
61  {
62  }
63 
64 public:
65  URFactory(std::string& host) : stream_(host, UR_PRIMARY_PORT)
66  {
67  URProducer<MessagePacket> prod(stream_, parser_);
68  std::vector<unique_ptr<MessagePacket>> results;
69 
70  prod.setupProducer();
71 
72  if (!prod.tryGet(results) || results.size() == 0)
73  {
74  LOG_FATAL("No version message received, init failed!");
75  std::exit(EXIT_FAILURE);
76  }
77 
78  for (auto const& p : results)
79  {
80  p->consumeWith(*this);
81  }
82 
83  if (major_version_ == 0 && minor_version_ == 0)
84  {
85  LOG_FATAL("No version message received, init failed!");
86  std::exit(EXIT_FAILURE);
87  }
88 
89  prod.teardownProducer();
90  }
91 
92  bool isVersion3()
93  {
94  return major_version_ == 3;
95  }
96 
97  std::unique_ptr<URCommander> getCommander(URStream& stream)
98  {
99  if (major_version_ == 1)
100  return std::unique_ptr<URCommander>(new URCommander_V1_X(stream));
101  else if (minor_version_ < 3)
102  return std::unique_ptr<URCommander>(new URCommander_V3_1__2(stream));
103  else
104  return std::unique_ptr<URCommander>(new URCommander_V3_3(stream));
105  }
106 
107  std::unique_ptr<URParser<StatePacket>> getStateParser()
108  {
109  if (major_version_ == 1)
110  {
111  return std::unique_ptr<URParser<StatePacket>>(new URStateParser_V1_X);
112  }
113  else
114  {
115  if (minor_version_ < 3)
116  return std::unique_ptr<URParser<StatePacket>>(new URStateParser_V3_0__1);
117  else if (minor_version_ < 5)
118  return std::unique_ptr<URParser<StatePacket>>(new URStateParser_V3_2);
119  else
120  return std::unique_ptr<URParser<StatePacket>>(new URStateParser_V3_5);
121  }
122  }
123 
124  std::unique_ptr<URParser<RTPacket>> getRTParser()
125  {
126  if (major_version_ == 1)
127  {
128  if (minor_version_ < 8)
129  return std::unique_ptr<URParser<RTPacket>>(new URRTStateParser_V1_6__7);
130  else
131  return std::unique_ptr<URParser<RTPacket>>(new URRTStateParser_V1_8);
132  }
133  else
134  {
135  if (minor_version_ < 3)
136  return std::unique_ptr<URParser<RTPacket>>(new URRTStateParser_V3_0__1);
137  else
138  return std::unique_ptr<URParser<RTPacket>>(new URRTStateParser_V3_2__3);
139  }
140  }
141 };
uint8_t minor_version
Definition: messages.h:66
std::unique_ptr< URParser< StatePacket > > getStateParser()
Definition: factory.h:107
#define LOG_FATAL(format,...)
Definition: log.h:37
URMessageParser parser_
Definition: factory.h:36
URStateParser< RobotModeData_V3_0__1, MasterBoardData_V3_0__1 > URStateParser_V3_0__1
Definition: state_parser.h:115
URRTStateParser< RTState_V1_8 > URRTStateParser_V1_8
Definition: rt_parser.h:54
URRTStateParser< RTState_V3_2__3 > URRTStateParser_V3_2__3
Definition: rt_parser.h:56
bool consume(VersionMessage &vm)
Definition: factory.h:41
std::string project_name
Definition: messages.h:64
std::string build_date
Definition: messages.h:68
uint8_t major_version
Definition: messages.h:65
bool isVersion3()
Definition: factory.h:92
URRTStateParser< RTState_V1_6__7 > URRTStateParser_V1_6__7
Definition: rt_parser.h:53
int32_t svn_version
Definition: messages.h:67
uint8_t minor_version_
Definition: factory.h:39
#define LOG_INFO(format,...)
Definition: log.h:35
void setupProducer()
Definition: producer.h:38
void teardownProducer()
Definition: producer.h:42
URStateParser< RobotModeData_V1_X, MasterBoardData_V1_X > URStateParser_V1_X
Definition: state_parser.h:114
std::unique_ptr< URParser< RTPacket > > getRTParser()
Definition: factory.h:124
void setupConsumer()
Definition: factory.h:54
std::unique_ptr< URCommander > getCommander(URStream &stream)
Definition: factory.h:97
bool tryGet(std::vector< unique_ptr< T >> &products)
Definition: producer.h:51
void teardownConsumer()
Definition: factory.h:57
URStream stream_
Definition: factory.h:35
URFactory(std::string &host)
Definition: factory.h:65
uint8_t major_version_
Definition: factory.h:38
URStateParser< RobotModeData_V3_2, MasterBoardData_V3_2 > URStateParser_V3_2
Definition: state_parser.h:116
URRTStateParser< RTState_V3_0__1 > URRTStateParser_V3_0__1
Definition: rt_parser.h:55
URStateParser< RobotModeData_V3_5, MasterBoardData_V3_2 > URStateParser_V3_5
Definition: state_parser.h:117
static const int UR_PRIMARY_PORT
Definition: factory.h:30
void stopConsumer()
Definition: factory.h:60


ur_modern_driver
Author(s): Thomas Timm Andersen, Simon Rasmussen
autogenerated on Fri Jun 26 2020 03:37:00