Euromap63-IMM模拟器

Euromap63-IMM模拟器

项目仓库:https://github.com/tang0-0/Eu63-Collecter
Euromap 63协议认识:https://blog.csdn.net/lblmlms/article/details/129431977
Euromap63-IMM模拟器:<>
Eu63-Collecter:<>

思路

  1. 建立FTP连接,进入指定目录
  2. 检索目标目录下有没有SESSION和JOB文件
  3. 生成会话层响应文件、表示层响应文件和应用层响应文件
  4. 上传这三个文件
  5. 删除会话请求文件,结束会话
  6. 周期扫描

实现


from ftplib import FTP
import os
import time


FTP_IP = "pp"
FTP_PORT = 21
FTP_USER = "adm"
FTP_PASSWORD = "123456"

IMM_FOLDER = "/IMM01"
SESSION_REQ_FILE_NAME = "SESS0000.REQ"
SESSION_RSP_FILE_NAME = "SESS0000.RSP"
REPORT_JOB_FILE_NAME = "0000R000.JOB"
REPORT_DAT_FILE_NAME = "0000R000.DAT"
SET_JOB_FILE_NAME = "0000S000.JOB"

PARAM_LIST=["ABC001","ABC002","ABC003","ABC004","ABC005","ABC006","ABC007","ABC008","ABC009","ABC010"]
PARAM_VALUE = 0


def write_session_rsp_file(job,result):
    if job == 0:
        return
    with open(os.path.abspath(SESSION_RSP_FILE_NAME), mode='wt+', encoding='utf-8') as file:
        if result:
            file.write("00000000 PROCESSED;\r")
            file.write("00000001 PROCESSED;")
        else:
            file.write("00000000 ERROR 05 00000001 \"This is a error\";\r")
            file.write("00000001 ERROR 05 00000007 \"This is a error\";")
        file.close()

def write_job_rsp_file(type,result):
    if type == 1:
        file_path = os.path.abspath("0000R000.RSP")
    elif type == 2:
        file_path = os.path.abspath("0000S000.RSP")
    else:
        return
    with open(file_path, mode='wt+', encoding='utf-8') as file:
        timestamp=time.time()
        local_time = time.localtime(timestamp)
        timestamp = time.strftime("%Y%m%d %H:%M:%S", local_time)
        if result:
            text1 = "COMMAND 1 PROCESSED \"OK\" "+timestamp+";\r"
            text2 = "COMMAND 1 PROCESSED \"OK\" "+timestamp+";"
            file.write(text1)
            file.write(text2)
        else:
            text1 = "00000000 ERROR 06 00000004 \"This is a error\" "+timestamp+";\r"
            text2 = "00000001 ERROR 06 00000006 \"This is a error\" "+timestamp+";"
            file.write(text1)
            file.write(text2)
        file.close()

def write_data_file(type):
    if type == 1:
        comma = ','
        global PARAM_VALUE
        print("Value:",PARAM_VALUE)
        with open(os.path.abspath(REPORT_DAT_FILE_NAME), mode='wt+', encoding='utf-8') as file:
            file.write(comma.join(PARAM_LIST))
            file.write("\r")
            value_list = [PARAM_VALUE for i in range(len(PARAM_LIST))]
            num_list_new = [str(x) for x in value_list]
            file.write(comma.join(num_list_new))
            file.write("\r")
            file.close()
            PARAM_VALUE = PARAM_VALUE+1

def ftp_connect():
    ftp = FTP()
    ftp.encoding = 'utf-8'
    ftp.set_debuglevel(0)
    try:
        ftp.connect(FTP_IP, FTP_PORT)
        ftp.login(FTP_USER, FTP_PASSWORD)
        print(ftp.getwelcome())
        ftp.cwd(IMM_FOLDER)
    except:
        print("FTP Connect failed")
        return None
    print("ftp connect success")
    return ftp

def check_job(ftp):
    filelist = ftp.nlst()
    # print(filelist)
    if SESSION_REQ_FILE_NAME in filelist:
        print("Has SESSION REQ File")
        if REPORT_JOB_FILE_NAME in filelist:
            print("Wao! Has REPORT JOB File")
            return 1
        if SET_JOB_FILE_NAME in filelist:
            print("Wao! Has SET JOB File")
            return 2
    return 0

def upload_file(ftp, job):
    if job == 0:
        return
    
    fp = open(os.path.abspath(SESSION_RSP_FILE_NAME), 'rb')
    res = ftp.storbinary('STOR ' + SESSION_RSP_FILE_NAME, fp, 1024)  # 上传文件
    if res.find('226') != -1:
        print('upload file complete', SESSION_RSP_FILE_NAME)
    fp.close()

    if 1 == job:
        fp = open(os.path.abspath('0000R000.RSP'), 'rb')
        res = ftp.storbinary('STOR ' + "0000R000.RSP", fp, 1024)  # 上传文件
        if res.find('226') != -1:
            print('upload file complete', "0000R000.RSP")
        fp.close()
    else:
        fp = open(os.path.abspath('0000S000.RSP'), 'rb')
        res = ftp.storbinary('STOR ' + "0000S000.RSP", fp, 1024)  # 上传文件
        if res.find('226') != -1:
            print('upload file complete', "0000S000.RSP")
        fp.close()

    if 1 == job:
        fp = open(os.path.abspath(REPORT_DAT_FILE_NAME), 'rb')
        res = ftp.storbinary('STOR ' + REPORT_DAT_FILE_NAME, fp, 1024)  # 上传文件
        if res.find('226') != -1:
            print('upload file complete', REPORT_DAT_FILE_NAME)
        fp.close()
    print("delte file complete",SESSION_REQ_FILE_NAME)
    ftp.delete(SESSION_REQ_FILE_NAME)

def ftp_quit(ftp):
    print("quit ftp")
    client.quit()


if __name__ == '__main__':
    with ftp_connect() as client:
        while True:
            job = check_job(client)
            write_session_rsp_file(job,False)
            write_job_rsp_file(job,False)
            write_data_file(job)
            upload_file(client, job)
            time.sleep(5)


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tangYi0_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值