def update(self, playboard):
		
		if not self.staff_randomized:
			for staff in playboard.model.staff:
				x, y = playboard.get_random_open_tile()
				staff.x = x * 32 + 16
				staff.y = y * 32 + 16
			self.staff_randomized = True
		
		event = self.get_events_for_frame()
		
		if event != None:
			type = event[0]
			if type == 'device':
				device_type = event[2]
				ailment = event[3]
				device = Device(playboard, self.counter, device_type, 200, 10, ailment)
				self.devices.append(device)
				self.active_devices.append(device)
		
		
		new_active_devices = []
		for device in self.active_devices:
			device.update()
			if device.state == 'new':
				playboard.animations.append({
					'type': 'device',
					'ttl': 10,
					'mx': device.x,
					'my': device.y,
					'x': device.x,
					'y': device.y,
					'device': device.device_type,
					'overlay': 'check',
					'vx': 0,
					'vy': -3
				})
			elif device.state == 'dead' and device.replaced:
				playboard.animations.append({
					'type': 'device',
					'ttl': 10,
					'mx': device.x,
					'my': device.y,
					'x': device.x,
					'y': device.y,
					'device': device.device_type,
					'overlay': 'dead',
					'vx': 0,
					'vy': -3
				})
				
			else:
				new_active_devices.append(device)
		self.active_devices = new_active_devices
		
		self.counter += 1
 def testGetDistanceBetweenDevices(self):
     section = LinacAbstractSection("test", None, None, 600)
     self.assertEqual(section.numberOfDevices(), 0)
     self.assertEqual(section.getDistanceBetweenDevices(), 600)
     dev1 = Device("dev1", None, None)
     section.addDevice(dev1)
     self.assertEqual(section.getDistanceBetweenDevices(), 300)
     dev2 = Device("dev2", None, None)
     section.addDevice(dev2)
     self.assertEqual(section.getDistanceBetweenDevices(), 200)
Exemple #3
0
 def testAddDevice(self):
     device = Device("abcde", None, None)
     section = Section("test", "grey")
     self.assertEqual(section.numberOfDevices(), 0)
     section.addDevice(device)
     self.assertEqual(section.numberOfDevices(), 1)
     self.assertEqual(section.devices[0], device)
     self.assertEqual(device.numberInSection, 0)
     otherDevice = Device("other", None, None)
     section.addDevice(otherDevice)
     self.assertEqual(otherDevice.numberInSection, 1)
Exemple #4
0
    def testSubsystemName(self):
        device = Device("I-TL/VAC/I-TL-CAB02-VAC-IPCU1", None, None)
        self.assertEqual(device.subsystemName, "VAC")

        device = Device("I-S00/MAG/I-S00-MAG-COBX2", None, None)
        self.assertEqual(device.subsystemName, "MAG")

        device = Device("I-K01/RF/I-K01CAB03-RF-LLRFL1", None, None)
        self.assertEqual(device.subsystemName, "RF")

        device = Device("I-S00/DIA/I-S00-DIA-SCRN1", None, None)
        self.assertEqual(device.subsystemName, "DIA")
Exemple #5
0
 def testGetDevice(self):
     dev1 = Device("foo", None, None)
     dev2 = Device("bar", None, None)
     section = Section("test", "grey")
     self.assertEqual(section.numberOfDevices(), 0)
     section.addDevice(dev1)
     section.addDevice(dev2)
     self.assertEqual(section.numberOfDevices(), 2)
     self.assertEqual(section.getDevice(0), dev1)
     self.assertNotEqual(section.getDevice(0), dev2)
     self.assertEqual(section.getDevice(1), dev2)
     self.assertEqual(section.getDevice(3), None)
    def testAssignDevicesBeforeDrawing(self):
        section = self.linac.addSection("I-K01", None, 200)
        subsection = section.addSubsection("I-K01A", None, 50)
        firstDevice = Device("I-K01/ab/cd", None, [12, 20])
        secondDevice = Device("I-K01A/ef/gh", None, [5, 20])
        thirdDevice = Device("I-K01A/ij/kl", None, [15, 20])
        self.linac.addDevice(firstDevice)
        self.linac.addDevice(secondDevice)
        self.linac.addDevice(thirdDevice)

        self.assertEqual(firstDevice.section, section)
        self.linac.assignDevicesBeforeDrawing()
        self.assertEqual(firstDevice.section, subsection)
 def testAddDevice(self):
     device = Device("ABC/MAG/device123", None, None)
     anotherDevice = Device("ABC/VAC/dev7", None, None)
     self.linac.addSection("ABC")
     section = self.linac.getSection("ABC")
     self.assertEqual(section.numberOfDevices(), 0)
     self.assertEqual(section.getDevice(0), None)
     self.linac.addDevice(device)
     self.assertEqual(section.getDevice(0), device)
     self.assertEqual(section.getDevice(1), None)
     self.linac.addDevice(anotherDevice)
     self.assertEqual(section.getDevice(1), anotherDevice)
     self.assertEqual(section.numberOfDevices(), 2)
