Skip to content

Iteration 3 SaC

OriHalyo edited this page Jun 5, 2018 · 5 revisions

Sending and compressing

In this iteration we will create the function that compressing the file we needs to send, as well as the function that transmit the relevant data for the source device to detestation device. Plus dealing we that we needs to create each sensor class

Roles:

Itamar:

  1. Compressing data
  2. Create each sensor class

Ori:

  1. Transmit function
  2. Testing

Issue Management:

SaC Issue Board

Iteration Result:

We created each sensor class.

Compress

path to folder fo files to get

def getDataFromInputFolder(self, path):
    print("Searching for files in input directory...")
    files = glob.glob(path + "/*.*")  # create list of files in directory
    try:
        while not files:
            sleep(10)
            files = glob.glob(path + "/*.*")
        else:  # then list (actually the directory) isn't empty
            print("File detected!")
            return (files)
    except KeyboardInterrupt:
        print("Exit Stand By mode")
        pass

# 'files': what files[0] to compress. 'path': to file goes after compress
def compress(self, files, path, original_file_name, date):  #   compress data with some known compress method
    #compress the data
    print("device: compressing data")
    original_data = open(files[0], 'rb').read()
    compressed_data = zlib.compress(original_data, zlib.Z_BEST_COMPRESSION)

    # compress_ratio = (float(len(original_data)) - float(len(compressed_data))) / float(len(original_data))
    # print('Compressed: %d%%' % (100.0 * compress_ratio))

    #save the compressed data to file
    f = open(path + '/' + original_file_name + '-' + date + '.zlib', 'wb')
    f.write(compressed_data)
    f.close(

Send

def sendData(self, path): print("device: sending data")

    dirs = os.listdir(path)

    while True:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        host = "127.0.0.1"
        port  = 5002
        try:
            sock.connect((host, port))
            break
        except:
            print("Falied to connect, auto try again after 10sec")
            time.sleep(10)
            continue


    for files in dirs:
        filename = files
        size = len(filename)
        size = bin(size)[2:].zfill(16)          #encode file name to 16 bit
        sock.sendall(size.encode('utf8'))       #encode so we could send it
        sock.sendall(filename.encode('utf8'))

        filename = os.path.join(path,filename)
        filesize = os.path.getsize(filename)
        filesize = bin(filesize)[2:].zfill(32)   # encode filesize as 32 bit binary
        sock.sendall(filesize.encode('utf8'))

        file_to_send = open(filename, 'rb')

        l = file_to_send.read()
        sock.sendall(l)
        file_to_send.close()

    sock.close()

Iteration Conclusions

We keep up on schedule, small delay. needs to figure how to run the code on the device as soon as possible. Needs to deal with deleting files. mean we have to delete each file from folder after we send it

Next Iteration:

Iteration 4

Clone this wiki locally