# simulation of a robot

from flask import Flask, request, jsonify
import requests
import time
import random

robot_control_server_url = 'http://localhost:50055/v1'

class RexRobot:
    def __init__(self, id):
        self.id = id
        self.firmwareVersion = "0.0.1"
        self.currentJobId = ""

        # connecting with robot server
        print("connecting with robot server")

        res = requests.api.post(robot_control_server_url + "/robot", 
                          json={'id': self.id, 'type': 1, 'firmwareVersion': self.firmwareVersion})

        if res.status_code == 403:
            print("permit join disabled")
            exit(1) # esp should here restart

    def setCurrentJobId(self, jobId):
        self.currentJobId = jobId

# generate random id
def randomId():
    return random.randint(0, 100)

# 10 random rexrobot

RexRobot("test")

for i in range(1):
    #RexRobot(str(randomId()))
    RexRobot("759")
    RexRobot("760")
    RexRobot("761")
    RexRobot("762")
    RexRobot("763")
    RexRobot("764")
    RexRobot("765")



#rex = RexRobot("k5")

app = Flask(__name__)

@app.route('/api/v1/control', methods=['POST'])
def hello():
    body = request.get_json()

    print("controlling robot", body)

    time.sleep(3)


    print("robot controlled")

    return jsonify({'status': 'ok'})

@app.route('/api/v1/ping', methods=['GET'])
def ping():
    print("pong")
    return jsonify({'status': 'ok'})

if __name__ == '__main__':
    app.run(debug=False, port=5003)