예제 #1
0
 def download_file(path, f, sess):
     log.info(f"Downloading: {f.name} ({f.size} bytes)")
     try:
         result = sess.download(f.selfRef, path)
         result, t, dsize = result.exnode, result.time, result.t_size
         if dsize != result.size:
             log.warning(f"Incorrect file size {result.name}: Transferred {dsize} of {result.size}")
         else:
             log.info(f"{result.name} ({result.size} {result.size/1e6/t} MB/s) {result.selfRef}")
     except (ConnectionError, TimeoutError, AllocationError) as e:
         log.warning(f"Could not download file: {e}")
예제 #2
0
    def query(self):
        if not self.sock:
            if self.gps_box:
                min_x, min_y, max_x, max_y = self.gps_box.bounds
                pt = Point([
                    random.uniform(min_x, max_x),
                    random.uniform(min_y, max_y)
                ])
                log.info('Location identified as %f,%f' % (pt.x, pt.y))
                return (pt.x, pt.y)
            return (GPS_DEFAULT[0], GPS_DEFAULT[1])

        lack_lat = True
        lack_long = True
        lack_alt = True

        for new_data in self.sock:
            latitude = 'n/a'
            longitude = 'n/a'
            altitude = 'n/a'

            if new_data:
                self.stream.unpack(new_data)
                self.read_count = self.read_count + 1

                latitude = self.stream.TPV['lat']
                longitude = self.stream.TPV['lon']
                altitude = self.stream.TPV['alt']

            if lack_lat and latitude != 'n/a' and type(latitude) == float:
                latitude = float(latitude)
                lack_lat = False

            if lack_long and longitude != 'n/a' and type(longitude) == float:
                longitude = float(longitude)
                lack_long = False

            if lack_alt and altitude != 'n/a' and type(altitude) == float:
                altitude = float(altitude)
                lack_alt = False

            if not lack_lat and not lack_long:  # optional: and not lack_alt
                log.info('Location identified as %f,%f' %
                         (latitude, longitude))
                return (latitude, longitude)

            if self.read_count > GPS_DEV_READ_LEN:
                break

            # only return default if socket is dead
            # otherwise location will jump if GPS signal is lost
            return (None, None)
        return (None, None)
예제 #3
0
    def on_any_event(self, event):
        if event.is_directory:
            return None

        if event.event_type == 'moved':
            if not os.path.basename(event.src_path).startswith("."):
                return
            
            fname = os.path.basename(event.src_path)
            dname = os.path.dirname(event.src_path)
            npath = os.path.join(dname, fname[1:])
            dpath = os.path.join(DOWNLOAD_DIR, fname[1:])
            if os.path.getsize(npath) > 0:
                log.info("Handling new file upload: %s" % npath)
                self._do_upload(npath)
                os.rename(npath, dpath)
예제 #4
0
def downloop(agent):
    def download_file(path, f, sess):
        log.info(f"Downloading: {f.name} ({f.size} bytes)")
        try:
            result = sess.download(f.selfRef, path)
            result, t, dsize = result.exnode, result.time, result.t_size
            if dsize != result.size:
                log.warning(f"Incorrect file size {result.name}: Transferred {dsize} of {result.size}")
            else:
                log.info(f"{result.name} ({result.size} {result.size/1e6/t} MB/s) {result.selfRef}")
        except (ConnectionError, TimeoutError, AllocationError) as e:
            log.warning(f"Could not download file: {e}")
            

    with libdlt.Session(agent.rt, bs="5m", depots={agent.service.accessPoint: {"enabled": True}}, threads=1) as sess:
        while True:
            log.info(f"[{agent.service.status}] Waiting for update...")
            for _ in range(10):
                agent.service.reload()
                if agent.service.status == "UPDATE":
                    agent.rt.exnodes.load()
                    agent.rt.extents.load()
                    dl_list = [f for f in getattr(agent.service, 'new_exnodes', [])]
                    log.info(f"Caught UPDATE status with {len(dl_list)} new exnodes")
                    for f in dl_list:
                        path = os.path.join(agent.cfg['file']['download'], f.name)
                        log.debug(f"Attempting download of {f.id} -> {path}")
                        if os.path.exists(path) and os.path.getsize(path) == f.size:
                            log.debug(f"File exists: {f.name}, skipping")
                            try: agent.service.new_exnodes.remove(f)
                            except (KeyError, AttributeError) as e: pass
                        else:
                            download_file(path, f, sess)
                    if not dl_list:
                        agent.service.status = "READY"
                    agent.rt.flush()
                time.sleep(1)
예제 #5
0
    def __init__(self):
        self.sock = gps3.GPSDSocket()
        self.stream = gps3.DataStream()

        # GPS3 throws no exceptions and returns no status,
        # so we have to capture stdout and do string matching, sigh
        f = io.StringIO()
        with redirect_stderr(f):
            self.sock.connect()
        if "Connection refused" in f.getvalue():
            log.warn(
                "Could not connect to GPSD socket, continuing with GPS_BOX")
            self.sock = None
        else:
            log.info("Connected to GPSD socket")
            self.sock.watch()

        # we will use the GPS_BOX setting to determine if
        # queries will return locations within a specified range
        if len(GPS_BOX) == 4:
            self.gps_box = box(GPS_BOX[0], GPS_BOX[1], GPS_BOX[2], GPS_BOX[3])
            log.info("Created GPS box")
        else:
            log.warn("Invalid GPS_BOX size, using GPS_DEFAULT")
예제 #6
0
 def update_ibp_server(self, istr):
     cmdstr = "sudo sed -i 's/^interfaces=.*$/interfaces=" + istr + "/g' " + self.cfg
     subprocess.run(shlex.split(cmdstr))
     cmdstr = "sudo /etc/init.d/ibp-server restart"
     subprocess.run(shlex.split(cmdstr), stdout=subprocess.DEVNULL)
     log.info("Detected interface change, restarted ibp-server")
예제 #7
0
def mainloop(agent):
    while True:
        log.info("Waiting for something to do...")
        for _ in range(10):
            time.sleep(1)