summaryrefslogtreecommitdiffstats
path: root/src/knutnet.h
blob: 393adacc79e349841aec1dd8565f1a2f270e0936 (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/***************************************************************************
                          knutnet.h  -  description
                             -------------------
    begin                : Ne led 12 2003
    copyright            : (C) 2003 by Daniel Prynych
    email                : Daniel@prynych.cz
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef KNUTNET_H
#define KNUTNET_H

//Od verze 3 je kapp jen odkaz na kapplication
//Since version 3 is kapp pointer on kapplication only
//#include <tdeapplication.h>
#include <kapp.h>

#include <tqobject.h>
#include <tqsocket.h>
#include <tqsocketdevice.h>
#include <tqnetworkprotocol.h>
#include <tqmutex.h>

#include <tqvaluevector.h>


  struct upsVarDef {
    TQString upsVarName;
    bool upsVarActivate;
    bool upsVarType;       // typ true(1) RO -  false (0) RW (muzeme menit)
    bool upsValueType;     // typ true(1) char  - false (0) enum (vyctova)
                           // ma vyznam jen pro RW promenne
    int  upsVarMax;        // udave max delku string promenne nebo pocet moznosti pro enum promennou
                           // ma vyznam jen pro RW promenne
    TQString upsValue;     // hodnota promene
    TQString upsDescription;  // informace o promene
    TQValueVector<TQString>* upsEnumValues;
    };


   struct upsICommDef {
    TQString upsCommName;
    TQString upsDescription;
    };

class TQString;
class TQCString;
class TQSocket;
class TQTimer;


/**
  *@author Daniel Prynych
  */

/**
 * This class reads data from serveru NUT.
 * Since version 0.5 is written and API is changet.
 * Since 0.6.1 supports protocol client-server and variables for upsd >= nut 1.3
 * Since 0.6.1 doesn't ups name like ups_name@host_address
 * Since 0.7 doesn't support UDP
 * Tato trida zajistuje cteni dat ze serveru NUT.
 * Od verze 0.5 je uplne prepsana a je zmeneno i API.
 * Od verze 0.6.1 proporuje take client-server protokol a promenne pro upsd >= nut 1.3 
 * Od verze 0.6.1 nepodporuje jmeno ups ve tvaru jmeno_ups@adresa_pc
 * Od verze 0.7 neni podporovano UDP
 *
 * @author Daniel Prynych
 * @short Communication with UPS - Komunikace s UPS
 * @version 0.7
*/
class KNutNet : public TQObject  {
   TQ_OBJECT
  
public: 

  enum NetState {Idle, HostLookup, Connecting, Connected, ConnectError,Closing };
  // idle is same like NotConnected
  // neni pripojeno chyba pri pripojeni,pripojeno, pripojuji


  enum typeOfVar {AllVars, ROVars, RWVars };

  enum NetError {
    NoError,
    LowMem,        // no memmory - nebyla pridelena pamet
    NullAddress,   // no address of host nebyla zadana adresa pocitace
    NoSuchHost,    // this host noexist  neexistuje takovy pocitac
    CantConnect,   // I cannot connect to server Nemohu se spojit se serverem
    SocketReadErr,     // I cannot read data from socket - Nelze cist data ze soketu
    ConnectErr,    // connect isn't maked - Nejde provest connect
    NutProtocolVarErr, //  Nelze zjistit verze protokolu
    NoUpsHere,  // ni UPS on this address na teto adrese neni zadna UPS
    ConnectionRefused, // server refused connection
    ConnetionClosedByServer, // connection was closed by server upsd // by the second side

    SendErr,       // doesn't send date - Nelze odeslat data
    RecvErr,       // doesn't receive data - Nelze prijmout data 
    NoData,        // no data was received, empty string was received - Nebyla prijmuta zadna data, byl prijmut prazdny retezec
    UnknownAnswer, // Server answer no right - Server neodpovedel spravne
    UnknownFormat, // Data hasn't got right structure -  Data nemaji spravnou strukturu
    UnknownFormatVer, //Data returned after command VER don't have right structure -  Data vracena po prikazu VER nemaji spravnou strukturu
    NoServerData,  // doesn't read data from server (Server isn't answering)  Nelze precist data ze serveru (Sever neodpovida)
    NoUpsnetOk,    // UpsNetOK is setting on faLse - je nastaven UpsNetOk na false
    UnknownErr,    // unknown error unknown text after ERR -  neznama chyba - neznamy text za ERR
    CommNotFind,   // command hasn't been found  in list of module's commands - prikaz nebyl nalezen v seznamu prikazu modulu
    VarNotFind,    // variable hasn't been found in list of module's variables promena nebyla nalezena v seznamu promenych modulu
    EnumValueNotFind, //value of enumereted variable hasn't been found hodnota vyctove promene nebyla nalezena

// tyto zpravy vraci server
// this mesages is returned server upsd
    AccessDenied,        // access denied - pristup odmitnut
    PasswordRequired,    // password is required -  pro akci je nutno zaslat heslo
    PasswordIncorrect,   // password is incorrect nespravane heslo
    UnknownUps,          // this ups isn't occured on address - tato ups neni na zadane adrese
    AlreadyLoggedIn,     // prihlaseni LOGIN jiz bylo provedeno
    AlreadySetPassword,  // password has been already entered  - heslo jiz bylo zadano
    AlreadySetUsername,  // username has been already entered  - uzivatelske jmeno jiz bylo zadano
    DataStale,           // data from module isn't got - neni mozne ziskat z modulu nove data 
    MissingArgument,     // misses argument - chyby argument
    UnknownType,         // unknown type of variable -neznamy typ promene
    UnknownCommand,      // umknown command example: sends maybe "UPERNAME admin"  - nezmamy  prikaz pr: poslem treba "UPERNAME admin"

    //tyto chyby se vztahuji k driveru pro danny typ UPS-ky
    // this mistake is refered for   selected driver of UPS
    DriverNotConnected,  // driver isn't connected -  driver neni pripojen
    NoResponse,          // driver pro ups neodpovedel ve vymezenem case
    UnknownReply,        // unknown reply - neznama odpoved
    NotImplemented,      //
    CommandFailed,       // failed command - chybny prikaz
    VarUnknown,          // values isn't selected by driver - promena neni driverem podporovana
    VarNotSupported,     // cannot get data for this values - neni mozno zislat data pro tuto promenou
    InvalidValue,        // unknown value - nespravna hodnota
    UnknownInstCmd,      // unknown instant command / neznamy okamzity prikaz
    CmdNotSupported,     // this instant command isn't supported - tento okamzity prikaz neni podporovan
    NotConnection,      // operace nelze provest protoze neni provedeno pripojeni
    // this not error 
    NoListVars};        //upsd doesn't return list of variables, variable for STATUS has got value WAIT

  enum typeOfStatus {
    NS = 0,     // - NONE status of UPS wasn't read
    OFF = 1,    // - UPS is off (not supplying power)
    OL = 2,     // - UPS is online (supplying power from the line/mains)
    OB = 4,     // - UPS is on battery
    LB = 8,     // - UPS battery is low (with OB = shutdown situation)
    CAL = 16,   // - UPS is performing calibration
    TRIM = 32,  // - UPS is trimming incoming voltage (APC "SmartTrim")
    BOOST = 64, // - UPS is boosting incoming voltage (APC "SmartBoost")
    OVER = 128, // - UPS is overloaded
    RB = 256,   // - UPS battery needs to be replaced
    WAIT = 512   // - UPS as for waiting
    };

  enum keyWord {
    NONE,
    OK,
    ERR,
    BEGIN,
    END,
    VAR,
    TYPE,
    DESC,
    UPSDESC,
    CMDDESC,
    UPS,
    LIST,
    RW,
    CMD,
    ENUM,
    STARTTLS,
    BEGIN_LIST_VAR,
    BEGIN_LIST_RW,
    BEGIN_LIST_CMD,
    BEGIN_LIST_UPS,
    BEGIN_LIST_ENUM,
    END_LIST_VAR,
    END_LIST_RW,
    END_LIST_CMD,
    END_LIST_UPS,
    END_LIST_ENUM
    };

  enum flagsVar {
    RW_FLAG = 1,
    ENUM_FLAG = 2,
    STRING_FLAG = 4
    };



/**
 * Constructor sets basic variables and structures only, but doesn't read variables from server NUT
 * Konstruktor pouze nastavi zakladni promene a struktury, ale necte promene se serveru NUT.
 * @param upsName in name of UPS 
 * @param upsAddress Is address of UPS (adresa pocitace na ktery je UPS pripojena)
 * netAddress can is entered like ip address or domain name
 * example: localhost karel.domain,cz 194.212.25.55.
 * @param tPort Is name if port, for NUT server (NUT is received on this port), default is 3493.
 * @param delay is delay in msec. System waits  before one tries to make connection again. When test of connection fails,
 * @param countRepeat is maximal number of repeating for make connection. 
 *
 * Since 0.7.5 version protocol UDP is not supported
 * @param upsAddress Je adresa ups-ky (adresa pocitace na ktery je UPS pripojena)
 * netAddress muze byt zadan jako ip adresa nebo domenove jmeno pocitace
 * @param upsName je jmeno UPSky 
 * pr.: localhost  karel.domena.cz 194.212.25.55.
 * @param tPort je cislo portu na kterem nasloucha NUT server, default je 3493.
 * @param delay je zpozdeni v msec, System ceka nez znovu pokusi ukutecnit spojeni. Kdyz pokus o spojeni selhal.
 * @param countRepeat je maximalni pocet opakovani spojeni pro navazani spojeni. 
 * Od verze 0.7 neni podporovano UDP
 *
 * @since  0.7.5
 **/
  KNutNet (const TQString upsAddress, const TQString upsName="", const unsigned short port=3493, unsigned int countRepeat =0, const unsigned int delay = 200);

/**
 * @since  0.5
 **/
  ~KNutNet();



/** 
 * Zavre spojeni a nastavi hodnoty pro novou UPSku
 * Closes conneting and sets values for new UPS
 *
 * @since  0.1
 **/
 void newUPS (const TQString upsAddress, const TQString upsName, const unsigned short port=3493);


/**
 * Provadi spojeni
 * Makes conneting
 *
 * @since  0.1
 **/
  void open (void);

/** 
 * Zavre spojeni
 * Closes conneting
 *
 * @since  0.1
 **/
 void close (void);


/**
 * Returns kod of error, which is setted after last colling of same function this calls.
 * Error NotConnected isn't returned (not for function getError, getState).
 * It means then error NotConnected isn't remembered.
 * Vraci kod chyby, ktera nastala pri poslednim volani kterekoliv funkce
 * teto tridy (s vyjimkou getError, getState ),
 * chyba NotConnected neni vracena;
 * To znamena ze chyba NotConnected neni zapamatovana.
 * 
 *
 * @since  0.5
 **/
  int getError ( void );


/**
 * Vraci stav pripojeni.
 * Returns state of connection.
 *
 * @since  0.5
 **/
  int getState ( void );


/**
 * Returns state of description, if descriptions is loaded.
 * Vraci stav popisu, zda je nahran;
 *
 * @since  0.5
 **/
  bool isDescription ( void );


/**
 * Vraci zda doslo k prepnuti z UDP do TCP;
 * Ma vyznam je pro verzi protokolu 2
 *
 * @since  0.1
 **/
bool switchedToTCP ( void );


/**
 * Vraci cislo protokolu pro komunikaci s NUT
 * Returns nunber of protocol for comunications with NUT
 * Od verze NUTu 1.3 je zmenen protokol
 * Since version 1.3 protocol is changed
 *
 * @since  0.1
 **/
  int getNutProtocol ( void );

/**
 * Return number of NUT's version
 * Vraci cislo verze nazvu NUT-u
 * Since version if nut 1.3 are changed names of values
 * Od verze NUTu 1.3 jsou zmeneny nazvy promenych
 *
 * @since  0.1
 **/
  int getNutVariables ( void );



/**
 * Nacte vsechny promene UPS-ky a nastavi jejich typ (znakove/char, vyctove/enum a status)
 * Nastavi priznak aktivity jen pro status.
 * Funkce vraci kod chyby nebo 0 pokud k chybe nedojde;
 * Vzdy jsou nacteny i hodnoty promenych.
 *
 * @since  0.6.1
 **/
  int getUpsVars ( void);  


/**
 * Nacte hodnoty vsech promenych UPS-ky pokud je nastaven jejich priznak aktivity,
 * nebo je nastaven allVars na true.
 * Pozor po provedeni getUpsVars je priznak aktivity nastaven jen pro status.
 *
 * @param allVars Urcuje zda se prikaz tyka vsech promenych, nebo jen tech ktere
 * maji nastaven priznak aktivity na TRUE.
 *
 * @since  0.6.1
 **/
  int getUpsValues (const bool allVars=true );


/**
 * Nacte popisy pro kazdou promennou
 * Read descriptions for every variable
 *
 * @param always Urcuje zda se nacteny provede vzdy nebo jen kdy popis jeste
 * nebyl nacten.
 *
 * @since  0.6
 **/
  int getDescription (bool always = false);


/**
 * Provede okamzity prikaz.
 * Vraci 0 pokud provedeni skoncilo v poradku jinak kod chyby.
 *
 * @param command Udava nazev prikazu.
 * @param userName Udava uzivatelske jmeno.
 * @param password Udava heslo.
 * @param onlyCommand udava zda ma byt poslan pouze prikaz, nebo i nastaveno uzivatelske jmeno a heslo.
 *        POZOR!! uzivatelske jmeno nejde zmenit bez odpojeni a znovu pripojeni.
 *
 * @since  0.7
 **/
  int instantCommand (const TQString command, const TQString userName, const TQString password, const bool onlyCommand = false);


/**
 * Nastavi RW promenou.
 * Vraci 0 pokud provedeni skoncilo v poradku jinak kod chyby;
 *
 * @param variable Udava nazev promenne.
 * @param value Udava hodnotu.
 * @param userName Udava uzivatelske jmeno.
 * @param password Udava heslo.
 * @param onlyVariable udava zda ma byt poslana pouze zadost na zmenu promenne, nebo i nastaveno uzivatelske jmeno a heslo.
 *        POZOR!! uzivatelske jmeno nejde zmenit bez odpojeni a znovu pripojeni.
 *
 * @since  0.7
 **/
  int setVariable (const TQString variable, const TQString value, const TQString userName, const TQString password, const bool onlyVariable = false);



/**
 * Returns number of UPS's variables. On error -1 is returned.
 * Vrati pocet promenych UPS-ky. Pri chybe vraci -1.
 *
 * @param typVar Is type of variable, its number is found out.
 * @param typVar Je typ promene pro ktere zjistujeme pocet
 * muze se jednat o RO promenne RW promene nebo o vsechny promenne.
 *
 * @since  0.6
 **/
  int readNumberVars (typeOfVar typVar = AllVars);


/**
 * Returns number of UPS's commands. On erroe -1 is returned.
 * Vrati pocet prikazu UPS-ky. Pri chybe vraci -1.
 *
 * @since  0.6
 **/
  int readNumberComms (void);



/**
 * Nastavi strukturu udavajici vsechny hodnoty pro prikaz.
 * Pri chybe vrati kod chyby
 *
 * @param seqNumber Udava cislo promene.
 * @param upsIComm Je struktura upsICommDef.
 *
 * @since  0.6
 **/
  int readIComm (const int seqNumber, upsICommDef& upsIComm);


/**
 * Nastavi strukturu obsahujici vsechny udaje promene.
 * Pri chybe vrati kod chyby.
 *
 * @param name Udava jmeno promene.
 *
 * @since  0.6
 **/
  int readVars (const TQString name, struct upsVarDef& allVar);


/**
 * Nastavi strukturu obsahujici vsechny udaje promene.
 * Pri chybe vrati kod chyby.
 *
 * @param seqNumber Udava poradove cislo promene, ta zacina VZDY cislem 1.
 * @param typVar Udava typ promene /RO/RW/ALL.
 *
 * @since  0.6
 **/
  int readVars (const int seqNumber, struct upsVarDef& allVar, const typeOfVar typVar = AllVars);


/**
 * Vrati hodnotu promenne.
 * Pri chybe vrati 0l.
 *
 * @param name Udava jmeno promene.
 *
 * @since  0.6.1
 **/
  TQString readStringVar (const TQString name);

/**
 * Nacte jednu z hodnot vyctove promenne.
 * Vraci jednu z hodnot vyctove promenne udanou poradovym cislem hodnoty.
 * Pri chybe vrati 0l.
 *
 * @param name Udava nazev vyctove promenne.
 * @param valueNumber Udava poradove cislo hodnoty vyctove promenne.
 * @param enumValue Je naplnena hodnotou vyctove promenne.
 *
 * @since  0.6.1
 **/
  TQString readEnumValueVar (const TQString name, const int valueNumber);


/**
 * Vraci kod stavu (statusu) UPS-ky
 * kody je mozne scitat napt OB+LB = 12
 * OFF = 1, UPS-ka je vypnuta
 * OL = 2, UPS-ka bezi na sit
 * OB = 4 UPS-ka bezi na baterie
 * LB = 8 baterie je vybyta (pokud je zaroven OB dojde k shutdownu)
 * CAL = je spustena kalibrace UPS-ky
 * OVER =128 UPS-ka je pretizena
 * RB = 256 UPS-ka pozaduje vymenu baterie
 *
 * @since  0.5
 **/
int readStatus(void);


/**
 * Nastavi priznak aktivity.
 * Pri chybe vrati kod chyby.
 *
 * @param name Udava jmeno promene.
 *
 * @since  0.6
 **/
  int setActivate ( const TQString name );

/**
 * Zrusi nastaveni priznaku aktivity.
 * Pri chybe vrati kod chyby.
 *
 * @param name Udava jmeno promene.
 *
 * @since  0.6
 **/
  int unSetActivate ( const TQString name );

/**
 * Zrusi nastaveni priznaku aktivity pro VSECHNY promene.
 * Pri chybe vrati kod chyby.
 *
 * @since  0.6
 **/
  int unSetActivateAll ( void );



/**
 * Testuje zda existuje promena danneho jmena.
 * POZOR ZMENA v NAVRATOVYCH HODNOTACH
 * Vraci 0  pokud existuje.
 * Pokud neexistuje nebo pri chybe vrati kod chyby.
 *
 * @param name Udava jmeno promene.
 *
 * @since  0.6
 **/
  int existName ( const TQString name );

/*********   Consts ****************/


static const int initState = OL;

signals:
/**
 * Signal je vyslan, kdyz dojde k chube.
 * Signal is emited, when error is occurred
 *
 * param@ error je cislo chyby.
 * param@ error je value of error.
 **/
void connectionError(int error);


/**
 * Signal je vyslan, kdyz druha starna uzavrela spojeni.
 * Signal is emited, when other side closed connection.
 *
 * @since  0.1
 **/
void connectionClosed (void);

/**
 * Signal je vyslan, kdyz dojde k prvemu spojeni se serverem.
 * Signal is emited, when the first connection with server is realized
 *
 * @since  0.1
 **/
void firstConnection(void);                                                                                                                                                                                                                                                                                                               /**
 * Signal je vyslan, kdyz dojde k dalsimu spojeni se serverem, znovu pripojeni. 
 * Signal is emited, when next connection with server is realized, reconnect.
 *
 * @since  0.1
 **/
void nextConnection(void);
 /**
 * Signal je vyslan, kdyz prevod jmena byl uspesny.
 * Signal is emited, when host lookup has succeeded
 *
 * @since  0.1
 **/
void hostFound(void);


 /**
 * Signal je vyslan, kdyz spojeni je opet zkouseno. 
 * Signal is emited, when connection is tried again.
 *
 * @since  0.1
 **/
void tryRepeatFirstConnection( int );


 /**
 * Signal je vyslan, kdyz spojeni je zkouseno poprve. 
 * Signal is emited, when connection is tried at first.
 *
 * @since  0.1
 **/
void tryFirstConnection( int);


//  signals:

  void getVarDesc (int count, int item);

/*********   Private part    **********/

  private:

// internal funkcion


/**
 * Cleans input buffer
 * Vycisti vstupni buffer
 *
 * @since  0.1
 **/
void cleanConnecting (void);


/**
 * Prevede textovy nazev klice na jeho numerickou hodnotu
 * Jen pro protokol verze 2.
 * Funkce vraci kod klice;
 * param@ line textovy nazev klice.
 *
 * @since  0.1
 **/
int setKey (const TQString line);

TQString parseValue (const TQString line);

/**
 * Parsuje typ promenne (RW ENUM STRING).
 * Jen pro protokol verze 2.
 * Funkce vraci soucet kodu typu promenych;
 * param@ line retezec ktery se parsuje.
 * param@ lenString vraci maximalni delka promenne typu STRING.
 *
 * @since  0.1.1
 **/
int parseTypeValue (TQString line, int& lenString );



/**
 * Parsuje navratovou hodnotu z upsd (protokol klient-server).
 * Jen pro protokol verze 2.
 * Funkce vraci kod prikazu (UPS, VAR BEGIN_LIST_VAR );
 *
 * @since  0.1
 **/
int parseLine(TQString& line, TQString& upsName ,TQString& varName, TQString& value, int& typeValue, int& lenString);



/**
 * Nacte popisy pro kazdou promennou
 * Jen pro protokol verze 1.
 *
 * @param always Urcuje zda se nacteny provede vzdy nebo jen kdy popis jeste
 * nebyl nacten.
 *
 * @since  0.1
 **/
  int getDescription1 (bool always = false);

/**
 * Nacte popisy pro kazdou promennou
 * Jen pro protokol verze 2.
 *
 * @param always Urcuje zda se nacteny provede vzdy nebo jen kdy popis jeste
 * nebyl nacten.
 *
 * @since  0.1
 **/
  int getDescription2 (bool always = false);



/**
 * internal
 * Nacte nazev prvni UPS-ky na danne adrese.
 * Jen pro protokol verze 2
 * Funkce vraci kod chyby nebo 0 pokud k chybe nedojde;
 *
 * @since  0.1
 **/
int getFirstUpsName (TQString& firstUpsName);


/**
 * internal
 * Nacte vsechny promene UPS-ky a nastavi jejich typ (znakove/char, vyctove/enum a status)
 * nastavi priznak aktivity jen pro status.
 * Jen pro protokol verze 2
 * Funkce vraci kod chyby nebo 0 pokud k chybe nedojde;
 *
 * @since  0.1
 **/
  int getUpsVars1 ( void);



/**
 * internal
 * Nacte vsechny promene UPS-ky a nastavi jejich typ (znakove/char, vyctove/enum a status)
 * nastavi priznak aktivity jen pro status.
 * Jen pro protokol verze 2
 * Funkce vraci kod chyby nebo 0 pokud k chybe nedojde;
 *
 * @since  0.2
 **/
  int getUpsVars2 ( void);



/**
 * Nacte hodnoty vsech promenych UPS-ky pokud je nastaven jejich priznak aktivity,
 * nebo je nastaven allVars na true.
 *
 * @param allVars Urcuje zda se prikaz tyka vsech promenych, nebo jen tech ktere
 * maji nastaven priznak aktivity na TRUE.
 * Jen pro protokol verze 1
 *
 * @since  0.6
 **/
  int getUpsValues1 (const bool allVars );

/**
 * Nacte hodnoty vsech promenych UPS-ky pokud je nastaven jejich priznak aktivity,
 * nebo je nastaven allVars na true.
 *
 * @param allVars Urcuje zda se prikaz tyka vsech promenych, nebo jen tech ktere
 * maji nastaven priznak aktivity na TRUE.
 * Jen pro protokol verze 2
 *
 * @since  0.6
 **/
  int getUpsValues2 (const bool allVars );


/**
 * @internal
 * Zjisti verzi upsd
 *
 * @param countRepeat maximalni pocet pokusu (ne opakovani !!)
 * @param delay zpozdeni, cas mezi opakovani
 */
  int version (int countRepeat, const int delay);


/**
 * @internal
 * Nenastavuje promenou error.
 * Doesn't set variable error
 */
  int getUpsData (const TQCString sbuffer, TQString& rbuffer, const TQCString endString=0);

/**
 * @internal
 */
    void setRWVars (const TQString varName, const bool valueType, const int varMax, TQValueVector<TQString>* enumValues);

/**
 * @internal
 */
void upsSetType (struct upsVarDef& upsVar, const TQString name, const TQString value = "");

/**
 * @internal
 */
  int upsTranslateError (const TQString string);


/**
 * @internal
 */
  void deleteVars (void);

/**
 * @internal
 */
 int upsOldTranslateError (const TQString string);


/**
 * @internal
 * Pro TQt mensi nez 3.0.0 nemuze byt value const
 */
 void genStatusFlags (TQString value);

/**
 * @internal
 * Nenastavuje promenou error.
 */
 int sendComm (const TQString command, const TQString arg1, const TQString arg2, const bool useUpsName=false);


/**
 * @internal
 */
  int activate ( const TQString name, const bool setActivate );


/**
 * @internal
 */
  int activateAll ( const bool setActivate );


/**
 * @internal
 */
  int newDescription (const TQCString  inBuffer, TQString& upsDescription);

private slots:
  void slotConnectionError(int error);

  void slotConnected(void);

  void slotTimerReconnect(void);

  void slotHostFound(void);

  void slotConnectionClosed(void);

  void slotClosed (void);


private:

  /******* CONST ********/

  static const TQ_ULONG rBufferLen = 1024;

  TQSocket *m_commandSocket;
  TQTimer* m_unConnectTimer;


  /****** VARS *************/
  TQString m_upsAddress;
  TQString m_upsName;

  unsigned int m_countRepeat;
  unsigned short m_port;
  unsigned int m_delay;

  NetState m_state;
  int m_error;
  bool m_firstConnect;
  unsigned int m_numberConnection;

  int m_nutProtocol;  // 1 for NUT <1.3,  2 for NUT => 1.3
  int m_nutVariables; // 1 for NUT <1.3,  2 for NUT => 1.3


  int m_numberVars; // number of all variables - pocet vsech promennych
  int m_numberRWVars;
  int m_numberIComms;

  int m_upsStatusVar;
  bool m_description;

  bool m_switchToTCP;

  TQMutex m_getUpsDataMutex;

  TQValueVector<upsVarDef> m_listVars;
  TQValueVector<upsICommDef> m_listIComms;

/********* Pomocne promenne ***********/
  int m_posChar;

};

#endif