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