コード例 #1
0
ファイル: dev_api.py プロジェクト: bovenyan/WL_dev
def dev_report_done(devId):
    content = request.json
    if not content:
        return jsonify({})

    if isinstance(content, dict):
        if ("servo" in content and content["servo"]):  # servo
            pos_x = int(content["pos_x"])
            pos_y = int(content["pos_y"])
            db_api.device_update_pos(devId, pos_x, pos_y)
            db_api.device_reset_user(devId)

        if ("picture" in content and content["picture"]):  # picture
            db_api.device_reset_user(devId)

        if ("ssh" in content and content["ssh"]):  # ssh   # tested
            db_api.device_reset_user(devId)

        # if ("update" in content):
        #     pass

        # if ("commit" in content):
        #     pass

    return jsonify({"success": True})
コード例 #2
0
ファイル: dev_api.py プロジェクト: bovenyan/WL_dev
def dev_check_status(devId):
    global ssh_timeout
    global manage_timeout
    global dev_ip_map

    record = db_api.device_get_rec(devId)

    if record is None:
        abort(400)

    reply = {"mode": "operation", "options": {}, "reason": None}
    # [mode, options, reason]

    last_updated = record[0]
    manage_flags = int(record[1])

    # TODO record device ip
    # if request.headers.getlist("X-Forwarded-For"):
    #     dev_ip_map[devId] = request.headers.getlist("X-Forwarded-For")[0]
    # else:
    #     dev_ip_map[devId] = request.remote_addr

    # RESET MODE OR FLOP TO RESET
    if ((manage_flags >> 4) % 2 != 0):  # reset
        db_api.device_reset_mgmt(devId)
        db_api.device_reset_op(devId)
        reply["mode"] = "reset"
        manage_timeout = int(config.get('opconfig', 'manageTO'))
        ssh_timeout = int(config.get('opconfig', 'sshTO'))
        return jsonify(reply)

    if (datetime.now() - last_updated > timedelta(0, ssh_timeout,  # ssh timeout
                                                  0)):
        db_api.device_reset_mgmt(devId)
        db_api.device_reset_op(devId)
        reply["mode"] = "reset"
        manage_timeout = int(config.get('opconfig', 'manageTO'))
        ssh_timeout = int(config.get('opconfig', 'sshTO'))
        return jsonify(reply)   # reset ssh

    # OPERATIONAL MODE
    if (manage_flags % 2 == 0):  # operation
        reply["mode"] = "operation"
        db_api.device_reset_user(devId)
        return jsonify(reply)

    # MANAGEMENT MODE
    reply["mode"] = "management"
    # management timeout
    if (datetime.now() - last_updated > timedelta(0, manage_timeout,
                                                  0)):
        db_api.device_reset_mgmt(devId)
        db_api.device_reset_op(devId)
        reply["mode"] = "operation"
        return jsonify(reply)   # flop to operation

    # no user_bz or result not ready
    if ((manage_flags >> 2) % 2 == 0 or (manage_flags >> 3) % 2 != 0):
        return jsonify(reply)  # do nothing , keep managing

    # management options
    op_codes = int(record[4])

    # exam servo turning
    servo_active = bool(op_codes % 2)
    if (servo_active):
        reply["options"]["type"] = "servo"
        reply["options"]["servo_turn_mode"] = bool((op_codes >> 1) % 2)
        reply["options"]["servo_inc_xy"] = int((op_codes >> 2) % 4)
        reply["options"]["pos_x"] = int(record[2])
        reply["options"]["pos_y"] = int(record[3])
        print "reply: " + str(reply)
        db_api.device_reset_user(devId)  # reset for next immediately
        return jsonify(reply)

    # picture taking
    picture = bool((op_codes >> 4) % 2)   # tested
    if (picture):
        reply["options"]["type"] = "picture"
        db_api.device_reset_user(devId)  # reset for next immediately
        return jsonify(reply)

    # ssh enable
    ssh_enable = bool((op_codes >> 5) % 2)   # tested
    ssh_disable = bool((op_codes >> 6) % 2)   # tested
    if (ssh_enable and not ssh_disable):
        reply["options"]["type"] = "ssh"
        reply["options"]["op"] = "start"
        db_api.device_reset_user(devId)  # reset for next immediately
        return jsonify(reply)

    if (ssh_enable and ssh_disable):
        # kill_pids_of_port(10000+devId)
        reply["options"]["type"] = "ssh"
        db_api.device_reset_user(devId)  # reset for next immediately
        ssh_timeout = int(config.get('opconfig', 'sshTO'))
        reply["options"]["op"] = "restart"
        return jsonify(reply)

    if (ssh_disable and not ssh_enable):
        # kill_pids_of_port(10000+devId)
        reply["options"]["type"] = "ssh"
        db_api.device_reset_user(devId)  # reset for next immediately
        reply["options"]["op"] = "stop"
        ssh_timeout = int(config.get('opconfig', 'sshTO'))
        return jsonify(reply)

    # TODO script updating
    # update = bool((op_codes >> 6) % 2)
    # commit = False
    # if (not update):  # update and commit cannot happen together
        # commit = bool((op_codes >> 7) % 2)
    #    pass

    # enable management
    db_api.device_reset_user(devId)
    return jsonify(reply)