@@ -61,29 +61,33 @@ class Device:
61
61
}
62
62
"""
63
63
64
- def __init__ (self , device_id , rack_params ):
65
-
66
- json_node = find_device (device_id )
67
-
68
- self .id = json_node ['ID' ]
69
- # self.name = json_node['Name']
70
- self .name = json_node ['Hardware' ]
71
- # self.category = json_node['Category']
72
- self .category = json_node ['Type' ]
73
- self .width = get_length_from_str (json_node ['LengthMm' ])
74
- self .depth = get_length_from_str (json_node ['Depth' ])
75
- self .height = get_length_from_str (json_node ['Height' ])
64
+ def __init__ (self , device_id , rack_params , dummy = False , dummy_data :dict | None = None ):
65
+
66
+ self .dummy = dummy
67
+ if not dummy :
68
+ device_dict = find_device (device_id )
69
+ else :
70
+ device_dict = generate_dummy_device_dict (device_id , dummy_data )
71
+
72
+ self .id = device_dict ['ID' ]
73
+ # self.name = device_dict['Name']
74
+ self .name = device_dict ['Hardware' ]
75
+ # self.category = device_dict['Category']
76
+ self .category = device_dict ['Type' ]
77
+ self .width = get_length_from_str (device_dict ['LengthMm' ])
78
+ self .depth = get_length_from_str (device_dict ['Depth' ])
79
+ self .height = get_length_from_str (device_dict ['Height' ])
76
80
77
81
try :
78
- self .height_in_u = int (json_node ['HeightUnits' ])
82
+ self .height_in_u = int (device_dict ['HeightUnits' ])
79
83
except ValueError as exc :
80
84
if self .height :
81
85
self .height_in_u = ceil ((self .height + 4 )/ rack_params .mounting_hole_spacing )
82
86
else :
83
87
raise RuntimeError ("Not enough information provided to generate shelf height" ) from exc
84
88
85
- self .shelf_id = json_node ['ShelfId' ]
86
- self .shelf_type = json_node ['Shelf' ]
89
+ self .shelf_id = device_dict ['ShelfId' ]
90
+ self .shelf_type = device_dict ['Shelf' ]
87
91
88
92
@property
89
93
def shelf_key (self ):
@@ -123,3 +127,35 @@ def get_length_from_str(length):
123
127
if match := re .match (r'^([0-9]+(?:\.[0-9]+)?) ?mm$' , length ):
124
128
return float (match [1 ])
125
129
return None
130
+
131
+ def generate_dummy_device_dict (device_id :str , dummy_data :dict | None = None ):
132
+ """
133
+ Create a dummy database record from a device id. If id is in the pattern
134
+ `dummy-<type>-<h>u` then the shelf type and height in u are set from this.
135
+ All device dictionary items that are used in the Device class can be set
136
+ by entering the desired key and value in the dummy_data parameter
137
+ """
138
+ if dummy_data is None :
139
+ dummy_data = {}
140
+
141
+ device_dict = {'ID' : device_id }
142
+
143
+ if match := re .match (r'^dummy-(.+)-([0-9])+u$' , device_id ):
144
+ default_shelf_id = match [1 ]+ '-6'
145
+ default_height_in_u = match [2 ]
146
+ else :
147
+ print (f"dummy device string `{ device_id } ` doesn't match pattern "
148
+ "`dummy-<type>-<h>u` shelf type and height in u may not have "
149
+ "been set as intended" )
150
+ default_shelf_id = "generic"
151
+ default_height_in_u = "2"
152
+
153
+ device_dict ['Hardware' ] = dummy_data .get ('Hardware' , "Dummy" )
154
+ device_dict ['Type' ] = dummy_data .get ('Type' , "Dummy" )
155
+ device_dict ['LengthMm' ] = dummy_data .get ('LengthMm' , "100mm" )
156
+ device_dict ['Depth' ] = dummy_data .get ('Depth' , "100mm" )
157
+ device_dict ['Height' ] = dummy_data .get ('Height' , "30mm" )
158
+ device_dict ['Shelf' ] = dummy_data .get ('Shelf' , "Dummy" )
159
+ device_dict ['HeightUnits' ] = dummy_data .get ('HeightUnits' , default_height_in_u )
160
+ device_dict ['ShelfId' ] = dummy_data .get ('ShelfId' , default_shelf_id )
161
+ return device_dict
0 commit comments