1+ from inputstr_generator import make_input_generators , NameGenerator
2+ from goody import input_int , input_str
3+ from device_sim import write_to_ifile , write_to_peramfile , read_from_peramfile , analyze_data
4+ import pickle
5+ import device_parser
6+
7+ # TODO
8+ # -start setting up that way we can simulate multiple environments and make comparisons
9+ # + make it so that way one can rename a device (maybe do this automatically if there are multiple devices with the same model)
10+ # + start by creating a class to represent a simulation this contains the associated CSV, and device map
11+
12+
13+ MENU_STR = '''MENU:
14+ a: add a device
15+ d: delete a device
16+ p: print the devices you have
17+ r: run the simulation
18+ q: quit
19+ '''
20+
21+ def convert_time (time : int , int_period : int ):
22+ '''converts time in minutes to the time in int_periods which is measured in seconds
23+ ie 5 minutes = 60 int_periods when int_period = 5s'''
24+
25+ return 60 / int_period * time
26+
27+ def input_device_model (devices_data : {dict }, p_string : str )-> list :
28+ if type (devices_data ) == set :
29+ z = zip (range (1 , len (devices_data )+ 1 ), devices_data )
30+ str_range = set (str (x ) for x in range (1 , len (devices_data )+ 1 ))
31+ inp = input_str ('Which type of {} device do you want to choose? {}: ' .format (p_string ,
32+ sorted (z )), valid = devices_data .union (str_range ))
33+ input_dict = dict (zip (range (1 , len (devices_data )+ 1 ), devices_data ))
34+
35+ if not inp in devices_data :
36+ inp = input_dict [int (inp )]
37+
38+ print ('selected: {}' .format (inp ))
39+ return [inp ]
40+ else :
41+ keys_set = set (devices_data .keys ())
42+ z = zip (range (1 , len (keys_set )+ 1 ), keys_set )
43+ str_range = set (str (x ) for x in range (1 , len (devices_data )+ 1 ))
44+ inp = input_str ('Which type of {} device do you want to choose? {}: ' .format (p_string ,
45+ sorted (z )), valid = keys_set .union (str_range ))
46+
47+ input_dict = dict (zip (range (1 , len (keys_set )+ 1 ), keys_set ))
48+
49+ if not inp in keys_set :
50+ inp = input_dict [int (inp )]
51+
52+ p_string += '{}:' .format (inp )
53+ to_return = [inp ]
54+ print ('selected: {}' .format (inp ))
55+ to_return .extend (input_device_model (devices_data [inp ], p_string ))
56+
57+ return to_return
58+
59+
60+ def input_at_interval (ig_list : ['InputGenerator' ], time_interval : int ):
61+ '''helper function for running the simulation'''
62+ for inp_gen in ig_list :
63+ inp = input_str ('Are you using the {} [yes/no]: ' .format (inp_gen .dev_name ), {'yes' , 'y' , 'no' , 'n' })
64+ if inp .lower () in ['yes' , 'y' ]:
65+ rlen = range (1 , len (inp_gen .states ())+ 1 )
66+ str_rlen = set (str (x ) for x in rlen )
67+ state = input_str ('Which of the following states is it in {}: ' .format (
68+ sorted (zip (rlen , inp_gen .states ()))), valid = inp_gen .states ().union (str_rlen ))
69+
70+ input_dict = dict (zip (rlen , inp_gen .states ()))
71+ if not state in inp_gen .states ():
72+ state = input_dict [int (state )]
73+
74+ inp_gen .write_on_state (state , time_interval )
75+ elif inp .lower () in ['no' , 'n' ]:
76+ inp_gen .write_on_state ('off' , time_interval )
77+
78+ def run_sim (integration_period : int , input_generators : list ):
79+ '''runs the simulation that creates the input csv'''
80+ print ('\n Input the start states:' )
81+ input_at_interval (input_generators , 1 )
82+ while True :
83+ print ()
84+ time_interval = input_int ('How long is this time interval (in minutes)[enter 0 to end the simulation]: ' )
85+ if time_interval == 0 :
86+ break
87+
88+ num_periods_interval = int (convert_time (time_interval , integration_period ))
89+ input_at_interval (input_generators , num_periods_interval )
90+
91+ def main ():
92+ name_gen = NameGenerator ()
93+ device_map = {}
94+ tree = device_parser .parse_data ('xmls/PLSim2Format.xml' )
95+ devices_data = device_parser .parse_groupings (tree )
96+
97+ while True :
98+ inp = input_str (MENU_STR , valid = {'a' , 'p' , 'r' , 'q' , 'd' ,'g' })
99+ print ()
100+ if inp == 'a' :
101+ dev_key = input_device_model (devices_data , '' )
102+ key ,value = device_parser .search_data (tree , dev_key )
103+ device_map [name_gen .generate_name (key )] = value
104+ if inp == 'd' :
105+ valid = set (device_map .keys ())
106+ if len (valid ) > 0 :
107+ to_delete = input_str ('Which device do you want to delete? {}: ' .format (valid ), valid )
108+ del device_map [to_delete ]
109+ else :
110+ print ('There are no devices to delete\n ' )
111+ if inp == 'p' :
112+ print (set (device_map .keys ()))
113+ if inp == 'r' :
114+ input_generators = make_input_generators (device_map )
115+ integration_period = input_int ('Enter integration period: ' )
116+ run_sim (integration_period , input_generators )
117+
118+ #Pickling integration_period
119+ integ_periods_file = open ('integ_periods' ,'wb' )
120+ pickle .dump (integration_period , integ_periods_file )
121+ integ_periods_file .close ()
122+
123+ #Pickling device_map
124+ devicemp_file = open ('devicemp' ,'wb' )
125+ pickle .dump (device_map , devicemp_file )
126+ devicemp_file .close ()
127+
128+ write_to_ifile ('csvs/test_group.csv' , integration_period , input_generators )
129+
130+ write_to_peramfile ('csvs/run_perams.cfg' , integration_period , device_map )
131+
132+ print ("CSV File Input Gen:" )
133+ print (input_generators )
134+ print ("Integration Period:" )
135+ print (integration_period )
136+ print ("Device Map:" )
137+ print (device_map )
138+
139+ print ()
140+
141+ if inp == 'q' :
142+ return
143+
144+ main ()
0 commit comments