def connect(self, config):
        if self.closed:
            return
        self.config = config
        if time.monotonic() - self.lastStart < 15:
            return

        # When we reconnect we stop the recording and motion
        self.set_data_point("record", False, None, automated_record_uuid)
        self.set_data_point("raw_motion_value", 0)
        self.set_data_point("motion_detected", 0)
        self.activeSegmentDir = self.segmentDir = None

        self.lastStart = time.monotonic()

        if self.process:
            try:
                self.process.stop()
            except Exception:
                print(traceback.format_exc())

        # Used to check that things are actually still working.
        # Set them to prevent a loop.
        self.lastSegment = time.monotonic()
        self.lastPushedWSData = time.monotonic()

        # Can't stop as soon as they push stop, still need to capture
        # the currently being recorded segment
        self.stoprecordingafternextsegment = 0

        try:
            shutil.rmtree("/dev/shm/knvr_buffer/" + self.name)
        except Exception:
            pass

        os.makedirs("/dev/shm/knvr_buffer/" + self.name)

        try:
            # Make it so nobody else can read the files
            os.chmod("/dev/shm/knvr_buffer/" + self.name, 0o700)
        except Exception:
            pass

        # Close the old thread
        self.runWidgetThread = time.monotonic()
        self.putTrashInBuffer()
        s = 10
        while s:
            s -= 1
            if self.threadExited:
                break
            time.sleep(0.1)
        # Exec is needed so we can kill it
        # self.process = reap.Popen("exec gst-launch-1.0 -q "+getGstreamerSourceData(self.data.get('device.source','')) +"! ",shell=True)
        self.process = Pipeline()
        self.process.dev = self

        self.process.getGstreamerSourceData(
            self.config.get('device.source', ''), self.config,  self.config.get('device.username', ''), self.config.get('device.password', ''))

        x = self.process.addElement(
            "queue", connectToOutput=self.process.h264source, max_size_time=10000000)

        self.process.addElement("mpegtsmux", connectToOutput=(
            x, self.process.mp3src))

        self.mpegtssrc = self.process.addElement("tee")

        # Path to be created
        path = self.rawFeedPipe

        # Get rid of the old one, it could be clogged
        try:
            os.remove(path)
        except OSError:
            pass

        try:
            os.mkfifo(path)
        except OSError:
            print("Failed to create FIFO")

        os.chmod(path, 0o700)

        self.process.addElement("queue", max_size_time=10000000)
        self.process.addElement("filesink", location=path,
                                buffer_mode=2, sync=self.process.syncFile)

        # # Motion detection part of the graph

        # # This flag discards every unit that cannot be handled individually
        self.process.addElement(
            "identity", drop_buffer_flags=8192, connectToOutput=self.process.h264source)
        self.process.addElement("queue", max_size_time=20000000,
                                leaky=2)
        self.process.addElement("capsfilter", caps="video/x-h264")

        try:
            self.process.addElement("omxh264dec")
        except:
            self.process.addElement("avdec_h264")
        # self.process.addElement("videorate",drop_only=True)
        # self.process.addElement("capsfilter", caps="video/x-raw,framerate=1/1")

        rawtee= self.process.addElement("tee")
        self.process.addElement("queue",max_size_buffers=1,leaky=2)

        self.snapshotter = self.process.addPILCapture()


        self.process.addElement("videoanalyse",connectToOutput=rawtee)

        if self.config.get('device.barcodes', '').lower() in ("yes", "true", "detect", "enable", "on"):
            self.process.addElement("zbar")
            self.print("Barcode detection enabled")

        #self.process.addElement("videoconvert", chroma_resampler=0)

        # self.process.addElement(
        #     "motioncells", sensitivity=float(self.config.get('device.motion_sensitivity', '0.75')), gap=1, display=False)

        # self.process.addElement("fakesink")

        # Not a real GST element. The iceflow backend hardcodes this motion/presense detection
        self.process.addPresenceDetector((640, 480))

        self.process.mcb = self.motion
        self.process.bcb = self.barcode
        self.process.acb = self.analysis

        self.process.presenceval = self.presencevalue

        self.process.addElement("hlssink", connectToOutput=self.mpegtssrc, message_forward=True, async_handling=True, max_files=0,
                                location=os.path.join(
                                    "/dev/shm/knvr_buffer/", self.name, r"segment%08d.ts"),
                                playlist_root=os.path.join(
                                    "/dev/shm/knvr_buffer/", self.name),
                                playlist_location=os.path.join(
                                    "/dev/shm/knvr_buffer/", self.name, "playlist.m3u8"),
                                target_duration=5)

        self.datapusher = threading.Thread(
            target=self.thread, daemon=True, name="NVR")
        self.datapusher.start()

        self.process.start()
        # Used to check that things are actually still working.
        # Set them to prevent a loop.
        self.lastSegment = time.monotonic()
        self.lastPushedWSData = time.monotonic()