-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcip_eds_types.py
923 lines (717 loc) · 27.7 KB
/
cip_eds_types.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
"""
MIT License
Copyright (c) 2021 Omid Kompani
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from collections import namedtuple
from calendar import monthrange
from string import digits
from datetime import datetime, timedelta
import inspect
from collections import namedtuple
RANGE = namedtuple('RANGE', 'min max')
import logging
logging.basicConfig(level=logging.WARNING,
format='%(asctime)s - %(name)s.%(levelname)-8s %(message)s')
logger = logging.getLogger(__name__)
class ENUMS(object):
def stringify(self, enum):
for attr in vars(self.__class__):
if isinstance(self.__class__.__dict__[attr], int) and self.__class__.__dict__[attr] == enum: return '{}'.format(attr)
for base in self.__class__.__bases__:
for attr in vars(base):
if isinstance(base.__dict__[attr], int) and base.__dict__[attr] == enum: return '{}'.format(attr)
return ''
@classmethod
def stringify(cls, enum):
for attr in vars(cls):
if isinstance(cls.__dict__[attr], int) and cls.__dict__[attr] == enum: return '{}'.format(attr)
for base in cls.__bases__:
for attr in vars(base):
if isinstance(base.__dict__[attr], int) and base.__dict__[attr] == enum: return '{}'.format(attr)
return ''
class CIP_STD_TYPES(ENUMS):
CIP_EDS_UTIME = 0xC0
CIP_EDS_BOOL = 0xC1
CIP_EDS_SINT = 0xC2
CIP_EDS_INT = 0xC3
CIP_EDS_DINT = 0xC4
CIP_EDS_LINT = 0xC5
CIP_EDS_USINT = 0xC6
CIP_EDS_UINT = 0xC7
CIP_EDS_UDINT = 0xC8
CIP_EDS_ULINT = 0xC9
CIP_EDS_REAL = 0xCA
CIP_EDS_LREAL = 0xCB
CIP_EDS_STIME = 0xCC
CIP_EDS_DATE = 0xCD
CIP_EDS_TIME_OF_DAY = 0xCE
CIP_EDS_DATE_AND_TIME = 0xCF
CIP_EDS_STRING = 0xD0
CIP_EDS_BYTE = 0xD1
CIP_EDS_WORD = 0xD2
CIP_EDS_DWORD = 0xD3
CIP_EDS_LWORD = 0xD4
CIP_EDS_STRING2 = 0xD5
CIP_EDS_FTIME = 0xD6
CIP_EDS_LTIME = 0xD7
CIP_EDS_ITIME = 0xD8
CIP_EDS_STRINGN = 0xD9
CIP_EDS_SHORT_STRING = 0xDA
CIP_EDS_TIME = 0xDB
CIP_EDS_EPATH = 0xDC
CIP_EDS_ENGUNIT = 0xDD
CIP_EDS_STRINGI = 0xDE
CIP_EDS_NTIME = 0xDF
def getnumber(data):
'''
Converts an input of string type into its numeric representaion.
'''
if data is None: return None
if data == '': return None
if isint(data): return int(data)
if isfloat(data): return float(data)
if ishex(data): return int(data, 16)
if isbin(data): return int(data, 2)
return None
def isnumber(data):
'''
Checks if a string represents a numeric value.
'''
if data is None: return False
if data == '': return False
if isint(data): return True
if isfloat(data): return True
if ishex(data): return True
if isbin(data): return True
return False
def isint(data):
'''
Checks if a string represents a decimal coded numeric value.
'''
if data is None: return False
try:
int(data)
except ValueError:
return False
return True
def isfloat(data):
'''
Checks if a string represents a floating point numeric value.
'''
if data is None: return False
try:
float(data)
except ValueError:
return False
return True
def ishex(data):
'''
Checks if a string represents a hexadecimal coded numeric value.
'''
if data is None: return False
try:
int(data, 16)
except ValueError:
return False
return True
def isbin(data):
'''
Checks if a string represents a binary coded numeric value.
'''
if data is None: return False
try:
int(data, 2)
except ValueError:
return False
return True
def isdate(data):
if data is None:
return False
try:
m, d, y = data.split('-')
if len(m) != 2 or len(d) != 2 or int(m) < 1 or int(m) > 12:
logger.error('Invalid EDS_DATE month length or month value!')
return False
if len(y) == 4:
if int(y) < 1994:
logger.error('Invalid EDS_DATE yyyy value!')
return False
elif len(y) == 2:
if int(y) < 94:
logger.error('Invalid EDS_DATE yy value!')
return False
else:
logger.error('Invalid EDS_DATE year format!')
return False
if int(d) < 1 or (int(d) > (monthrange(int(y), int(m))[1]) ):
logger.error('Invalid EDS_DATE day value!')
return False
except:
return False
return True
def getdate():
return datetime.strftime(datetime.now(), "%m-%d-%Y")
def cast2date(val):
'''
Converts a 16-bit value to a valid DATE string between 01.01.1972 and 06.06.2151
'''
return datetime.strftime(datetime.strptime('01-01-1972', "%m-%d-%Y") + timedelta(days=val), "%m-%d-%Y")
def istime(data):
if data is None: return False
data = data.split(':')
if len(data) != 3:
return False
hh = data[0]
mm = data[1]
ss = data[2]
if len(mm) != 2 or len(hh) != 2 or len(ss) != 2:
return False
if int(hh) > 24 or int(hh) < 0:
return False
if int(mm) > 60 or int(mm) < 0:
return False
if int(ss) > 60 or int(ss) < 0:
return False
return True
def gettime():
hh = format(datetime.now().hour, '02')
mm = format(datetime.now().minute, '02')
ss = format(datetime.now().second, '02')
return "%s:%s:%s" %(hh, mm, ss)
class CIP_EDS_BASE_TYPE(object):
_typeid = None
_range = []
def __init__(self, value, *args):
self._value = value
#sele._range = *args
@property
def range(self):
return self._range
@property
def value(self):
return self._value
@classmethod
def validate(cls, value, *args):
raise(NotImplementedError)
def __repr__(self):
return "{}({})".format(self.__class__.__name__, self.__value)
def __str__(self):
return '{}'.format(self._value)
class CIP_EDS_BASE_INT(CIP_EDS_BASE_TYPE):
def __init__(self, value, *args):
self._value = value
#sele._range = *args
@classmethod
def validate(cls, value, *args):
value = getnumber(value)
return value is not None and value >= cls._range.min and value <= cls._range.max
def __format__(self, format_spec):
return format(self._value, format_spec)
def __hex__(self):
return '0x{:X}'.format(self._value)
def __eq__( self , other):
return self._value == other
def __ne__( self , other):
return self._value != other
def __lt__( self , other):
return self._value < other
def __gt__( self , other):
return self._value > other
def __le__( self , other):
return self._value <= other
def __ge__( self , other):
return self._value <= other
def __add__(self, other):
return self._value + other
def __sub__(self, other):
return self._value - other
def __mul__(self, other):
return self._value * other
def __truediv__(self, other):
return self._value / other
def __floordiv__(self, other):
return self._value // other
def __int__(self):
return self._value
def __index__(self):
return self.__int__()
def __len__(self):
return self._size
class BOOL(CIP_EDS_BASE_TYPE):
_typeid = CIP_STD_TYPES.CIP_EDS_BOOL
_range = RANGE(0, 1)
def __new__(cls, value, *args):
if cls.validate(value):
return super(BOOL, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(BOOL, self).__init__(value)
def __str__(self):
return str(self._value != 0)
class USINT(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_USINT
_range = RANGE(0, 255)
def __new__(cls, value, *args):
if cls.validate(value):
return super(USINT, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(USINT, self).__init__(value)
class SINT(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_USINT
_range = RANGE(0, 255)
def __new__(cls, value, *args):
if cls.validate(value):
return super(SINT, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(SINT, self).__init__(value)
class UINT(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_UINT
_range = RANGE(0, 65535)
def __new__(cls, value, *args):
if cls.validate(value):
return super(UINT, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(UINT, self).__init__(value)
class INT(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_INT
_range = RANGE(-32768, 32767)
def __new__(cls, value, *args):
if cls.validate(value):
return super(INT, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(INT, self).__init__(value)
class UDINT(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_UDINT
_range = RANGE(0, 4294967295)
def __new__(cls, value, *args):
if cls.validate(value):
return super(UDINT, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(UDINT, self).__init__(value)
class DINT(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_DINT
_range = RANGE(-2147483648, 2147483647)
def __new__(cls, value, *args):
if cls.validate(value):
return super(DINT, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(DINT, self).__init__(value)
class ULINT(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_ULINT
_range = RANGE(0, 18446744073709551615)
def __new__(cls, value, *args):
if cls.validate(value):
return super(ULINT, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(ULINT, self).__init__(value)
class LINT(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_LINT
_range = RANGE(-9223372036854775808, 9223372036854775807)
def __new__(cls, value, *args):
if cls.validate(value):
return super(LINT, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(LINT, self).__init__(value)
class BYTE(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_BYTE
_range = RANGE(0, 255)
def __new__(cls, value, *args):
if cls.validate(value):
return super(BYTE, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(BYTE, self).__init__(value)
class WORD(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_WORD
_range = RANGE(0, 65535)
def __new__(cls, value, *args):
if cls.validate(value):
return super(WORD, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(WORD, self).__init__(value)
class DWORD(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_DWORD
_range = RANGE(0, 4294967295)
def __new__(cls, value, *args):
if cls.validate(value):
return super(DWORD, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(DWORD, self).__init__(value)
class LWORD(CIP_EDS_BASE_INT):
_typeid = CIP_STD_TYPES.CIP_EDS_LWORD
_range = RANGE(0, 18446744073709551615)
def __new__(cls, value, *args):
if cls.validate(value):
return super(LWORD, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(LWORD, self).__init__(value)
class REAL(CIP_EDS_BASE_INT): # TODO: improve validate
_typeid = CIP_STD_TYPES.CIP_EDS_REAL
_range = RANGE(-16777216.0, 16777216.0)
def __new__(cls, value, *args):
if cls.validate(value):
return super(REAL, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(REAL, self).__init__(value)
class LREAL(CIP_EDS_BASE_INT): # TODO: improve validate
_typeid = CIP_STD_TYPES.CIP_EDS_LREAL
_range = RANGE(-9007199254740992.0, 9007199254740992.0)
def __new__(cls, value, *args):
if cls.validate(value):
return super(LREAL, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(LREAL, self).__init__(value)
class STIME(CIP_EDS_BASE_TYPE): # dummy type! TODO
_typeid = CIP_STD_TYPES.CIP_EDS_STIME
def __new__(cls, value, *args):
if cls.validate(value):
return super(STIME, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(STIME, self).__init__(value)
class STRING(CIP_EDS_BASE_TYPE):
_typeid = CIP_STD_TYPES.CIP_EDS_STRING
def __new__(cls, value, *args):
if cls.validate(value):
return super(STRING, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(STRING, self).__init__(value)
@classmethod
def validate(cls, value, *args):
return isinstance(value, str)
def __str__(self):
return '\n'.join('\"{}\"'.format(self.value[offset : offset + 60])
for offset in range(0, len(self.value), 60))
class STRINGI(CIP_EDS_BASE_TYPE):
_typeid = CIP_STD_TYPES.CIP_EDS_STRINGI
def __new__(cls, value, *args):
if cls.validate(value):
return super(STRINGI, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
self._range = ['mm-dd-yyyy']
super(STRINGI, self).__init__(value)
@classmethod
def validate(cls, value, *args):
# TODO
pass
def __str__(self):
return 'STRINGI...' # TODO
class DATE(CIP_EDS_BASE_TYPE):
# EDS_DATE mm.dd.yyyy from 1994 to 9999
_range = ['mm.dd.yyyy', 'mm.dd.yy']
def __new__(cls, value, *args):
if cls.validate(value):
return super(DATE, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(DATE, self).__init__(value)
@staticmethod
def validate(value, *args):
return isdate(value)
class TIME(CIP_EDS_BASE_TYPE):
_typeid = CIP_STD_TYPES.CIP_EDS_TIME
def __new__(cls, value, *args):
if cls.validate(value):
return super(TIME, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
self.__value = value
super(TIME, self).__init__(value)
@property
def range(self):
return ['HH:MM:SS'] # TODO
@staticmethod
def validate(value, *args):
try:
data = value.split(':')
except:
return False
if len(data) != 3:
return False
hh = data[0]
mm = data[1]
ss = data[2]
# Tolerate no leading zeros
#if len(mm) < 2 or len(hh) < 2 or len(ss) < 2:
# return False
if ((len(hh) < 1 or len(hh) > 2) or
(len(mm) < 1 or len(mm) > 2) or
(len(ss) < 1 or len(ss) > 2)):
return False
if int(hh) > 24 or int(hh) < 0:
return False
if int(mm) > 60 or int(mm) < 0:
return False
if int(ss) > 60 or int(ss) < 0:
return False
return True
class EPATH(CIP_EDS_BASE_TYPE):
_typeid = CIP_STD_TYPES.CIP_EDS_EPATH
def __new__(cls, value, *args):
if cls.validate(value):
return super(EPATH, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{} (CIP typeID: 0x{:X})> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(EPATH, self).__init__(value)
@staticmethod
def validate(value, *args):
try:
elements = value.split()
except:
return False
for element in elements:
if len(element) < 2:
return False
if not isnumber(element):
if (element[0] == '[' and element[-1] == ']'): #TODO: accept references without brackets
continue
return False
elif not ishex(element):
return False
return True
def __str__(self):
return "\"{}\"".format(self.value)
class REVISION(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value):
return super(REVISION, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{}> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(REVISION, self).__init__(value)
@staticmethod
def validate(value, *args):
try:
elements = value.split('.')
except:
return False
if len(elements) != 2:
return False
for element in elements:
if not isnumber(element):
return False
return True
class ETH_MAC_ADDR(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value):
return super(ETH_MAC_ADDR, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{}> data type."
.format(cls.__name__, cls._typeid))
def __init__(self, value, *args):
super(ETH_MAC_ADDR, self).__init__(value)
@staticmethod
def validate(value, *args):
macaddr = value.rstrip('}').lstrip('{').strip().replace(':', '-').replace('.', '-').split('-')
if len(macaddr) != 6:
return False
for field in macaddr:
if USINT.validate(field) == False:
return False
return True
class REF(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value, *args):
return super(REF, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{}> data type."
.format(cls.__name__))
def __init__(self, value, *args):
self._range = args[0] # TODO
super(REF, self).__init__(value)
@staticmethod
def validate(value, *args):
if not isinstance(value, str):
return False
for keyword in args[0]:
keyword = keyword.rstrip('N').lower()
if value[:len(keyword)].lower() == keyword:
return True
return False
class KEYWORD(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value, *args):
return super(KEYWORD, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} ".format(arg[0])
+ "for <{}> data type."
.format(cls.__name__))
def __init__(self, value, *args):
self._range = args[0]
super(KEYWORD, self).__init__(value)
@staticmethod
def validate(value, *args):
for keyword in args[0]:
if value.lower() == keyword.lower():
return True
return False
class DATATYPE_REF(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value):
return super(DATATYPE_REF, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{}> data type."
.format(cls.__name__))
def __init__(self, value, *args):
super(DATATYPE_REF, self).__init__(value)
@staticmethod
def validate(value, *args):
return True # TODO
class EDS_SERVICE(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value):
return super(EDS_SERVICE, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{}> data type."
.format(cls.__name__))
def __init__(self, value, *args):
super(EDS_SERVICE, self).__init__(value)
@staticmethod
def validate(value, *args):
return True # TODO
class EMPTY(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value):
return super(EMPTY, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{}> data type."
.format(cls.__name__))
def __init__(self, value, *args):
super(EMPTY, self).__init__(value)
@staticmethod
def validate(value, *args):
if args is None or value == '':
return True
return False
def __str__(self):
return ''
class VENDOR_SPECIFIC(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value):
return super(VENDOR_SPECIFIC, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{}> data type."
.format(cls.__name__))
def __init__(self, value, *args):
super(VENDOR_SPECIFIC, self).__init__(value)
@staticmethod
def validate(value, *args):
if isinstance(value, str) and value != '': #TODO
if value[0].isdigit():
return True
return False
class UNDEFINED(CIP_EDS_BASE_TYPE):
def __new__(cls, value, *args):
if cls.validate(value):
return super(UNDEFINED, cls).__new__(cls)
else:
raise Exception(__name__ + ":> Invalid value: {} for ".format(value)
+ "<{}> data type."
.format(cls.__name__))
def __init__(self, value, *args):
self.__value = value
super(UNDEFINED, self).__init__(value)
@staticmethod
def validate(value, *args):
return True