aboutsummaryrefslogtreecommitdiff
path: root/trunk-recorder/talkgroups.cc
blob: 7c1c75c6812ca2a09a46088c52fa9549fb02cf20 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "talkgroups.h"

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/intrusive_ptr.hpp>
#include <boost/log/trivial.hpp>
#include <boost/tokenizer.hpp>

#include "csv_helper.h"
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>

Talkgroups::Talkgroups() {}

void Talkgroups::load_talkgroups(int sys_num, std::string filename) {
  if (filename == "") {
    return;
  }

  std::ifstream in(filename.c_str());

  if (!in.is_open()) {
    BOOST_LOG_TRIVIAL(error) << "Error Opening TG File: " << filename << std::endl;
    return;
  }

  boost::escaped_list_separator<char> sep("\\", ",\t", "\"");
  typedef boost::tokenizer<boost::escaped_list_separator<char>> t_tokenizer;

  std::vector<std::string> vec;
  std::string line;

  int lines_read = 0;
  int lines_pushed = 0;
  int priority = 1;
  bool radioreference_format = false;

  while (!safeGetline(in, line).eof()) // this works with \r, \n, or \r\n
  {
    if (line.size() && (line[line.size() - 1] == '\r')) {
      line = line.substr(0, line.size() - 1);
    }

    lines_read++;

    if (line == "")
      continue;

    t_tokenizer tok(line, sep);

    vec.assign(tok.begin(), tok.end());
    if (strcmp(vec[0].c_str(), "Decimal") == 0) {
      radioreference_format = true;
      BOOST_LOG_TRIVIAL(info) << "Found radioreference.com header, assuming direct csv download";
      // don't warn later on about the header line not being a valid talk group
      lines_pushed++;
      continue;
    }
    Talkgroup *tg = NULL;
    if (radioreference_format) {
      // Talkgroup configuration columns:
      //
      // [0] - talkgroup number
      // [1] - unused (talkgroup number in hex)
      // [2] - alpha_tag
      // [3] - mode
      // [4] - description
      // [5] - tag
      // [6] - group

      if (vec.size() != 7) {
        BOOST_LOG_TRIVIAL(error) << "Malformed radioreference talkgroup entry at line " << lines_read << ".";
        continue;
      }

      tg = new Talkgroup(sys_num, atoi(vec[0].c_str()), vec[3].c_str(), vec[2].c_str(), vec[4].c_str(), vec[5].c_str(), vec[6].c_str(), 1);
    } else {
      // Talkgroup configuration columns:
      //
      // [0] - talkgroup number
      // [1] - unused
      // [2] - mode
      // [3] - alpha_tag
      // [4] - description
      // [5] - tag
      // [6] - group
      // [7] - priority

      if (!((vec.size() == 8) || (vec.size() == 7))) {
        BOOST_LOG_TRIVIAL(error) << "Malformed talkgroup entry at line " << lines_read << ".";
        continue;
      }
      // TODO(nkw): more sanity checking here.
      priority = (vec.size() == 8) ? atoi(vec[7].c_str()) : 1;

      tg = new Talkgroup(sys_num, atoi(vec[0].c_str()), vec[2].c_str(), vec[3].c_str(), vec[4].c_str(), vec[5].c_str(), vec[6].c_str(), priority);
    }
    talkgroups.push_back(tg);
    lines_pushed++;
  }

  if (lines_pushed != lines_read) {
    // The parser above is pretty brittle. This will help with debugging it, for
    // now.
    BOOST_LOG_TRIVIAL(error) << "Warning: skipped " << lines_read - lines_pushed << " of " << lines_read << " talkgroup entries! Invalid format?";
    BOOST_LOG_TRIVIAL(error) << "The format is very particular. See https://github.com/robotastic/trunk-recorder for example input.";
  } else {
    BOOST_LOG_TRIVIAL(info) << "Read " << lines_pushed << " talkgroups.";
  }
}

void Talkgroups::load_channels(int sys_num, std::string filename) {
  if (filename == "") {
    return;
  }

  std::ifstream in(filename.c_str());

  if (!in.is_open()) {
    BOOST_LOG_TRIVIAL(error) << "Error Opening Channel File: " << filename << std::endl;
    return;
  }

  boost::escaped_list_separator<char> sep("\\", ",\t", "\"");
  typedef boost::tokenizer<boost::escaped_list_separator<char>> t_tokenizer;

  std::vector<std::string> vec;
  std::string line;

  int lines_read = 0;
  int lines_pushed = 0;

  while (!safeGetline(in, line).eof()) // this works with \r, \n, or \r\n
  {
    if (line.size() && (line[line.size() - 1] == '\r')) {
      line = line.substr(0, line.size() - 1);
    }

    lines_read++;

    if (line == "")
      continue;

    t_tokenizer tok(line, sep);

    vec.assign(tok.begin(), tok.end());

    Talkgroup *tg = NULL;

    // Channel File  columns:
    //
    // [0] - talkgroup number
    // [1] - channel freq
    // [2] - tone
    // [3] - alpha_tag
    // [4] - description
    // [5] - tag
    // [6] - group
    // [7] - enable (Optional, default is True)

    if ((vec.size() != 7) && (vec.size() != 8)) {
      BOOST_LOG_TRIVIAL(error) << "Malformed channel entry at line " << lines_read << ". Found: " << vec.size() << " Expected 7 or 8";
      continue;
    }
    // TODO(nkw): more sanity checking here.
    bool enable = true;
    if (vec.size() == 8) {
      boost::trim(vec[7]);
      if (boost::iequals(vec[7], "false")) {
        enable = false;
      }
    }

    // Talkgroup(long num, double freq, double tone, std::string mode, std::string alpha_tag, std::string description, std::string tag, std::string group) {
    if (enable) {
      tg = new Talkgroup(sys_num, atoi(vec[0].c_str()), std::stod(vec[1]), std::stod(vec[2]), vec[3].c_str(), vec[4].c_str(), vec[5].c_str(), vec[6].c_str());

      talkgroups.push_back(tg);
    }
    lines_pushed++;
  }

  if (lines_pushed != lines_read) {
    // The parser above is pretty brittle. This will help with debugging it, for
    // now.
    BOOST_LOG_TRIVIAL(error) << "Warning: skipped " << lines_read - lines_pushed << " of " << lines_read << " channel entries! Invalid format?";
    BOOST_LOG_TRIVIAL(error) << "The format is very particular. See https://github.com/robotastic/trunk-recorder for example input.";
  } else {
    BOOST_LOG_TRIVIAL(info) << "Read " << lines_pushed << " channels.";
  }
}

Talkgroup *Talkgroups::find_talkgroup(int sys_num, long tg_number) {
  Talkgroup *tg_match = NULL;

  for (std::vector<Talkgroup *>::iterator it = talkgroups.begin(); it != talkgroups.end(); ++it) {
    Talkgroup *tg = (Talkgroup *)*it;

    if ((tg->sys_num == sys_num) && (tg->number == tg_number)) {
      tg_match = tg;
      break;
    }
  }
  return tg_match;
}

Talkgroup *Talkgroups::find_talkgroup_by_freq(int sys_num, double freq) {
  Talkgroup *tg_match = NULL;

  for (std::vector<Talkgroup *>::iterator it = talkgroups.begin(); it != talkgroups.end(); ++it) {
    Talkgroup *tg = (Talkgroup *)*it;

    if ((tg->sys_num == sys_num) && (tg->freq == freq)) {
      tg_match = tg;
      break;
    }
  }
  return tg_match;
}

std::vector<Talkgroup *> Talkgroups::get_talkgroups() {
  return talkgroups;
}