Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 andreas 1
/*
101 andreas 2
 * Copyright (C) 2020 to 2022 by Andreas Theofilu <andreas@theosys.at>
11 andreas 3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "tamxcommands.h"
20
#include "terror.h"
21
#include "tresources.h"
14 andreas 22
#include "tconfig.h"
78 andreas 23
#include "texpat++.h"
11 andreas 24
 
83 andreas 25
#include <string>
26
#include <vector>
27
 
11 andreas 28
using std::string;
29
using std::vector;
78 andreas 30
using namespace Expat;
11 andreas 31
 
32
typedef struct CMD_DEFINATIONS
33
{
34
    string cmd;
35
    bool hasChannels{false};
36
    bool hasPars{false};
14 andreas 37
    char separator{0};
11 andreas 38
}CMD_DEFINATIONS;
39
 
40
CMD_DEFINATIONS cmdDefinations[] = {
14 andreas 41
    { "@WLD", false, true, ',' },
42
    { "@AFP", false, true, ',' },
43
    { "@GCE", false, true, ',' },
44
    { "@APG", false, true, ';' },
45
    { "@CPG", false, true, ',' },
46
    { "@DPG", false, true, ';' },
47
    { "@PDR", false, true, ';' },
48
    { "@PHE", false, true, ';' },
49
    { "@PHP", false, true, ';' },
50
    { "@PHT", false, true, ';' },
51
    { "@PPA", false, true, ',' },
104 andreas 52
    { "^PPA", false, true, ',' },
14 andreas 53
    { "@PPF", false, true, ';' },
104 andreas 54
    { "^PPF", false, true, ';' },
14 andreas 55
    { "@PPG", false, true, ';' },
104 andreas 56
    { "^PPG", false, true, ';' },
14 andreas 57
    { "@PPK", false, true, ',' },
104 andreas 58
    { "^PPK", false, true, ',' },
14 andreas 59
    { "@PPM", false, true, ';' },
104 andreas 60
    { "^PPM", false, true, ';' },
14 andreas 61
    { "@PPN", false, true, ';' },
104 andreas 62
    { "^PPN", false, true, ';' },
14 andreas 63
    { "@PPT", false, true, ';' },
104 andreas 64
    { "^PPT", false, true, ';' },
14 andreas 65
    { "@PPX", false, false, '\0' },
104 andreas 66
    { "^PPX", false, false, '\0' },
14 andreas 67
    { "@PSE", false, true, ';' },
68
    { "@PSP", false, true, ';' },
69
    { "@PST", false, true, ';' },
70
    { "PAGE", false, true, ',' },
104 andreas 71
    { "^PGE", false, true, ',' },
14 andreas 72
    { "PPOF", false, true, ';' },
73
    { "PPOG", false, true, ';' },
74
    { "PPON", false, true, ';' },
75
    { "^ANI", true, true, ',' },
76
    { "^APF", true, true, ',' },
77
    { "^BAT", true, true, ',' },
78
    { "^BAU", true, true, ',' },
79
    { "^BCB", true, true, ',' },
80
    { "?BCB", true, true, ',' },
81
    { "^BCF", true, true, ',' },
82
    { "?BCF", true, true, ',' },
83
    { "^BCT", true, true, ',' },
84
    { "?BCT", true, true, ',' },
85
    { "^BDO", true, true, ',' },
86
    { "^BFB", true, true, ',' },
87
    { "^BIM", true, true, ',' },
88
    { "^BLN", true, true, ',' },
89
    { "^BMC", true, true, ',' },
90
    { "^BMF", true, true, ',' },
91
    { "^BMI", true, true, ',' },
92
    { "^BML", true, true, ',' },
93
    { "^BMP", true, true, ',' },
94
    { "?BMP", true, true, ',' },
95
    { "^BNC", true, true, ',' },
96
    { "^BNN", true, true, ',' },
97
    { "^BNT", true, true, ',' },
98
    { "^BOP", true, true, ',' },
99
    { "?BOP", true, true, ',' },
100
    { "^BOR", true, true, ',' },
101
    { "^BOS", true, true, ',' },
102
    { "^BPP", true, true, ',' },
103
    { "^BRD", true, true, ',' },
104
    { "?BRD", true, true, ',' },
105
    { "^BSF", true, true, ',' },
106
    { "^BSP", true, true, ',' },
107
    { "^BSM", true, false, ',' },
108
    { "^BSO", true, true, ',' },
109
    { "^BVL", true, true, ',' },
110
    { "^BVN", false, true, ',' },
111
    { "^BVP", true, true, ',' },
112
    { "^BVT", true, true, ',' },
113
    { "^BWW", true, true, ',' },
114
    { "?BWW", true, true, ',' },
115
    { "^CPF", true, false, ',' },
116
    { "^DLD", false, false, ',' },
117
    { "^DPF", true, true, ',' },
118
    { "^ENA", true, true, ',' },
119
    { "^FON", true, true, ',' },
120
    { "?FON", true, true, ',' },
121
    { "^GDI", true, true, ',' },
122
    { "^GIV", true, true, ',' },
123
    { "^GLH", true, true, ',' },
124
    { "^GLL", true, true, ',' },
125
    { "^GRD", true, true, ',' },
126
    { "^GRU", true, true, ',' },
127
    { "^GSC", true, true, ',' },
128
    { "^GSN", true, true, ',' },
129
    { "^ICO", true, true, ',' },
130
    { "?ICO", true, true, ',' },
131
    { "^IRM", false, true, ',' },
132
    { "^JSB", true, true, ',' },
133
    { "?JSB", true, true, ',' },
134
    { "^JSI", true, true, ',' },
135
    { "?JSI", true, true, ',' },
136
    { "^JST", true, true, ',' },
137
    { "?JST", true, true, ',' },
138
    { "^MBT", false, false, ',' },
139
    { "^MDC", false, false, ',' },
140
    { "^SHO", true, true, ',' },
141
    { "^TEC", true, true, ',' },
142
    { "?TEC", true, true, ',' },
143
    { "^TEF", true, true, ',' },
144
    { "?TEF", true, true, ',' },
145
    { "^TOP", false, true, ',' },
146
    { "^TXT", true, true, ',' },
147
    { "?TXT", true, true, ',' },
148
    { "^UNI", true, true, ',' },
104 andreas 149
    { "^UTF", true, true, ',' },
14 andreas 150
    { "^LPC", false, true, ',' },
151
    { "^LPR", false, true, ',' },
152
    { "^LPS", false, true, ',' },
153
    { "ABEEP", false, false, ',' },
154
    { "ADBEEP", false, false, ',' },
155
    { "@AKB", false, true, ';' },
156
    { "AKEYB", false, true, ',' },
157
    { "AKEYP", false, true, ',' },
158
    { "AKEYR", false, true, ',' },
159
    { "@AKP", false, true, ';' },
160
    { "@AKR", false, false, ',' },
161
    { "BEEP", false, false, ',' },
104 andreas 162
    { "^ABP", false, false, ',' },
14 andreas 163
    { "BRIT", false, true, ',' },
164
    { "@BRT", false, true, ',' },
165
    { "DBEEP", false, false, ',' },
104 andreas 166
    { "^ADP", false, false, ',' },
14 andreas 167
    { "@EKP", false, true, ';' },
168
    { "PKEYP", false, true, ',' },
63 andreas 169
    { "@PKB", false, true, ';' },
104 andreas 170
    { "^PKB", false, true, ';' },
14 andreas 171
    { "@PKP", false, true, ';' },
104 andreas 172
    { "^PKP", false, true, ';' },
14 andreas 173
    { "SETUP", false, false, ',' },
104 andreas 174
    { "^STP", false, false, ',' },
14 andreas 175
    { "SHUTDOWN", false, false, ',' },
176
    { "SLEEP", false, false, ',' },
177
    { "@SOU", false, true, ',' },
104 andreas 178
    { "^SOU", false, true, ',' },
14 andreas 179
    { "@TKP", false, true, ';' },
104 andreas 180
    { "^TKP", false, true, ';' },
14 andreas 181
    { "TPAGEON", false, false, ',' },
182
    { "TPAGEOFF", false, false, ',' },
183
    { "@VKB", false, false, ',' },
104 andreas 184
    { "^VKB", false, false, ',' },
14 andreas 185
    { "WAKE", false, false, ',' },
186
    { "^CAL", false, false, ',' },
187
    { "^KPS", false, true, ',' },
188
    { "^VKS", false, true, ',' },
189
    { "@PWD", false, true, ',' },
190
    { "^PWD", false, true, ',' },
191
    { "^BBR", true, true, ',' },
192
    { "^RAF", false, true, ',' },
193
    { "^RFR", false, true, ',' },
194
    { "^RMF", false, true, ',' },
195
    { "^RSR", false, true, ',' },
196
    { "^MODEL?", false, false, ',' },
197
    { "^ICS", false, true, ',' },
198
    { "^ICE", false, false, ',' },
199
    { "^ICM", false, true, ',' },
200
    { "^PHN", false, true, ',' },
201
    { "?PHN", false, true, ',' },
202
    { "LEVON", false, false, ',' },
203
    { "RXON", false, false, ',' },
204
    { "ON", false, true, ',' },
205
    { "OFF", false, true, ',' },
15 andreas 206
    { "LEVEL", false, true, ',' },
207
    { "BLINK", false, true, ',' },
23 andreas 208
    { "#FTR", false, true, ':' },
14 andreas 209
    { "", false, false, '\0' }
11 andreas 210
};
211
 
212
CMD_DEFINATIONS& findCmdDefines(const string& cmd)
213
{
214
    DECL_TRACER("findCmdDefines(const string& cmd)");
215
 
216
    int i = 0;
101 andreas 217
    string uCmd = cmd;
218
    uCmd = toUpper(uCmd);
11 andreas 219
 
220
    while (cmdDefinations[i].cmd.length() > 0)
221
    {
101 andreas 222
        if (cmdDefinations[i].cmd.compare(uCmd) == 0)
11 andreas 223
            return cmdDefinations[i];
224
 
225
        i++;
226
    }
227
 
228
    return cmdDefinations[i];
229
}
230
 
231
TAmxCommands::TAmxCommands()
232
{
233
    DECL_TRACER("TAmxCommands::TAmxCommands()");
234
}
235
 
236
TAmxCommands::~TAmxCommands()
237
{
238
    DECL_TRACER("TAmxCommands::~TAmxCommands()");
239
}
240
 
14 andreas 241
bool TAmxCommands::readMap()
242
{
243
    DECL_TRACER("TAmxCommands::readMap()");
244
 
88 andreas 245
    string path = makeFileName(TConfig::getProjectPath(), "map.xma");
14 andreas 246
    vector<string> elements = { "cm", "am", "lm", "bm", "sm", "strm", "pm" };
247
 
88 andreas 248
    if (!isValidFile())
249
    {
250
        MSG_ERROR("File \"" << path << "\" is not a regular readable file!");
251
        return false;
252
    }
14 andreas 253
 
78 andreas 254
    TExpat xml(path);
255
    xml.setEncoding(ENC_CP1250);
14 andreas 256
 
78 andreas 257
    if (!xml.parse())
14 andreas 258
        return false;
259
 
78 andreas 260
    int depth = 0;
261
    size_t index = 0;
14 andreas 262
    vector<string>::iterator mapIter;
78 andreas 263
    size_t oldIndex = 0;
14 andreas 264
 
88 andreas 265
    if (elements.size() == 0)
266
        return false;
267
 
118 andreas 268
    for (mapIter = elements.begin(); mapIter != elements.end(); ++mapIter)
14 andreas 269
    {
78 andreas 270
        if ((index = xml.getElementIndex(*mapIter, &depth)) == TExpat::npos)
14 andreas 271
        {
23 andreas 272
            MSG_WARNING("Element \"" << *mapIter << "\" was not found!");
273
            continue;
14 andreas 274
        }
275
 
276
        MAP_T map;
277
        MAP_BM_T mapBm;
278
        MAP_PM_T mapPm;
78 andreas 279
        string name, content;
14 andreas 280
 
78 andreas 281
        while ((index = xml.getNextElementFromIndex(index, &name, nullptr, nullptr)) != TExpat::npos)
14 andreas 282
        {
78 andreas 283
            string el = name;
14 andreas 284
 
285
            if (el.compare("me") == 0)
286
            {
78 andreas 287
                while ((index = xml.getNextElementFromIndex(index, &name, &content, nullptr)) != TExpat::npos)
14 andreas 288
                {
78 andreas 289
                    string e = name;
14 andreas 290
 
291
                    if (mapIter->compare("cm") == 0 || mapIter->compare("am") == 0 ||
292
                        mapIter->compare("lm") == 0 || mapIter->compare("strm") == 0)
293
                    {
294
                        if (e.compare("p") == 0)
78 andreas 295
                            map.p = xml.convertElementToInt(content);
14 andreas 296
                        else if (e.compare("c") == 0)
78 andreas 297
                            map.c = xml.convertElementToInt(content);
14 andreas 298
                        else if (e.compare("ax") == 0)
78 andreas 299
                            map.ax = xml.convertElementToInt(content);
14 andreas 300
                        else if (e.compare("pg") == 0)
78 andreas 301
                            map.pg = xml.convertElementToInt(content);
14 andreas 302
                        else if (e.compare("bt") == 0)
78 andreas 303
                            map.bt = xml.convertElementToInt(content);
14 andreas 304
                        else if (e.compare("pn") == 0)
78 andreas 305
                            map.pn = content;
14 andreas 306
                        else if (e.compare("bn") == 0)
78 andreas 307
                            map.bn = content;
14 andreas 308
                    }
309
                    else if (mapIter->compare("bm") == 0)
310
                    {
78 andreas 311
                        while ((index = xml.getNextElementFromIndex(index, &name, &content, nullptr)) != TExpat::npos)
14 andreas 312
                        {
78 andreas 313
                            string im = name;
14 andreas 314
 
315
                            if (im.compare("i") == 0)
78 andreas 316
                                mapBm.i = content;
14 andreas 317
                            else if (im.compare("id") == 0)
78 andreas 318
                                mapBm.id = xml.convertElementToInt(content);
14 andreas 319
                            else if (im.compare("rt") == 0)
78 andreas 320
                                mapBm.rt = xml.convertElementToInt(content);
14 andreas 321
                            else if (im.compare("pg") == 0)
78 andreas 322
                                mapBm.pg = xml.convertElementToInt(content);
14 andreas 323
                            else if (im.compare("bt") == 0)
78 andreas 324
                                mapBm.bt = xml.convertElementToInt(content);
14 andreas 325
                            else if (im.compare("st") == 0)
78 andreas 326
                                mapBm.st = xml.convertElementToInt(content);
14 andreas 327
                            else if (im.compare("sl") == 0)
78 andreas 328
                                mapBm.sl = xml.convertElementToInt(content);
14 andreas 329
                            else if (im.compare("pn") == 0)
78 andreas 330
                                mapBm.pn = content;
14 andreas 331
                            else if (im.compare("bn") == 0)
78 andreas 332
                                mapBm.bn = content;
14 andreas 333
 
78 andreas 334
                            oldIndex = index;
14 andreas 335
                        }
336
 
337
                        mMap.map_bm.push_back(mapBm);
78 andreas 338
 
339
                        if (index == TExpat::npos)
340
                            index = oldIndex + 1;
14 andreas 341
                    }
342
                    else if (mapIter->compare("sm") == 0)
343
                    {
344
                        if (e.compare("i") == 0)
78 andreas 345
                            mMap.map_sm.push_back(content);
14 andreas 346
                    }
347
                    else if (mapIter->compare("pm") == 0)
348
                    {
349
                        if (e.compare("a") == 0)
78 andreas 350
                            mapPm.a = xml.convertElementToInt(content);
14 andreas 351
                        else if (e.compare("t") == 0)
78 andreas 352
                            mapPm.t = content;
14 andreas 353
                        else if (e.compare("pg") == 0)
78 andreas 354
                            mapPm.pg = xml.convertElementToInt(content);
14 andreas 355
                        else if (e.compare("bt") == 0)
78 andreas 356
                            mapPm.bt = xml.convertElementToInt(content);
14 andreas 357
                        else if (e.compare("pn") == 0)
78 andreas 358
                            mapPm.pn = content;
14 andreas 359
                        else if (e.compare("bn") == 0)
78 andreas 360
                            mapPm.bn = content;
14 andreas 361
                    }
362
 
78 andreas 363
                    oldIndex = index;
14 andreas 364
                }
365
 
366
                if (mapIter->compare("cm") == 0)
367
                    mMap.map_cm.push_back(map);
368
                else if (mapIter->compare("am") == 0)
369
                    mMap.map_am.push_back(map);
370
                else if (mapIter->compare("lm") == 0)
371
                    mMap.map_lm.push_back(map);
372
                else if (mapIter->compare("strm") == 0)
373
                    mMap.map_strm.push_back(map);
374
                else if (mapIter->compare("pm") == 0)
375
                    mMap.map_pm.push_back(mapPm);
78 andreas 376
 
377
                if (index == TExpat::npos)
378
                    index = oldIndex + 1;
14 andreas 379
            }
380
 
78 andreas 381
            oldIndex = index;
14 andreas 382
        }
383
    }
384
 
385
    return true;
386
}
387
 
388
vector<string> TAmxCommands::getFields(string& msg, char sep)
389
{
390
    DECL_TRACER("TAmxCommands::getFields(string& msg, char sep)");
391
 
392
    vector<string> flds;
393
    bool bStr = false;
394
    string part;
395
 
16 andreas 396
    for (size_t i = 0; i < msg.length(); i++)
14 andreas 397
    {
16 andreas 398
        if (msg.at(i) == sep && !bStr)
14 andreas 399
        {
400
            flds.push_back(part);
401
            part.clear();
402
            continue;
403
        }
16 andreas 404
        else if (msg.at(i) == '\'' && !bStr)
14 andreas 405
            bStr = true;
16 andreas 406
        else if (msg.at(i) == '\'' && bStr)
14 andreas 407
            bStr = false;
408
        else
16 andreas 409
            part.append(msg.substr(i, 1));
14 andreas 410
    }
411
 
412
    if (!part.empty())
413
        flds.push_back(part);
414
 
83 andreas 415
    if (flds.size() > 0 && TStreamError::checkFilter(HLOG_DEBUG))
14 andreas 416
    {
417
        MSG_DEBUG("Found fields:");
418
        vector<string>::iterator iter;
419
        int i = 1;
420
 
118 andreas 421
        for (iter = flds.begin(); iter != flds.end(); ++iter)
14 andreas 422
        {
423
            MSG_DEBUG("    " << i << ": " << *iter);
424
            i++;
425
        }
426
    }
427
 
428
    return flds;
429
}
430
 
19 andreas 431
vector<MAP_T> TAmxCommands::findButtons(int port, vector<int>& channels, MAP_TYPE mt)
14 andreas 432
{
19 andreas 433
    DECL_TRACER("TAmxCommands::findButtons(int port, vector<int>& channels, MAP_TYPE mt)");
14 andreas 434
 
435
    vector<MAP_T> map;
436
    vector<int>::iterator iter;
437
 
19 andreas 438
    if (channels.empty())
439
    {
440
        MSG_WARNING("Got empty channel list!");
441
        return map;
442
    }
443
 
444
    vector<MAP_T> localMap;
445
 
446
    switch (mt)
447
    {
448
        case TYPE_AM:   localMap = mMap.map_am; break;
449
        case TYPE_CM:   localMap = mMap.map_cm; break;
450
        case TYPE_LM:   localMap = mMap.map_lm; break;
451
    }
452
 
453
    if (localMap.empty())
454
    {
455
        MSG_WARNING("The internal list of elements is empty!")
456
        return map;
457
    }
458
 
118 andreas 459
    for (iter = channels.begin(); iter != channels.end(); ++iter)
14 andreas 460
    {
461
        vector<MAP_T>::iterator mapIter;
462
 
118 andreas 463
        for (mapIter = localMap.begin(); mapIter != localMap.end(); ++mapIter)
14 andreas 464
        {
465
            if (mapIter->p == port && mapIter->c == *iter)
466
                map.push_back(*mapIter);
467
        }
468
    }
469
 
470
    MSG_DEBUG("Found " << map.size() << " buttons.");
471
    return map;
472
}
473
 
97 andreas 474
vector<MAP_T> TAmxCommands::findButtonByName(const string& name)
475
{
476
    DECL_TRACER("TAmxCommands::findButtonByName(const string& name)");
477
 
478
    vector<MAP_T> map;
479
 
480
    if (mMap.map_cm.empty())
481
    {
482
        MSG_WARNING("The internal list of elements is empty!")
483
        return map;
484
    }
485
 
486
    vector<MAP_T>::iterator mapIter;
487
 
118 andreas 488
    for (mapIter = mMap.map_cm.begin(); mapIter != mMap.map_cm.end(); ++mapIter)
97 andreas 489
    {
490
        if (mapIter->bn == name)
491
            map.push_back(*mapIter);
492
    }
493
 
494
    MSG_DEBUG("Found " << map.size() << " buttons.");
495
    return map;
496
}
497
 
15 andreas 498
vector<MAP_T> TAmxCommands::findBargraphs(int port, vector<int>& channels)
499
{
500
    DECL_TRACER("TAmxCommands::findBargraphs(int port, vector<int>& channels)");
501
 
502
    vector<MAP_T> map;
503
    vector<int>::iterator iter;
504
 
83 andreas 505
    if (channels.size() == 0)
506
        return map;
507
 
118 andreas 508
    for (iter = channels.begin(); iter != channels.end(); ++iter)
15 andreas 509
    {
510
        vector<MAP_T>::iterator mapIter;
511
 
83 andreas 512
        if (mMap.map_lm.size() > 0)
15 andreas 513
        {
118 andreas 514
            for (mapIter = mMap.map_lm.begin(); mapIter != mMap.map_lm.end(); ++mapIter)
83 andreas 515
            {
516
                if (mapIter->p == port && mapIter->c == *iter)
517
                    map.push_back(*mapIter);
518
            }
15 andreas 519
        }
520
    }
521
 
522
    MSG_DEBUG("Found " << map.size() << " buttons.");
523
    return map;
524
}
525
 
107 andreas 526
vector<string> TAmxCommands::findSounds()
527
{
528
    DECL_TRACER("TAmxCommands::findSounds()");
529
 
530
    return mMap.map_sm;
531
}
532
 
118 andreas 533
bool TAmxCommands::soundExist(const string& sname)
107 andreas 534
{
535
    DECL_TRACER("TAmxCommands::soundExist(const string sname)");
536
 
537
    if (mMap.map_sm.size() == 0)
538
        return false;
539
 
540
    vector<string>::iterator iter;
541
 
542
    for (iter = mMap.map_sm.begin(); iter != mMap.map_sm.end(); ++iter)
543
    {
544
        if (iter->compare(sname) == 0)
545
            return true;
546
    }
547
 
548
    return false;
549
}
550
 
11 andreas 551
bool TAmxCommands::parseCommand(int device, int port, const string& cmd)
552
{
553
    DECL_TRACER("TAmxCommands::parseCommand(int device, int port, const string& cmd)");
65 andreas 554
/*
60 andreas 555
    if (device != TConfig::getChannel())    // This happens on a filetransfer only!
14 andreas 556
    {
557
        MSG_WARNING("Command is for device " << device << ", but this device is " << TConfig::getChannel());
558
    }
65 andreas 559
*/
11 andreas 560
    vector<CMD_TABLE>::iterator iter;
