def test_message_federate_send(mFed): epid1 = h.helicsFederateRegisterEndpoint(mFed, "ep1", None) epid2 = h.helicsFederateRegisterGlobalEndpoint(mFed, "ep2", "random") h.helicsFederateSetTimeProperty(mFed, h.helics_property_time_delta, 1.0) h.helicsFederateEnterExecutingMode(mFed) data = "random-data" h.helicsEndpointSendEventRaw(epid1, "ep2", data, 1.0) granted_time = h.helicsFederateRequestTime(mFed, 2.0) assert granted_time == 1.0 res = h.helicsFederateHasMessage(mFed) assert res == 1 res = h.helicsEndpointHasMessage(epid1) assert res == 0 res = h.helicsEndpointHasMessage(epid2) assert res == 1 message = h.helicsEndpointGetMessage(epid2) assert message.data == "random-data" assert message.length == 11 assert message.original_dest == "" assert message.original_source == "TestA Federate/ep1" assert message.source == "TestA Federate/ep1" assert message.time == 1.0
def test_messagefederate_test_message_federate_send(mFed): epid1 = h.helicsFederateRegisterEndpoint(mFed, "ep1", "") epid2 = h.helicsFederateRegisterGlobalEndpoint(mFed, "ep2", "random") h.helicsFederateSetTimeProperty(mFed, h.HELICS_PROPERTY_TIME_DELTA, 1.0) h.helicsFederateEnterExecutingMode(mFed) data = "random-data" h.helicsEndpointSendEventRaw(epid1, "ep2", data, 1.0) granted_time = h.helicsFederateRequestTime(mFed, 2.0) assert granted_time == 1.0 res = h.helicsFederateHasMessage(mFed) assert res is True res = h.helicsEndpointHasMessage(epid1) assert res is False res = h.helicsEndpointHasMessage(epid2) assert res is True message = h.helicsEndpointGetMessage(epid2) assert h.helicsMessageGetMessageID(message) == 55 assert h.helicsMessageIsValid(message) is True assert h.helicsMessageGetString(message) == "random-data" assert h.helicsMessageGetRawDataSize(message) == 11 assert h.helicsMessageGetOriginalDestination(message) == "" assert h.helicsMessageGetOriginalSource(message) == "TestA Federate/ep1" assert h.helicsMessageGetSource(message) == "TestA Federate/ep1" assert h.helicsMessageGetTime(message) == 1.0
def send_to_endpoint(self, endpoint_name, destination='', value=0): """ Send the message to specific endpoint :param endpoint_name: endpoint name :param destination: destination name if any :param value: message :return: """ endpoint_idx = self.endpoints[endpoint_name] try: h.helicsEndpointSendEventRaw(endpoint_idx, destination, str(56.7), self.current_time) except h._helics.HelicsException as e: _log.exception( "Error sending endpoint message to HELICS {}".format(e))
def test_message_federate_endpoint_registration(mFed): epid1 = h.helicsFederateRegisterEndpoint(mFed, "ep1", None) epid2 = h.helicsFederateRegisterGlobalEndpoint(mFed, "ep2", "random") h.helicsFederateSetTimeDelta(mFed, 1.0) h.helicsFederateEnterExecutionMode(mFed) data = "random-data" status = h.helicsEndpointSendEventRaw(epid1, "ep2", data, 1.0) status, granted_time = h.helicsFederateRequestTime(mFed, 1.0) assert granted_time == 1.0 res = h.helicsFederateHasMessage(mFed) assert res == 1 res = h.helicsEndpointHasMessage(epid1) # TODO: Figure out why this is returning zero assert res == 0 res = h.helicsEndpointHasMessage (epid2) assert res == 1 # This causes a segfault message = h.helicsEndpointGetMessage(epid2) assert message.data == 'random-data' assert message.length == 11 assert message.original_dest == '' assert message.original_source == 'TestA Federate/ep1' assert message.source == 'TestA Federate/ep1' assert message.time == 1.0
def federate_example(config_path): # Registering federate from json try: fed = h.helicsCreateCombinationFederateFromConfig(config_path) except h._helics.HelicsException as exc: print("Exception occured".format(exc)) exit(-1) federate_name = h.helicsFederateGetName(fed) print(federate_name) endpoint_count = h.helicsFederateGetEndpointCount(fed) subkeys_count = h.helicsFederateGetInputCount(fed) pubkeys_count = h.helicsFederateGetPublicationCount(fed) # Reference to Publications and Subscription form index endid = {} subid = {} pubid = {} for i in range(0,endpoint_count): endid["m{}".format(i)] = h.helicsFederateGetEndpointByIndex(fed, i) end_name = h.helicsEndpointGetName(endid["m{}".format(i)]) logger.info( 'Registered Endpoint ---> {}'.format(end_name)) for i in range(0, subkeys_count): idx = h.helicsFederateGetInputByIndex(fed, i) subid["m{}".format(i)] = idx status = h.helicsInputSetDefaultComplex(subid["m{}".format(i)], 0, 0) sub_key = h.helicsSubscriptionGetKey(idx) logger.info( 'Registered Subscription ---> {}'.format(sub_key)) for i in range(0, pubkeys_count): idx = h.helicsFederateGetPublicationByIndex(fed, i) pubid["m{}".format(i)] = idx pub_key = h.helicsPublicationGetKey(idx) logger.info( 'Registered Publications ---> {}'.format(pub_key)) print('###############################################################################################') print('######################## Entering Execution Mode ##########################################') # Entering Execution Mode h.helicsFederateEnterExecutingMode(fed) print('###############################################################################################') hours = 0.1 total_inteval = int(60 * 60 * hours) grantedtime = -1 update_interval = 1 # 5*60 k = 0 data ={} time.sleep(5) time_sim = [] real = 0 for t in range(0, total_inteval, update_interval): while grantedtime < t: grantedtime = h.helicsFederateRequestTime (fed, t) time.sleep(0.1) print('######################## Time interval {} ##########################################'.format(t)) print('######################## Publishing to topics ######################################') real = real + 1 for i in range(0, pubkeys_count): idx = pubid["m{}".format(i)] h.helicsPublicationPublishComplex(idx, real*i, 78) print( '######################## Get input from subscribed topics #########################') for i in range(0, subkeys_count): idx = subid["m{}".format(i)] value = h.helicsInputGetDouble(idx) key = h.helicsSubscriptionGetKey(idx) print("Value for key: {} is {}".format(key, value)) print('######################## Get from Endpoint #########################################') idx = endid["m{}".format(0)] while h.helicsEndpointHasMessage(idx): msg = h.helicsEndpointGetMessage(idx) end_name = h.helicsEndpointGetName(idx) print("Value from endpoint name: {} is {}".format(end_name, msg.data)) print('######################## Send to VOLTTRON Endpoint #################################') for i in range(0, endpoint_count): idx = endid["m{}".format(i)] value = i + random.randint(1, 101) + 89.7 end_name = h.helicsEndpointGetName(idx) print("Sending Value:{0} for endpoint: {1}".format(value, end_name)) h.helicsEndpointSendEventRaw(idx, '', str(value), t) end_name = h.helicsEndpointGetName(idx) logger.info("Destroying federate") destroy_federate(fed)
# Enter execution mode # h.helicsFederateEnterExecutingMode(vfed) print("PI SENDER: Entering execution mode") # This federate will be publishing deltat*pi for numsteps steps # this_time = 0.0 value = pi for t in range(5, 10): val = value currenttime = h.helicsFederateRequestTime(vfed, t) h.helicsPublicationPublishDouble(pub, val) print("PI SENDER: Sending value pi = {} at time {} to PI RECEIVER".format( val, currenttime)) h.helicsEndpointSendEventRaw(epid, "endpoint2", str(t), t) time.sleep(1) h.helicsFederateFinalize(vfed) print("PI SENDER: Federate finalized") while h.helicsBrokerIsConnected(broker): time.sleep(1) h.helicsFederateFree(vfed) h.helicsCloseLibrary() print("PI SENDER: Broker disconnected")