forked from zykh/walNUT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
5046 lines (3843 loc) · 123 KB
/
extension.js
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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* walNUT: A Gnome Shell Extension for NUT (Network UPS Tools)
*
* Copyright (C)
* 2013 Daniele Pezzini <[email protected]>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
const Atk = imports.gi.Atk,
Clutter = imports.gi.Clutter,
Config = imports.misc.config,
Lang = imports.lang,
Main = imports.ui.main,
Mainloop = imports.mainloop,
ModalDialog = imports.ui.modalDialog,
PanelMenu = imports.ui.panelMenu,
Pango = imports.gi.Pango,
PopupMenu = imports.ui.popupMenu,
ShellEntry = imports.ui.shellEntry,
Slider = imports.ui.slider,
St = imports.gi.St,
Tweener = imports.ui.tweener,
Util = imports.misc.util;
// Gettext
const Gettext = imports.gettext.domain('gnome-shell-extensions-walnut'),
_ = Gettext.gettext;
const Me = imports.misc.extensionUtils.getCurrentExtension(),
Convenience = Me.imports.convenience,
// Import GJS implementation of NUT's net protocol
Nut = Me.imports.nut,
// Import utilities.js
Utilities = Me.imports.utilities;
// Panel Icons
const Icons = {
// Error = E
E: 'nut-error',
// Battery | Load
// full = 2 | full = 23
// good = 3 | good = 17
// low = 5 | low = 13
// empty = 7 | empty = 11
// no battery/no load = 1
// +: lightning = online (= status OL = not on battery (OB = B) = mains is not absent): full opacity = charging => O | transparent = charged => C
// +: ! = caution (ALARM, BYPASS, OVER, RB..) => A
// status = OB (-> B) - no caution
// no battery/no load -> 1
B1: 'nut-ghost-ob',
// battery full -> 2
B2: 'nut-battery-full',
B46: 'nut-battery-full-load-full',
B34: 'nut-battery-full-load-good',
B26: 'nut-battery-full-load-low',
B22: 'nut-battery-full-load-empty',
// battery good -> 3
B3: 'nut-battery-good',
B69: 'nut-battery-good-load-full',
B51: 'nut-battery-good-load-good',
B39: 'nut-battery-good-load-low',
B33: 'nut-battery-good-load-empty',
// battery low -> 5
B5: 'nut-battery-low',
B115: 'nut-battery-low-load-full',
B85: 'nut-battery-low-load-good',
B65: 'nut-battery-low-load-low',
B55: 'nut-battery-low-load-empty',
// battery empty -> 7
B7: 'nut-battery-empty',
B161: 'nut-battery-empty-load-full',
B119: 'nut-battery-empty-load-good',
B91: 'nut-battery-empty-load-low',
B77: 'nut-battery-empty',
// just load
// load full -> 23
B23: 'nut-battery-na-load-full',
// load good -> 17
B17: 'nut-battery-na-load-good',
// load low -> 13
B13: 'nut-battery-na-load-low',
// load empty -> 11
B11: 'nut-battery-na-load-empty',
// status = OB (->B) - caution (->A)
// no battery/no load -> 1
BA1: 'nut-ghost-ob-caution',
// battery full -> 2
BA2: 'nut-battery-full-caution',
BA46: 'nut-battery-full-load-full-caution',
BA34: 'nut-battery-full-load-good-caution',
BA26: 'nut-battery-full-load-low-caution',
BA22: 'nut-battery-full-load-empty-caution',
// battery good -> 3
BA3: 'nut-battery-good-caution',
BA69: 'nut-battery-good-load-full-caution',
BA51: 'nut-battery-good-load-good-caution',
BA39: 'nut-battery-good-load-low-caution',
BA33: 'nut-battery-good-load-empty-caution',
// battery low -> 5
BA5: 'nut-battery-low-caution',
BA115: 'nut-battery-low-load-full-caution',
BA85: 'nut-battery-low-load-good-caution',
BA65: 'nut-battery-low-load-low-caution',
BA55: 'nut-battery-low-load-empty-caution',
// battery empty -> 7
BA7: 'nut-battery-empty-caution',
BA161: 'nut-battery-empty-load-full-caution',
BA119: 'nut-battery-empty-load-good-caution',
BA91: 'nut-battery-empty-load-low-caution',
BA77: 'nut-battery-empty-caution',
// just load
// load full -> 23
BA23: 'nut-battery-na-load-full-caution',
// load good -> 17
BA17: 'nut-battery-na-load-good-caution',
// load low -> 13
BA13: 'nut-battery-na-load-low-caution',
// load empty -> 11
BA11: 'nut-battery-na-load-empty-caution',
// status = OL (->O[+C])
// no battery/no load -> 1
OC1: 'nut-ghost-ol-charged',
O1: 'nut-ghost-ol-charging',
// battery full -> 2
OC2: 'nut-battery-full-charged',
O2: 'nut-battery-full-charging',
OC46: 'nut-battery-full-load-full-charged',
O46: 'nut-battery-full-load-full-charging',
OC34: 'nut-battery-full-load-good-charged',
O34: 'nut-battery-full-load-good-charging',
OC26: 'nut-battery-full-load-low-charged',
O26: 'nut-battery-full-load-low-charging',
OC22: 'nut-battery-full-load-empty-charged',
O22: 'nut-battery-full-load-empty-charging',
// battery good -> 3
O3: 'nut-battery-good-charging',
O69: 'nut-battery-good-load-full-charging',
O51: 'nut-battery-good-load-good-charging',
O39: 'nut-battery-good-load-low-charging',
O33: 'nut-battery-good-load-empty-charging',
// battery low -> 5
O5: 'nut-battery-low-charging',
O115: 'nut-battery-low-load-full-charging',
O85: 'nut-battery-low-load-good-charging',
O65: 'nut-battery-low-load-low-charging',
O55: 'nut-battery-low-load-empty-charging',
// battery empty -> 7
O7: 'nut-battery-empty-charging',
O161: 'nut-battery-empty-load-full-charging',
O119: 'nut-battery-empty-load-good-charging',
O91: 'nut-battery-empty-load-low-charging',
O77: 'nut-battery-empty-charging',
// just load
// load full -> 23
OC23: 'nut-battery-na-load-full-charged',
O23: 'nut-battery-na-load-full-charging',
// load good -> 17
OC17: 'nut-battery-na-load-good-charged',
O17: 'nut-battery-na-load-good-charging',
// load low -> 13
OC13: 'nut-battery-na-load-low-charged',
O13: 'nut-battery-na-load-low-charging',
// load empty -> 11
OC11: 'nut-battery-na-load-empty-charged',
O11: 'nut-battery-na-load-empty-charging',
// status = OL (->O[+C]) - caution (->A)
// no battery/no load ->
OAC1: 'nut-ghost-ol-caution-charged',
OA1: 'nut-ghost-ol-caution-charging',
// battery full -> 2
OAC2: 'nut-battery-full-caution-charged',
OA2: 'nut-battery-full-caution-charging',
OAC46: 'nut-battery-full-load-full-caution-charged',
OA46: 'nut-battery-full-load-full-caution-charging',
OAC34: 'nut-battery-full-load-good-caution-charged',
OA34: 'nut-battery-full-load-good-caution-charging',
OAC26: 'nut-battery-full-load-low-caution-charged',
OA26: 'nut-battery-full-load-low-caution-charging',
OAC22: 'nut-battery-full-load-empty-caution-charged',
OA22: 'nut-battery-full-load-empty-caution-charging',
// battery good -> 3
OA3: 'nut-battery-good-caution-charging',
OA69: 'nut-battery-good-load-full-caution-charging',
OA51: 'nut-battery-good-load-good-caution-charging',
OA39: 'nut-battery-good-load-low-caution-charging',
OA33: 'nut-battery-good-load-empty-caution-charging',
// battery low -> 5
OA5: 'nut-battery-low-caution-charging',
OA115: 'nut-battery-low-load-full-caution-charging',
OA85: 'nut-battery-low-load-good-caution-charging',
OA65: 'nut-battery-low-load-low-caution-charging',
OA55: 'nut-battery-low-load-empty-caution-charging',
// battery empty -> 7
OA7: 'nut-battery-empty-caution-charging',
OA161: 'nut-battery-empty-load-full-caution-charging',
OA119: 'nut-battery-empty-load-good-caution-charging',
OA91: 'nut-battery-empty-load-low-caution-charging',
OA77: 'nut-battery-empty-caution-charging',
// just load
// load full -> 23
OAC23: 'nut-battery-na-load-full-caution-charged',
OA23: 'nut-battery-na-load-full-caution-charging',
// load good -> 17
OAC17: 'nut-battery-na-load-good-caution-charged',
OA17: 'nut-battery-na-load-good-caution-charging',
// load low -> 13
OAC13: 'nut-battery-na-load-low-caution-charged',
OA13: 'nut-battery-na-load-low-caution-charging',
// load empty -> 11
OAC11: 'nut-battery-na-load-empty-caution-charged',
OA11: 'nut-battery-na-load-empty-caution-charging'
}
// Battery icon @ menu
const BatteryIcon = {
B1: 'imported-battery-missing',
B2: 'imported-battery-full',
B3: 'imported-battery-good',
B5: 'imported-battery-low',
B7: 'imported-battery-empty'
}
// Errors
const ErrorType = {
UPS_NA: 2, // 'Chosen' UPS is not available
NO_UPS: 4, // No device found
}
// Max length (in chars)
const Lengths = {
ERR_LABEL: 35, // ErrorBox Label
ERR_DESC: 35, // ErrorBox Description
MODEL: 40, // Device manufacturer+model
TOPDATA: 40, // Topdata (status/alarm) description (2nd row)
RAW_VAR: 35, // Raw data list: variable's name
RAW_VALUE: 40, // Raw data list: variable's value
CMD: 45, // UPS commands list - description
CRED_DIALOG: 60 // Credentials dialog description
}
// Interval in milliseconds after which the extension should update the availability of the stored devices (15 minutes)
const INTERVAL = 900000;
// Raw data button ornament
const RawDataButtonOrnament = {
NONE: 0,
OPENED: 1,
CLOSED: 2
};
// Data table items
// Supported data format: {
// '<data identifier #1>': {
// label: function that shall return the label to be shown in panel menu
// icon: name of the icon (sans the '-symbolic' suffix) to be shown in panel menu, if static,
// or function that will be called with the actual value of data and shall return the icon name
// value: function that will be called with the actual value of data and shall return the string to be shown in panel menu
// gsetting: name of the gsetting that toggles the view of this type of data
// },
// '<data identifier #2>': {
// ...
// },
// ...
// }
const DataTableItems = {
'battery.charge': { // Battery charge
// TRANSLATORS: Label of battery charge @ data table
label: function() { return _("Battery Charge"); },
icon: function(value) { return BatteryIcon['B' + Utilities.parseBatteryLevel(value)]; },
// TRANSLATORS: Battery charge level @ data table
value: function(value) { return _("%s %").format(value); },
gsetting: 'display-battery-charge'
},
'ups.load': { // Device load
// TRANSLATORS: Label of device load @ data table
label: function() { return _("Device Load"); },
icon: 'imported-system-run',
// TRANSLATORS: Device load level @ data table
value: function(value) { return _("%s %").format(value); },
gsetting: 'display-load-level'
},
'battery.runtime': { // Backup time
// TRANSLATORS: Label of estimated backup time @ data table
label: function() { return _("Backup Time"); },
icon: 'imported-preferences-system-time',
value: function(value) { return Utilities.parseTime(value); },
gsetting: 'display-backup-time'
},
'ups.temperature': { // Device temperature
// TRANSLATORS: Label of device temperature @ data table
label: function() { return _("Temperature"); },
icon: 'nut-thermometer',
value: function(value) { return Utilities.formatTemp(value); },
gsetting: 'display-device-temperature'
}
};
// UpscMonitor: get vars from NUT at a given interval and deliver infos
const UpscMonitor = new Lang.Class({
Name: 'UpscMonitor',
_init: function() {
// Actual status
this._state = ErrorType.NO_UPS | ErrorType.UPS_NA;
// Device list
this._devices = [];
this._prevDevices = [];
// Here we'll store chosen UPS's variables
this._vars = {};
// Update devices
this.update({ forceRefresh: true });
// Get time between updates
this._interval = gsettings.get_int('update-time');
// Connect update on settings changed
this._settingsChangedId = gsettings.connect('changed', Lang.bind(this, function() {
// Update interval between updates
this._interval = gsettings.get_int('update-time');
// Remove timers
if (this._timer)
Mainloop.source_remove(this._timer);
if (this._forceRefresh)
Mainloop.source_remove(this._forceRefresh);
// Update devices
this.update({ forceRefresh: true });
// Update infos
this._updateTimer();
}));
this._updateTimer();
},
// getDevices: Get available devices
// if a host:port is given call a function to check whether new UPSes are found there and add them to the already listed ones
// otherwise, get stored UPSes or if there's no stored UPS try to find new ones at localhost:3493
// args = {
// hostname: hostname,
// port: port,
// notify: whether we have to notify new devices found/not found or not
// }
getDevices: function(args) {
let host, port, notify;
if (args) {
host = args.hostname;
port = args.port;
notify = args.notify;
}
// Save actual devices
this._prevDevices = JSON.parse(JSON.stringify(this._devices));
let got = [];
// Retrieve actual UPSes stored in schema
let stored = gsettings.get_string('ups');
// e.g.:
// got = [
// {
// name: 'name',
// host: 'host',
// port: 'port'
// },
// {
// name: 'name1',
// host: 'host1',
// port: 'port1',
// user: 'user1',
// pw: 'pw1'
// },
// ...
// ]
got = JSON.parse(!stored || stored == '' ? '[]' : stored);
if (!got.length)
this._state |= ErrorType.NO_UPS;
// If list is empty we'll check localhost:3493
if (!host && !got.length)
host = 'localhost';
if (!port && !got.length)
port = '3493';
if (host && port) {
let client = new Nut.NUTHelper({
host: host,
port: port
});
client.listUPS({
callback: Lang.bind(this, this._postGetDevices),
opts: [ notify ]
});
return;
}
// No new UPS to search
this._devices = got;
// Check which stored UPS is available
this._checkAll();
this._state &= ~ErrorType.NO_UPS;
},
// _postGetDevices: process the result of the *NUTHelper.listUPS()* function called by *this.getDevices()* and save found devices in schema and in *this._devices* as an array of objects:
// {
// name: upsname,
// host: upshostname,
// port: upsport,
// user: username,
// pw: password
// }
// then call *this._checkAll()* to fill the devices list with the availability of each stored UPS
// args = {
// host: hostname we tried to connect to,
// port: port used to connect,
// opts: optional data passed to the callback function, i.e. [ *notify* ],
// data: data got from client, parsed - NOTE: either *data* or *error* is passed to callback function, not both,
// error: errors got from client - NOTE: either *data* or *error* is passed to callback function, not both
// }
// args.*data* = {
// 'ups #1 name': 'ups #1's description',
// 'ups #1 name': 'ups #2's description',
// ...
// }
// args.*error* may be:
// - one of NUT's net protocol errors
// - 'ERR CLIENT-BUSY' if the TCPClient is busy doing something else when the method is called
// - 'ERR CONNECTION-ERROR'/'ERR CONNECTION-ERROR (<error>)' if the TCPClient had some problem connecting
// - 'ERR CONNECTION-CANCELLED' if you cancel (i.e. TCPClient.*destroy()*) the connection before it's connected
// - 'ERR WRITE-ERROR (<error>)' upon errors writing to the TCPClient
// - 'ERR READ-ERROR (<error>)' upon errors reading from the TCPClient
// - 'ERR TOO-FEW-ARGUMENTS' if you called a method without all the required data
// - 'ERR UNKNOWN' - unknown errors
_postGetDevices: function(args) {
let notify = args.opts[0];
let host = args.host;
let port = args.port;
let got = [];
let stored = gsettings.get_string('ups');
// e.g.:
// got = [
// {
// name: 'name',
// host: 'host',
// port: 'port'
// },
// {
// name: 'name1',
// host: 'host1',
// port: 'port1',
// user: 'user1',
// pw: 'pw1'
// },
// ...
// ]
got = JSON.parse(!stored || stored == '' ? '[]' : stored);
// Unable to find an UPS -> returns already available ones
if (args.error || !Object.keys(args.data).length) {
// Notify
if (notify)
Main.notifyError(
// TRANSLATORS: Notify title/description for error while searching new devices
_("NUT: Error!"),
_("Unable to find new devices at host %s:%s").format(host, port)
);
this._devices = got;
// No stored UPSes
if (!this._devices.length) {
this._state |= ErrorType.NO_UPS;
return;
}
// Check which stored UPS is available
this._checkAll();
this._state &= ~ErrorType.NO_UPS;
return;
}
// Store here the actual length of the retrieved list
let l = got.length;
// Found devices
let devices = args.data;
// Number of devices found
let foundDevices = 0;
// Iterate through each device
for (let device in devices) {
let ups = {};
ups.name = device;
ups.host = host;
ups.port = port;
// Check if we already have this UPS
let isNew = 1;
// Don't do anything if there aren't stored UPSes in the list
if (l > 0) {
for (let j = 0; j < got.length; j++) {
if (got[j].name != ups.name)
continue;
if (got[j].host != ups.host)
continue;
if (got[j].port != ups.port)
continue;
isNew = 0;
break;
}
}
// New UPS found!
if (isNew) {
got.push(ups);
// Notify
if (notify) {
Main.notify(
// TRANSLATORS: Notify title/description on every new device found
_("NUT: new device found"),
_("Found device %s at host %s:%s").format(ups.name, ups.host, ups.port)
);
foundDevices++;
}
}
}
// Notify
if (notify) {
// Devices found (more than 1)
if (foundDevices > 1)
Main.notify(
// TRANSLATORS: Notify title/description on new devices found (more than one)
_("NUT: new devices found"),
_("Found %d devices at host %s:%s").format(foundDevices, host, port)
);
// No devices found
else if (!foundDevices)
Main.notifyError(
// TRANSLATORS: Notify title/description for error while searching new devices
_("NUT: Error!"),
_("Unable to find new devices at host %s:%s").format(host, port)
);
}
// First item of got array is the 'chosen' UPS: preserve it
let chosen = got.shift();
// Then sort UPSes in alphabetical order (host:port, and then name)
got.sort(
function(a, b) {
return ((a.host + a.port + a.name) > (b.host + b.port + b.name)) ? 1 : (
((a.host + a.port + a.name) > (b.host + b.port + b.name)) ? -1 : 0
);
}
);
// And now restore chosen UPS
got.unshift(chosen);
// Store new devices in schema
if (got.length > l)
gsettings.set_string('ups', '%s'.format(JSON.stringify(got)));
this._devices = got;
// Check which stored UPS is available
this._checkAll();
this._state &= ~ErrorType.NO_UPS;
},
// _checkAll: Check which stored UPS is available
_checkAll: function() {
for (let i = 0; i < this._devices.length; i++) {
let item = this._devices[i];
// Just in case we lose the UPS..
item.av = 0;
let client = new Nut.NUTHelper({
host: item.host,
port: item.port
});
client.getVar({
upsName: item.name,
varName: 'ups.status',
callback: Lang.bind(this, this._checkUps),
opts: [ item ]
});
}
},
// _checkUps: callback function to tell whether a given UPS is available or not
// The currently processed UPS will get added to its properties its availability:
// - if available -> av = 1:
// e.g. {
// name: 'name',
// host: 'host',
// port: 'port',
// av: 1
// }
// - if not available -> av = 0:
// e.g. {
// name: 'name1',
// host: 'host1',
// port: 'port1',
// user: 'user1',
// pw: 'pw1',
// av: 0
// }
// args = {
// host: hostname we tried to connect to,
// port: port used to connect,
// upsName: name of the UPS,
// varName: name of the variable, i.e. 'ups.status',
// opts: optional data passed to the callback function, i.e. [ *device* ],
// data: data got from client, parsed - NOTE: either *data* or *error* is passed to callback function, not both,
// error: errors got from client - NOTE: either *data* or *error* is passed to callback function, not both
// }
// args.*data* = var's value (e.g. 'OL CHRG')
// args.*error* may be:
// - one of NUT's net protocol errors
// - 'ERR CLIENT-BUSY' if the TCPClient is busy doing something else when the method is called
// - 'ERR CONNECTION-ERROR'/'ERR CONNECTION-ERROR (<error>)' if the TCPClient had some problem connecting
// - 'ERR CONNECTION-CANCELLED' if you cancel (i.e. TCPClient.*destroy()*) the connection before it's connected
// - 'ERR WRITE-ERROR (<error>)' upon errors writing to the TCPClient
// - 'ERR READ-ERROR (<error>)' upon errors reading from the TCPClient
// - 'ERR TOO-FEW-ARGUMENTS' if you called a method without all the required data
// - 'ERR UNKNOWN' - unknown errors
_checkUps: function(args) {
// The UPS we're checking
let ups = args.opts[0];
if (args.error || !args.data.length)
ups.av = 0;
else
ups.av = 1;
let updateNeeded = true;
for (let i = 0; i < this._prevDevices.length; i++) {
let prev = this._prevDevices[i];
if (prev.name != ups.name)
continue;
if (prev.host != ups.host)
continue;
if (prev.port != ups.port)
continue;
if (prev.av != ups.av)
continue;
// Don't update the displayed list of devices if nothing changes
updateNeeded = false;
break;
}
if (updateNeeded && walnut) {
// Refresh the list of devices
walnut.refreshList();
}
},
// _getVars: Retrieve chosen UPS's variables
_getVars: function() {
// Reset status
this._state |= ErrorType.UPS_NA;
let client = new Nut.NUTHelper({
host: this._devices[0].host,
port: this._devices[0].port
});
client.listVars({
upsName: this._devices[0].name,
callback: Lang.bind(this, this._processVars)
});
},
// _processVars: callback function for *this._getVars()* - update status and vars
// args = {
// host: hostname we tried to connect to,
// port: port used to connect,
// upsName: name of the UPS,
// data: data got from client, parsed - NOTE: either *data* or *error* is passed to callback function, not both,
// error: errors got from client - NOTE: either *data* or *error* is passed to callback function, not both
// }
// args.*data* = {
// 'var.1 name': 'var.1's value',
// 'var.2 name': 'var.2's value',
// ...
// }
// args.*error* may be:
// - one of NUT's net protocol errors
// - 'ERR CLIENT-BUSY' if the TCPClient is busy doing something else when the method is called
// - 'ERR CONNECTION-ERROR'/'ERR CONNECTION-ERROR (<error>)' if the TCPClient had some problem connecting
// - 'ERR CONNECTION-CANCELLED' if you cancel (i.e. TCPClient.*destroy()*) the connection before it's connected
// - 'ERR WRITE-ERROR (<error>)' upon errors writing to the TCPClient
// - 'ERR READ-ERROR (<error>)' upon errors reading from the TCPClient
// - 'ERR TOO-FEW-ARGUMENTS' if you called a method without all the required data
// - 'ERR UNKNOWN' - unknown errors
_processVars: function(args) {
let hasChanged = false;
// The actually chosen device
let act = this._devices[0] || { name: '' };
// The device the currently processed function belongs to is no longer the chosen one
if (act.name != args.upsName || act.host != args.host || act.port != args.port)
return;
if (args.error || !Object.keys(args.data).length) {
this._state |= ErrorType.UPS_NA;
act.av = 0;
} else {
this._vars = args.data;
this._state &= ~ErrorType.UPS_NA;
act.av = 1;
// Update setvars/commands
let prev = this._prevChosen || { name: '' };
// Update only if something changed
if (act.name != prev.name || act.host != prev.host || act.port != prev.port || act.av != prev.av) {
if (upsrwDo)
upsrwDo.update();
if (upscmdDo)
upscmdDo.update();
hasChanged = true;
}
}
if (this._forceRefresh) {
Mainloop.source_remove(this._forceRefresh);
delete this._forceRefresh;
}
// Update panel/menu
if (walnut) {
walnut.refreshPanel();
if (walnut.menu.isOpen)
walnut.refreshMenu({ forceRefresh: hasChanged });
}
// Save actually processed device
this._prevChosen = JSON.parse(JSON.stringify(act));
},
// _updateTimer: update infos at a given interval
_updateTimer: function() {
this.update();
this._timer = Mainloop.timeout_add_seconds(this._interval, Lang.bind(this, this._updateTimer));
// Just in case we lose the UPS..
this._forceRefresh = Mainloop.timeout_add_seconds(2, Lang.bind(this, function() {
if (walnut) {
walnut.refreshPanel();
if (walnut.menu.isOpen)
walnut.refreshMenu({ forceRefresh: true });
}
delete this._forceRefresh;
}));
},
// update: Search for available devices and then for the first one's variables
// args = {
// forceRefresh: whether to do a refresh also if INTERVAL time isn't elapsed
// }
update: function(args) {
let forceRefresh = args ? args.forceRefresh : false;
// milliseconds
let now = Date.now();
// Last time the list has been updated
if (!this._lastTime)
this._lastTime = now;
// Update the list
if (forceRefresh || ((now - this._lastTime) > INTERVAL)) {
this.getDevices();
this._lastTime = now;
}
if (this._state & ErrorType.NO_UPS)
return;
this._getVars();
},
// getState: return actual UpscMonitor status (ErrorType.{NO_UPS, ..})
getState: function() {
return this._state;
},
// getList: return actual device list and their availability
getList: function() {
return this._devices;
},
// getVars: return actual chosen device's variables in an Object where keys are variables' names
// (e.g.: {
// 'battery.charge': '100',
// 'ups.status': 'OL',
// ...
// })
getVars: function() {
return this._vars;
},
// destroy: remove timer and disconnect signals
destroy: function() {
// Remove timers
if (this._timer)
Mainloop.source_remove(this._timer);
if (this._forceRefresh)
Mainloop.source_remove(this._forceRefresh);
// Disconnect settings-changed connection
gsettings.disconnect(this._settingsChangedId);
}
});
// UpscmdDo: handle instant commands
const UpscmdDo = new Lang.Class({
Name: 'UpscmdDo',
_init: function() {
this._hasCmds = false;
this._cmds = [];
},
update: function() {
// Reset status
this._hasCmds = false;
// Get actual device
this._device = upscMonitor.getList()[0];
// Don't do anything in case of errors
if (upscMonitor.getState() & (ErrorType.NO_UPS | ErrorType.UPS_NA))
return;
this._retrieveCmds();
},
// _retrieveCmds: get instant commands from the UPS
_retrieveCmds: function() {
let client = new Nut.NUTHelper({
host: this._device.host,
port: this._device.port
});
client.listCmds({
upsName: this._device.name,
callback: Lang.bind(this, this._processRetrievedCmds)
});
},
// _processRetrievedCmds: callback function for _retrieveCmds()
// args = {