wangrong
2025-09-23 f19a0e925da02a08b88784a534b16ec6d833df4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import paho.mqtt.client as mqtt
 
 
readfile = ""
writefile = ""
 
 
# File I/O processing
def file_processor():
    with open(writefile, 'w') as out:
        with open(readfile, 'r') as f:
            for chunk in read_chunks_from_file(f):
                # Appending chunks with newline/linefeed(Hex: 0x0A)
                chunk.append(0x0A)
                out.write(chunk.decode())
    print("[LOG] The processed file is " + writefile)
 
 
# Read chunks of 1KB from the file
def read_chunks_from_file(fileobj, chunksize=1024):
    while True:
        chunk = fileobj.read(chunksize)
        if not chunk:
            break
        yield bytearray(chunk.encode())
 
 
# The callback when the client receives a CONNACK response from the broker
def on_connect(client, userdata, flags, rc):
    print("[LOG] Connected with result code "+str(rc))
    client.subscribe("read/file")
    if rc != 0:
        client.reconnect()
 
 
# The callback when a PUBLISH message is received from the broker
def on_message(client, userdata, msg):
    if msg.payload.decode() == "start":
        print("[LOG] Processing file " + readfile)
        file_processor()
        client.publish("read/file", "done")
        client.disconnect()
 
 
def main():
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--brokerip", "-b", help="ip address of the broker",
                        type=str, default="localhost")
    parser.add_argument("--port", "-p", help="broker port",
                        type=int, default=1883)
    parser.add_argument("--file", "-f", help="input file path",
                        type=str, required=True)
    args = parser.parse_args()
 
    global readfile
    readfile = args.file
    global writefile
    writefile = args.file + ".processed"
 
    processor = mqtt.Client(parser.prog)
    processor.connect(args.brokerip, args.port)
    processor.on_connect = on_connect
    processor.on_message = on_message
    processor.loop_forever()
 
 
if __name__ == "__main__":
    main()