|
| 1 | +"""First Service for example. Sends a message to service two and emits an event for the client.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from intersect_sdk import ( |
| 6 | + HierarchyConfig, |
| 7 | + IntersectBaseCapabilityImplementation, |
| 8 | + IntersectDirectMessageParams, |
| 9 | + IntersectEventDefinition, |
| 10 | + IntersectService, |
| 11 | + IntersectServiceConfig, |
| 12 | + default_intersect_lifecycle_loop, |
| 13 | + intersect_event, |
| 14 | + intersect_message, |
| 15 | + intersect_status, |
| 16 | +) |
| 17 | + |
| 18 | +logging.basicConfig(level=logging.INFO) |
| 19 | +logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + |
| 22 | +class ExampleServiceOneCapabilityImplementation(IntersectBaseCapabilityImplementation): |
| 23 | + """Service One Capability.""" |
| 24 | + |
| 25 | + @intersect_status() |
| 26 | + def status(self) -> str: |
| 27 | + """Basic status function which returns a hard-coded string.""" |
| 28 | + return 'Up' |
| 29 | + |
| 30 | + @intersect_message() |
| 31 | + def pass_text_to_service_2(self, text: str) -> None: |
| 32 | + """Takes in a string parameter and sends it to service 2.""" |
| 33 | + logger.info('maing it to service one') |
| 34 | + msg_to_send = IntersectDirectMessageParams( |
| 35 | + destination='example-organization.example-facility.example-system.example-subsystem.service-two', |
| 36 | + operation='ServiceTwo.test_service', |
| 37 | + payload=text, |
| 38 | + ) |
| 39 | + |
| 40 | + # Send intersect message to another service |
| 41 | + self.intersect_sdk_call_service(msg_to_send, self.service_2_handler) |
| 42 | + |
| 43 | + @intersect_event(events={'response_event': IntersectEventDefinition(event_type=str)}) |
| 44 | + def service_2_handler(self, msg: str) -> None: |
| 45 | + """Handles response from service two and emites the response as an event for the client.""" |
| 46 | + logger.info('maing it to right before emitting event') |
| 47 | + self.intersect_sdk_emit_event('response_event', f'Received Response from Service 2: {msg}') |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + from_config_file = { |
| 52 | + 'brokers': [ |
| 53 | + { |
| 54 | + 'username': 'intersect_username', |
| 55 | + 'password': 'intersect_password', |
| 56 | + 'port': 1883, |
| 57 | + 'protocol': 'mqtt3.1.1', |
| 58 | + }, |
| 59 | + ], |
| 60 | + } |
| 61 | + config = IntersectServiceConfig( |
| 62 | + hierarchy=HierarchyConfig( |
| 63 | + organization='example-organization', |
| 64 | + facility='example-facility', |
| 65 | + system='example-system', |
| 66 | + subsystem='example-subsystem', |
| 67 | + service='service-one', |
| 68 | + ), |
| 69 | + status_interval=30.0, |
| 70 | + **from_config_file, |
| 71 | + ) |
| 72 | + capability = ExampleServiceOneCapabilityImplementation() |
| 73 | + capability.capability_name = 'ServiceOne' |
| 74 | + service = IntersectService([capability], config) |
| 75 | + logger.info('Starting service one, use Ctrl+C to exit.') |
| 76 | + default_intersect_lifecycle_loop( |
| 77 | + service, |
| 78 | + ) |
0 commit comments