118 lines
3.5 KiB
Python
118 lines
3.5 KiB
Python
|
|
|
|
import argparse
|
|
from netdicom import AE, StorageSOPClass, VerificationSOPClass
|
|
from pydicom.dataset import Dataset, FileDataset
|
|
from pydicom.uid import ExplicitVRLittleEndian, ImplicitVRLittleEndian, ExplicitVRBigEndian
|
|
import pydicom
|
|
import tempfile
|
|
import os
|
|
import hashlib
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
# import numpy as np
|
|
# import httpUpload
|
|
import traceback
|
|
|
|
# parse commandline
|
|
parser = argparse.ArgumentParser(description='storage SCP example')
|
|
parser.add_argument('-port', type=int, default='3030')
|
|
parser.add_argument('-aet', help='AE title of this server', default='RAYPLUSAE') # ae title of rayplus
|
|
parser.add_argument('-dicompath',help='dicom store path',default='/home/www/rayplus_cms/app/public/DICOM',)
|
|
parser.add_argument('-baseUrl', help='server url', default='http://localhost:7002/v1')
|
|
parser.add_argument('-username', help='username', default='admin_huashan')
|
|
parser.add_argument('-password', help='password', default='rayplus1234')
|
|
args = parser.parse_args()
|
|
|
|
|
|
# uploadHander = httpUpload.httpUpload(args.baseUrl, args.username, args.password)
|
|
|
|
|
|
# callbacks
|
|
def OnAssociateRequest(association):
|
|
print "association requested"
|
|
|
|
|
|
def OnAssociateResponse(association):
|
|
print "Association response received"
|
|
|
|
|
|
def OnReceiveEcho(self):
|
|
print "Echo received"
|
|
|
|
|
|
def OnReceiveStore(SOPClass, DS):
|
|
print "Received C-STORE"
|
|
# do something with dataset. For instance, store it on disk.
|
|
try:
|
|
file_meta = Dataset()
|
|
file_meta.MediaStorageSOPClassUID = DS.SOPClassUID
|
|
file_meta.MediaStorageSOPInstanceUID = "1.2.3" # !! Need valid UID here
|
|
file_meta.ImplementationClassUID = "1.2.3.4" # !!! Need valid UIDs here
|
|
file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
|
|
|
|
# print DS
|
|
filename = '/%s.dcm' % (DS.SOPInstanceUID)
|
|
ds = FileDataset(filename, {}, file_meta=file_meta, preamble="\0" * 128)
|
|
ds.update(DS)
|
|
|
|
ds.is_little_endian = True
|
|
ds.is_implicit_VR = True
|
|
|
|
filePath = args.dicompath+"/"+DS.StudyInstanceUID
|
|
|
|
if not os.path.isdir(filePath):
|
|
os.mkdir(filePath)
|
|
filePath = filePath+"/"+DS.SeriesInstanceUID
|
|
if not os.path.isdir(filePath):
|
|
os.mkdir(filePath)
|
|
print filePath+filename
|
|
ds.save_as(filePath+filename)
|
|
print 'recieve success'
|
|
|
|
#upload by http
|
|
# try:
|
|
# if uploadHander.testConnect() == False:
|
|
# print 'login failed'
|
|
# os._exit(0)
|
|
# except:
|
|
# print 'system error, exit'
|
|
# os._exit(0)
|
|
|
|
# print 'start uploading'
|
|
|
|
# try:
|
|
# if uploadHander.uploadDCM(ds) == False:
|
|
# print 'upload file faild'
|
|
# else:
|
|
# print 'upload success'
|
|
# except:
|
|
# print 'upload file faild'
|
|
except:
|
|
traceback.print_exc()
|
|
print 'recieve file faild'
|
|
# must return appropriate status
|
|
return SOPClass.Success
|
|
|
|
|
|
# setup AE
|
|
MyAE = AE(args.aet, args.port, [], [StorageSOPClass, VerificationSOPClass])
|
|
MyAE.OnAssociateRequest = OnAssociateRequest
|
|
MyAE.OnAssociateResponse = OnAssociateResponse
|
|
MyAE.OnReceiveStore = OnReceiveStore
|
|
MyAE.OnReceiveEcho = OnReceiveEcho
|
|
|
|
# start AE
|
|
print "starting AE ... "
|
|
MyAE.start()
|
|
# try:
|
|
# if uploadHander.login() == False:
|
|
# print 'login failed'
|
|
# os._exit(0)
|
|
# except Exception:
|
|
# print traceback.print_exc()
|
|
# print 'system error, exit'
|
|
# os._exit(0)
|
|
|
|
print "done"
|
|
MyAE.QuitOnKeyboardInterrupt()
|