forked from yuani520/Meter698-45Simulator
/
Meter698_core.py
1166 lines (1063 loc) · 41.6 KB
/
Meter698_core.py
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
import Comm, time, traceback, configparser, random, Meter645_core, re
def check(code):
if len(code) < 20:
print('不符合698长度', code)
return 1
lenth = int(code[2] + code[1], 16) # 长度
if len(code) >= lenth + 2:
if code[lenth + 1] == '16':
full_message = Comm.strto0x(code[1:lenth - 1])
FCS_calc = str(hex(Comm.pppfcs16(0xffff, full_message, len(full_message)))).zfill(4)[2:]
if len(FCS_calc) == 3:
FCS_calc = '0' + FCS_calc
FCS_calc = FCS_calc[2:] + FCS_calc[0:2]
if len(FCS_calc) == 2:
FCS_calc = FCS_calc + '00'
if FCS_calc[0] == "x":
FCS_calc = '0' + FCS_calc[1:]
print("FCS_calc: ", FCS_calc)
FCS_rec = code[lenth - 1] + code[lenth]
print("FCS_rec: ", FCS_rec)
if FCS_calc == FCS_rec:
print('check granted')
return 0
else:
print("校验错误")
return 1
else:
print('lenth check denied')
return 1
else:
print('698 check denied')
return 1
def B_W_add(stat, add):
global black_white_SA_address, black, white, b_w_stat
if stat == 0:
b_w_stat = 0
black = []
white = []
elif stat == 1:
b_w_stat = 1
black_white_SA_address = add.replace(' ', '')
black_white_SA_address = black_white_SA_address.split('/')
black = black_white_SA_address
elif stat == 2:
b_w_stat = 2
black_white_SA_address = add.replace(' ', '')
black_white_SA_address = black_white_SA_address.split('/')
white = black_white_SA_address
print('B_W_add:', black_white_SA_address)
Meter645_core.B_W_add(stat, add)
def Wild_match_Analysis(code):
code = Comm.makelist(code)
re_ = check(code)
print('Wild_match_Analysis: ', re_)
if re_ == 0:
lenth = SASign(Comm.dec2bin(int(code[4], 16)).zfill(8))
wild_a_full = 'aa' * lenth
add_wild = Comm.list2str(code[5:5 + lenth])
global SA_num
if SA_num == 1 and wild_a_full == add_wild:
return 0
else:
return 1
else:
return 1
def Analysis(code):
if code.find("21300202") != -1 or code.find("21300200") != -1 or code.find("21300201") != -1:
print("111111111111")
return 2
code = Comm.makelist(code)
re_ = check(code)
if re_ == 0:
try:
ctrlc_1(Comm.dec2bin(int(code[3], 16))) # 控制码
except:
print("控制码错误")
code_remain = code[4:]
SA_len_num = SASign(Comm.dec2bin(int(code_remain[0], 16)).zfill(8))
global SA_num_len, LargeOAD, relen, data, data_list, frozenSign, b_w_stat, black, white
relen = 0
LargeOAD = ''
data = ''
data_list = []
frozenSign = 0
SA_num_len = code_remain[0:1 + SA_len_num]
print('SA_num_len:', SA_num_len)
global black_white_SA_address # 收到报文的地址
black_white_SA_address = Comm.list2str(SA_num_len[::-1][0:SA_len_num])
print('black_white_SA_address', black_white_SA_address)
print('black_white_SA_address a?', black_white_SA_address.find('a'))
black_white_SA_address_makestr = Comm.makelist(black_white_SA_address)
if b_w_stat == 0 and black_white_SA_address.find('a') > -1:
print("blocked")
return 3
if black_white_SA_address.find('a') == -1:
if b_w_stat == 1:
for add in black:
print('add: ', add)
if add.find('-') > 0:
add_range = add.split('-')
start = int(add_range[0])
end = int(add_range[1])
print("start: ", start, "end: ", end)
if start <= int(black_white_SA_address) <= end:
print('检测到黑名单地址范围')
return 1
else:
continue
elif add == black_white_SA_address:
return 1
elif b_w_stat == 2:
num = 0
for add in white:
print('add: ', add)
if add.find('-') > 0:
add_range = add.split('-')
start = int(add_range[0])
end = int(add_range[1])
print("start: ", start, "end: ", end)
if start <= int(black_white_SA_address) <= end:
print('检测到白名单地址范围')
num = -1
break
else:
num = 1
elif add == black_white_SA_address:
print('发现白名单')
break
else:
num = 1
print("白名单判断 ", black_white_SA_address, num)
if num != 1:
print('通过')
else:
print('不通过')
return 1
else:
print(" find a")
if black_white_SA_address != 'aaaaaaaaaaaa':
if b_w_stat == 2:
whitelist = []
find_aa = 0
match = 0
black_white_SA_address_makelist = Comm.makelist(black_white_SA_address)
for x in range(6):
if black_white_SA_address_makelist[x] == 'aa':
find_aa += 1
black_white_SA_address_makelist_int = int(Comm.list2str(black_white_SA_address_makelist[find_aa:]))
for add in white:
print('add: ', add)
if add.find('-') > 0:
add_range = add.split('-')
start = int(add_range[0])
end = int(add_range[1])
print("start: ", start, "end: ", end)
while start <= end:
whitelist.append(start)
start += 1
print("whitelist: ", whitelist)
for x in whitelist:
x_makelist = int(Comm.list2str(Comm.makelist(str(x).zfill(12))[find_aa:]))
print('matching', black_white_SA_address_makelist_int, x_makelist)
if black_white_SA_address_makelist_int == x_makelist:
print('match')
match = 1
break
if match == 0:
return 1
else:
for x in white:
x_makelist = int(Comm.list2str(Comm.makelist(str(x))[find_aa:]))
print('matching', black_white_SA_address_makelist_int, x_makelist)
if black_white_SA_address_makelist_int == x_makelist:
print('match')
match = 1
break
if match == 0:
return 1
else:
print("blocked")
return 1
else:
global trans
trans = white[0]
CA = code_remain[1 + SA_len_num:][0]
HCS = code_remain[1 + SA_len_num:][1] + code_remain[1 + SA_len_num:][2]
APDU = code_remain[1 + SA_len_num:][3:-3]
if Information(APDU[0], APDU[1], APDU[2:]) is None:
print("198")
return 1
s = LargeOAD
LargeOAD = ''
data_list = []
data = ''
relen = 0
frozenSign = 0
return s
else:
print('非698,尝试645')
Meter645_core.plus_one = plus_645
text = Meter645_core.deal_receive(code)
if text is None:
print('645解析失败')
return 1
if text[0] == '2':
return 3
global OI
OI = text[1:]
black_white_SA_address = Comm.list2str(Meter645_core.address[::-1])
return text[0]
def Information(num, detail, APDU):
global service_code, LargeOAD, GetRequestNormal_0501
service_code = APDU[0]
if num == '01':
print(num, '预链接请求')
elif num == '81':
print(num, '预链接响应')
elif num == '02':
print(num, '应用链接请求')
elif num == '82':
print(num, '应用链接响应')
elif num == '03':
print(num, '断开链接响应')
elif num == '83':
print(num, '断开链接请求')
elif num == '05':
print(num, '读取请求', end=' ')
if detail == '01':
print(detail, '读取一个对象属性请求(GetRequestNormal) ')
GetRequestNormal_0501 = 1
returnvalue = A_ResultRecord_SEQUENCE(APDU[1:5])
if returnvalue == 0:
print('0501抄事件') # 3320
# LargeOAD = '850100' + Comm.list2str(
# APDU[1:5]) + '01 01 04 51 30 1b 02 00 51 30 2a 02 00 51 30 13 02 00 51 30 11 02 00 00 00'.replace(
# ' ', '')
if APDU[1:5] == ["33", "20", "02", "00"]:
global apdu_3320
LargeOAD = apdu_3320
print('0501抄事件', LargeOAD)
st = [Comm.list2str(APDU[1:5]), '事件特殊处理', '']
global OI # 屏幕显示
OI = OI + st[0:2]
else:
LargeOAD = LargeOAD + '0000'
ReturnMessage().reAPDUtype(num + detail + service_code)
ReturnMessage().head()
GetRequestNormal_0501 = 0
elif detail == '02':
print(detail, '读取若干个对象属性请求 (GetRequestNormalList) ')
if SequenceOfLen(APDU[1:]) is None:
print("读取若干个对象属性请求 None")
return None
LargeOAD = LargeOAD + '0000'
ReturnMessage().reAPDUtype(num + detail + service_code)
ReturnMessage().head()
print('返回项数量', relen)
elif detail == '03':
print(detail, '读取一个记录型对象属性请求 (GetRequestRecord) ')
returnvalue = A_ResultRecord_SEQUENCE(APDU[1:5])
if returnvalue is None:
print("读取一个记录型对象属性请求 None")
return None
global frozenSign, data_list
if returnvalue == 0:
print('抄事件')
if Event(APDU[1:]) is None:
print("抄事件 None")
return None
return 0
if returnvalue == '5002':
frozenSign = 1
if returnvalue == '5004':
frozenSign = 2
if returnvalue == '5006':
frozenSign = 3
if returnvalue == '5032':
frozenSign = 4
if returnvalue == '5005':
frozenSign = 5
reCSD = RSD(APDU[5:])
if reCSD is None:
return 0
if RCSD(reCSD[0], reCSD[1:]) is None:
print("RCSD is None")
return None
# print('LargeOAD-1', LargeOAD)
# print('返回项数量', relen)
datatype = num + detail + service_code
datatype = '8' + datatype[1:]
datatype = datatype.replace(' ', '')
global from_to_sign, from_to, hour_, minute_
print('from_to_sign', from_to_sign)
if from_to_sign == 1:
print('from_to', from_to)
no_timme = int(hour_) * 60 + int(minute_)
print('no_timme', no_timme)
if from_to[0] < no_timme < from_to[1]:
print('pass the times')
LargeOAD = datatype + str(returnvalue) + '0200' + hex(relen)[2:].zfill(2) + LargeOAD + '01000000'
else:
LargeOAD = str(returnvalue) + '0200' + hex(relen)[2:].zfill(2) + LargeOAD + '0101'
LargeOAD = datatype + LargeOAD + Comm.list_append(data_list) + '0000'
else:
LargeOAD = str(returnvalue) + '0200' + hex(relen)[2:].zfill(2) + LargeOAD + '0101'
# print('data_list', data_list)
LargeOAD = datatype + LargeOAD + Comm.list_append(data_list) + '0000'
ReturnMessage().head()
# print('data_list', Comm.list_append(data_list))
# print('组成', LargeOAD)
elif detail == '04':
print(detail, '读取若干个记录型对象属性请求 (GetRequestRecordList) ')
elif detail == '05':
print(detail, '读取分帧响应的下一个数据块请求 (GetRequestNext) ')
elif detail == '06':
print(detail, '读取一个对象属性的 MD5 值 (GetRequestMD5) ')
else:
print('ERROR:05??')
elif num == '85':
if detail == '01':
print(detail, '读取一个对象属性的响应(GetResponseNormal) ')
elif detail == '02':
print(detail, '读取若干个对象属性的响应 (GetResponseNormalList) ')
elif detail == '03':
print(detail, '读取一个记录型对象属性的响应 (GetResponseRecord) ')
elif detail == '04':
print(detail, '读取若干个记录型对象属性的响应 (GetResponseRecordList) ')
elif detail == '05':
print(detail, '分帧响应一个数据块 (GetResponseNext) ')
elif detail == '06':
print(detail, '读取一个对象属性的 MD5 值的响应 (GetResponseMD5) ')
else:
print('ERROR:85??')
elif num == '06':
print(num, '设置请求')
elif num == '86':
print(num, '设置响应')
elif num == '07':
print(num, '操作请求')
elif num == '87':
print(num, '操作响应')
elif num == '08':
print(num, '上报回应')
elif num == '88':
print(num, '上报请求')
elif num == '10':
print(num, '安全请求', end=' ')
if detail == '00':
print('解析明文')
seclen = int(APDU[0], 16)
realAPDU = APDU[1:seclen + 1]
left = APDU[seclen + 1:]
global SecType
SecType = left[0]
if SecType == '01':
global mac
if mac == 1:
pass
else:
SecType = '00'
if Information(realAPDU[0], realAPDU[1], realAPDU[2:]) is None:
print("Information None")
return None
else:
print('非随机数无法读取')
if detail == '01':
print('密文无法读取!!')
return 0
def SequenceOfLen(remain):
global LargeOAD, relen
lenth = int(remain[0], 16)
remain = remain[1:]
while lenth > 0:
returnvalue = A_ResultRecord_SEQUENCE(remain[0:4])
if returnvalue is None:
print('returnvalue is None2')
return None
if returnvalue == 0:
print('0502抄事件临时处理')
ReturnMessage().save(['事件响应', Comm.list2str(remain[0:4]), ''])
LargeOAD = Comm.list2str(remain[0:4]) + '0100'
relen += 1
remain = remain[4:]
lenth -= 1
return 1
def A_ResultRecord_SEQUENCE(remain):
OAD = str(remain[0] + remain[1])
print("oad: " + OAD)
if OAD == '5004' or OAD == '5002' or OAD == '5006' or OAD == '5005':
print('冻结')
return OAD
if OAD == '5032':
print('直流冻结')
return OAD
if OAD[0] == '3':
return 0
else:
if OAD_SEQUENCE(OAD, remain[2], remain[3]) is None:
print("OAD_SEQUENCE is None")
return None
else:
return 1
def Event(APDU):
global LargeOAD
'''Selector'''
message = '850300' + Comm.list2str(APDU[0:4])
if APDU[4] == '09':
print('事件响应', Comm.list2str(APDU[0:4]))
ReturnMessage().save(['事件响应', Comm.list2str(APDU[0:4]), ''])
last_n = APDU[5]
RCSD_len = int(APDU[6], 16)
message = message + APDU[6]
remain = APDU[7:]
while RCSD_len:
RCSD_len -= 1
message = message + Comm.list2str(remain[0:5])
value = Comm.list2str(remain[1:5]).zfill(4)
if event_compose_data(value) is None:
print("event_compose_data None")
return None
remain = remain[5:]
global event_stat
print("event_stat:", event_stat)
if event_stat == 0:
LargeOAD = message + '0101' + LargeOAD + '0000'
elif event_stat == 2:
LargeOAD = message + '01000000'
else:
stat = 0
global event_blacklist
print("event_blacklist:", event_blacklist)
print("Comm.list2str(APDU[0:4]):", Comm.list2str(APDU[0:4]))
for x in event_blacklist:
if x == Comm.list2str(APDU[0:4]):
print("x:", x)
print("Comm.list2str(APDU[0:4]):", Comm.list2str(APDU[0:4]))
LargeOAD = message + '01000000'
stat = 1
break
if stat == 0:
LargeOAD = message + '0101' + LargeOAD + '0000'
ReturnMessage().head()
# print('组成', LargeOAD)
return 1
else:
print('其他')
return 0
def event_compose_data(OI):
conf_new = configparser.ConfigParser()
conf_new.read('config.ini', encoding='utf-8')
try:
get = conf_new.get('MeterData698', OI)
get = get.split(' ')
text = [OI, get[0], get[1]]
print("事件", text)
global LargeOAD
LargeOAD = LargeOAD + text[2]
return 0
except:
print('未知数据标识: ', OI)
traceback.print_exc(file=open('bug.txt', 'a+'))
return None
def A_ResultRecord_SEQUENCE_RSD(remain):
try:
OI = remain[0] + remain[1]
unsigned11 = Comm.dec2bin(int(remain[2])).zfill(8) # 特征值
unsigned11 = int(unsigned11[0:4], 10)
print(OI + remain[2] + remain[3], end=' ')
except:
traceback.print_exc(file=open('bug.txt', 'a+'))
def RSD(remain):
global relen, sele
if remain[0] == '01':
sele = 1
print('Selector 01')
A_ResultRecord_SEQUENCE_RSD(remain[1:5])
reMessage = Data(remain[5], remain[6:]) # 收到的冻结时间
relen = 0
return reMessage
if remain[0] == '02':
sele = 2
print('Selector 02')
A_ResultRecord_SEQUENCE_RSD(remain[1:5])
reMessage = Data(remain[5], remain[6:])
reMessage = Data(reMessage[8], reMessage[9:]) # 收到的冻结时间
relen = 0
return reMessage
if remain[0] == '09':
sele = 9
print('Selector 09')
return remain[2:]
else:
print('Selector ERROR: ', remain[0])
def RCSD(remain_len, args):
lens = int(remain_len, 16)
print('lens', lens)
while lens > 0:
args = CSD_CHOICE(args)
if args is None:
print("arg is None")
return None
lens -= 1
return args
def CSD_CHOICE(args):
type = args[0]
if type == '00':
OAD = str(args[1] + args[2])
if OAD_SEQUENCE(OAD, args[3], args[4]) is None:
print("OAD_SEQUENCE(OAD, args[3], args[4]) is None")
return None
if args == []:
print('CSD_CHOICE is NULL')
return args[5:]
elif type == '01':
value = ROAD(args[1:])
if value == []:
print('CSD_CHOICE is NULL')
return value
else:
print('ERRORS:CSD_CHOICE')
def ROAD(args):
pass
def OAD_SEQUENCE(OI, unsigned1, unsigned2):
try:
# unsigned11 = Comm.dec2bin(int(unsigned1)).zfill(8) # 特征值
# unsigned11 = int(unsigned11[0:4], 10)
# unsigned1 = '属性 ' + unsigned1[1]
# print('OI, unsigned1', OI, unsigned1)
value = str(OI).zfill(4) + unsigned1[-1].zfill(2) + str(unsigned2).zfill(2)
ReturnMessage().sequence_of_len()
global frozenSign
if frozenSign != 0:
ReturnMessage().composefrozen(value.lower())
return 1
else:
if ReturnMessage().compose_data(value.lower()) is None:
traceback.print_exc(file=open('bug.txt', 'a+'))
print("ReturnMessage().compose_data(value.lower()) is None")
return None
else:
return 1
except:
traceback.print_exc(file=open('bug.txt', 'a+'))
print("???")
return None
def Data(DataDescribe, args):
try:
DataDescribe = str(int(DataDescribe, 16)).zfill(2)
if DataDescribe == '00':
print('NULL', DataDescribe)
return args
elif DataDescribe == '01':
print('array:', DataDescribe, end=' ')
len1 = int(args[0], 16)
lenori = len1
args = args[1:]
while len1 > 0:
args = Data(args[0], args[1:])
len1 -= 1
print('Data', args)
return args
elif DataDescribe == '02':
print('structure: ', DataDescribe)
len2 = int(args[0], 16)
lenori = len2
args = args[1:]
print('len:', len2)
while len2 > 0:
args = Data(args[0], args[1:])
len2 -= 1
print('Data', args)
return args
elif DataDescribe == '03':
print('bool:', DataDescribe)
elif DataDescribe == '04':
print('bit-string:', DataDescribe)
value = Comm.list2str(args[1:3])
print('value', Comm.list2str(value))
return args[3:]
elif DataDescribe == '05':
print('double-long: ', DataDescribe)
value = int(args[0] + args[1] + args[2] + args[3], 16)
if value > 2147483647:
value = Comm.Inverse_code(bin(value))
value = int(value, 2) + 1
value = -value
print('value', value)
return args[4:]
elif DataDescribe == '06': # 4byte
print('double-long-unsigned: ', DataDescribe)
value = int(args[0] + args[1] + args[2] + args[3], 16)
if value > 2147483647:
value = Comm.Inverse_code(bin(value))
value = int(value, 2) + 1
value = -value
print('value', value)
return args[4:]
elif DataDescribe == '09':
print('octet-string: ', DataDescribe)
elif DataDescribe == '10':
print('visible-string: ', DataDescribe)
elif DataDescribe == '12':
print('UTF8-string:', DataDescribe)
elif DataDescribe == '15':
print('integer:', DataDescribe)
elif DataDescribe == '16':
print('long: ', DataDescribe)
value = int(args[0] + args[1], 16)
if value > 32767:
value = Comm.Inverse_code(bin(value))
value = int(value, 2) + 1
value = -value
print('value', value)
return args[2:]
elif DataDescribe == '17':
print('unsigned:', DataDescribe)
elif DataDescribe == '18':
print('long-unsigned:', DataDescribe)
value = int(args[0] + args[1], 16)
if value > 32767:
value = Comm.Inverse_code(bin(value))
value = int(value, 2) + 1
value = -value
print('value', value)
return args[2:]
elif DataDescribe == '20':
print('long64: ', DataDescribe)
elif DataDescribe == '21':
print('long64-unsigned', DataDescribe)
elif DataDescribe == '22':
print('enum', DataDescribe)
elif DataDescribe == '23':
print('float32', DataDescribe)
elif DataDescribe == '24':
print('float64', DataDescribe)
elif DataDescribe == '25':
print('date_time', DataDescribe)
elif DataDescribe == '26':
print('date', DataDescribe)
elif DataDescribe == '27':
print('time', DataDescribe)
elif DataDescribe == '28':
print('DataDescribe:', DataDescribe, 'date_time_s', end=' ')
year = int(args[0] + args[1], 16)
mouth = int(args[2], 16)
day = int(args[3], 16)
global hour, hour_, minute, minute_
hour = int(args[4], 16)
hour_ = hour
minute = int(args[5], 16)
minute_ = minute
second = int(args[6], 16)
datatime = str(year) + '年' + str(mouth) + '月' + str(day) + '日' + ' ' + str(hour).zfill(2) + ':' + str(
minute).zfill(2) + ':' + str(
second).zfill(2)
print(datatime)
global Daily_freeze
Daily_freeze = '1c' + Comm.list2str(args[0:7]) # 冻结返回时间
print('Daily_freeze', Daily_freeze)
global Difference
Difference = abs(int(time.strftime('%m%d'), 10) - (mouth * 100 + day))
return args[7:]
elif DataDescribe == '80':
print('OAD ', DataDescribe)
elif DataDescribe == '82':
print('ROAD ', DataDescribe)
elif DataDescribe == '83':
print('OMD ', DataDescribe)
elif DataDescribe == '84':
print('TI', DataDescribe)
timeUnit = int(args[0], 16)
times = int(args[1] + args[2], 16)
if timeUnit == 1:
print(times, '分钟')
return args[3:]
elif DataDescribe == '85':
print('TSA', DataDescribe)
value = args[0:8]
print('TSA', value)
return args[8:]
elif DataDescribe == '86':
print('MAC', DataDescribe)
elif DataDescribe == '87':
print('RN', DataDescribe)
elif DataDescribe == '88':
print('Region', DataDescribe)
elif DataDescribe == '89':
print('Scaler_Unit ', DataDescribe)
elif DataDescribe == '90':
print('RSD', DataDescribe)
elif DataDescribe == '91':
print('CSD', DataDescribe)
elif DataDescribe == '92':
print('MS', DataDescribe)
elif DataDescribe == '93':
print('SID', DataDescribe)
elif DataDescribe == '94':
print('SID_MAC', DataDescribe)
elif DataDescribe == '95':
print('COMDCB', DataDescribe)
elif DataDescribe == '96':
print('RCSD', DataDescribe)
else:
print('ERROR on Data')
except:
traceback.print_exc(file=open('bug.txt', 'a+'))
def SASign(num):
global SA_num
numadd = int(str(num[0] + num[1]), 2)
if numadd == 0:
# print('0 单地址')
SA_num = 0
elif numadd == 1:
print('1 通配地址')
SA_num = 1
elif numadd == 2:
print('2 组地址')
else:
print('3 广播地址')
# print(' 逻辑地址: ', num[2], num[3])
numadd1 = int(num[4:], base=2)
print('地址长度 N: ', numadd1 + 1)
return numadd1 + 1
def ctrlc_1(num):
if num[0] == '0' and num[1] == '0':
# print(num, 'DIR=0 PRM=0 客户机对服务器上报的响应')
return 0
elif num[0] == '0' and num[1] == '1':
# print(num, 'DIR=0 PRM=1 客户机发起请求')
return 1
elif num[0] == '1' and num[1] == '0':
# print(num, 'DIR=1 PRM=0 服务器发起上报')
return 2
else:
# print(num, 'DIR=1 PRM=1 服务器对客户机请求的响应')
return 3
def SequenceOf_ARecordRow(data):
global data_list
data_list.append(data)
class ReturnMessage():
def __init__(self):
self.conf_new = configparser.ConfigParser()
self.conf_new.read('config.ini', encoding='utf-8')
def head(self):
global SA_num
self.ctrlzone = 'c3'
self.add = Comm.list2str(SA_num_len)
if SA_num == 0:
if self.add.find('a') > -1:
self.add = '05' + Comm.list2str(Comm.makelist(self.Re_add())[::-1])
print('add', self.add)
elif SA_num == 1:
if self.add.find('a') == -1:
pass
else:
self.add = '05' + Comm.list2str(Comm.makelist(self.Re_add())[::-1])
print('add', self.add)
self.CA = '00'
self.totallenth()
def totallenth(self):
self.total = self.ctrlzone + self.add + self.CA
global LargeOAD, SecType
print('完整的APDU ', LargeOAD)
print('SecType', SecType)
if SecType == '01':
sec_len = len(LargeOAD) // 2
print('sec_len1:', sec_len)
if sec_len > 127:
if sec_len < 255:
sec_len = '81' + hex(len(LargeOAD) // 2)[2:]
elif sec_len > 255 and sec_len < 65535:
sec_len = '82' + hex(len(LargeOAD) // 2)[2:].zfill(4)
else:
sec_len = hex(len(LargeOAD) // 2)[2:].zfill(2)
print('sec_len2:', sec_len)
LargeOAD = '9000' + sec_len + LargeOAD + '0100040a0b0c0d'
SecType == '00'
print('完整的APDU加MAC ', LargeOAD)
APDU_len = hex(len(Comm.makelist(LargeOAD)) + 6 + len(Comm.makelist(self.total)))[2:].zfill(4)
print('总长(不包括头和尾)', APDU_len)
self.head_message = Comm.strto0x(Comm.makelist(APDU_len[2:] + APDU_len[0:2] + self.total))
self.HCS = str(hex(Comm.pppfcs16(0xffff, self.head_message, len(self.head_message)))).zfill(4)[2:]
if len(self.HCS) == 3:
self.HCS = '0' + self.HCS
self.HCS = self.HCS[2:] + self.HCS[0:2]
if len(self.HCS) == 2:
self.HCS = self.HCS + '00'
print("HCS: ", self.HCS)
LargeOAD = APDU_len[2:] + APDU_len[0:2] + self.total + self.HCS + LargeOAD
self.full_message = Comm.strto0x(Comm.makelist(LargeOAD))
self.FCS = str(hex(Comm.pppfcs16(0xffff, self.full_message, len(self.full_message)))).zfill(4)[2:]
if len(self.FCS) == 3:
self.FCS = '0' + self.FCS
self.FCS = self.FCS[2:] + self.FCS[0:2]
if len(self.FCS) == 2:
self.FCS = self.FCS + '00'
if self.FCS[0] == "x":
self.FCS = '0' + self.FCS[1:]
print("FCS: ", self.FCS)
LargeOAD = '68' + LargeOAD + self.FCS + '16'
print('发送报文:', LargeOAD, '\n')
def Full_LargeOAD(self):
global LargeOAD
return LargeOAD
def reAPDUtype(self, datatype):
datatype = '8' + datatype[1:]
self.datatype = datatype.replace(' ', '')
global LargeOAD, GetRequestNormal_0501
if GetRequestNormal_0501 == 1:
LargeOAD = self.datatype + LargeOAD
else:
LargeOAD = self.datatype + hex(relen)[2:].zfill(2) + LargeOAD
print('reapdu', self.datatype)
def sequence_of_len(self):
global relen
relen += 1
def compose_data(self, OI):
global LargeOAD, auto_increase, trans, SA_num_len
try:
self.get = self.conf_new.get('MeterData698', OI)
self.get = self.get.split(' ')
text = [OI, self.get[0], self.get[1]]
except:
print('未知数据标识: ', OI)
traceback.print_exc(file=open('bug.txt', 'a+'))
return None
if OI == '40010200' or OI == '40020200' or OI == '202a0200':
st = ['202a0200/40010200/40020200', '目标服务器地址/通信地址/表号', '']
self.save(st)
print('b_w_stat: ', b_w_stat, 'black_white_SA_address', black_white_SA_address, 'white', white)
if b_w_stat == 2 and black_white_SA_address.find('a') != -1 and black_white_SA_address != 'aaaaaaaaaaaa':
add_aa_2 = int(black_white_SA_address[-2:])
for add in white:
if add.find('-') > 0:
add_range = add.split('-')
start = int(add_range[0])
end = int(add_range[1])
add_list = []
while start <= end:
add_list.append(start)
start += 1
for y in add_list:
if y % 100 == add_aa_2:
trans = str(int(y / 100) * 100 + add_aa_2).zfill(12)
SA_num_len = '05' + Comm.list2str(Comm.makelist(trans)[::-1])
print('compose_data_trans', trans)
self.message = OI + '01' + '0906' + trans
print('message', self.message)
LargeOAD = LargeOAD + self.message
return 0
if Comm.list2str(SA_num_len).find('a') == -1:
trans = Comm.list2str(SA_num_len[1:][::-1])
else:
trans = str(int(text[2][6::])).zfill(12)
print('compose_data_trans', trans)
self.message = OI + '01' + '0906' + trans
print('message', self.message)
LargeOAD = LargeOAD + self.message
return 0
if OI == '40000200':
text = time.strftime('%Y%m%d%H%M%S')
year = hex(int(text[0:4], 10))[2:].zfill(4)
mouth = hex(int(text[4:6], 10))[2:].zfill(2)
day = hex(int(text[6:8], 10))[2:].zfill(2)
hour = hex(int(text[8:10], 10))[2:].zfill(2)
min = hex(int(text[10:12], 10))[2:].zfill(2)
sec = hex(int(text[12:], 10))[2:].zfill(2)
times = '1c' + year + mouth + day + hour + min + sec
st = ['40000200', '(当前)日期时间', '']
self.save(st)
self.message = '40000200' + '01' + times
print('message', self.message)
LargeOAD = LargeOAD + self.message
return 1
elif text[0] == OI:
print('text', text)
self.save(text)
if text[0] == '00100200' and auto_increase == 1:
global start_time
stop_time = int(time.time() - start_time)
OI_B = str(int(text[2][16:24], 16) + stop_time + 4).zfill(8)
OI_C = str(int(text[2][26:34], 16) + stop_time + 3).zfill(8)
OI_D = str(int(text[2][36:44], 16) + stop_time + 2).zfill(8)
OI_E = str(int(text[2][46:54], 16) + stop_time + 1).zfill(8)
OI_A = str(int(text[2][6:14], 16) + stop_time * 2 + 10).zfill(8)
print('start time:', start_time, 'stop time:', stop_time)
print('正向有功递增数值', OI_A)
text[2] = '010506' + OI_A + '06' + OI_B + '06' + OI_C + '06' + OI_D + '06' + OI_E
self.message = text[0] + '01' + text[2]
print('message', self.message, 'text[2]', text[2])
LargeOAD = LargeOAD + self.message
return 1
else:
print('compose_data ERROR')
return 0
def Re_add(self):
global _max, trans
compose = open('source\\698data', 'r', encoding='UTF-8', errors='ignore')
while 1:
text = compose.readline()
text = text.split(' ')
if text[0] == '40010200' or text[0] == '40020200':
compose.close()
te = trans
print('Re_add return', te)
break
return te
def save(self, text):
global OI
OI = OI + text[0:2]
def transport(self):
global OI
return OI
def clear_OI(self):
global OI
OI = []
def composefrozen(self, OI):
global frozenSign, auto_day_frozon_sign, auto_curve_sign, sele, SA_num_len
print("composefrozen OI", OI, "frozenSign", frozenSign)
if frozenSign == 1 and OI[0] != '5':
newOI = '50020200_' + OI
if frozenSign == 2 and OI[0] != '5':
newOI = '50040200_' + OI
if frozenSign == 3 and OI[0] != '5':
newOI = '50060200_' + OI
if frozenSign == 4 and OI[0] != '5':
newOI = '50320200_' + OI