Skip to content
Data & Connectivity > AMI Client

Python Client

Overview

The Python Client provides users the option to interface with AMI from external Python applications.

It is built on the same foundation as the Java Client and shares the same interface.

To quickly configure and set up, follow the steps below.

Setup

Requirements

  1. AMI Python Client files

    • Available upon request
    • Please contact us at support@3forge.com to have the latest version assigned to your account
  2. chardet library

    • Install via pip into your Python environment:

      pip install chardet
      

Quickstart: AMI Client Python Code

from com.f1.ami.client import AmiClient
from com.f1.ami.listener import AmiClientListenerInterface
from com.f1.utils.options import Options

class TestClient(AmiClientListenerInterface):
    "A script demonstrating how this client can be used to connect to AMI"
    def __init__(self) -> None:
        super().__init__()

        ad = AmiClient()
        options = Options.ENABLE_SEND_SEQNUM + Options.ENABLE_SEND_TIMESTAMPS
        ad.addListener(self)

        # ad.setDebugMessages()

        # Connecting to the AMI instance, replace with appropriate connection parameters
        ad.start("127.0.0.1", 3289, "demo", options)

        # Sending a row of data to the table 'Sample'
        ad.startObjectMessage("Sample")
        ad.addMessageParamString("Symbol", "GBP")
        ad.addMessageParamFloat("Quantity", 11.0)
        ad.sendMessageAndFlush()

        # print(ad.getOutputBuffer())

        ad.close()

    def onMessageSent(self, client: AmiClient, message: str):
        print("msg:" + message)

    def onConnect(self, client: AmiClient):
        print("Connected to server!")

    def onDisconnect(self, client: AmiClient):
        print("Disconnected from server!")

    def onLoggedIn(self, client: AmiClient):
        print("Logged in to AMI!")

    def onMessageReceived(self, client: AmiClient, now, seqnum, status, message: str):
        print(f"Message received from server '{message}' at {now} with sequence number {seqnum} and status {status}")

test = TestClient()

Register and Send Commands

Commands can be created and registered to AMI via the AmiClientCommandDef class. This defines the command and how it appears in AMI.

Any behavior and callbacks invoked in AMI by the command must then be configured in the Client. This is done with the AmiClientListenerInterface onCommand() method.

The examples below show how to register a command within your Client application and process it.

Note

This is equivalent to sending a "Command" AMI Realtime Message from the Client and receiving a "Response" message from AMI. See this page for more information.

Example: Register Commands Python Code

1
2
3
4
5
6
#Creates a new command
#Constructor: __init__(self, id, name, argumentsjson=None, whereclause="True", help=None, enabledexpression="True", fields=None, filterclause=None, selectmode=None, style=None, conditions=None, level=None, priority=None):

commandDef = AmiClientCommandDef("COMMAND_ID", "Command Name",filterclause="panel.title==\"PanelName\"", fields="id")
#Sends a command (C) declaration via the AMI Client
client.sendCommandDefinition(commandDef)

Example: Process Command Callback Python Code

1
2
3
4
5
6
7
8
9
def onCommand(self, client : AmiClient, requestId : str, cmd : str, userName : str, objectType : str, objectId : str, params : dict):
        status = 1
        message = "Okay"
        #Starts a response (R) message
        client.startResponseMessage(requestId, status, message)
        #Get additional params defined by AmiClientCommandDef.setFields
        id = params.get("id")
        client.addMessageParamLong("id", id)
        client.sendMessageAndFlush()