Skip to content

Return messages for 2 vessels from a list fo 15  #11

@StHermes

Description

@StHermes

Hi, i created a list with MMSI numbers and the code below:
async def connect_ais_stream():

ship_columns1 = ['Last Update', 'Ship ID', 'Latitude', 'Longitude', 'TrueHeading']
ship_columns2 = ['Last Update', 'Ship ID','Name', 'Dest.', 'ETA',]
ship_dataframe1 = pd.DataFrame(columns = ship_columns1)
ship_dataframe2 = pd.DataFrame(columns = ship_columns2)
running_list1 = []
running_list2 = []
ship_list = [240848000,636020291,241387000,241409000,636021702,241410000,240849000,241389000,241388000,241107000,636020889,240901000]
# Use a for loop to create a new connection every time the previous one is closed
for _ in range(10): # You can change the number of attempts here
    try:
        async with websockets.connect("wss://stream.aisstream.io/v0/stream", ping_interval=None) as websocket: # Disable the ping interval here
            subscribe_message = {"APIKey": "ed151fd1e624bf331d94d6411e73cbeea72b7d35", "BoundingBoxes": [[[-90, -180], [90, 180]]]}

            subscribe_message_json = json.dumps(subscribe_message)
            await websocket.send(subscribe_message_json)
           
           

            async for message_json in websocket:
                message = json.loads(message_json)
                message_type = message["MessageType"]
               

                if message_type == "PositionReport":
                    # the message parameter contains a key of the message type which contains the message itself
                    ais_message = message['Message']['PositionReport']
                    ShipId =  int(ais_message['UserID'])
                    
                    if ShipId in ship_list:
                        #running_list = ["none", "none","none", "none","none", "none","none", "none"]
                    #print(f"[{datetime.now(timezone.utc)}] ShipId: {ais_message['UserID']} Latitude: {ais_message['Latitude']} Longitude: {ais_message['Longitude']} Heading: {ais_message['TrueHeading']}")
                        running_list1.append(datetime.now().strftime('%d-%m-%Y %H:%M:%S'))
                        running_list1.append(int(ais_message['UserID']))
                        running_list1.append(ais_message['Latitude'])
                        running_list1.append(ais_message['Longitude'])
                        running_list1.append(ais_message['TrueHeading'])
                        
                        ship_dataframe1.loc[len(ship_dataframe1)] = running_list1
                       # ship_dataframe1 = ship_dataframe1.drop_duplicates(subset=['Ship ID'], keep="last")
                        ship_dataframe1.to_pickle("ship_dataframe1.pkl")
                        running_list1 = []
                        print(ship_dataframe1) 

and i constantly receive data for only 2 vessels out of 12

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions