#!/usr/bin/env python3

import os
import sys

from pydicom import dcmread

from pynetdicom import AE, debug_logger
from pynetdicom.sop_class import CTImageStorage, RTStructureSetStorage

debug_logger()

def SendDCM(fp):

    # Initialise the Application Entity
    ae = AE()
    ae.ae_title = 'OUR_STORE_SCP'

    # Add a requested presentation context
    # ae.add_requested_context(CTImageStorage)
    ae.add_requested_context(RTStructureSetStorage)

    # Read in our DICOM CT dataset
    ds = dcmread(fp)

    # Associate with peer AE at IP 127.0.0.1 and port 11112
    assoc = ae.associate("127.0.0.1", 11112)
    assoc = ae.associate("172.16.40.36", 104, 
        ae_title = 'N1000_STORAGE',
        )
    if assoc.is_established:
        # Use the C-STORE service to send the dataset
        # returns the response status as a pydicom Dataset
        status = assoc.send_c_store(ds)

        # Check the status of the storage request
        if status:
            # If the storage request succeeded this will be 0x0000
            print('C-STORE request status: 0x{0:04x}'.format(status.Status))
        else:
            print('Connection timed out, was aborted or received invalid response')

        # Release the association
        assoc.release()
    else:
        print('Association rejected, aborted or never connected')


def main():
    if len(sys.argv) < 2:
        print('Usage:', sys.argv[0], 'RTSS')
        sys.exit()
    print('hello')
    print(sys.argv[0])
    print(sys.argv[1])
    SendDCM(sys.argv[1])

if __name__ == '__main__':
    main()