Exemple #8
0
    def testAssigningSvgCoordinate(self):
        device = Device("abc/testDevice", None, None)
        self.assertEqual(device.svgCoordinateX, None)

        linac = Linac()
        linac.addSection("abc")
        linac.addDevice(device)
        device.assignSvgCoordinatesForLinacDevice()
        testSection = linac.getSection("abc")
        sectionStartCoordinate = testSection.startCoordinate
        distanceBetweenDevices = testSection.getDistanceBetweenDevices()

        self.assertEqual(device.svgCoordinateX,
                         sectionStartCoordinate + distanceBetweenDevices)
Exemple #9
0
    def testSortDevices(self):
        firstDevice = Device("First", None, [184, 99])
        secondDevice = Device("Second", None, [964, 80])
        section = Section("test", None)

        section.addDevice(secondDevice)
        section.addDevice(firstDevice)

        self.assertEqual(section.getDevice(0), secondDevice)
        self.assertEqual(section.getDevice(0).numberInSection, 0)
        self.assertEqual(section.getDevice(1).numberInSection, 1)
        section.sortDevices()
        self.assertEqual(section.getDevice(0), firstDevice)
        self.assertEqual(section.getDevice(0).numberInSection, 0)
        self.assertEqual(section.getDevice(1).numberInSection, 1)
Exemple #10
0
 def testConfiguration(self):
     coord = (12, 13.901)
     icon = Icon("test/path")
     device = Device("name", icon, coord)
     self.assertEqual(device.icon, icon)
     self.assertEqual(device.realCoordinates, coord)
     self.assertEqual(device.name, "name")
    def testGetAllDevicesSorted(self):
        section = self.linac.addSection("A", None, 100)
        subsection = section.addSubsection("A-1", None, 20)

        firstDevice = Device("first", None, [301, 222])
        secondDevice = Device("second", None, [154, 222])
        thirdDevice = Device("third", None, [101, 222])
        fourthDevice = Device("fourth", None, [55, 30])
        subsection.addDevice(firstDevice)
        subsection.addDevice(secondDevice)
        section.addDevice(thirdDevice)
        section.addDevice(fourthDevice)

        devices = self.linac.getAllDevicesSorted()
        self.assertEqual(
            devices, [fourthDevice, thirdDevice, secondDevice, firstDevice])
        self.assertEqual(devices[0].numberInSection, 0)
        self.assertEqual(devices[2].numberInSection, 0)
        self.assertEqual(devices[3].numberInSection, 1)
Exemple #12
0
    def testSortDevicesRecursively(self):
        section = LinacSection("A", None, 100)
        subsection = section.addSubsection("A01", None, 50)
        firstDevice = Device("first", None, [301,222])
        secondDevice = Device("second", None, [154,222])
        thirdDevice = Device("third", None, [101,222])
        fourthDevice = Device("fourth", None, [55, 30])
        subsection.addDevice(firstDevice)
        subsection.addDevice(secondDevice)
        section.addDevice(thirdDevice)
        section.addDevice(fourthDevice)

        devices = section.getAllDevices()
        self.assertEqual(devices[0], thirdDevice)
        self.assertEqual(devices[2], firstDevice)

        section.sortDevicesRecursively()
        devices = section.getAllDevices()
        self.assertEqual(devices[0], fourthDevice)
        self.assertEqual(devices[2], secondDevice)
    def __init__(self, config_file="config.yaml", device_init=True):
        self.FILE_FOLDER = "./files/"
        self.SRC_FOLDER = "./src/"

        with open(self.SRC_FOLDER+config_file, "r") as fp:
            self.config = yaml.safe_load(fp)

        self.test_config = self.config["test"]
        self.test_file = self.test_config["test_script"]
        self.input_points_header = self.test_config.get("input_points_header", "Simulation (controller) Inputs")
        self.conditions_header = self.test_config.get("conditions_header", "Result Time")
        self.output_points_header = self.test_config.get("output_points_header", "Expected Controller BACnet Outputs")

        if device_init:
            self.controller = Device(device_config=self.config["device"])

            self.map_file = self.test_config["point_map"]
            self.init_device(mapping_file=self.map_file)

            self.init_test_sequence(filename=self.test_file, ip_header=self.input_points_header, cond_header=self.conditions_header, op_header=self.output_points_header, point_prop=self.point_properties)
