Example #1
0
 def handle(self):
     self.data = self.request.recv(1024).strip()
     if self.data == "getStreamUrl":
         stream_url = Stream.getStream()
         self.request.sendall(stream_url)
     elif self.data == "stopStream":
         Stream.stopStream()
         self.request.sendall("StreamClosed")
class WirelessHeadset(Headset):
    """This class represents the wireless version of the mindwave

    Args:
        dev: device link 
        headset: the id of mindwave wireless version

    It has the basic functionality to connect, autoconnect and disconnect

    """

    def __init__(self, dev=None, headset_id=None, rate=None):

        Headset.__init__(self, headset_id)

        self.device = dev
        self.bauderate = rate

        self.stream = Stream(device=self.device, bauderate=rate, version=Version.MINDWAVE)
        time.sleep(2)

        self.connect()
        self.run(self.stream)

    # def open(self):
    #     if not self.stream or not self.stream.IsOpen():
    #         #self.stream = stream.stream(self.device, baudrate=115200, parity=stream.PARITY_NONE, stopbits=stream.STOPBITS_ONE,
    #         #    bytesize=stream.EIGHTBITS, writeTimeout=0, timeout=3, rtscts=True, xonxoff=False)
    #         self.stream = serial.Serial(self.device, self.baudrate, timeout=0.001, rtscts=True)

    def autoconnect(self):
        """This method autoconnects to the mindwave every."""

        self.stream.getStream().write(BytesStatus.AUTOCONNECT)  
        #the dongle switch to autoconnect mode it must wait 10 second to connect any headset
        time.sleep(10)

    def connect(self):
        """This method connects to the mindwave with the id."""

        if self.id is not None:
            # we send a byte to CONNECTED and other byte in hex of headset id
            self.stream.getStream().write(''.join([BytesStatus.CONNECT, self.id.decode('hex')]))
        else:
            self.autoconnect()

    def disconnect(self):
        """This method disconnects the mindwave."""

        self.stream.getStream().write(BytesStatus.DISCONNECT)
         
    def echo_raw(self):
        """This method prints the raw data from mindwave."""
        
        while 1:
            #time.sleep()
            data = self.stream.read(1)
    
            for b in data:
                print '0x%s, ' % b.encode('hex'),
            print ""
Example #3
0
    def do_GET(self):
        stream = Stream()
        stream.setStream(output)

        tracker = Tracker()
        tracker.setStream(output)

        if self.path == '/':
            with open('templates/harper.html', 'rb') as fh:
                html = fh.read()
                content = html
            self.send_response(200)
            self.send_header('Content-Type', 'text/html')
            self.send_header('Content-Length', len(content))
            self.end_headers()
            self.wfile.write(content)
        if self.path == '/harper.css':
            with open('templates/harper.css', 'rb') as fh:
                html = fh.read()
                content = html
            self.send_response(200)
            self.send_header('Content-Type', 'text/css')
            self.send_header('Content-Length', len(content))
            self.end_headers()
            self.wfile.write(content)
        elif self.path == '/stream':
            self.send_response(200)
            self.send_header('Age', 0)
            self.send_header('Cache-Control', 'no-cache, private')
            self.send_header('Pragma', 'no-cache')
            self.send_header('Content-Type',
                             'multipart/x-mixed-replace; boundary=FRAME')
            self.end_headers()
            try:
                while True:
                    stream.setLastSpeed(tracker.getLastSpeed())
                    stream.setSpeed(tracker.getSpeed())
                    stream.setSpeedInt(tracker.getSpeedInt())
                    stream.setMountColor(tracker.getMotionColor())
                    stream.setMonitoredBoundary(tracker.getMonitoredBoundary())

                    with output.condition:
                        output.condition.wait()

                    if stream.getStream() == None:
                        frame = output.frame
                    else:
                        frame = stream.getStream()

                    Thread(target=tracker.rawStream, args=()).start()
                    Thread(target=stream.liveStream, args=()).start()

                    # ret, frame = camera.read()
                    # ret, jpeg = cv2.imencode('.jpeg', frame)
                    # frame = jpeg.tobytes()

                    self.wfile.write(b'--FRAME\r\n')
                    self.send_header('Content-Type', 'image/jpeg')
                    self.send_header('Content-Length', len(frame))
                    self.end_headers()
                    self.wfile.write(frame)
                    self.wfile.write(b'\r\n')
            except Exception as e:
                logging.warning('Removed streaming client %s: %s',
                                self.client_address, str(e))
        else:
            self.send_error(404)
            self.end_headers()