16 andreas 561
    size_t pos = cmd.find_first_of("-");
11 andreas 562
 
104 andreas 563
    MSG_TRACE("Parsing for device <" << device << ":" << port << ":" << TConfig::getSystem() << "> the command: " << getCommand(cmd));
13 andreas 564
 
11 andreas 565
    if (pos != string::npos)    // Command with parameters
566
    {
567
        string bef = cmd.substr(0, pos);
568
        string rest = cmd.substr(pos + 1);
569
 
118 andreas 570
        for (iter = mCmdTable.begin(); iter != mCmdTable.end(); ++iter)
11 andreas 571
        {
13 andreas 572
            iter->channels.clear();
573
            iter->pars.clear();
574
 
11 andreas 575
            if (iter->cmd.compare(bef) == 0 && iter->command)
576
            {
577
                CMD_DEFINATIONS cdef = findCmdDefines(bef);
578
 
579
                if (cdef.cmd.empty())
580
                {
581
                    MSG_WARNING("Command \"" << bef << "\" not found in command table! Ignoring it.");
582
                    continue;
583
                }
584
 
585
                if (cdef.hasChannels || cdef.hasPars)
586
                {
14 andreas 587
                    vector<string> parts = getFields(rest, cdef.separator);
11 andreas 588
 
589
                    if (cdef.hasChannels)
13 andreas 590
                        extractChannels(parts[0], &iter->channels);
11 andreas 591
 
592
                    if (cdef.hasPars)
593
                    {
13 andreas 594
                        MSG_DEBUG("Command may have parameters. Found " << parts.size() << " parameters.");
11 andreas 595
 
13 andreas 596
                        if (parts.size() > 0)
11 andreas 597
                        {
13 andreas 598
                            vector<string>::iterator piter;
599
                            int cnt = 0;
600
 
118 andreas 601
                            for (piter = parts.begin(); piter != parts.end(); ++piter)
11 andreas 602
                            {
13 andreas 603
                                if (cdef.hasChannels && !cnt)
604
                                {
605
                                    cnt++;
606
                                    continue;
607
                                }
608
 
609
                                iter->pars.push_back(*piter);
11 andreas 610
                                cnt++;
611
                            }
612
                        }
13 andreas 613
                        else
614
                            iter->pars.push_back(rest);
11 andreas 615
                    }
616
                }
617
 
618
                iter->command(port, iter->channels, iter->pars);
619
                return true;
620
            }
621
        }
622
    }
