Custom code, and what append if no data connexion?

Hi
i have 8 custom code, all are in python, with mysql request.
all work fine, but sometime 1, or 2 or more of the code crash. sometime all.
My question is:
If a custom code not succeed, what append ?
autopi don’t care ? after X time go to sleep if so ? did I need to put a special code a the end like “break” or other code to ignore the error, and try the next cron job execute to do it again and again ?
another question:
If there is no connexion because 3/4 G connexion is bad, in country side for exemple, what append to atop ? go to sleep ? what is the delay ?

Thanks

I am sorry to say that, and it may be depends of your needs, but i think you should use the classic implementation of the dongle & autopi.
I think, but correct me if i am wrong, you just want to get the pos of your dongle/car.
So, to do that, my advice should be to get a webserver, with kind of php or thing like that to receive the datas.
You should create an api nammed http/myWebsite/logbook/storage
In this page, you will take parse the data message sended from the dongle and connect to your db to save datas.
Why to do that? Because in case of connection error, the dongle will keep the data in cache and will try again until it succeed.
Hope it will help…

Do you need to add in exception handling ? Maybe something like -

    try:
        args = ['driving']
        kwargs = {
            'mode': '220',
            'pid': '101',
            'header': '7E4',
            'baudrate': 500000,
            'formula': 'bytes_to_int(message.data[53:54])',
            'protocol': '6',
            'verify': False,
            'force': True,
        }
        # note - sums are done outside of the forumla due to autopi failing
        # with 0
        #
        return (int(__salt__['obd.query'](*args, **kwargs)['value'])&4)/4
    except:
        return -1

ok, thanks… but I try a lots of time to do that… I only succeed to have json data but was unable to do more. I am not a specialist of php and other code lol :sweat_smile:

so, i use python code, more simple for me, not the good way I know…

yes, thanks.
I try before to put “try” like that and also except:
But after execpt, it try “pass” and I saw it is not good

Should I try “return -1” ?
What does it mean ?

try code but if no success then return last value ? something like that ?

Maybe you can share a code snippet for a better suggestion ?

Sure :sweat_smile:
this is one of them:

import mysql.connector

def sendin():

connection = mysql.connector.connect(host='xx.xx.xx.xxx',
                         database='xxxxx',
                         user='xxxxxxxx',
                         password='xxxxxxx')

tempin = get_tempin()
sql_insert_query = """ INSERT INTO temp_in (tempin) VALUES (%s)"""
cursor = connection.cursor()
result = cursor.execute(sql_insert_query,(tempin,))
connection.commit()

def get_tempin():
args = [‘Indoor_Temperature’]
kwargs = {
‘mode’: ‘220’,
‘pid’: ‘100’,
‘header’: ‘7B3’,
‘baudrate’: 500000,
‘formula’: ‘(bytes_to_int(message.data[8:9])/2.0)-40’,
‘protocol’: ‘6’,
‘verify’: False,
‘force’: True,
}
return salt[‘obd.query’](*args, **kwargs)[‘value’]

Like this ?

import mysql.connector
import ConnectionError

log = logging.getLogger(name)

def sendkw():
try:

    connection = mysql.connector.connect(host='xx.xx.xx.xxx',
                            database='kona',
                            user='xxxxxxxxx',
                            password='xxxxxxxxx')

    kw = get_charging_power()
    sql_insert_query = """ INSERT INTO power (kw) VALUES (%s)"""
    cursor = connection.cursor()
    result = cursor.execute(sql_insert_query,(kw,))
    connection.commit()

exept:
    ConnectionError

def get_charging_power():
try:

    args = ['charging_power']
    kwargs = {
        'mode': '220',
        'pid': '101',
        'header': '7E4',
        'baudrate': 500000,
        'formula': '((~(bytes_to_int(message.data[53:54]))&4)/4)*-((twos_comp(bytes_to_int(message.data[13:14])*256+bytes_to_int(message.data[14:15]),16)/      10.0)*((bytes_to_int(message.data[15:16])*256+bytes_to_int(message.data[16:17]))/10.0)/1000.0)',
        'protocol': '6',
        'verify': False,
        'force': True,
    }
    return __salt__['obd.query'](*args, **kwargs)['value']
    
    exept:
        return -1

So the failure cases include failure to connect to the database and failure to read data from the car. I suppose in either case the script can’t complete its job, so not much point in letting it continue.

Maybe -

import mysql.connector
log = logging.getLogger(__name__)

def sendin():
  try:
    connection = mysql.connector.connect(host='xx.xx.xx.xxx', database='xxxxx', user='xxxxxxxx', password='xxxxxxx')
  except:
    log.warn('Unable to connect to database')
    return

  try:
    tempin = get_tempin()
  except:
    log.warn('Unable to read data from car')
    return

  try:
    sql_insert_query = """ INSERT INTO temp_in (tempin) VALUES (%s)"""
    cursor = connection.cursor()
    result = cursor.execute(sql_insert_query,(tempin,))
    connection.commit()
  except:
    log.warn('Unable to update database')
    return
1 Like

Thank’s a lot.
and I put the last code “def: guet_tempin()” at the end.

I try now and reboot autopi

:smile:

still not work no data come.
but I understand the logic of it
i will try by different way :thinking: