예제 #1
0
파일: api.py 프로젝트: jjoaosilva/horus
def all_sensor():
    sensor_all = []
    udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    for sensor in sensors:
        dest = (sensor['ip'], sensor['port'])
        udp.sendto (bytes('/GET', 'utf-8'), dest)
        msg, cliente = udp.recvfrom(1024)
        response = sensor_pb2.Sensor()
        response.ParseFromString(msg)

        sensor_all.append({'name': response.nome, 'status': response.status})
    return jsonify({'sensors': sensor_all})
예제 #2
0
파일: api.py 프로젝트: jjoaosilva/horus
def sensor_set():
    content = request.get_json(silent = True)

    get_sensor = None

    for sensor in sensors:
        if(sensor['name'] == content['sensor']):
            get_sensor = sensor
            break

    udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    dest = (get_sensor['ip'], get_sensor['port'])
    udp.sendto (bytes('/SET', 'utf-8'), dest)
    msg, cliente = udp.recvfrom(1024)
    response = sensor_pb2.Sensor()
    response.ParseFromString(msg)

    return jsonify({'name': response.nome, 'status': response.status})
예제 #3
0
파일: api.py 프로젝트: jjoaosilva/horus
def descoberta():
    global sensors
    sensors = []
    SERVER_IP = ""
    SERVER_PORT = 5000
    multicast = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    multicast.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
    multicast.bind((SERVER_IP, SERVER_PORT))
    multicast.settimeout(2)
    for i in range(1, 4):
        multicast.sendto(bytes('/INFO','utf-8'),('<broadcast>', 5000+i))
        try:    
            msg_rx, client = multicast.recvfrom(1024)
            sensorLerDados = sensor_pb2.Sensor()
            sensorLerDados.ParseFromString(msg_rx)
            sensors.append({'name': sensorLerDados.nome, 'ip': client[0], 'port': client[1]})
        except:
            print('')
    print(sensors)
    multicast.close()
예제 #4
0
 def debug(self):
     sensor = sensor_pb2.Sensor()
     sensor.odometry_left   = 23
     sensor.odometry_right  = 42
     sensor.battery_voltage = 12.23
     sensor.temperature     = 22.22
     sensor.ultrasonic_01   = 23
     sensor.ultrasonic_02   = 23
     sensor.ultrasonic_03   = 23
     sensor.ultrasonic_04   = 23
     sensor.ultrasonic_05   = 23
     sensor.ultrasonic_06   = 23
     sensor.ultrasonic_07   = 23
     sensor.ultrasonic_08   = 23
     sensor.ultrasonic_09   = 23
     sensor.ultrasonic_10   = 23
     self._receiveQueue.put(sensor)
     status               = status_pb2.Status()
     status.version       = 42
     status.uptime        = 2232
     status.sensorInError = 4
     status.debug         = 666
     self._receiveQueue.put(status)
예제 #5
0
    def run(self):

        state         = 0
        hexsize       = ''
        messagetype   = 0
        messagelength = 0

        while True:

            while state < 7:

                c = self._ser.read()

                if state == 0:
                    if ord(c) == 0x41:
                        state += 1
                        print ("A")
                        continue
                    else:
                        state = 0

                if state == 1:
                    if ord(c) == 0x4E:
                        state += 1
                        print ("N")
                        continue
                    else:
                        state = 0

                if state == 2:
                    if ord(c) == 0x53:
                        state += 1
                        print ("S")
                        continue
                    else:
                        state = 0

                if state == 3:
                    if ord(c) == 0x49:
                        state += 1
                        print ("I")
                        continue
                    else:
                        state = 0

                if state == 4:
                    messagetype = ord(c)
                    state += 1
                    print ("MessageType: %d" % messagetype)
                    continue

                if state == 5:
                    hexsize = c
                    state += 1
                    print ("size")
                    continue

                if state == 6:
                    hexsize += c
                    state += 1
                    messagelength = unpack('<H', hexsize)[0]
                    print ("size:%d" % messagelength)
                    continue

            state = 0
            message = self._ser.read(messagelength)
            print ("Buffersize:%d" % len(message))

            if messagetype == 4:
                print ("STATUS:")
                status = status_pb2.Status()
                status.ParseFromString(message)
                print (status)
                self._queue.put(status)

            if messagetype == 3:
                print ("Sensor:")
                sensor = sensor_pb2.Sensor()
                sensor.ParseFromString(message)
                print (sensor)
                self._queue.put(sensor)

            if messagetype == 2:
                print ("Motor:")
                motor = motor_pb2.Motor()
                motor.ParseFromString(message)
                print (motor)
                self._queue.put(motor)

            if messagetype == 1:
                print ("Config:")
                config = config_pb2.Config()
                config.ParseFromString(message)
                print (config)
                self._queue.put(config)
예제 #6
0
    elif (info == bytes('/INFO', 'utf-8')):
        return True

    else:
        return False


HOST = ''
PORT = 5001

udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
orig = (HOST, PORT)
udp.bind(orig)

cafeteira = sensor_pb2.Sensor()
cafeteira.nome = "Cafeteira"
cafeteira.status = "Desligada"

while True:
    msg, cliente = udp.recvfrom(1024)
    print(cliente, msg)
    if (msg == bytes("DES", 'utf-8')):
        break

    if (process(msg) == True):
        udp.sendto(cafeteira.SerializeToString(), cliente)
    else:
        udp.sendto(bytes('False', 'utf-8'), cliente)

udp.close()
예제 #7
0
    elif (info == bytes('/INFO', 'utf-8')):
        return True

    else:
        return False


HOST = ''
PORT = 5003

udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
orig = (HOST, PORT)
udp.bind(orig)

lampada = sensor_pb2.Sensor()
lampada.nome = "Lampada"
lampada.status = "Desligada"

while True:
    msg, cliente = udp.recvfrom(1024)
    print(cliente, msg)
    if (msg == bytes("DES", 'utf-8')):
        break

    if (process(msg)):
        udp.sendto(lampada.SerializeToString(), cliente)
    else:
        udp.sendto(bytes('False', 'utf-8'), cliente)

udp.close()
예제 #8
0
    elif (info == bytes('/INFO', 'utf-8')):
        temperatura.status = str(random.randint(-10, 40))
        return True

    else:
        return False


HOST = ''
PORT = 5002

udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
orig = (HOST, PORT)
udp.bind(orig)

temperatura = sensor_pb2.Sensor()
temperatura.nome = "Temperatura"
temperatura.status = '0'

while True:
    msg, cliente = udp.recvfrom(1024)
    print(cliente, msg)
    if (msg == bytes("DES", 'utf-8')):
        break

    if (process(msg)):
        udp.sendto(temperatura.SerializeToString(), cliente)
    else:
        udp.sendto(bytes('False', 'utf-8'), cliente)

udp.close()