623
    else        // Command without parameter
624
    {
118 andreas 625
        for (iter = mCmdTable.begin(); iter != mCmdTable.end(); ++iter)
11 andreas 626
        {
627
            if (iter->cmd.compare(cmd) == 0 && iter->command)
628
            {
629
                iter->command(port, iter->channels, iter->pars);
630
                return true;
631
            }
632
        }
633
    }
634
 
635
    MSG_WARNING("Command \"" << cmd << "\" currently not supported!");
636
    return false;
637
}
638
 
639
bool TAmxCommands::extractChannels(const string& schan, vector<int>* ch)
640
{
641
    DECL_TRACER("TAmxCommands::extractChannels(const string& schan, vector<int>* ch)");
642
 
643
    if (!ch || schan.empty())
644
        return false;
645
 
16 andreas 646
    if (schan.find("&") == string::npos && schan.find(".") == string::npos)
11 andreas 647
    {
648
        int c = atoi(schan.c_str());
649
        ch->push_back(c);
650
        return true;
651
    }
652
 
653
    if (schan.find("&") != string::npos)
654
    {
655
        vector<string> parts = StrSplit(schan, "&");
656
        vector<string>::iterator iter;
657
 
83 andreas 658
        if (parts.size() > 0)
11 andreas 659
        {
118 andreas 660
            for (iter = parts.begin(); iter != parts.end(); ++iter)
11 andreas 661
            {
83 andreas 662
                if (iter->find(".") != string::npos)
663
                {
664
                    vector<string> p2 = StrSplit(*iter, ".");
11 andreas 665
 
83 andreas 666
                    if (p2.size() >= 2)
667
                    {
668
                        for (int i = atoi(p2[0].c_str()); i <= atoi(p2[1].c_str()); i++)
669
                            ch->push_back(i);
670
                    }
671
                    else if (p2.size() > 0)
672
                        ch->push_back(atoi(p2[0].c_str()));
38 andreas 673
                }
83 andreas 674
                else
675
                    ch->push_back(atoi(iter->c_str()));
11 andreas 676
            }
677
        }
678
    }
679
    else
680
    {
681
        vector<string> parts = StrSplit(schan, ".");
682
 
38 andreas 683
        if (parts.size() >= 2)
684
        {
60 andreas 685
            for (int i = atoi(parts[0].c_str()); i <= atoi(parts[1].c_str()); i++)
38 andreas 686
                ch->push_back(i);
687
        }
688
        else if (parts.size() > 0)
689
            ch->push_back(atoi(parts[0].c_str()));
11 andreas 690
    }
691
 
692
    return true;
693
}
694
 
695
void TAmxCommands::registerCommand(std::function<void (int port, vector<int>& channels, vector<string>& pars)> command, const string& name)
696
{
697
    DECL_TRACER("TAmxCommands::registerCommand(std::function<void (vector<int>& channels, vector<string>& pars)> command, const string& name)");
698
 
699
    vector<CMD_TABLE>::iterator iter;
700
 
118 andreas 701
    for (iter = mCmdTable.begin(); iter != mCmdTable.end(); ++iter)
11 andreas 702
    {
703
        if (iter->cmd.compare(name) == 0)
704
        {
705
            iter->command = command;
706
            iter->channels.clear();
707
            iter->pars.clear();
708
            return;
709
        }
710
    }
711
 
712
    CMD_TABLE ctbl;
713
    ctbl.cmd = name;
714
    ctbl.command = command;
715
    mCmdTable.push_back(ctbl);
716
}