class TestDevice(unittest.TestCase):
    def setUp(self):
        self.device = Device()
        self.device2 = Device("Jumpy Whale")

    def test_check_initial(self):
        for attrib in [
                "device_name", "device_uuid", "polling_rate", "sensor_manager"
        ]:
            self.assertTrue(hasattr(self.device, attrib))

    def test_polling_bad_Set(self):
        with self.assertRaises(Exception):
            self.device.polling_rate = "25"

    def test_polling_good_Set(self):
        new_value = 10
        self.device.polling_rate = new_value
        self.assertTrue(self.device.polling_rate == new_value)

    def test_name_bad_Set(self):
        with self.assertRaises(Exception):
            self.device.device_name = "super cool name"

    def test_name_good_Set(self):
        new_name = "sleepy dog"
        self.device.device_name = new_name
        self.assertTrue(self.device.device_name == new_name)

    def test_uuid_bad_Set(self):
        with self.assertRaises(Exception):
            new_uuid = 1234
            self.device.device_uuid = new_uuid

    def test_uuid_good_Set(self):
        new_val = uuid.uuid4()
        self.device.device_uuid = new_val
        self.assertTrue(self.device.device_uuid == new_val)

    def test_payload_contents(self):
        sample_payload = self.device.generate_payload()
        for attrib in [
                "Device_Name",
                "Device_UUID",
                "GPS",
                "ambient_temperature",
                "internal_temperature",
                "Latch",
                "timestamp",
        ]:
            self.assertTrue(attrib in sample_payload)
Exemple #15
0
    def testDrawDevice(self):
        icon = Icon("symbol-quadrupole.svg")
        device = Device("I-K01/VAC/I-K01CAB05-VAC-IPCU1", icon, (113, 122))
        linac = Linac()
        linac.addSection("I-K01")
        linac.addDevice(device)

        blankSVGpath = 'blank.svg'
        svgTree = etree.parse(blankSVGpath)
        svgRoot = svgTree.getroot()
        svgFile = svg.SVG()
        svgFile.setSvg(svgRoot)

        device.updateSvg()
        vacNode = svgFile.getSubsystemZoomNode("VAC")
        deviceNode = svgFile.getElementById("ik01vacik01cab05vacipcu1",
                                            vacNode)
        self.assertEqual(
            deviceNode.attrib["{http://www.w3.org/1999/xlink}href"],
            "#symbol-quadrupole")
        descriptionNode = svgFile.getElementById(
            "ik01vacik01cab05vacipcu1Desc", deviceNode)
        self.assertEqual(descriptionNode.text,
                         "device=I-K01/VAC/I-K01CAB05-VAC-IPCU1")
Exemple #16
0
    def testGetAllDevices(self):
        section = LinacSection("I-K00", None, 0)

        firstDevice = Device("test1", None, None)
        secondDevice = Device("test2", None, None)
        thirdDevice = Device("test3", None, None)
        fourthDevice = Device("test4", None, None)

        section.addSubsection("I-K01A", None, 100)
        section.addSubsection("I-K01B", None, 100)
        firstSubsection = section.getSubsection("I-K01A")
        secondSubsection = section.getSubsection("I-K01B")

        firstSubsection.addDevice(firstDevice)
        section.addDevice(secondDevice)
        secondSubsection.addDevice(thirdDevice)
        firstSubsection.addDevice(fourthDevice)

        allDevices = section.getAllDevices()
        self.assertTrue(firstDevice in allDevices)
        self.assertTrue(secondDevice in allDevices)
        self.assertTrue(thirdDevice in allDevices)
        self.assertTrue(fourthDevice in allDevices)
        self.assertEqual(len(allDevices), 4)
