def test_rm_dplugin(self): #Create dplugins d_pl_1 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) d_pl_2 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) #Create dblocks d_bl_1 = DBlock('Block_1') d_bl_2 = DBlock('Block_2') #assign dblocks to DPlugin d_pl_1 d_pl_1.add_dblock(d_bl_1) d_pl_1.add_dblock(d_bl_2) #create subscribtions self.dcore.subscribe(d_pl_2.id, d_pl_1.id, d_bl_1.name) self.dcore.subscribe(d_pl_2.id, d_pl_1.id, d_bl_2.name) self.assertEqual(len(d_pl_2.get_subscribtions()[d_pl_1.id]), 2) self.assertEqual(len(d_bl_1.get_subscribers()), 1) self.assertEqual(len(d_bl_2.get_subscribers()), 1) self.dcore.rm_dplugin(d_pl_1.id) #Check if DPlugin d_pl_1 is missing self.assertFalse(self.dcore.get_dplugin_by_id(d_pl_1.id)) #Check if all subscribtions were canceled self.assertEqual(len(d_pl_2.get_subscribtions().keys()), 0) self.assertEqual(len(d_bl_1.get_subscribers()), 0) self.assertEqual(len(d_bl_2.get_subscribers()), 0) pass
def test_unsubscribe_all(self): #Create dplugins d_pl_1 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) d_pl_2 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) d_pl_3 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) #Create dblocks d_bl_1 = DBlock('Block_1') d_bl_2 = DBlock('Block_2') #assign dblocks to DPlugin d_pl_1 d_pl_1.add_dblock(d_bl_1) d_pl_1.add_dblock(d_bl_2) self.assertTrue(self.dcore.subscribe(d_pl_2.id, d_pl_1.id, d_bl_1.name)) self.assertTrue(self.dcore.subscribe(d_pl_3.id, d_pl_1.id, d_bl_1.name)) self.assertTrue(self.dcore.subscribe(d_pl_2.id, d_pl_1.id, d_bl_2.name)) print(d_bl_1.get_subscribers()) self.assertEqual(len(d_pl_2.get_subscribtions()[d_pl_1.id].keys()), 2) self.dcore.unsubscribe_all(d_pl_2.id) self.assertNotIn(d_pl_1.id, d_pl_2.get_subscribtions()) print(d_bl_1.get_subscribers()) self.assertTrue(len(d_bl_1.get_subscribers())==1) self.assertTrue(len(d_bl_2.get_subscribers())==0) pass
def test_dblocks(self): dpl_1 = DPlugin() dpl_1.id = 1 dbl_1 = DBlock('Block1') dbl_1.dplugin_id = dpl_1.id dbl_2 = DBlock('Block2') dbl_2.dplugin_id = dpl_1.id #check: add Parameter self.assertTrue(dpl_1.add_dblock(dbl_1)) self.assertTrue(dpl_1.add_dblock(dbl_2)) self.assertFalse(dpl_1.add_dblock(dbl_1)) self.assertFalse(dpl_1.add_dblock(dbl_2)) #Check: count of parameters self.assertEqual(len(dpl_1.get_dblocks().keys()), 2) #Check: rm parameter self.assertTrue(dpl_1.rm_dblock(dbl_1)) self.assertEqual(len(dpl_1.get_dblocks().keys()), 1) self.assertTrue(dpl_1.rm_dblock(dbl_2)) self.assertEqual(len(dpl_1.get_dblocks().keys()), 0)
def cb_initialize_plugin(self): self.block1 = DBlock('Progress') signal = DSignal('percent') self.block1.add_signal(signal) self.block2 = DBlock('Trigger') signal = DSignal('trigger') self.block2.add_signal(signal) self.block3 = DBlock('ResetTrigger') signal = DSignal('reset') self.block3.add_signal(signal) self.block4 = DBlock('ParaToSig') signal = DSignal('parameter') self.block4.add_signal(signal) blockList = [self.block1, self.block2, self.block3, self.block4] self.pl_send_new_block_list(blockList) self.para3 = DParameter('Choose', default=0, Regex='\d+') para_l = [self.para3] self.pl_send_new_parameter_list(para_l) self.pl_set_event_trigger_mode(True) self.initialized = True return True
def test_rm_all_subscribers(self): #Create dplugins d_pl_1 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) d_pl_2 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) d_pl_3 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) #Create dblocks d_bl_1 = DBlock('Block_1') d_bl_2 = DBlock('Block_2') #assign dblocks to DPlugin d_pl_1 d_pl_1.add_dblock(d_bl_1) d_pl_1.add_dblock(d_bl_2) self.assertTrue(self.dcore.subscribe(d_pl_2.id, d_pl_1.id, d_bl_1.name)) self.assertTrue(self.dcore.subscribe(d_pl_2.id, d_pl_1.id, d_bl_2.name)) self.assertTrue(self.dcore.subscribe(d_pl_3.id, d_pl_1.id, d_bl_1.name)) self.assertTrue(self.dcore.subscribe(d_pl_3.id, d_pl_1.id, d_bl_2.name)) self.assertEqual(len(d_bl_1.get_subscribers()), 2) self.assertEqual(len(d_bl_2.get_subscribers()), 2) self.dcore.rm_all_subscribers(d_pl_1.id) self.assertEqual(len(d_bl_1.get_subscribers()), 0) self.assertEqual(len(d_bl_2.get_subscribers()), 0) pass
def test_subscribtions(self): dpl_1 = DPlugin() dpl_1.id = 1 dpl_2 = DPlugin() dpl_2.id = 2 dbl_1 = DBlock('DBlock1') dbl_1.id = 3 dbl_1.dplugin_id = dpl_2.id dbl_2 = DBlock('DBlock2') dbl_2.id = 4 dbl_2.dplugin_id = dpl_2.id dpl_2.add_dblock(dbl_1) dpl_2.add_dblock(dbl_2) #Check: subscribe DBlocks self.assertTrue(dpl_1.subscribe(dbl_1)) self.assertTrue(dpl_1.subscribe(dbl_2)) self.assertFalse(dpl_1.subscribe(dbl_1)) self.assertFalse(dpl_1.subscribe(dbl_2)) #Check: count of subscribtions self.assertEqual(len(dpl_1.get_subscribtions()[dpl_2.id].keys()), 2) #Check: unsubscribe DBlock self.assertTrue(dpl_1.unsubscribe(dbl_1)) #Check: count of subscribtions self.assertEqual(len(dpl_1.get_subscribtions().keys()), 1) #Check: count of subscribtions self.assertEqual(len(dpl_1.get_subscribtions()[dpl_2.id].keys()), 1)
def test_pl_send_new_block_list(self): self.assertRaises(pe.WrongType, self.basePlugin.pl_send_new_block_list, None) self.assertRaises(pe.WrongLength, self.basePlugin.pl_send_new_block_list, []) blocks = [DBlock('1'), 1, DBlock('3')] self.assertRaises(pe.WrongType, self.basePlugin.pl_send_new_block_list, blocks) blocks = [DBlock('1'), DBlock('2'), DBlock('3')] self.assertRaises(AttributeError, self.basePlugin.pl_send_new_block_list, blocks)
def cb_initialize_plugin(self): self.config = self.pl_get_current_config_ref() self.t = 0 self.amax = Fourier_Rect.amax self.amplitude = 1 self.max_approx = Fourier_Rect.max_approx self.freq = 1 self.vec = numpy.ones(self.amax * (self.max_approx + 1)) print(['Fourier: process id: ', os.getpid()]) self.HOST = self.config['host']['value'] self.PORT = int(self.config['port']['value']) # SOCK_DGRAM is the socket type to use for UDP sockets self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.setblocking(0) self.block1 = DBlock('Rectangle') for i in range(1, self.max_approx): self.block1.add_signal(DSignal('rect' + str(i))) self.pl_send_new_block_list([self.block1]) return True
def config_loader_subs(self, pl_to_start, subs_to_make, parameters_to_change, signals_to_change): """ Function for callback when timer finished to apply subscriptions and parameter changed of config. :param pl_to_start: list of plugins to start :type pl_to_start: list :param subs_to_make: list of subscriptions to make :type subs_to_make: list :param parameters_to_change: parameter changes to apply :type parameters_to_change: list :param signals_to_change: signal name changes to apply :type signals_to_change: list :return: """ for sub in subs_to_make: self.do_subscribe_uname(sub[0], sub[1], sub[2], sub[3], sub[4]) for para in parameters_to_change: self.do_set_parameter_uname(para[0], para[1], para[2]) for sig in signals_to_change: plugin_uname = sig[0] dblock_name = sig[1] dsignal_uname = sig[2] dsignal_dname = sig[3] self.do_edit_plugin_uname( plugin_uname, DBlock(dblock_name), {'edit': DSignal(dsignal_uname, dsignal_dname)})
def cb_initialize_plugin(self): self.t = 0 self.amax = Fourier_Rect_MOD.amax self.amplitude = 1 self.max_approx = Fourier_Rect_MOD.max_approx self.freq = 1 self.vec = numpy.ones(self.amax * (self.max_approx + 1)) print(['Fourier: process id: ', os.getpid()]) self.HOST = "130.149.155.73" self.PORT = 9999 # SOCK_DGRAM is the socket type to use for UDP sockets self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.setblocking(0) self.block1 = DBlock('Rectangle') for i in range(1, self.max_approx): self.block1.add_signal(DSignal('rect' + str(i))) self.pl_send_new_block_list([self.block1]) self.pl_set_event_trigger_mode(True) self.goOn = True self.thread = threading.Thread(target=self.thread_execute, args=(self.HOST, self.PORT)) self.thread.start() return True
def test_delete_block(self): self.assertRaises(pe.WrongType, self.basePlugin.pl_send_delete_block, None) self.assertRaises(AttributeError, self.basePlugin.pl_send_delete_block, 'DBlock') self.assertRaises(AttributeError, self.basePlugin.pl_send_delete_block, DBlock('DBlock'))
def test_subscribe_signals(self): #Create dplugins d_pl_1 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) d_pl_2 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) d_pl_3 = self.dcore.add_plugin(None, 1, None, None, None, self.dcore.create_id()) #Create dblocks d_bl_1 = DBlock('Block1') d_bl_2 = DBlock('Block2') #assign dblocks to DPlugin d_pl_1 d_pl_1.add_dblock(d_bl_1) d_pl_1.add_dblock(d_bl_2) self.dcore.subscribe(d_pl_2.id, d_pl_1.id, d_bl_1.name) self.dcore.subscribe(d_pl_2.id, d_pl_1.id, d_bl_2.name) self.dcore.subscribe(d_pl_3.id, d_pl_1.id, d_bl_1.name) self.dcore.subscribe(d_pl_3.id, d_pl_1.id, d_bl_2.name) self.assertTrue(self.dcore.subscribe_signals(d_pl_2.id, d_pl_1.id, d_bl_1.name, [1, 2, 3])) self.assertTrue(self.dcore.subscribe_signals(d_pl_2.id, d_pl_1.id, d_bl_2.name, [6, 4, 5])) self.assertTrue(self.dcore.subscribe_signals(d_pl_3.id, d_pl_1.id, d_bl_1.name, [9])) self.assertTrue(self.dcore.subscribe_signals(d_pl_3.id, d_pl_1.id, d_bl_2.name, [17,19,18])) subscription = d_pl_2.get_subscribtions()[d_pl_1.id][d_bl_1.name] self.assertIn(1, subscription.get_signals()) self.assertIn(2, subscription.get_signals()) self.assertIn(3, subscription.get_signals()) subscription = d_pl_3.get_subscribtions()[d_pl_1.id][d_bl_2.name] self.assertIn(19, subscription.get_signals()) self.assertIn(17, subscription.get_signals()) self.assertIn(18, subscription.get_signals()) subscription = d_pl_3.get_subscribtions()[d_pl_1.id][d_bl_1.name] self.assertIn(9, subscription.get_signals())
def test_remove_signal(self): dblock = DBlock('SinMit_f1') ds_1 = DSignal(CORE_TIME_SIGNAL) dblock.add_signal(ds_1) subscription = DSubscription(dblock) subscription.add_signal(ds_1) self.assertTrue(subscription.rm_signal(ds_1)) self.assertNotIn(ds_1, subscription.get_signals()) self.assertTrue(subscription.add_signal(ds_1))
def pl_create_DBlock(self, block_name): """ Creates a DBlock for use in a PaPI Plugin. :param block_name: Name of the block :type block_name: str :return: DBlock Object or None in case of error """ if isinstance(block_name, str): block = DBlock(name=block_name) return block else: return None
def cb_initialize_plugin(self): config = self.pl_get_current_config_ref() self.t = 0 self.amax = int(config['amax']['value']) self.f = float(config['f']['value']) self.block1 = DBlock('SinMit_f1') signal = DSignal('f1_1') signal.dname = 'f1_f1DNAME' self.block1.add_signal(signal) self.block2 = DBlock('SinMit_f2') signal = DSignal('f2_1') self.block2.add_signal(signal) self.block3 = DBlock('SinMit_f3') signal = DSignal('f3_1') self.block3.add_signal(signal) signal = DSignal('f3_2') self.block3.add_signal(signal) signal = DSignal('f3_scalar') self.block3.add_signal(signal) #self.block4 = self.create_new_block('Sin4', [CORE_TIME_SIGNAL,'f3_1','f3_2', 'Scalar'], [ 'numpy_vec', 'numpy_vec', 'numpy_vec', 'int'], 100 ) blockList = [self.block1, self.block2, self.block3] self.pl_send_new_block_list(blockList) self.para3 = DParameter('Frequenz Block SinMit_f3', default= 0.3, Regex='[0-9]+.[0-9]+') para_l = [self.para3] self.pl_send_new_parameter_list(para_l) print('Sinus started working') return True
def test_attach_signal(self): dblock = DBlock('SinMit_f1') ds_1 = DSignal(CORE_TIME_SIGNAL) ds_2 = DSignal('f1_1') dblock.add_signal(ds_1) dblock.add_signal(ds_2) subscription = DSubscription(dblock) self.assertTrue(subscription.add_signal(ds_1)) self.assertIn(ds_1, subscription.get_signals()) self.assertFalse(subscription.add_signal(ds_1))
def test_add_susbcribers(self): dbl = DBlock('DBlock1') dbl.id = 1 dpl_1 = DPlugin() dpl_1.id = 2 dpl_2 = DPlugin() dpl_2.id = 3 self.assertTrue(dbl.add_subscribers(dpl_1)) self.assertTrue(dbl.add_subscribers(dpl_2)) self.assertEqual(len(dbl.get_subscribers()), 2)
def test_parameterChanges(self): self.assertRaises(pe.WrongType, self.basePlugin.pl_emit_event, None, None) self.assertRaises(AttributeError, self.basePlugin._send_parameter_change, None, "DBlock") self.assertRaises(AttributeError, self.basePlugin.pl_emit_event, None, "DEvent") self.assertRaises(AttributeError, self.basePlugin._send_parameter_change, None, DBlock("Block")) self.assertRaises(AttributeError, self.basePlugin.pl_emit_event, None, DEvent("Event"))
def cb_initialize_plugin(self): self.t = 0 #print(['ADD: process id: ',os.getpid()] ) self.approx_max = 300 self.fac = 1 self.amax = 20 self.approx = self.approx_max * self.fac self.block1 = DBlock('AddOut1') signal = DSignal('Sum') self.block1.add_signal(signal) self.pl_send_new_block_list([self.block1]) return True
def update_block_list(self, ORTDSources): #self.block_id = self.block_id +1 #newBlock = DBlock('SourceGroup'+str(self.block_id)) #self.blocks['SourceGroup'+str(self.block_id)] = newBlock if 'SourceGroup0' in self.blocks: self.pl_send_delete_block('SourceGroup0') newBlock = DBlock('SourceGroup0') self.blocks['SourceGroup0'] = newBlock self.Sources = ORTDSources keys = list(self.Sources.keys()) for key in keys: Source = self.Sources[key] sig_name = Source['SourceName'] newBlock.add_signal(DSignal(sig_name)) self.pl_send_new_block_list([newBlock])
def test_rm_subscribers(self): dbl = DBlock('DBlock1') dbl.id = 1 dpl_1 = DPlugin() dpl_1.id = 2 dpl_2 = DPlugin() dpl_2.id = 3 dbl.add_subscribers(dpl_1) dbl.add_subscribers(dpl_2) self.assertEqual(len(dbl.get_subscribers()), 2) dbl.rm_subscriber(dpl_1) self.assertEqual(len(dbl.get_subscribers()), 1) dbl.rm_subscriber(dpl_2) self.assertEqual(len(dbl.get_subscribers()), 0) self.assertFalse(dbl.rm_subscriber(dpl_1))
def cb_initialize_plugin(self): # --------------------------- # Read configuration # --------------------------- # -------------------------------- # Create Widget # -------------------------------- self.ConsoleW = PaPIConsoleWidget(plugin=self) # This call is important, because the background structure needs to know the used widget! # In the background the qmidiwindow will becreated and the widget will be added self.pl_set_widget_for_internal_usage(self.ConsoleW) self.ConsoleW.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.ConsoleW.customContextMenuRequested.connect( self.show_context_menu) block = DBlock('Command') self.pl_send_new_block_list([block]) return True
def cb_initialize_plugin(self): # -------------------------------- # Step 0: Read documentation: http://tub-control.github.io/PaPI/ # -------------------------------- # -------------------------------- # Step 1: Read configuration items (startup cfg) # OPTIONAL # -------------------------------- # Please read the Documentation of pl_get_config_element() to understand what is happening here. # e.g. a configuration item named 'offset' is read. offset = self.pl_get_config_element('offset') offset = float(offset) if offset is not None else 0 # -------------------------------- # Step 2:Define blocks for output signals and define signals of blocks! # OPTIONAL # -------------------------------- # Here: block called CPU_LOAD with 2 signals for Core1 and Core2 blockLoad = DBlock('CPU_LOAD') sig_core1 = DSignal('Load_C1') sig_core2 = DSignal('Load_C2') blockLoad.add_signal(sig_core1) blockLoad.add_signal(sig_core2) self.pl_send_new_block_list([blockLoad]) # -------------------------------- # Step 3: Define parameter that are offered to PaPI # OPTIONAL # Sub-Step 1: Create Parameter Object # Sub-Step 2: Send list of Parameter Objects created to PaPI # -------------------------------- # use self. to remember the parameter object in this class/context for own usage! # e.g. parameter named parameterName1 is created! # Refer to the Doc of DParameter to read about advanced usages! self.parameter1 = DParameter('parameterName1') self.parameter2 = DParameter('parameterName2') self.pl_send_new_parameter_list([self.parameter1, self.parameter1]) # -------------------------------- # Step 4: Set the plugin trigger mode # OPTIONAL # In most cases for IOP you will leave it default, for DPP you will set it to true # Default will mean, that PaPI will decide on it. # -------------------------------- self.pl_set_event_trigger_mode('default') # -------------------------------- # Step 5: Developer and Plugin specific configuration # OPTIONAL # -------------------------------- # -------------------------------- # Step 6: Return Value # OBLIGATORY # Return True if the everything is alright! # False will lead to PaPI not starting this Plugin! # -------------------------------- return True
def cb_initialize_plugin(self): print('ORTD', self.__id__, ':process id', os.getpid()) self.config = self.pl_get_current_config_ref() # open UDP self.HOST = self.config['address']['value'] self.SOURCE_PORT = int(self.config['source_port']['value']) self.OUT_PORT = int(self.config['out_port']['value']) self.LOCALBIND_HOST = '' # config['source_address']['value'] #CK self.sendOnReceivePort = True if self.config['SendOnReceivePort'][ 'value'] == '1' else False self.UseSocketIO = True if self.config['UseSocketIO'][ 'value'] == '1' else False if self.UseSocketIO: self.SocketIOPort = int(self.config['socketio_port']['value']) #self.sendOnReceivePort = True # NOTE: remove this #self.UseSocketIO = True # NOTE: remove this print("SendOnReceivePort = ", self.sendOnReceivePort) print("UseSocketIO = ", self.UseSocketIO) self.PAPI_SIMULINK_BLOCK = False self.separate = int(self.config['SeparateSignals']['value']) self.onlyInitialConfig = self.config['OnlyInitialConfig'][ 'value'] == '1' self.hasInitialConfig = False if (not self.sendOnReceivePort): # SOCK_DGRAM is the socket type to use for UDP sockets self.sock_parameter = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock_parameter.setblocking(1) self.ControlBlock = DBlock('ControllerSignals') self.ControlBlock.add_signal(DSignal('ControlSignalReset')) self.ControlBlock.add_signal(DSignal('ControlSignalCreate')) self.ControlBlock.add_signal(DSignal('ControlSignalSub')) self.ControlBlock.add_signal(DSignal('ControllerSignalParameter')) self.ControlBlock.add_signal(DSignal('ControllerSignalClose')) self.ControlBlock.add_signal(DSignal('ActiveTab')) self.pl_send_new_block_list([self.ControlBlock]) self.t = 0 self.pl_set_event_trigger_mode(True) self.sock_recv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if (not self.sendOnReceivePort): self.sock_recv.bind((self.LOCALBIND_HOST, self.SOURCE_PORT)) # CK print("UDP_Plugin-plugin listening on: ", self.LOCALBIND_HOST, ":", self.SOURCE_PORT) #CK else: print("---- Using client UDP mode (not binding to a port) ----") self.sock_recv.settimeout(1) self.thread_goOn = True self.lock = threading.Lock() self.thread = threading.Thread(target=self.thread_execute) self.thread.start() if self.UseSocketIO: print("Using socket.io connection on port", self.SocketIOPort) self.thread_socket_goOn = True self.thread_socketio = threading.Thread( target=self.thread_socketio_execute) self.blocks = {} self.Sources = {} self.parameters = {} self.signal_values = {} self.block_id = 0 self.config_complete = False self.config_buffer = {} self.timer = Timer(3, self.callback_timeout_timer) self.timer_active = False self.ConsoleBlock = DBlock('ConsoleSignals') self.ConsoleBlock.add_signal(DSignal('MainSignal')) self.pl_send_new_block_list([self.ConsoleBlock]) self.consoleIn = DParameter('consoleIn', default='') self.pl_send_new_parameter_list([self.consoleIn]) if self.UseSocketIO: self.thread_socketio.start() return True