首页 > 解决方案 > Script stops after a couple of hours

问题描述

I'm using requests and getting data from API in loop like every 11 seconds and display it on LCD. It works perfectly but after couple hours I get json.decoder.jsondecodeerror expecting value line 1 column 1 (char 0) error and I have to rerun the program manually. I couldn't figure out how to fix.

This is the code. api link shows where the buses is in json format and refreshes every 11seconds. Im running it on raspbian os (raspberry pi 3) with 1602 lcd display.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import datetime
import requests
import json
import I2C_LCD_driver

# from time import *
mylcd = I2C_LCD_driver.lcd()

turkcec = [
    [0b01110,
     0b10001,
     0b10000,
     0b10000,
     0b10000,
     0b10001,
     0b01110,
     0b00100],
]


def get_data():
    url = "***"
    response = requests.get(url)
    data = response.text
    parsed = json.loads(data)
    list = []
    for i in range(len(parsed["busList"])):
        if parsed["busList"][i]["disabledPerson"] == 0:
            list.append(
                str(parsed["busList"][i]["displayRouteCode"]) + " " + parsed["busList"][i]["stopDiff"] + "drk " +
                parsed["busList"][i]["timeDiff"] + "dk " + "E")
        else:
            list.append(
                str(parsed["busList"][i]["displayRouteCode"]) + " " + parsed["busList"][i]["stopDiff"] + "drk " +
                parsed["busList"][i]["timeDiff"] + "dk")
    return list

data = get_data()
while True:
    starttime = time.time()
    while len(data) > 0:
        #time.sleep(5)
        for x in range(0, len(data), 2):
            mylcd.lcd_clear()
            print(data[x])
            if data[x][0] == "Ç":
                mylcd.lcd_load_custom_chars(turkcec)
                mylcd.lcd_write(0x80)
                mylcd.lcd_write_char(0)
                mylcd.lcd_display_string(data[x][1:], 1, 1)
            else:
                mylcd.lcd_display_string(data[x], 1)
            if x == len(data) - 1:
                time.sleep(11)
                break

            if data[x + 1][0] == "Ç":
                mylcd.lcd_load_custom_chars(turkcec)
                mylcd.lcd_write(0xc0)
                mylcd.lcd_write_char(0)
                mylcd.lcd_display_string(data[x + 1][1:], 2, 1)
            else:
                mylcd.lcd_display_string(data[x + 1], 2)
            print(data[x + 1])
            time.sleep(11)

        if (
                time.time() - starttime) >= 11.0:
            starttime = time.time()
            data = get_data()
            print("--------")
    else:
        mylcd.lcd_clear()
        while True:
            data = get_data()
            mylcd.lcd_display_string(time.strftime("%d/%m/%Y"), 1, 3)
            mylcd.lcd_display_string(time.strftime("%H:%M:%S"), 2, 4)

            time.sleep(1)
            if len(data) > 0:
                break
        continue

This is the result from api link. I need values from "busList".

