Ejemplo n.º 1
0
def action(q: str, a1: str = "", a2: str = "", a3: str = ""):
    if q == "login":
        try:
            dt.config["mofid_login"] = int(a1)
        except:
            return "فقط عدد مجاز است!"
        dt.config["mofid_pass"] = a2
        dt.save_config()
        return str(dt.init_mofid())

    elif q == "classify":
        global classifier
        if classifier is not None and classifier.active: return "already"
        classifier = Classify()
        classifier.start()
        return "started"

    elif q == "reset":
        c = dt.cur(True)
        try:
            for rt in fn.required_tables.keys():
                c.execute("DROP TABLE IF EXISTS " + rt)
        except:
            return "aborted"
        dt.cur(False)
        return "done"

    elif q == "check":
        c = dt.cur(True)
        c.execute("SELECT auto FROM symbol WHERE id='" + a1 + "' LIMIT 1")
        try:
            stat = c.fetchone()[0]  # int
        except IndexError:
            return "not found"
        binary = fn.auto_to_binary(stat)
        if a2 == "-1":
            binary = "".join([a3 for _ in range(len(dt.config["timeframes"]))])
        else:
            binary = list(binary)
            binary[int(a2)] = a3
            binary = "".join(binary)
        c.execute("UPDATE symbol SET auto = '" + str(int(binary, 2)) + "' WHERE id='" + a1 + "'")
        dt.connect.commit()
        dt.cur(False)
        return binary

    elif q == "analyze":
        ret = fn.persian_board(a3)
        if ret is None: return "invalid date"
        a = ret[0]
        b = ret[1]
        Analyzer.put_temp(a1, int(a2), a, b)
        return '<img src="./html/img/indicator_1.png" class="indicator">'

    elif q == "delete":
        a = b = tfr = None
        if a2 != "":
            tfr = dt.config["timeframes"][int(a2)]["value"]
        if a3 != "":
            ret = fn.persian_board(a3)
            if ret is None: return "invalid date"
            a = ret[0]
            b = ret[1]
        Analyzer.put_temp(a1, tfr, a, b, "delete")
        return "saved"

    elif q == "update_all":
        global updating
        if updating: return "already"
        updating = True
        c = dt.cur(True)
        c.execute("SELECT name FROM sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%';")
        tbs = fn.tables(c)
        dt.cur(False)
        for tb in tbs:
            if tb not in fn.required_tables:
                update_table(tb)
        updating = False
        return "saved"

    elif q == "update_symbol":
        c = dt.cur(True)
        c.execute("SELECT name FROM sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%';")
        tbs = fn.tables(c)
        dt.cur(False)
        for tb in tbs:
            if tb.startswith("s" + str(a1) + "_"):
                update_table(tb)
        return "saved"

    elif q == "update_table":
        since = None
        if a3 != "":
            try:
                since = fn.persian_date(a3)
            except:
                return "invalid date"
        tb = "s" + str(a1) + "_" + dt.config["timeframes"][int(a2)]["name"].lower()
        update_table(tb, since)
        return "saved"

    elif q == "change_timeframe":
        which = -1
        for tfr in range(len(dt.config["timeframes"])):
            if dt.config["timeframes"][tfr]["name"] == a1:
                which = tfr
        if which != -1:
            dt.config["timeframes"].pop(which)
        else:
            for ctf in fn.all_timeframes:
                if ctf["name"] == a1:
                    dt.config["timeframes"].append(ctf)
            dt.config["timeframes"] = sorted(dt.config["timeframes"], key=lambda i: i['value'])
        dt.save_config()
        return str(not (which != -1))

    elif q == "shutdown":
        mt5.shutdown()
        dt.connect.close()
        os.kill(os.getpid(), signal.SIGTERM)

    else:
        return 500