Exemplo n.º 1
0
def get_data_from_sensor(sensor_name):
    FIFO_NAME = Pipes.FIFO_REQUEST
    FIFO_NAME_ANSWER = Pipes.FIFO_ANSWER
    my_answer_pipe = Pipes()

    fifo_writer = os.open(FIFO_NAME, os.O_WRONLY)
    ret_val = "NOK", 400

    try:
        my_answer_thread = Thread(target=my_answer_pipe.listening_pipe, args=(FIFO_NAME_ANSWER, ))
        my_answer_thread.start()
        message = create_msg(b"Hello sensor")
        os.write(fifo_writer, message)
        my_answer_thread.join()
        my_answer_pipe.kill_all_threads()
        ret_val = jsonify({"message": str(decode_value(my_answer_pipe.get_received_data()))})
    except KeyboardInterrupt as kdb_ex:
        print('Closing...')
    finally:
        os.close(fifo_writer)
    return ret_val
Exemplo n.º 2
0
from pipes import Pipes
from threading import Thread
from message_encoder import create_msg
from helper import read_temp_senzor1

import struct

if __name__ == '__main__':
    FIFO_NAME = Pipes.FIFO_REQUEST
    my_reader_pipe = Pipes()
    while my_reader_pipe.killthreads is False:
        try:
            my_thread = Thread(target=my_reader_pipe.listening_pipe,
                               args=(FIFO_NAME, ))
            my_thread.start()
            my_thread.join()

            print(my_reader_pipe.get_received_data())

            writer_endpoint = my_reader_pipe.connect_to_pipe(Pipes.FIFO_ANSWER)
            my_reader_pipe.write_to_pipe(
                writer_endpoint,
                message=create_msg(
                    bytearray(struct.pack('f', read_temp_senzor1()))))

        except KeyboardInterrupt as kbd_ex:
            print('Closing reader... ')
            my_reader_pipe.kill_all_threads()
        finally:
            pass