Ejemplo n.º 1
0
def verify_cache(index, res_folder):
    num_files = len(index)

    missing = 0
    corrupt = 0
    scanned = 0
    for entry in index:
        scanned += 1
        progress.write("Scanned %6.1d of %6.1d files (%6.1d corrupt, %6.1d missing)\r" %
                       (scanned, num_files, corrupt, missing))
        filename = os.path.join(res_folder, entry.cached_name)
        if not os.path.exists(filename):
            missing += 1
            continue

        checksum = calc_checksum(filename)
        if checksum != entry.md5_checksum:
            corrupt += 1
            try:
                os.remove(filename)
            except IOError:
                pass

    progress.clear()
    print "Verified %d files:" % num_files
    print "  %6.1d files corrupt" % corrupt
    print "  %6.1d files not yet downloaded" % missing

    return corrupt, missing
Ejemplo n.º 2
0
def DownloadResourceFile(target_url, expected_checksum, target_path):
    try:
        contents = urllib2.urlopen(target_url)
    except urllib2.URLError:
        return False, "Couldn't open %s" % target_url

    data = contents.read()
    headers = contents.info()
    if ('content-encoding' in headers.keys() and headers['content-encoding']=='gzip'):
        sio = StringIO.StringIO(data)
        gzipper = gzip.GzipFile(fileobj=sio)
        data = gzipper.read()

    temp_path = target_path + ".tmp"

    with open(temp_path, "wb") as f:
        f.write(data)

    checksum_from_file = calc_checksum(temp_path)
    if checksum_from_file != expected_checksum:
        try:
            os.remove(temp_path)
        except OSError:
            pass
        return False, "Checksum didn't match"

    try:
        os.rename(temp_path, target_path)
    except OSError:
        return False, "Failed to rename temp file to final name"

    return True, "OK"
Ejemplo n.º 3
0
def DownloadResourceFile(target_url, expected_checksum, target_path):
    try:
        contents = urllib2.urlopen(target_url)
    except urllib2.URLError:
        return False, "Couldn't open %s" % target_url

    data = contents.read()
    headers = contents.info()
    if ('content-encoding' in headers.keys()
            and headers['content-encoding'] == 'gzip'):
        sio = StringIO.StringIO(data)
        gzipper = gzip.GzipFile(fileobj=sio)
        data = gzipper.read()

    temp_path = target_path + ".tmp"

    with open(temp_path, "wb") as f:
        f.write(data)

    checksum_from_file = calc_checksum(temp_path)
    if checksum_from_file != expected_checksum:
        try:
            os.remove(temp_path)
        except OSError:
            pass
        return False, "Checksum didn't match"

    try:
        os.rename(temp_path, target_path)
    except OSError:
        return False, "Failed to rename temp file to final name"

    return True, "OK"
Ejemplo n.º 4
0
def DownloadResourceFile(target_url, expected_checksum, target_path):
    try:
        contents = urllib2.urlopen(target_url)
    except urllib2.URLError:
        return False

    temp_path = target_path + ".tmp"
    with open(temp_path, "wb") as f:
        f.write(contents.read())

    checksum_from_file = calc_checksum(temp_path)
    if checksum_from_file != expected_checksum:
        try:
            os.remove(temp_path)
        except OSError:
            pass
        return False

    try:
        os.rename(temp_path, target_path)
    except OSError:
        pass

    return True
Ejemplo n.º 5
0
def DownloadResourceFile(target_url, expected_checksum, target_path):
    try:
        contents = urllib2.urlopen(target_url)
    except urllib2.URLError:
        return False

    temp_path = target_path + ".tmp"
    with open(temp_path, "wb") as f:
        f.write(contents.read())

    checksum_from_file = calc_checksum(temp_path)
    if checksum_from_file != expected_checksum:
        try:
            os.remove(temp_path)
        except OSError:
            pass
        return False

    try:
        os.rename(temp_path, target_path)
    except OSError:
        pass

    return True
