1

Generating messages layer with python

(01/12/2016 09:46:14 отредактировано florin1)

Тема: Generating messages layer with python

Hello guys. I need some help creating messages layer for downloading history period of units. I have this code, works ok getting the units list, but i get this error "WialonError(Invalid input render_create_messages_layer (4))" from the render_create_messages_layer. Any advice? Thanks

from wialon.api import Wialon, WialonError
from wialon import flags
import json

starttime = "1478736000"
endtime = "1479600000"

try:
    wialon_api = Wialon()
    result = wialon_api.token_login(token='token')
    wialon_api.sid = result['eid']

    result = wialon_api.avl_evts()

    print result

    spec = {
        'itemsType': 'avl_unit',
        'propName': 'sys_name',
        'propValueMask': '*',
        'sortType': 'sys_name'
    }
    
  

    interval = {"from": 0, "to": 0}

    try:
        units = wialon_api.core_search_items(spec=spec, force=1, flags=flags.ITEM_DATAFLAG_BASE, **interval)
        for i in units['items']:
            
            gen = {
                'layerName': 'arhivamesaje',
                'itemId': i['id'],
                'timeFrom': starttime,
                'timeTo': endtime,
                'tripDetector': 0,
                'trackColor': 'cc713cff',
                'trackWidth':5,
                'arrows':1,
                'points':1,
                'pointColor':0,
                'annotations':1,
                'flags':0
            }
            
            print i['nm']
            try:
                meslayer = wialon_api.render_create_messages_layer(spec=gen)
            except WialonError as e:
                print e
    except WialonError as e:
        print e


    wialon_api.core_logout()
except WialonError as e:
    pass
2

Generating messages layer with python

Re: Generating messages layer with python

Hello

You passed wrong params to render_create_messages_layer call

// Wrong
meslayer = wialon_api.render_create_messages_layer(spec=gen)
// Right
meslayer = wialon_api.render_create_messages_layer(**gen) 
3

Generating messages layer with python

Re: Generating messages layer with python

Thanks shmi. It Works

4

Generating messages layer with python

Re: Generating messages layer with python

Another question.
This python script that i made, runs every night to generate and download the messages archive for all the units of a specific account, from the day before. The problem is that it generates messages from the day before at hour 02:00:32 am to next day at hour 01:59:47 am, not from 00:00:00 day before to 00:00:00 current day.

Any ideea why? Thanks

from wialon.api import Wialon, WialonError
from wialon import flags
import json
import urllib
import time
import datetime
import dateutil.relativedelta
import calendar
import os
import re


d = datetime.datetime.now().date()
d2 = d - datetime.timedelta(days=1)

timpstart = calendar.timegm(d2.timetuple())
timpsfarsit = calendar.timegm(d.timetuple())
print "Data Start " + str(d2) + ". Data sfarsit " + str(d)
print "Data start unix " + str(timpstart) + ". Data sfarsit unix " + str(timpsfarsit)

try:
    wialon_api = Wialon()
    result = wialon_api.token_login(token='TOKEEEEEENNNN')
    wialon_api.sid = result['eid']

    result = wialon_api.avl_evts()

    print result

    spec = {
        'itemsType': 'avl_unit',
        'propName': 'sys_name',
        'propValueMask': '*',
        'sortType': 'sys_name'
    }
    
  

    interval = {"from": 0, "to": 0}


    try:
        units = wialon_api.core_search_items(spec=spec, force=1, flags=flags.ITEM_DATAFLAG_BASE, **interval)
        for i in units['items']:
            layerN = i['nm']
            repl = re.sub('[() ]', '', layerN)
            gen = {
            'layerName': repl,
            'itemId': i['id'],
            'timeFrom': timpstart,
            'timeTo': timpsfarsit,
            'tripDetector': 0,
            'trackColor': 'cc713cff',
            'trackWidth': 5,
            'arrows': 1,
            'points': 1,
            'pointColor': 0,
            'annotations': 1,
            'flags': 0
            }
            
            print i['nm'] + "ID: " + str(i['id'])
            for j in range (0,5):
                try:
                    print ("Generating messages layer...")
                    meslayer = wialon_api.render_create_messages_layer(**gen)
                    if meslayer:
                        print ("Downloading unit history...")
                        papi = wialon_api.sid
                        folder = str(d2)
                        if not os.path.exists(folder):
                            os.makedirs(folder)
                            urllib.urlretrieve("https://hst-api.wialon.com/wialon/ajax.html?svc=exchange/export_messages&params={'layerName':" + str(repl) +",'format':'wlb','compress':1}&sid=" + papi +"", "" + folder + "/" + repl +".zip")
                        else:
                            urllib.urlretrieve("https://hst-api.wialon.com/wialon/ajax.html?svc=exchange/export_messages&params={'layerName':" + str(repl) +",'format':'wlb','compress':1}&sid=" + papi +"", "" + folder + "/" + repl +".zip")
                            
                   
                except WialonError as e:
                    print e
                    continue
                break
        
    except WialonError as e:
        print e


    wialon_api.core_logout()
except WialonError as e:
    pass
5

Generating messages layer with python

Re: Generating messages layer with python

florin1
I think problem in your timezone (looks like UTC+02:00)
You have to send time in UNIX time without any timezone offset