-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata.go
More file actions
2954 lines (2509 loc) · 104 KB
/
data.go
File metadata and controls
2954 lines (2509 loc) · 104 KB
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
// Copyright: 2024 Dragos Vingarzan vingarzan -at- gmail -dot- com
// License: AGPL-3.0
//
// This file is part of sun2000-modbus.
//
// sun2000-modbus is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General
// Public License Version 3 (AGPL-3.0) as published by the Free Software Foundation.
//
// sun2000-modbus 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 Affero General Public License for more
// details.
//
// You should have received a copy of the AGPL-3.0 along with sun2000-modbus. If not,
// see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"strings"
"sync"
"time"
)
type sun2000DataStruct struct {
// Identification Data
identification identificationData
product productData
hardware1 hardwareData1
hardware2 hardwareData2
hardware3 hardwareData3
hardware4 hardwareData4
hardware5 hardwareData5
hardware6 hardwareData6
remoteSignalling remoteSignallingData
alarm1 alarmData1
pv pvData
inverter inverterData
cumulative1 cumulativeData1
cumulative2 cumulativeData2
cumulative3 cumulativeData3
mppt1 mpptData1
alarm2 alarmData2
stringAccess stringAccessData
mppt2 mpptData2
internalTemperature internalTemperatureData
meter meterData
esu1 esu1Data
esu2 esu2Data
esuTemperatures esuTemperaturesData
}
func init() {
for i := 0; i < 3; i++ {
parsedData.esu1.pack[i].esuId = 1
parsedData.esu1.pack[i].id = i + 1
parsedData.esu2.pack[i].esuId = 2
parsedData.esu2.pack[i].id = i + 1
}
}
func (x *sun2000DataStruct) metricsString() string {
sb := strings.Builder{}
sb.WriteString("# Huawei Sun2000 inverter scraped data from ModBus TCP\n#\n")
sb.WriteString(fmt.Sprintf("# - last read success at %s\n", lastSuccessTime.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# - consecutive read errors %d\n", errorCount))
sb.WriteString(fmt.Sprintf("# - total read errors %d\n", totalErrorCount))
sb.WriteString(fmt.Sprintf("# - total read successes %d\n", totalSuccessCount))
sb.WriteString("\n")
sb.WriteString(x.identification.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.product.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.hardware1.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.hardware2.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.hardware3.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.hardware4.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.hardware5.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.hardware6.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.remoteSignalling.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.alarm1.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.pv.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.inverter.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.cumulative1.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.cumulative2.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.cumulative3.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.mppt1.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.alarm2.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.stringAccess.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.mppt2.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.internalTemperature.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.meter.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.esu1.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.esu2.metricsString(&x.identification))
sb.WriteString("\n")
sb.WriteString(x.esuTemperatures.metricsString(&x.identification))
sb.WriteString("\n")
return sb.String()
}
type genericData struct {
// RW mutex to protect the data
sync.RWMutex
lastRead time.Time
nextRead time.Time
}
func (x *genericData) isExpired() bool {
x.RLock()
defer x.RUnlock()
return x.nextRead.Before(time.Now())
}
func (x *genericData) setLastRead(t time.Time) {
x.Lock()
defer x.Unlock()
x.lastRead = t
}
func (x *genericData) setNextRead(t time.Time) {
x.Lock()
defer x.Unlock()
x.nextRead = t
}
func (x *genericData) getNextRead() time.Time {
x.RLock()
defer x.RUnlock()
return x.nextRead
}
func (x *genericData) parse(data []byte) (err error) {
return fmt.Errorf("parse not implemented")
}
func (x *genericData) metricsString(id *identificationData) string {
return fmt.Sprintf("# No metrics for %T\n", x)
}
type identificationData struct {
genericData
// 30000 STR 15
model string
// 30015 STR 10
sn string
// 30025 STR 10
pn string
// 30035 STR 15
firmwareVersion string
// 30050 STR 15
softwareVersion string
// 30068 U32 2
protocolVersion uint32 // modbus
// 30070 U16 1
modelID uint16
// 30071 U16 1
numberOfStrings uint16
// 30072 U16 1
numberOfMPPTs uint16
// 30073 U32 2 gain 1000 kW
ratedPower float32
// 30075 U32 2 gain 1000 kW
maxActivePowerPmax float32
// 30077 U32 2 gain 1000 kVA Pmax
maxApparentPowerSmax float32
// 30079 I32 2 gain 1000 kVar Qmax
realtimeMaxReactivePowerQmaxFeedToGrid float32
// 30081 I32 2 gain 1000 kVar -Qmax
realtimeMaxReactivePowerQmaxAbsorbedFromGrid float32
// 30083 U32 2 gain 1000 kW Pmax_real - 0<Pmax≤Smax≤Pmax_real≤Smax_real or 0<Pmax≤Pmax_real≤Smax≤Smax_real
maxActiveCapabilityPmaxReal float32
// 30085 U32 2 gain 1000 kVA Smax_real - 0<Pmax≤Smax≤Pmax_real≤Smax_real or 0<Pmax≤Pmax_real≤Smax≤Smax_real
maxApparentCapabilitySmaxReal float32
}
func (x *identificationData) parse(data []byte) (err error) {
if len(data) < 70 {
return fmt.Errorf("data length %d < 70", len(data))
}
x.Lock()
defer x.Unlock()
var idx uint
x.model, idx, _ = getSTR(data, idx, 15)
x.sn, idx, _ = getSTR(data, idx, 10)
x.pn, idx, _ = getSTR(data, idx, 10)
x.firmwareVersion, idx, _ = getSTR(data, idx, 15)
x.softwareVersion, idx, _ = getSTR(data, idx, 15)
idx, _ = skipRecords(data, idx, 3)
x.protocolVersion, idx, _ = getU32(data, idx)
x.modelID, idx, _ = getU16(data, idx)
x.numberOfStrings, idx, _ = getU16(data, idx)
x.numberOfMPPTs, idx, _ = getU16(data, idx)
u32, idx, _ := getU32(data, idx)
x.ratedPower = float32(u32) / 1000
u32, idx, _ = getU32(data, idx)
x.maxActivePowerPmax = float32(u32) / 1000
u32, idx, _ = getU32(data, idx)
x.maxApparentPowerSmax = float32(u32) / 1000
i32, idx, _ := getI32(data, idx)
x.realtimeMaxReactivePowerQmaxFeedToGrid = float32(i32) / 1000
i32, idx, _ = getI32(data, idx)
x.realtimeMaxReactivePowerQmaxAbsorbedFromGrid = float32(i32) / 1000
u32, idx, _ = getU32(data, idx)
x.maxActiveCapabilityPmaxReal = float32(u32) / 1000
u32, _, _ = getU32(data, idx)
x.maxApparentCapabilitySmaxReal = float32(u32) / 1000
return nil
}
func (x *identificationData) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Identification Data\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# Model = %q\n", x.model))
sb.WriteString(fmt.Sprintf("# SN = %q\n", x.sn))
sb.WriteString(fmt.Sprintf("# PN = %q\n", x.pn))
sb.WriteString(fmt.Sprintf("# Firmware Version = %q\n", x.firmwareVersion))
sb.WriteString(fmt.Sprintf("# Software Version = %q\n", x.softwareVersion))
sb.WriteString(fmt.Sprintf("# Protocol Version = %#08x (D%d.%d)\n", x.protocolVersion, x.protocolVersion>>16&0xffff, x.protocolVersion&0xffff))
sb.WriteString(fmt.Sprintf("# Model ID = %d\n", x.modelID))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# Number of Strings = %d\n", x.numberOfStrings))
sb.WriteString(fmt.Sprintf("# Number of MPPTs = %d\n", x.numberOfMPPTs))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# Rated Power = %2.3f kW\n", x.ratedPower))
sb.WriteString(fmt.Sprintf("# Max Active Power Pmax = %2.3f kW\n", x.maxActivePowerPmax))
sb.WriteString(fmt.Sprintf("# Max Apparent Power Smax = %2.3f kVA\n", x.maxApparentPowerSmax))
sb.WriteString(fmt.Sprintf("# Realtime Max Reactive Power Qmax Feed to Grid = %2.3f kVar\n", x.realtimeMaxReactivePowerQmaxFeedToGrid))
sb.WriteString(fmt.Sprintf("# Realtime Max Reactive Power Qmax Absorbed from Grid = %2.3f kVar\n", x.realtimeMaxReactivePowerQmaxAbsorbedFromGrid))
sb.WriteString(fmt.Sprintf("# Max Active Capability Pmax Real = %2.3f kW 0<Pmax≤Smax≤Pmax_real≤Smax_real or 0<Pmax≤Pmax_real≤Smax≤Smax_real\n", x.maxActiveCapabilityPmaxReal))
sb.WriteString(fmt.Sprintf("# Max Apparent Capability Smax Real = %2.3f kVA 0<Pmax≤Smax≤Pmax_real≤Smax_real or 0<Pmax≤Pmax_real≤Smax≤Smax_real\n", x.maxApparentCapabilitySmaxReal))
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() {
sb.WriteString("# No identification data read yet\n")
} else {
sb.WriteString(fmt.Sprintf("sun2000_number_of_MPPTs{model=%q,sn=%q} %d\n", x.model, x.sn, x.numberOfMPPTs))
sb.WriteString(fmt.Sprintf("sun2000_number_of_strings{model=%q,sn=%q} %d\n", x.model, x.sn, x.numberOfStrings))
sb.WriteString(fmt.Sprintf("sun2000_rated_power{model=%q,sn=%q,unit=\"kW\",description=\"Rated Power\"} %.3f\n", x.model, x.sn, x.ratedPower))
sb.WriteString(fmt.Sprintf("sun2000_Pmax{model=%q,sn=%q,unit=\"kW\",description=\"Maximum Active Power Pmax\"} %.3f\n", x.model, x.sn, x.maxActivePowerPmax))
sb.WriteString(fmt.Sprintf("sun2000_Smax{model=%q,sn=%q,unit=\"kVA\",description=\"Maximum Apparent Power Smax\"} %.3f\n", x.model, x.sn, x.maxApparentPowerSmax))
sb.WriteString(fmt.Sprintf("sun2000_Qmax_feed_to_grid{model=%q,sn=%q,unit=\"kVar\",description=\"Realtime Max Reactive Power Qmax Feed to Grid\"} %.3f\n", x.model, x.sn, x.realtimeMaxReactivePowerQmaxFeedToGrid))
sb.WriteString(fmt.Sprintf("sun2000_Qmax_absorbed_from_grid{model=%q,sn=%q,unit=\"kVar\",description=\"Realtime Max Reactive Power Qmax Absorbed from Grid\"} %.3f\n", x.model, x.sn, x.realtimeMaxReactivePowerQmaxAbsorbedFromGrid))
sb.WriteString(fmt.Sprintf("sun2000_Pmax_real{model=%q,sn=%q,unit=\"kW\",description=\"Maximum Active Capability Pmax Real\"} %.3f\n", x.model, x.sn, x.maxActiveCapabilityPmaxReal))
sb.WriteString(fmt.Sprintf("sun2000_Smax_real{model=%q,sn=%q,unit=\"kVA\",description=\"Maximum Apparent Capability Smax Real\"} %.3f\n", x.model, x.sn, x.maxApparentCapabilitySmaxReal))
}
sb.WriteString("\n")
return sb.String()
}
type productData struct {
genericData
// 30105 STR 2
productSalesArea string
// 30107 U16 1
productSoftwareNumber uint16
// 30108 U16 1
productSoftwareVersionNumber uint16
// 30109 U16 1
gridStandardCodeProtocolVersion uint16
// 30110 U16 1
uniqueIDOfTheSoftware uint16
// 30111 U16 1
numberOfPackagesToBeUpgraded uint16
// 30112-30130 U32 2x10
subpackageInformation [10]uint32
}
func (x *productData) parse(data []byte) (err error) {
if len(data) < 2*17 {
return fmt.Errorf("data length %d < 2*17", len(data))
}
x.Lock()
defer x.Unlock()
var idx uint
x.productSalesArea, idx, _ = getSTR(data, idx, 2)
x.productSoftwareNumber, idx, _ = getU16(data, idx)
x.productSoftwareVersionNumber, idx, _ = getU16(data, idx)
x.gridStandardCodeProtocolVersion, idx, _ = getU16(data, idx)
x.uniqueIDOfTheSoftware, idx, _ = getU16(data, idx)
x.numberOfPackagesToBeUpgraded, idx, _ = getU16(data, idx)
for i := 0; i < 10; i++ {
x.subpackageInformation[i], idx, _ = getU32(data, idx)
}
return nil
}
func (x *productData) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Product Data\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# Product Sales Area = %q\n", x.productSalesArea))
sb.WriteString(fmt.Sprintf("# Product Software Number = %d\t%#04x\n", x.productSoftwareNumber, x.productSoftwareNumber))
sb.WriteString(fmt.Sprintf("# Product Software Version Number = %d\t%#04x\n", x.productSoftwareVersionNumber, x.productSoftwareVersionNumber))
sb.WriteString(fmt.Sprintf("# Grid Standard Code Protocol Version = %d\t%#04x\n", x.gridStandardCodeProtocolVersion, x.gridStandardCodeProtocolVersion))
sb.WriteString(fmt.Sprintf("# Unique ID Of The Software = %d\t%#04x\n", x.uniqueIDOfTheSoftware, x.uniqueIDOfTheSoftware))
sb.WriteString(fmt.Sprintf("# Number Of Packages To Be Upgraded = %d\n", x.numberOfPackagesToBeUpgraded))
sb.WriteString("\n")
for i, v := range x.subpackageInformation {
sb.WriteString(fmt.Sprintf("# Subpackage %2d Information = %#08x\tfileTypeID=%3d\tdeviceTypeId=%d\n", i+1, v, v>>16&0xff, v&0xffff))
}
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No product or identification data read yet\n")
} else {
id.RLock()
defer id.RUnlock()
sb.WriteString(fmt.Sprintf("sun2000_unique_id_of_the_software{model=%q,sn=%q} %d\n", id.model, id.sn, x.uniqueIDOfTheSoftware))
sb.WriteString(fmt.Sprintf("sun2000_number_of_packages_to_be_upgraded{model=%q,sn=%q} %d\n", id.model, id.sn, x.numberOfPackagesToBeUpgraded))
}
sb.WriteString("\n")
return sb.String()
}
type hardwareData1 struct {
genericData
// 30206 Bitfield16 1
hardwareFunctionalUnitConfigurationIdentifier uint16
// 30207 Bitfield32 2
subdeviceSupportFlag uint32
// 30209 Bitfield32 2
subdeviceInPositionFlag uint32
// 30211-30217 Bitfield32 2x4
featureMask [4]uint32
// 30219-30250 Bitfield16 1x32
gridStandardCodeMask [32]uint16
}
func (x *hardwareData1) parse(data []byte) (err error) {
size := 1 + 2 + 2 + 2*4 + 1*32
if len(data) < size*2 {
return fmt.Errorf("data length %d < %d*2", len(data), size)
}
x.Lock()
defer x.Unlock()
var idx uint
x.hardwareFunctionalUnitConfigurationIdentifier, idx, _ = getU16(data, idx)
x.subdeviceSupportFlag, idx, _ = getU32(data, idx)
x.subdeviceInPositionFlag, idx, _ = getU32(data, idx)
for i := 0; i < 4; i++ {
x.featureMask[i], idx, _ = getU32(data, idx)
}
for i := 0; i < 32; i++ {
x.gridStandardCodeMask[i], idx, _ = getU16(data, idx)
}
return nil
}
func (x *hardwareData1) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Hardware Data Part 1\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
var text string
switch x.hardwareFunctionalUnitConfigurationIdentifier {
case 0:
text = "no functional unit hardware configuration"
case 1:
text = "The hardware configuration of the functional unit is available"
case 2:
text = ""
}
sb.WriteString(fmt.Sprintf("# Hardware Functional Unit Configuration Identifier = %#04x\t%#016b\t%s\n", x.hardwareFunctionalUnitConfigurationIdentifier, x.hardwareFunctionalUnitConfigurationIdentifier, text))
sb.WriteString(fmt.Sprintf("# Subdevice Support Flag = %#08x\t%#032b\n", x.subdeviceSupportFlag, x.subdeviceSupportFlag))
sb.WriteString(fmt.Sprintf("# Subdevice In Position Flag = %#08x\t%#032b\n", x.subdeviceInPositionFlag, x.subdeviceInPositionFlag))
sb.WriteString("\n")
for i, v := range x.featureMask {
sb.WriteString(fmt.Sprintf("# Feature Mask %d = %#08x\t%#032b\n", i+1, v, v))
}
sb.WriteString("\n")
for i, v := range x.gridStandardCodeMask {
sb.WriteString(fmt.Sprintf("# Grid Standard Code Mask %2d = %#04x\t%#016b\n", i+1, v, v))
}
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No hardware or identification data read yet\n")
}
// else {
// // no useful metrics here
// id.RLock()
// defer id.RUnlock()
// }
sb.WriteString("\n")
return sb.String()
}
type hardwareData2 struct {
genericData
// 30300-30307 Bitfield16 1x8
monitoringParameterMask [8]uint16
// 30308-30326 Bitfield16 1x19
powerParameterMask [19]uint16
}
func (x *hardwareData2) parse(data []byte) (err error) {
size := 8 + 19
if len(data) < size*2 {
return fmt.Errorf("data length %d < %d*2", len(data), size)
}
x.Lock()
defer x.Unlock()
var idx uint
for i := 0; i < 8; i++ {
x.monitoringParameterMask[i], idx, _ = getU16(data, idx)
}
for i := 0; i < 19; i++ {
x.powerParameterMask[i], idx, _ = getU16(data, idx)
}
return nil
}
func (x *hardwareData2) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Hardware Data Part 2\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
for i, v := range x.monitoringParameterMask {
sb.WriteString(fmt.Sprintf("# Monitoring Parameter Mask %2d = %#04x\t%#016b\n", i+1, v, v))
}
sb.WriteString("\n")
for i, v := range x.powerParameterMask {
sb.WriteString(fmt.Sprintf("# Power Parameter Mask %2d = %#04x\t%#016b\n", i+1, v, v))
}
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No hardware or identification data read yet\n")
}
// else {
// // no useful metrics here
// id.RLock()
// defer id.RUnlock()
// }
sb.WriteString("\n")
return sb.String()
}
type hardwareData3 struct {
genericData
// 30350 U16 1
builtinPIDParameterMask uint16
}
func (x *hardwareData3) parse(data []byte) (err error) {
if len(data) < 2 {
return fmt.Errorf("data length %d < 2", len(data))
}
x.Lock()
defer x.Unlock()
var idx uint
x.builtinPIDParameterMask, _, _ = getU16(data, idx)
return nil
}
func (x *hardwareData3) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Hardware Data Part 3\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# Builtin PID Parameter Mask = %#04x\t%#016b\n", x.builtinPIDParameterMask, x.builtinPIDParameterMask))
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No hardware or identification data read yet\n")
}
// else {
// // no useful metrics here
// id.RLock()
// defer id.RUnlock()
// }
sb.WriteString("\n")
return sb.String()
}
type hardwareData4 struct {
genericData
// 30364 U32 2
realtimeMaxActiveCapability uint32
// 30366 I32 2
realtimeMaxCapacitiveReactiveCapacityPlus int32
// 30368 I32 2
realtimeMaxInductiveReactiveCapacityMinus int32
}
func (x *hardwareData4) parse(data []byte) (err error) {
if len(data) < 6 {
return fmt.Errorf("data length %d < 6", len(data))
}
x.Lock()
defer x.Unlock()
var idx uint
x.realtimeMaxActiveCapability, idx, _ = getU32(data, idx)
x.realtimeMaxCapacitiveReactiveCapacityPlus, idx, _ = getI32(data, idx)
x.realtimeMaxInductiveReactiveCapacityMinus, _, _ = getI32(data, idx)
return nil
}
func (x *hardwareData4) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Hardware Data Part 4\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# Realtime Max Active Capability = %d\n", x.realtimeMaxActiveCapability))
sb.WriteString(fmt.Sprintf("# Realtime Max Capacitive Reactive Capacity (+) = %d\n", x.realtimeMaxCapacitiveReactiveCapacityPlus))
sb.WriteString(fmt.Sprintf("# Realtime Max Inductive Reactive Capacity (-) = %d\n", x.realtimeMaxInductiveReactiveCapacityMinus))
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No hardware or identification data read yet\n")
}
// else {
// // no useful metrics here
// id.RLock()
// defer id.RUnlock()
// }
sb.WriteString("\n")
return sb.String()
}
type hardwareData5 struct {
genericData
// 31000 STR 15
hardwareVersion string
// 31015 STR 10
monitoringBoardSN string
// 31025 STR 15
monitoringSoftwareVersion string
// 31040 STR 15
primaryDSPVersion string
// 31055 STR 15
slaveDSPVersion string
// 31070 STR 15
cplDRevNo string
// 31085 STR 15
afciVersion string
// 31100 STR 15
builtinPID string
}
func (x *hardwareData5) parse(data []byte) (err error) {
size := 15*7 + 10
if len(data) < size {
return fmt.Errorf("data length %d < %d", len(data), size)
}
x.Lock()
defer x.Unlock()
var idx uint
x.hardwareVersion, idx, _ = getSTR(data, idx, 15)
x.monitoringBoardSN, idx, _ = getSTR(data, idx, 10)
x.monitoringSoftwareVersion, idx, _ = getSTR(data, idx, 15)
x.primaryDSPVersion, idx, _ = getSTR(data, idx, 15)
x.slaveDSPVersion, idx, _ = getSTR(data, idx, 15)
x.cplDRevNo, idx, _ = getSTR(data, idx, 15)
x.afciVersion, idx, _ = getSTR(data, idx, 15)
x.builtinPID, _, _ = getSTR(data, idx, 15)
return nil
}
func (x *hardwareData5) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Hardware Data Part 5\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# Hardware Version = %q\n", x.hardwareVersion))
sb.WriteString(fmt.Sprintf("# Monitoring Board SN = %q\n", x.monitoringBoardSN))
sb.WriteString(fmt.Sprintf("# Monitoring Software Version = %q\n", x.monitoringSoftwareVersion))
sb.WriteString(fmt.Sprintf("# Primary DSP Version = %q\n", x.primaryDSPVersion))
sb.WriteString(fmt.Sprintf("# Slave DSP Version = %q\n", x.slaveDSPVersion))
sb.WriteString(fmt.Sprintf("# CPL D Rev. No. = %q\n", x.cplDRevNo))
sb.WriteString(fmt.Sprintf("# AFCI Version = %q\n", x.afciVersion))
sb.WriteString(fmt.Sprintf("# Builtin PID = %q\n", x.builtinPID))
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No hardware or identification data read yet\n")
}
// else {
// // no useful metrics here
// id.RLock()
// defer id.RUnlock()
// }
sb.WriteString("\n")
return sb.String()
}
type hardwareData6 struct {
genericData
// 31130 STR 15
elModuleSoftwareVersion string
// 31145 STR 15
afci2SoftwareVersion string
}
func (x *hardwareData6) parse(data []byte) (err error) {
size := 15 * 2
if len(data) < size {
return fmt.Errorf("data length %d < %d", len(data), size)
}
x.Lock()
defer x.Unlock()
var idx uint
x.elModuleSoftwareVersion, idx, _ = getSTR(data, idx, 15)
x.afci2SoftwareVersion, _, _ = getSTR(data, idx, 15)
return nil
}
func (x *hardwareData6) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Hardware Data Part 6\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# EL Module Software Version = %q\n", x.elModuleSoftwareVersion))
sb.WriteString(fmt.Sprintf("# AFCI2 Software Version = %q\n", x.afci2SoftwareVersion))
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No hardware or identification data read yet\n")
}
// else {
// // no useful metrics here
// id.RLock()
// defer id.RUnlock()
// }
sb.WriteString("\n")
return sb.String()
}
type remoteSignallingData struct {
genericData
// 32000 Bitfield16 1
singleMachineTelesignalling uint16
// 32001 Bitfield16 1
runningStatusMonitoringProcessing uint16
// 32002 Bitfield16 1
runningStatusPowerProcessing uint16
}
func (x *remoteSignallingData) parse(data []byte) (err error) {
if len(data) < 4 {
return fmt.Errorf("data length %d < 4", len(data))
}
x.Lock()
defer x.Unlock()
var idx uint
x.singleMachineTelesignalling, idx, _ = getU16(data, idx)
x.runningStatusMonitoringProcessing, idx, _ = getU16(data, idx)
x.runningStatusPowerProcessing, _, _ = getU16(data, idx)
return nil
}
func (x *remoteSignallingData) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Remote Signalling Data\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
sb.WriteString(fmt.Sprintf("# Single Machine Telesignalling = %#04x\t%#016b\n", x.singleMachineTelesignalling, x.singleMachineTelesignalling))
sb.WriteString(fmt.Sprintf("# Running Status Monitoring Processing = %#04x\t%#016b\n", x.runningStatusMonitoringProcessing, x.runningStatusMonitoringProcessing))
sb.WriteString(fmt.Sprintf("# Running Status Power Processing = %#04x\t%#016b\n", x.runningStatusPowerProcessing, x.runningStatusPowerProcessing))
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No remote signalling or identification data read yet\n")
}
// else {
// // no useful metrics here
// id.RLock()
// defer id.RUnlock()
// }
sb.WriteString("\n")
return sb.String()
}
const alarmCount = 5
type alarmData1 struct {
genericData
// 32008-32010 Bitfield16 1x3 - but Alarms chapters defines also Alarm4 and Alarm 5
alarm [alarmCount]uint16
}
func (x *alarmData1) parse(data []byte) (err error) {
if len(data) < 6 {
return fmt.Errorf("data length %d < 6", len(data))
}
x.Lock()
defer x.Unlock()
var idx uint
for i := 0; i < 3; i++ {
x.alarm[i], idx, _ = getU16(data, idx)
}
return nil
}
func (x *alarmData1) metricsString(id *identificationData) string {
sb := strings.Builder{}
x.RLock()
defer x.RUnlock()
sb.WriteString("# Alarm Data\n")
sb.WriteString(fmt.Sprintf("# Last Read = %s\n", x.lastRead.Format(time.RFC3339)))
sb.WriteString(fmt.Sprintf("# Next Read = %s\n", x.nextRead.Format(time.RFC3339)))
sb.WriteString("\n")
for i, v := range x.alarm {
sb.WriteString(fmt.Sprintf("# Alarm %d = %#04x\t%#016b\n", i+1, v, v))
}
alarms := getAlarms(x.alarm)
for _, a := range alarms {
sb.WriteString(fmt.Sprintf("# Alarm Triggered: %s\n", a))
}
sb.WriteString("\n")
// skip metrics if the data is empty
if x.lastRead.IsZero() || id.lastRead.IsZero() {
sb.WriteString("# No alarm or identification data read yet\n")
} else {
id.RLock()
defer id.RUnlock()
for i, a := range x.alarm {
sb.WriteString(fmt.Sprintf("sun2000_alarm{model=%q,sn=%q,name=\"Alarm%d\"} %d\n", id.model, id.sn, i+1, a))
}
// might be a bit much, but nice for some historical visibility
for _, a := range sun2000Alarms {
value := "0"
if a.isTriggered(x.alarm) {
value = "1"
}
sb.WriteString(fmt.Sprintf("sun2000_alarm_triggered{model=%q,sn=%q,name=%q,id=\"%d\",level=%q} %s\n", id.model, id.sn, a.name, a.id, a.level, value))
}
}
sb.WriteString("\n")
return sb.String()
}
type sun2000AlarmLevel uint8
const (
alarmLevelWarning sun2000AlarmLevel = iota
alarmLevelMinor
alarmLevelMajor
)
func (x sun2000AlarmLevel) String() string {
switch x {
case alarmLevelWarning:
return "Warning"
case alarmLevelMinor:
return "Minor"
case alarmLevelMajor:
return "Major"
default:
return "Unknown"
}
}
type sun2000Alarm struct {
mask [alarmCount]uint16
name string
id uint16
level sun2000AlarmLevel
}
// TODO: figure out if bit0 is LSB, or MSB
var sun2000Alarms = []sun2000Alarm{
{[alarmCount]uint16{0b1000000000000000}, "High String Input Voltage", 2001, alarmLevelMajor},
{[alarmCount]uint16{0b0100000000000000}, "DC Arc Fault", 2002, alarmLevelMajor},
{[alarmCount]uint16{0b0010000000000000}, "String Reverse Connection", 2011, alarmLevelMajor},
{[alarmCount]uint16{0b0001000000000000}, "String Current Backfeed ", 2012, alarmLevelWarning},
{[alarmCount]uint16{0b0000100000000000}, "Abnormal String Power", 2013, alarmLevelWarning},
{[alarmCount]uint16{0b0000010000000000}, "AFCI Self-Check Fail", 2021, alarmLevelMajor},
{[alarmCount]uint16{0b0000001000000000}, "Phase Wire Short-Circuited to PE", 2031, alarmLevelMajor},
{[alarmCount]uint16{0b0000000100000000}, "Grid Loss", 2032, alarmLevelMajor},
{[alarmCount]uint16{0b0000000010000000}, "Grid Undervoltage", 2033, alarmLevelMajor},
{[alarmCount]uint16{0b0000000001000000}, "Grid Overvoltage", 2034, alarmLevelMajor},
{[alarmCount]uint16{0b0000000000100000}, "Grid Volt. Imbalance", 2035, alarmLevelMajor},
{[alarmCount]uint16{0b0000000000010000}, "Grid Overfrequency", 2036, alarmLevelMajor},
{[alarmCount]uint16{0b0000000000001000}, "Grid Underfrequency", 2037, alarmLevelMajor},
{[alarmCount]uint16{0b0000000000000100}, "Unstable Grid Frequency", 2038, alarmLevelMajor},
{[alarmCount]uint16{0b0000000000000010}, "Output Overcurrent", 2039, alarmLevelMajor},
{[alarmCount]uint16{0b0000000000000001}, "Output DC Component Overhigh", 2040, alarmLevelMajor},
{[alarmCount]uint16{0, 0b1000000000000000}, "Abnormal Residual Current", 2051, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0100000000000000}, "Abnormal Grounding", 2061, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0010000000000000}, "Low Insulation Resistance", 2062, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0001000000000000}, "Overtemperature", 2063, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0000100000000000}, "Device Fault", 2064, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0000010000000000}, "Upgrade Failed or Version Mismatch", 2065, alarmLevelMinor},
{[alarmCount]uint16{0, 0b0000001000000000}, "License Expired", 2066, alarmLevelWarning},
{[alarmCount]uint16{0, 0b0000000100000000}, "Faulty Monitoring Unit", 61440, alarmLevelMinor},
{[alarmCount]uint16{0, 0b0000000010000000}, "Faulty Power Collector", 2067, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0000000001000000}, "Battery Abnormal", 2068, alarmLevelMinor},
{[alarmCount]uint16{0, 0b0000000000100000}, "Active Islanding", 2070, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0000000000010000}, "Passive Islanding", 2071, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0000000000001000}, "Transient AC Overvoltage", 2072, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0000000000000100}, "Peripheral Port Short Circuit", 2075, alarmLevelWarning},
{[alarmCount]uint16{0, 0b0000000000000010}, "Churn Output Overload", 2077, alarmLevelMajor},
{[alarmCount]uint16{0, 0b0000000000000001}, "Abnormal PV Module Configuration", 2080, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0b1000000000000000}, "Optimizer Fault", 2081, alarmLevelWarning},
{[alarmCount]uint16{0, 0, 0b0100000000000000}, "Built-in PID Operation Abnormal", 2085, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0b0010000000000000}, "High Input String Voltage to Ground", 2014, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0b0001000000000000}, "External Fan Abnormal", 2086, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0b0000100000000000}, "Battery Reverse Connection", 2069, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0b0000010000000000}, "On-grid/Off-grid Controller Abnormal", 2082, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0b0000001000000000}, "PV String Loss", 2015, alarmLevelWarning},
{[alarmCount]uint16{0, 0, 0b0000000100000000}, "Internal Fan Abnormal", 2087, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0b0000000010000000}, "DC Protection Unit Abnormal", 2088, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0, 0b0000000000100000}, "Management System Cert Valid Time Ineffective", 2095, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0, 0b0000000000010000}, "Management System Cert Valid Time Being Overdue", 2096, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0, 0b0000000000001000}, "Management System Cert Valid Time Overdue", 2097, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0, 0, 0b0001000000000000}, "CT Disconnection", 2067, alarmLevelMajor},
{[alarmCount]uint16{0, 0, 0, 0, 0b0000100000000000}, "PT Disconnection", 2067, alarmLevelMajor},
}
func (x sun2000Alarm) isTriggered(alarm [alarmCount]uint16) bool {
for i := 0; i < alarmCount; i++ {
if x.mask[i]&alarm[i] != 0 {
return true
}
}
return false
}
func getAlarms(id [alarmCount]uint16) (out []sun2000Alarm) {
out = make([]sun2000Alarm, 0, 8)
for _, a := range sun2000Alarms {
for i := 0; i < alarmCount; i++ {
if a.mask[i]&id[i] != 0 {
out = append(out, a)
break
}
}
}
return out
}
func (x sun2000Alarm) String() string {
return fmt.Sprintf("%s : %s id=%d", x.level, x.name, x.id)
}
type pvData struct {
genericData
// 32015 U16 1
deviceSNSignatureCode uint16
// This could be either pv[x].voltage/current, or MPPT1[x].voltage/current
pv [20]struct {
// 32016 I16 1 gain 10 V
voltage float32
// 32017 I16 1 gain 100 A
current float32
}
}
func (x *pvData) parse(data []byte) (err error) {
if len(data) < 2+2*20*2 {
return fmt.Errorf("data length %d < 2+2*20*2", len(data))
}
x.Lock()
defer x.Unlock()
var idx uint
x.deviceSNSignatureCode, idx, _ = getU16(data, idx)
var i16 int16
for i := 0; i < 20; i++ {
i16, idx, _ = getI16(data, idx)
x.pv[i].voltage = float32(i16) / 10
i16, idx, _ = getI16(data, idx)
x.pv[i].current = float32(i16) / 100
}