Ejemplo n.º 6
0
    def run(self):
        try:
            #Connections
            if (useServer):
                self.connectToServer()
                print("Connected to test server")
                data = Data(self.sock)

            self.connectToArduino()

            #Handshaking with Arduino
            while (self.isHandshakeDone == False):
                self.serial_port.write('H')
                print("H sent")
                time.sleep(0.5)
                reply = self.serial_port.read(1)
                if (reply == 'B'):
                    self.isHandshakeDone = True
                    self.serial_port.write('F')
                    print("Connected to Arduino")
                    self.serial_port.readline()
                    time.sleep(1)
                else:
                    time.sleep(0.5)

            #Receive data from Arduino(periodically) and save to CSV
            send_flag = True
            while (collect_test_data):
                if (send_flag == True):
                    self.serial_port.write('R')
                    send_flag = False
                time.sleep(0.1)
                if (self.serial_port.inWaiting() > 0):
                    arduino_data = readlineCR(self.serial_port)
                    print(arduino_data)
                    cs.save_data(arduino_data)
                    send_flag = True
                    cs.calc_checksum(arduino_data)

            i = 0
            #Read and Predict the results
            while (testing_samples):
                if (send_flag == True):
                    self.serial_port.write('R')
                    send_flag = False
                time.sleep(0.1)
                if (self.serial_port.inWaiting() > 0):
                    send_flag = True
                    arduino_data = readlineCR(self.serial_port)
                    #print(arduino_data)

                    if (cs.calc_checksum(arduino_data)
                        ):  #if checksum is correct
                        last_comma_index = arduino_data.rfind(",")
                        arduino_data = arduino_data[:
                                                    last_comma_index]  #strip out the checksum
                        arduino_data_list = arduino_data.split(
                            "\n")[:-1]  #split into 4 rows of samples
                        for item in arduino_data_list:
                            data.sample_queue.appendleft(item)

                        self.retrieve_data(data, arduino_data)

                    if (len(data.sample_queue) == 20):
                        if (i % 2 == 1):
                            self.result_queue.appendleft(
                                predict.predict_data(data.sample_queue))
                            #print(list(self.result_queue))
                            data.sample_queue.clear()

                    if (len(list(self.result_queue)) == 3
                            and len(set(list(self.result_queue))) == 1):
                        predictedMove = (list(self.result_queue)[0])
                        self.result_queue.clear()
                        if (predictedMove != "Idle"):
                            data.sendData(predictedMove)
                    i += 1
            #test communication with server.


##          while(True):
##              name = raw_input("What is the dance move?")
##              data.voltage = 12
##              data.current = 1
##              data.power = 100
##              data.cumpower
##              data.sendData(name)

        except KeyboardInterrupt:
            sys.exit(1)
Ejemplo n.º 7
0
print('Waiting to GPS be free')
drainer = True
while drainer:
    drainer = gps_s.any()
    if drainer > 0:
        gps_s.read(drainer)

print('Writing AGPS data')
gps_s.write(r.content)
print('Done')

# RXM Configuration
print('Writing configuration')
header = struct.pack('BBBBH', 0xb5, 0x62, 0x06, 0x11, 2)
payload = b'\x00' + struct.pack('B', 0)
ck_a, ck_b = calc_checksum(struct.pack('<BBH', 0x06, 0x11, 2) + payload)
gps_s.write(header + payload + struct.pack('BB', ck_a, ck_b))
print('Done')


def print_buf(b):
    if b.startswith(b'$'):
        print(b.decode('utf-8').strip())
    if b.startswith(b'\xb5'):
        print(hexlify(b).decode('utf-8'))


c = 0
buf = b''
while True:
    char = gps_s.read(1)
Ejemplo n.º 8
0
                    "type": "Feature",
                    "properties": {
                        "remote_ID": str(remote_ID.decode()),
                        "altitude": str(altitude),
                        "speed": str(speed),
                        "course": str(course),
                        "battery": str(vBatt),
                        "RSSI": str(stats.rssi),
                        "datetime": str(GPSdatetime)
                    }
                }
                # write received data to log file in CSV format in append mode
                #with open("/sd/log.csv", 'a') as Log_file:
                #    Log_file.write(remote_ID + ',' + str(GPSFix) + ',' + str(lat) + ',' + str(lon) + ',' + str(vBatt) + ',' + str(stats.rssi) + '\n')
            else:
                # print received data to serial port / screen
                print(
                    str(gc.mem_free()) + ',' + str(remoteMem) + ',RX:' +
                    str(remote_ID) + ",NOGPS," + str(lat) + ',' + str(lon) +
                    ',' + str(altitude) + ',' + str(speed) + ',' +
                    str(course) + ',' + str(vBatt) + ',' + str(GPSdatetime) +
                    ',' + str(stats.rssi))
        else:
            print("Checksum ERROR")
            print('Recv CRC: ')
            print(databytes[:-4])
            print('Calc CRC: ')
            print(hex(calc_checksum(databytes)))

    time.sleep(receiveInterval)