1

Trying to create Geofence in Wialon through Python

(04/10/2024 00:58:09 отредактировано johnied21)

Тема: Trying to create Geofence in Wialon through Python

Hello,

I am trying to create a new geozone, or to update an existing one, using python requests.
The command I am using is update_zone.
But whatever I am trying the result is always the same, WRONG_PARAMS.

I suspect that the problem is with the resource id ("rid"), or the geofence flag("f") I am using, but what is the way to find the existing geozone's rid and flag?

The first part of the script logs in succesfully btw, so no worries there.
Below is the python code for both tasks:

import requests
import json


base_url = hst-api.wialon.com #(no link accepted)

token = "token"

def wialon_login(token):
    # Parameters for login request
    params = {
        'svc': 'token/login',
        'params': '{"token": "' + token + '"}'
    }
    
    # Make the request
    response = requests.get(base_url, params=params)
    
    # Parse response
    if response.status_code == 200:
        result = response.json()
        if 'eid' in result:
            session_id = result['eid']  # This is your session ID
            print(f"Successfully logged in! Session ID: {session_id}")
            return session_id
        else:
            print(f"Login failed: {result}")
            return None
    else:
        print(f"Error: {response.status_code} - {response.text}")
        return None

def update_geozone(session_id):
    # Wialon API endpoint for creating a line layer
    BASE_URL = hst-api.wialon.com/wialon/ajax.html?sid={session_id} #(no links accepted)
    url = f"{BASE_URL}&svc=resource/update_zone&"   
    print(url)
    # Parameters for the request
    params = {
            "t":3, # type: 1 - line, 2 - polygon, 3 - circle 
            "w":5000, #line thickness or circle radius 
            "f":590, # geofence flags (see below) 
            "c":2568583984, #color (ARGB)
            "tc":16733440, #text color RGB
            "ts":12, #font size
            "min":0, #show on map from this zoom
            "max":18, #show on map till this zoom 
            "libId":"", #id of icon library , 0 - id for default icon library 
            "path":"", #short path to default icon
#            "p":[{"x":-3.715235,"y":40.415273,"r":10000}], #
#            "itemId":37, #
            "id":0, # zone id inside resource/account zone array
            "callMode":"update", #
            "rid":''
        }
    response = requests.post(url, json={"params": params})
    return response.json()

def create_geozone(session_id, x, y):
    # Wialon API endpoint for creating a line layer
    BASE_URL = hst-api.wialon.com/wialon/ajax.html?sid={session_id} #(no links accepted)
    url = f"{BASE_URL}&svc=resource/update_zone&"   
    print(url)
    # Parameters for the request
    params = {
            "n": 'test1asdiadgiasdgi asdadsuaids',       # Name of the geofence
            "itemId":37,
            "rid":'dionsa',
            "id": 1,            # Resource ID (0 for global resource)
            "callMode": "create",
            "t": 3,            # Type of geofence: 3 for circle
            "p":[{ "x":x, "y":y, "r":1000 }]
        }
    response = requests.post(url, json={"params": params})
    return response.json()

session_id = wialon_login(token)
geozone_update = update_geozone(session_id)
geozone_create = create_geozone(session_id, 38.2658, 23.7273)
wialon_logout(session_id)

I am grateful for your time.

2

Trying to create Geofence in Wialon through Python

Re: Trying to create Geofence in Wialon through Python

Dear johnied21 , the resource/update_zone do not contain rid parameter, the recourse ID should be passed in itemId.
Please, check all required parameters here -- https://sdk.wialon.com/wiki/en/sidebar/ … pdate_zone
In order to find all resources and geofences inside them, you may use core/search_items requests with the flags value of 4097.
More information about the search request -- https://sdk.wialon.com/wiki/en/sidebar/ … arch_items

Anton Zinovyev
Technical Care Engineer (L2)
Wialon
3

Trying to create Geofence in Wialon through Python

(06/10/2024 20:11:13 отредактировано johnied21)

Re: Trying to create Geofence in Wialon through Python

Thank you very much for the quick reply.

You are right, no rid parameter is needed.
The process I am trying to implement is the following:
1. Login (success)
2. Get Resource List - a list with the following elements is returned: nm, cls, id, mu, uacl. I am saving the id in a new variable assuming this is the resource_ID. Am I right to assume that?
3. After that, I am trying to modify the color of an existing geozone. Hovering over the specific geozone a the Geofences tab, I get FID and ObjectID of the Geozone (as seen in the attached .png).
4. Finally I am trying to run the update geozone function as stated below:

def update_geozone_2(session_id):
    params = {
        'svc': 'resource/update_zone',
        'params': '{"itemId":[b][i]resource_ID[/i][/b], "id":[i][b]Geozone_OLD_ID [/b][/i], "callMode":"update","c":2568583984,"oldItemId":[b][i]resource_ID[/i][/b] , "oldZoneId":[i][b]Geozone_OLD_ID [/b][/i] }',
#        'params': '{"spec":{"callMode":"update","c":2568583984,"oldItemId":1 , "oldZoneId":1}}',
        'sid': session_id
    }
    
    # Make the request
    response = requests.get(base_url, params=params)
    
    # Parse the response
    if response.status_code == 200:
        result = response.json()
        print("Update Geozone:", result)
    else:
        print(f"Error: {response.status_code} - {response.text}")

But I get the following error:

{'error': 4, 'reason': 'VALIDATE_PARAMS_ERROR: {n: text, t: uint, w: uint, f: uint, c: uint, p: [{x: double, y: double, r: uint}]}'}

Since I am trying to update an existing Geozone, I should only be providing oldItemId (resource_ID) and oldZoneId (Existing Geozone ID), am I right?
Also which of the two IDs of the Geozone should I provide, FID or ObjectID?

Thank you very much for your time.