Тема: 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.