Beispiel #1
0
    def download_file(self, file_no):
        file_name = "%s.%02x.dat" % (self.hostname, file_no)
        file_name = os.path.join(common.get_current_directory(), file_name)
        try:
            socket.setdefaulttimeout(10)
            conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            conn.connect((self.hostaddr, self.port))
            record_no = 0
            with open(file_name, "w") as fp:
                while True:
                    request_data = ReqData().create_request_data(
                        ReqData.Read_File, file_no, record_no)
                    request_data = request_data.decode("hex")
                    conn.send(request_data)
                    received_data = conn.recv(1460)
                    received_length = len(received_data)
                    if received_length == 1:
                        ack = ord(received_data[0])
                        if ack == 0xE0:
                            raise TwsException("Error on Reading")
                        if ack == 0xE2:
                            break

                    else:
                        received_data = received_data.encode("hex").upper()
                        if not received_data:
                            common.log_err("Received data is Empty")
                            break
                        # ¼ÆË㳤¶È
                        real_size = int(received_data[8:12], 16)
                        while real_size > received_length:
                            # for STE48 3.00+
                            # if received_length % 1460 == 0:
                            conn.send('\x06')
                            #

                            received_data_plus = conn.recv(1460)
                            if not received_data_plus: break
                            received_data_plus_length = len(received_data_plus)
                            received_data_plus = received_data_plus.encode(
                                "hex").upper()
                            received_length += received_data_plus_length
                            received_data += received_data_plus
                        #
                        try:
                            record_no = int(received_data[:8])
                        except Exception, e:
                            record_no = int(received_data[:8], 16)
                        fp.write(received_data + "\r\n")

        except TwsException, e:
            # print e
            common.log_err(self.hostname + ' - ' + str(e))
            return ""
Beispiel #2
0
    def create_csv(self, master):
        try:
            # local_file = self.ip + "_" + master.scale_file_name
            local_file = self.get_scale_file_name_entire(master)
            local_file = os.path.join(common.get_current_directory(),
                                      local_file)
            master.to_csv(local_file)
            return local_file

        except Exception, e:
            common.log_err(traceback.format_exc())
            return ""
Beispiel #3
0
 def send_by_name(self, name, data):
     try:
         local_file = self.ip + "_" + name
         local_file = os.path.join(common.get_current_directory(),
                                   local_file)
         remote_file = name
         with open(local_file, "wb") as fp:
             fp.write(data)
         # ftp = smftp(self.ip, self.usr, self.pwd)
         # try:
         # ftp.login()
         self.ftp.upload_file(local_file, remote_file)
         # time.sleep(0.3)
         return True
     # except Exception as e:
     # print e
     except Exception, e:
         common.log_err(traceback.format_exc())
         return False
Beispiel #4
0
 def get_scale_file_name_entire(self, master):
     return \
         os.path.join(common.get_current_directory(), "%s.%02x.dat" % (self.hostname, master.file_no))
Beispiel #5
0
from common import common

# 根据当前可执行文件取当前文件夹
# determine if application is a script file or frozen exe
if getattr(sys, 'frozen', False):
    application_path = os.path.dirname(sys.executable)
elif __file__:
    application_path = os.path.dirname(__file__)
# print application_path

# common.set_current_directory(os.path.dirname(os.path.realpath(__file__)))
common.set_current_directory(application_path)

# 默认为gbk,在encode.txt可设置编码
current_encoding = 'gbk'
encoding_file_path = os.path.join(common.get_current_directory(), 'encode.txt')
# encoding_file_path = os.path.join("", 'encode.txt')
if os.path.isfile(encoding_file_path):
    with open(encoding_file_path) as fp:
        current_encoding = fp.readline().strip()

# print("encoding: " + current_encoding)

sys.getdefaultencoding()
reload(sys)
# sys.setdefaultencoding("utf-8")
sys.setdefaultencoding(current_encoding)  # @UndefinedVariable

reload(common)
from common.converter import ScalesConverter
from common import converter