""" V200727 xiaogui robot car runner bar help: python runbar.py xxxxx.py """ import os, json, re, sys from tkinter import * from tkinter import ttk, tix, filedialog, messagebox, scrolledtext import urllib.request import urllib.parse class Car(): def __init__(self): self.car_ip = "" self.car_name = "" self.car_url = "" self.source_path = "" self.cars = [] def set_source(self, source_path): self.source_path = source_path def list(self): url = "https://api.guidan.com/my/car.list" headers = {} req = urllib.request.Request(url=url, headers=headers) resp = urllib.request.urlopen(req) data = resp.read() self.cars = json.loads(data) return self.cars def get_carlist(self): carlist = [] for one in self.cars: carlist.append("%s - %s"%(one["name"], one["ip"])) return carlist if carlist else ["no car"] def auto_select(self): if not self.cars: return True for one in self.cars: if one["ip"] == self.car_ip: return True one = self.cars[0] self.car_ip, self.car_name, self.car_url = one["ip"], one["name"], "http://%s/"%(one["ip"],) def select(self, car_ip, car_name): self.car_ip = car_ip self.car_name = car_name self.car_url = "http://%s/"%(self.car_ip,) def exec(self, code): url = self.car_url + "py" params = b"code=" + urllib.parse.quote(code).encode() req = urllib.request.Request(url=url, data=params, method="POST") resp = urllib.request.urlopen(req, timeout=4) if resp.getcode()!=200: return (False, "") data = resp.read() return (True, json.loads(data)) def exec_source(self): if (not self.source_path) or (not os.path.exists(self.source_path)): return with open(self.source_path, "r") as fp: code = fp.read() if not code: return self.exec(code) def log(self): url = self.car_url + "log" req = urllib.request.Request(url=url) resp = urllib.request.urlopen(req) if resp.getcode()!=200: return (False, "") data = resp.read() return (True, json.loads(data)) class Form(): RE_IP = re.compile(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}") def __init__(self, source=""): self.car = Car() self.car.list() self.__runbar = Tk() self.__runbar.title("RunnerBar") self.__runbar.geometry("280x35") self.__runbar.resizable(0, 0) self.__runbar.wm_attributes("-topmost", 1) self.__btn_run = Button(self.__runbar, text="Run", command=self.__btn_run_click) self.__btn_run.grid(row=0, column=0, padx=2, sticky=W+E+S+N) self.__btn_code = Button(self.__runbar, text="File", command=self.__btn_code_click) self.__btn_code.grid(row=0, column=2, padx=2, sticky=W+E+S+N) self.__cmb_carlist = ttk.Combobox(self.__runbar) self.__cmb_carlist["values"] = self.car.get_carlist() self.__cmb_carlist.current(0) self.__cmb_carlist.grid(row=0, column=1, padx=2, sticky=W+E+S+N) self.__txt_log = scrolledtext.ScrolledText(self.__runbar, width=35, height=4) self.__txt_log.grid(row=1, column=0, columnspan=3, pady=5) if os.path.exists(source): _, basename = os.path.split(source) self.car.set_source(source) self.__runbar.title(basename) self.__runbar.mainloop() # def action_get_log(): # txt_log.insert(END, code) def __btn_run_click(self, *args): info = self.__cmb_carlist.get() rst = self.RE_IP.search(info) if (not rst) or (not rst.group()): messagebox.showerror("Error", "Please select a car!") return self.car.select(rst.group(), "") if not self.car.source_path: messagebox.showerror("Error", "Please select a python file!") return try: self.car.exec_source() except BaseException as _: messagebox.showerror("Error", "Run fail, maybe car is power off!") def __btn_code_click(self, *args): file_path = filedialog.askopenfilename(title="Select a python file", filetypes=[("Python File(.py)", "*.py")]) if (not file_path) or (not os.path.exists(file_path)): return _, basename = os.path.split(file_path) self.car.set_source(file_path) self.__runbar.title(basename) def proc(): if not source_file: return False car = Car() car.list() if len(car.cars)!=1: return False car.auto_select() car.set_source(source_file) if not car.car_ip: return False try: car.exec_source() except: messagebox.showerror("Error", "Run fail, maybe car is power off!") return True source_file = "" if len(sys.argv)>1 and os.path.exists(sys.argv[1]): source_file = sys.argv[1] if not proc(): form = Form(source=source_file)