{
  "result": {
    "cmd": "/api/bus/closest",
    "code": 0,
    "message": "OK",
    "dateTime": "20200215145439",
    "debugTimeDiff": "",
    "authType": ""
  },
  "stopInfo": {
    "busStopid": "18",
    "busStopName": "İSKELE",
    "lat": "40.1502179",
    "lng": "26.4031012"
  },
  "busList": [
    {
      "busId": "17059",
      "plate": "17-H-0059",
      "disabledPerson": "0",
      "displayRouteCode": "Ç3",
      "displayName": "Ç-3",
      "headSign": "Ç-3",
      "routeCode": "70003",
      "shapeId": "700030",
      "stopDiff": "2",
      "timeDiff": "1",
      "lat": "40.14762",
      "lng": "26.406181"
    },
    {
      "busId": "17070",
      "plate": "17-H-0070",
      "disabledPerson": "0",
      "displayRouteCode": "Ç8",
      "displayName": "Ç-8",
      "headSign": "Ç-8",
      "routeCode": "70008",
      "shapeId": "700080",
      "stopDiff": "2",
      "timeDiff": "5",
      "lat": "40.14622",
      "lng": "26.40825"
    },
    {
      "busId": "17076",
      "plate": "17-H-0076",
      "disabledPerson": "0",
      "displayRouteCode": "Ç1",
      "displayName": "Ç-1",
      "headSign": "Ç-1",
      "routeCode": "70001",
      "shapeId": "700010",
      "stopDiff": "3",
      "timeDiff": "9",
      "lat": "40.14187",
      "lng": "26.40788"
    },
    {
      "busId": "17034",
      "plate": "17-H-0034",
      "disabledPerson": "0",
      "displayRouteCode": "Ç7",
      "displayName": "Ç-7",
      "headSign": "Ç-7",
      "routeCode": "70007",
      "shapeId": "700070",
      "stopDiff": "9",
      "timeDiff": "14",
      "lat": "40.132263",
      "lng": "26.409042"
    },
    {
      "busId": "17029",
      "plate": "17-H-0029",
      "disabledPerson": "0",
      "displayRouteCode": "Ç3",
      "displayName": "Ç-3",
      "headSign": "Ç-3",
      "routeCode": "70003",
      "shapeId": "700030",
      "stopDiff": "5",
      "timeDiff": "14",
      "lat": "40.145737",
      "lng": "26.4206"
    },
    {
      "busId": "17018",
      "plate": "17-H-0018",
      "disabledPerson": "0",
      "displayRouteCode": "ÇT3",
      "displayName": "ÇT3",
      "headSign": "ÇT3",
      "routeCode": "70016",
      "shapeId": "700160",
      "stopDiff": "5",
      "timeDiff": "15",
      "lat": "40.14575",
      "lng": "26.420376"
    },
    {
      "busId": "17042",
      "plate": "17-H-0042",
      "disabledPerson": "0",
      "displayRouteCode": "11K",
      "displayName": "11K",
      "headSign": "11K",
      "routeCode": "70012",
      "shapeId": "700120",
      "stopDiff": "9",
      "timeDiff": "19",
      "lat": "40.12353",
      "lng": "26.410328"
    }
  ],
  "routeList": [
    {
      "routeCode": "70004",
      "displayRouteCode": "Ç4",
      "name": "Ç-4",
      "headSign": "KOCATEPE CAMİ",
      "stopArrivalTime": ""
    },
    {
      "routeCode": "70005",
      "displayRouteCode": "Ç5",
      "name": "Ç-5",
      "headSign": "SSK Durağı",
      "stopArrivalTime": ""
    },
    {
      "routeCode": "70011",
      "displayRouteCode": "11G",
      "name": "11G",
      "headSign": "SSK Durağı",
      "stopArrivalTime": ""
    },
    {
      "routeCode": "71016",
      "displayRouteCode": "960",
      "name": "960",
      "headSign": "Eski Hastane",
      "stopArrivalTime": ""
    },
    {
      "routeCode": "72011",
      "displayRouteCode": "11G-T",
      "name": "11G",
      "headSign": "NUSRET YURDU",
      "stopArrivalTime": ""
    }
  ]
}

This is the error.

Traceback (most recent call last):
  File "/home/pi/Desktop/xxx/xxx.py", line x, in <module>
    data = get_data()
  File "/home/pi/Desktop/xxx/xxx.py", line, in get_data
    parsed = json.loads(data)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I found solution. Thanks

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
import datetime
import requests
import json
import I2C_LCD_driver

# from time import *
mylcd = I2C_LCD_driver.lcd()

turkcec = [
    [0b01110,
     0b10001,
     0b10000,
     0b10000,
     0b10000,
     0b10001,
     0b01110,
     0b00100],
]
while True:
    try:
    def get_data():
        url = "***"
        response = requests.get(url)
        data = response.text
        parsed = json.loads(data)
        list = []
        for i in range(len(parsed["busList"])):
            if parsed["busList"][i]["disabledPerson"] == 0:
                list.append(
                    str(parsed["busList"][i]["displayRouteCode"]) + " " + parsed["busList"][i]["stopDiff"] + "drk " +
                    parsed["busList"][i]["timeDiff"] + "dk " + "E")
            else:
                list.append(
                    str(parsed["busList"][i]["displayRouteCode"]) + " " + parsed["busList"][i]["stopDiff"] + "drk " +
                    parsed["busList"][i]["timeDiff"] + "dk")
        return list


    data = get_data()
    while True:
        starttime = time.time()
        while len(data) > 0:
            # time.sleep(5)
            for x in range(0, len(data), 2):
                mylcd.lcd_clear()
                print(data[x])
                if data[x][0] == "Ç":
                    mylcd.lcd_load_custom_chars(turkcec)
                    mylcd.lcd_write(0x80)
                    mylcd.lcd_write_char(0)
                    mylcd.lcd_display_string(data[x][1:], 1, 1)
                else:
                    mylcd.lcd_display_string(data[x], 1)
                if x == len(data) - 1:
                    time.sleep(11)
                    break

                if data[x + 1][0] == "Ç":
                    mylcd.lcd_load_custom_chars(turkcec)
                    mylcd.lcd_write(0xc0)
                    mylcd.lcd_write_char(0)
                    mylcd.lcd_display_string(data[x + 1][1:], 2, 1)
                else:
                    mylcd.lcd_display_string(data[x + 1], 2)
                print(data[x + 1])
                time.sleep(11)

            if (
                    time.time() - starttime) >= 11.0:
                starttime = time.time()
                data = get_data()
                print("--------")
        else:
            mylcd.lcd_clear()
            while True:
                data = get_data()
                mylcd.lcd_display_string(time.strftime("%d/%m/%Y"), 1, 3)
                mylcd.lcd_display_string(time.strftime("%H:%M:%S"), 2, 4)

                time.sleep(1)
                if len(data) > 0:
                    break
            continue
    except:
        if (time.time() - starttime) >= 11.0:
            continue

标签: pythonjson

解决方案


推荐阅读