Exemple #17
0
    def testCheckingConditions(self):
        deviceWithoutIcon = Device("A/B/C", None, None)
        deviceWithoutIcon.section = True
        with self.assertRaises(Exception):
            deviceWithoutIcon.checkNecessaryConditions()

        deviceWithoutSection = Device("D/E/F", Icon("test"), None)
        with self.assertRaises(Exception):
            deviceWithoutSection.checkNecessaryConditions()

        deviceWithWrongSubsystem = Device("GHI", Icon("test"), None)
        deviceWithWrongSubsystem.section = True
        with self.assertRaises(Exception):
            deviceWithWrongSubsystem.checkNecessaryConditions()
Exemple #18
0
 def add_new_device(self, dev):
     d = Device(dev)
     self.data_list.append(d)
     print "Device added: %s (%s)" % (d.device_name(), d.device_type())
     self.dataChanged.emit(self.index(len(self.data_list) - 1, 0), self.index(len(self.data_list) - 1, 1))
class Test:
    def __init__(self, config_file="config.yaml", device_init=True):
        self.FILE_FOLDER = "./files/"
        self.SRC_FOLDER = "./src/"

        with open(self.SRC_FOLDER+config_file, "r") as fp:
            self.config = yaml.safe_load(fp)

        self.test_config = self.config["test"]
        self.test_file = self.test_config["test_script"]
        self.input_points_header = self.test_config.get("input_points_header", "Simulation (controller) Inputs")
        self.conditions_header = self.test_config.get("conditions_header", "Result Time")
        self.output_points_header = self.test_config.get("output_points_header", "Expected Controller BACnet Outputs")

        if device_init:
            self.controller = Device(device_config=self.config["device"])

            self.map_file = self.test_config["point_map"]
            self.init_device(mapping_file=self.map_file)

            self.init_test_sequence(filename=self.test_file, ip_header=self.input_points_header, cond_header=self.conditions_header, op_header=self.output_points_header, point_prop=self.point_properties)

    def init_device(self, mapping_file):
        with open(self.FILE_FOLDER+mapping_file, "r") as fp:
            mapping_dict = json.load(fp)

        self.mapping = pd.DataFrame(data=list(mapping_dict.keys()), index=list(mapping_dict.values()), columns=['name_in_test'])
        self.mapping.index.name = 'bacnet_name'
        self.point_properties = self.controller.get_point_properties()
        self.point_properties = pd.merge(left=self.point_properties, right=self.mapping, how='inner', left_index=True, right_index=True)
        object_list = self.point_properties.apply(lambda x: (x['type'], x['address']), axis=1).values.tolist()
        self.controller.reset_device(object_list = object_list)
        self.points = {}

    def init_test_sequence(self, filename, ip_header, cond_header, op_header, point_prop):
        self.test_df = pd.read_excel(self.FILE_FOLDER+filename, index_col=0, header=None)
        self.ip = self.format_excel_df(df=self.test_df.loc[ip_header:cond_header].iloc[1:-1], point_prop=point_prop)
        self.cond = self.format_excel_df(df=self.test_df.loc[cond_header:op_header].iloc[1:-1], is_cond_df=True, point_prop=point_prop)
        self.op = self.format_excel_df(df=self.test_df.loc[op_header:].iloc[1:], point_prop=point_prop)
        self.acceptable_op_bounds = self.op.loc["acceptable_bounds"]

        self.current_step = None
        self.step_outputs = {}

        self.ramp_step = False
        self.ramp_variables = {}

        self.periodic_step = False
        self.periodic_variables = {}

    def format_excel_df(self, df, is_cond_df=False, point_prop=None):
        df_new = df.reset_index().drop([0, 1], axis=1)
        cols = ['step%d' % i for i in range(len(df_new.columns) - 2)]
        cols = ['variable_name', 'acceptable_bounds'] + cols
        df_new.columns = cols

        if not is_cond_df:
            df_new['variable_name'] = df_new['variable_name'].map(lambda x: point_prop.loc[point_prop['name_in_test'] == x].name[0])
            return df_new.set_index('variable_name').T
        else:
            df_new = df_new.set_index('variable_name').T
            df_new.loc[df_new['or'] == 1, 'VariableName'] = df_new.loc[df_new['or'] == 1, 'VariableName'].map(lambda x: point_prop.loc[point_prop["name_in_test"] == x].name[0])
            time_vals = df_new.loc[df_new['ClkTime'].notnull()].index
            cond_time = pd.to_datetime(df_new.loc[time_vals, 'ClkTime'], format="%H:%M:%S")
            df_new.loc[time_vals, 'ClkTime'] = cond_time.dt.hour * 3600 + cond_time.dt.minute * 60 + cond_time.dt.second
            return df_new

    def read_points(self):
        for point in sorted(self.point_properties.name.values):
            var_name_in_test = self.point_properties.loc[point].name_in_test
            self.points[var_name_in_test] = self.controller.device[point].value
        return self.points

    def print_points(self, to_csv=False, name=None):
        points = self.read_points()
        for k in sorted(points):
            print("%s: %s" % (k, str(points[k])))
        print()

        if to_csv:
            file = self.FILE_FOLDER + name + "_values.csv"
            if not os.path.exists(file):
                fp = open(file, "w")
                column_names = 'time,' + ','.join(list(points.keys())) + '\n'
                fp.write(column_names)
            else:
                fp = open(file, "a")
            values = time.strftime("%Y-%m-%d %H:%M:%S")+','+','.join([str(value) for value in points.values()])+'\n'
            fp.write(values)

    def save_test_times(self, to_csv=False, name=None, step=None, st=None, et=None, duration=None):
        if to_csv:
            file = self.FILE_FOLDER + name + "_test_times.csv"
            if not os.path.exists(file):
                fp = open(file, "w")
                column_names = 'step,start_time,end_time,duration\n'
                fp.write(column_names)
            else:
                fp = open(file, "a")

            values = "%d,%f,%f,%f\n"%(step, st, et, duration)
            fp.write(values)

    def start_test(self, to_csv=False, name=None):
        output_acceptable_bounds = self.acceptable_op_bounds.to_dict()
        start_time = time.time()
        for i in range(1, self.ip.shape[0]):
            self.current_step = i
            print("starting step %d"%i)

            ip = self.ip.iloc[i].to_dict()
            cond = self.cond.iloc[i]
            expected_op = self.op.iloc[i].to_dict()

            # self.controller.set_values(point_value_dict = ip)
            self.set_values(variable_value_dict=ip)
            print("Successfully set input values=================================")
            print()

            step_start_time = time.time()
            self.test_conditions(condition=cond, st=step_start_time, to_csv=to_csv, name=name)
            print("Conditions met. Current values = ")
            self.print_points(to_csv=to_csv, name=name)

            actual_outputs = self.get_current_variable_values(variable_list = self.op.columns.values)
            self.step_outputs[self.current_step] = actual_outputs

            if i > 1:
                print("Checking if outputs match the expected values")
                assertion_op = self.assert_output(expected_op_dict = expected_op, actual_output_dict=actual_outputs, acceptable_bounds_dict = output_acceptable_bounds)
                if not assertion_op:
                    end_time = time.time()
                    time_elapsed = round((end_time - start_time)/60, 2)
                    print("Test failed! Total time = %f minutes"%round(time_elapsed, 2))
                    self.save_test_times(to_csv=to_csv, name=name, step=-1, st=start_time, et=end_time,
                                         duration=time_elapsed)

                    return
                step_end_time = time.time()
                step_time_elapsed = round((step_end_time - step_start_time)/60, 2)
                print("Passed step %d; Time taken for this step = %f minutes"%(i, round(step_time_elapsed, 2)))
                self.save_test_times(to_csv=to_csv, name=name, step=i, st=step_start_time, et=step_end_time, duration=step_time_elapsed)

            else:
                print("not checking first step values")

            print("moving to the next step")
            print()
        end_time = time.time()
        time_elapsed = round((end_time - start_time) / 60, 2)
        print("Controller passed the test successfully! Total time = %f minutes"%round(time_elapsed, 2))
        self.save_test_times(to_csv=to_csv, name=name, step=999, st=start_time, et=end_time,
                             duration=time_elapsed)
        return

    def set_values(self, variable_value_dict):
        # start with assumption that there are no ramping variables in this step
        self.ramp_step = False
        self.ramp_variables = {}

        self.periodic_step = False
        self.periodic_variables = {}

        for key in variable_value_dict:
            val = variable_value_dict[key]
            if type(val) == str:
                # remove all whitespaces
                val = val.replace(" ","")

                if val.startswith("ramp("):
                    ramp_params_dict = self.get_ramp_parameter_dict(val=val)
                    self.ramp_variables[key] = ramp_params_dict
                    value_to_set = ramp_params_dict['ramp_start']
                elif val.startswith("periodic("):
                    periodic_params_dict = self.get_periodic_parameter_dict(val=val)
                    self.periodic_variables[key] = periodic_params_dict
                    value_to_set = periodic_params_dict['periodic_start']
                elif val.startswith("="):
                    expression = val[1:]
                    value_to_set = self.evaluate_expression(expression=expression)
                else:
                    if val.lower() in ['open', 'present', 'on']:
                        value_to_set = 'active'
                    elif val.lower() in ['closed', 'absent', 'off']:
                        value_to_set = 'inactive'
                    else:
                        value_to_set = val
            else:
                # TODO: handle units == 'percent'
                value_to_set = val
            var_name_in_test = self.point_properties.loc[key].name_in_test
            print("Setting input %s to %s"%(var_name_in_test, value_to_set))
            self.controller.device[key] = value_to_set

    def get_ramp_parameter_dict(self, val, default_ramp_period=10):
        val = val.split("ramp(")[1][:-1]
        string_parameters = val.split(';')
        ramp_params = []
        for param in string_parameters:
            if param.startswith("="):
                expression = param[1:]
                val_expression = self.evaluate_expression(expression=expression)
                ramp_params.append(val_expression)
            else:
                ramp_params.append(float(param))

        ramp_params_dict = {}

        # if start == end, no ramping
        if ramp_params[0] != ramp_params[1]:
            self.ramp_step = True

        ramp_params_dict['ramp_start'] = ramp_params[0]
        ramp_params_dict['ramp_end'] = ramp_params[1]

        # convert ramp rate to value per second
        ramp_params_dict['ramp_rate'] = ramp_params[2]/60.0

        if len(ramp_params) == 4:
            ramp_params_dict['ramp_period'] = ramp_params[3]
        else:
            ramp_params_dict['ramp_period'] = default_ramp_period

        return ramp_params_dict

    def get_periodic_parameter_dict(self, val, default_period = 10):
        val = val.split("periodic(")[1][:-1]
        string_parameters = val.split(";")
        periodic_params = []
        for param in string_parameters:
            if param.startswith("="):
                periodic_params.append(param[1:])
            else:
                periodic_params.append(float(param))

        self.periodic_step = True

        periodic_params_dict = {}
        periodic_params_dict['periodic_start'] = self.evaluate_expression(expression=periodic_params[0])
        periodic_params_dict['periodic_expression'] = periodic_params[0]
        if len(periodic_params) == 2:
            periodic_params_dict['period'] = int(periodic_params[1])
        else:
            periodic_params_dict['period'] = default_period

        return periodic_params_dict

    def set_ramp_value(self, variable, params, seconds_since_start):
        ramp_start = params['ramp_start']
        ramp_end = params['ramp_end']
        ramp_rate = params['ramp_rate']
        ramp_period = params['ramp_period']

        if seconds_since_start % ramp_period == 0:
            current_period = seconds_since_start / ramp_period
            if ramp_start < ramp_end:
                value_to_set = ramp_start + ramp_rate * current_period * ramp_period
                if value_to_set > ramp_end:
                    value_to_set = ramp_end
            elif ramp_start > ramp_end:
                value_to_set = ramp_start - ramp_rate * current_period * ramp_period
                if value_to_set < ramp_end:
                    value_to_set = ramp_end

            current_value = self.controller.device[variable].value
            if round(value_to_set, 2) != round(current_value, 2):
                var_name_in_test = self.point_properties.loc[variable].name_in_test
                print("Ramping input %s to %f" % (var_name_in_test, value_to_set))
                print()
                self.controller.device[variable] = value_to_set

    def set_periodic_value(self, variable, params, seconds_since_start):
        periodic_expression = params['periodic_expression']
        period = params['period']

        if seconds_since_start%period == 0:
            value_to_set = self.evaluate_expression(expression=periodic_expression)
            current_value = self.controller.device[variable].value
            if round(value_to_set, 2) != round(current_value, 2):
                var_name_in_test = self.point_properties.loc[variable].name_in_test
                print("Periodic: Changing variable %s to %f" % (var_name_in_test, value_to_set))
                print()
                self.controller.device[variable] = value_to_set

    def test_conditions(self, condition, st, sleep_interval=None, verbose=False, to_csv=False, name=None):

        print("step = %d " % self.current_step)
        current_time = time.time()
        last_print = None

        while current_time - st <= condition['ClkTime']:

            seconds_since_start = int(current_time - st)

            if self.ramp_step:
                for variable in self.ramp_variables:
                    params = self.ramp_variables[variable]
                    self.set_ramp_value(variable=variable, params=params, seconds_since_start=seconds_since_start)

            if self.periodic_step:
                for variable in self.periodic_variables:
                    params = self.periodic_variables[variable]
                    self.set_periodic_value(variable=variable, params=params, seconds_since_start=seconds_since_start)

            if verbose:
                print("current time = %f, wait until %f" % (current_time - st, condition['ClkTime']))

            if condition['or'] == 1:
                output_variable_to_check = condition['VariableName']
                output_value_to_check = condition['VariableValue']

                if type(output_value_to_check) == str:
                    operator = re.findall("\A\D+", output_value_to_check)
                    if len(operator) == 1:
                        operator = operator[0]
                    else:
                        #TODO: handle this better
                        raise Exception("Invalid condition value in step %d for variable %s"%(self.current_step, output_variable_to_check))

                    output_value_to_check = output_value_to_check.split(operator)[1]
                    if output_value_to_check.endswith("%"):
                        output_value_to_check = float(output_value_to_check[:-1])/100
                    else:
                        output_value_to_check = float(output_value_to_check)
                else:
                    operator = ">="

                actual_output_variable_value = self.controller.device[output_variable_to_check].value

                # handle percent values
                if self.point_properties.loc[output_variable_to_check].units_state == 'percent':
                    actual_output_variable_value = actual_output_variable_value/100

                if self.evaluate_boolean_expression(operator=operator, actual_value=actual_output_variable_value, expected_value=output_value_to_check):
                    print("condition satisfied, variable %s value %f %s condition value %f"%(output_variable_to_check, actual_output_variable_value, operator, output_value_to_check))
                    print()
                    return

            if seconds_since_start%60 == 0:
                if last_print == None or last_print != seconds_since_start/60:
                    last_print = seconds_since_start/60
                    print("Completed minute %d of step %d of the test; Current values=" % (int(seconds_since_start/60), self.current_step))
                    self.print_points(to_csv=to_csv, name=name)

            if sleep_interval:
                time.sleep(sleep_interval)

            current_time = time.time()
        print("wait time condition met")

    def evaluate_boolean_expression(self, operator, actual_value, expected_value):
        if operator == ">" and actual_value > expected_value:
            return True
        elif operator == ">=" and actual_value >= expected_value:
            return True
        elif operator == "<" and actual_value < expected_value:
            return True
        elif operator == "<=" and actual_value <= expected_value:
            return True
        elif operator == "==" and actual_value == expected_value:
            return True
        else:
            return False

    def get_current_variable_values(self, variable_list):
        vals = {}
        for var in variable_list:
            vals[var] = self.controller.device[var].value
        return vals

    def assert_output(self, expected_op_dict, actual_output_dict, acceptable_bounds_dict):
        for key in expected_op_dict:
            expected_val = expected_op_dict[key]
            actual_val = actual_output_dict[key]
            error_bound = acceptable_bounds_dict[key]

            if type(actual_val) == str:
                if actual_val == 'inactive':
                    actual_val = 0
                else:
                    actual_val = 1

            if expected_val == "Any":
                continue
            elif type(expected_val) == str:
                if "last" in expected_val:
                    operator = expected_val.split('last')[0]
                    variable = key
                    expected_val = self.step_outputs[self.current_step - 1][variable]

                    if self.evaluate_boolean_expression(operator=operator, actual_value=actual_val, expected_value=expected_val):
                        continue
                    else:
                        var_name = self.point_properties.loc[self.point_properties.name == key].name_in_test.values[0]
                        print("For variable %s [or %s], actual value = %f not %s expected value = %f"%(key, var_name, actual_val, operator, expected_val))
                        return False
                if expected_val.startswith("="):
                    expression = expected_val[1:]
                    expected_value = self.evaluate_expression(expression=expression)

                    if abs(expected_value - actual_val) > error_bound:
                        var_name = self.point_properties.loc[self.point_properties.name == key].name_in_test.values[0]
                        print ("outside bounds for %s [or %s], actual value = %f, expected value = %f, bounds = %f" % (
                        key, var_name, actual_val, expected_val, error_bound))
                        return False

            else:
                if self.point_properties.loc[key].units_state == 'percent':
                    actual_val = actual_val/100

                if abs(expected_val - actual_val) > error_bound:
                    var_name = self.point_properties.loc[self.point_properties.name == key].name_in_test.values[0]
                    print ("outside bounds for %s [or %s], actual value = %f, expected value = %f, bounds = %f"%(key, var_name, actual_val, expected_val, error_bound))
                    return False

        return True

    def evaluate_expression(self, expression):
        if expression.startswith("="):
            expression = expression[1:]

        while expression.find(')') != -1:
            e_loc = expression.find(')')
            s_loc = expression[:e_loc].rfind('(')
            op = self.get_value_from_expression(expression=expression[s_loc + 1:e_loc])
            expression = expression.replace(expression[s_loc:e_loc + 1], str(op))
        return self.get_value_from_expression(expression=expression)

    def get_value_from_expression(self, expression):

        operator_found = False
        result = None
        for operator in ['+', '-', '*', '/']:
            if operator in expression:
                operator_found = True
                parts = expression.split(operator)
                for part in parts:
                    current_res = self.get_value_from_expression(expression=part)
                    if result:
                        if operator == '+':
                            result = result + current_res
                        elif operator == '-':
                            result = result - current_res
                        elif operator == '*':
                            result = result * current_res
                        elif operator == '/':
                            result = result / current_res
                    else:
                        result = current_res
                break
        if operator_found:
            return result
        else:
            names_df = self.point_properties.loc[self.point_properties.name_in_test == expression]
            if not names_df.empty:
                var_name = names_df.name.values[0]
                return self.controller.device[var_name].value
            else:
                try:
                    float_value = float(expression)
                except Exception as e:
                    raise Exception("cannot find variable %s"%expression)
                return float_value
Exemple #20
0
    def testShortName(self):
        firstDevice = Device("I-K01/VAC/I-K01CAB05-VAC-IPCU1", None, None)
        secondDevice = Device("R1-SGA/MAG/R1-SGACAB14-MAG-PS04", None, None)

        self.assertEqual(firstDevice.getShortName(), "IPCU1")
        self.assertEqual(secondDevice.getShortName(), "PS04")
Exemple #21
0
    def testSectionName(self):
        device = Device("I-TL/VAC/I-TL-CAB02-VAC-IPCU1", None, None)
        self.assertEqual(device.getSectionName(), "I-TL")

        device = Device("I-S00/MAG/I-S00-MAG-COBX2", None, None)
        self.assertEqual(device.getSectionName(), "I-S00")

        device = Device("I-K01/VAC/I-K01CAB05-VAC-IPCU1", None, None)
        self.assertEqual(device.getSectionName(), "I-K01")

        device = Device("R1-SGA/MAG/CAB1R1-SGA4-MAG-PS04", None, None)
        self.assertEqual(device.getSectionName(), "R1-SGA")
 def setUp(self):
     self.device = Device()
     self.device2 = Device("Jumpy Whale")
Exemple #23
0
 def testGenerateSimpleName(self):
     device = Device("I-K01/VAC/I-K01CAB05-VAC-IPCU1", None, None)
     self.assertEqual(device.generateSimpleName(),
                      "ik01vacik01cab05vacipcu1")
Exemple #24
0
    def testLinacElement(self):
        device = Device("I-K01/VAC/I-K01CAB05-VAC-IPCU1", None, None)
        self.assertEqual(device.isLinacElement(), True)

        device = Device("R1-SGA/MAG/R1-SGACAB14-MAG-PS04", None, None)
        self.assertEqual(device.isLinacElement(), False)
 def testAddDeviceToSubsection(self):
     section = self.linac.addSection("I-S01", None, 200)
     subsection = section.addSubsection("I-S01B", None, 50)
     device = Device("I-S01B/ab/cd", None, [0, 20])
     self.linac.addDevice(device)
     self.assertEqual(device.section, subsection)