This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXCTU_maker.py
163 lines (149 loc) · 6.29 KB
/
XCTU_maker.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import random
import binascii
import time
import json
from datetime import datetime
import sys
from os import path
class Package:
def __init__(self, name, frequency, seperater, ending, package) -> None:
self.name = name
self.frequency = frequency
self.package = package
self.seperater = seperater
self.data = ["" for i in package]
self.ending = ending
self.pkg = 0
def set_data_range(self):
for key, item in self.package.items():
if item == "pkg":
pass
elif item == "time":
pass
elif isinstance(item, list):
if all(isinstance(x, float) for x in item):
pass
elif all(isinstance(x, str) for x in item):
pass
else:
raise f"invalid form of random at {key}:{item}"
def write(self):
self.pkg += 1
for index, key in enumerate(self.package.keys()):
if self.package[key] == "pkg":
self.data[index] = str(self.pkg)
elif self.package[key] == "time":
times = datetime.now().time()
self.data[index] = str('%02d' % times.hour) + ':' + str('%02d' %
times.minute) + ':' + str('%02d' % times.second)
elif isinstance(self.package[key], list):
if all(isinstance(x, int) for x in self.package[key]):
self.data[index] = str(random.randrange(
self.package[key][0], self.package[key][1]))
elif all(isinstance(x, float) for x in self.package[key]):
self.data[index] = str(round(random.uniform(
self.package[key][0], self.package[key][1]), 7 if key.startswith("GPS") else 2))
elif all(isinstance(x, str) for x in self.package[key]):
self.data[index] = random.choice(self.package[key])
else:
raise f"invalid form of random at {key}:{ self.package[key]}"
elif isinstance(self.package[key], str):
self.data[index] = self.package[key]
elif isinstance(self.package[key], dict):
if self.data[index] == "":
self.data[index] = str(self.package[key]["start"])
try:
if eval(self.data[index] + self.package[key]["condition"]):
self.data[index] = str(eval(
f"{self.data[index]}{self.package[key]['action']}"))
else:
self.data[index] = str(self.package[key]["else"])
except ValueError:
self.data[index] = self.package[key]["else"]
return self.data
@staticmethod
def convert_to_hex(data):
data = str(data)
return str(binascii.hexlify((data.encode())), "ascii")
def printProgressBar(iteration, total, prefix='', suffix='', decimals=1, length=100, fill='█', printEnd="\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 *
(iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end=printEnd)
# Print New Line on Complete
if iteration == total:
print()
def main(filename):
config = open(filename)
config = json.load(config)
file_header = """
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<data>
<loop>true</loop>
<repeat_times>1</repeat_times>
<repeat_period>1000</repeat_period>
<packets_list>
"""
file_data = """"""
file_ending = """
</packets_list>
</data>
"""
print(f"Makeing tester of {config['projectName']}")
output_num = config['outputNum']
output_name = config['outputName']
containers = []
try:
for data_type in config['data']:
containers.append(
Package(data_type["name"], data_type["frequency"], data_type["seperater"], data_type["ending"], data_type["package"]))
except KeyError as E:
raise E
printProgressBar(0, output_num, prefix="Progress:",
suffix="Complete", length=50)
for index in range(output_num):
for package in containers:
for frequency in range(package.frequency):
unhex = package.seperater.join(package.write())
content = package.convert_to_hex(unhex+package.ending)
file_data += f"""
<packet name=\"{package.name}_{package.pkg}\">
<payload>{content}</payload>
</packet>"""
printProgressBar(index+1, output_num, prefix="Progress:",
suffix="Complete", length=50)
with open(output_name, "w") as file:
file.write(file_header)
file.write(file_data)
file.write(file_ending)
if __name__ == "__main__":
print(
"""
#############################################################
# _____ ____ ____ __ ___ ____ __ #
# / ___/| \ / | / ] / _] / | / ] #
# ( \_ | o )| o | / / / [_ | o | / / #
# \__ || _/ | | / / | _] | | / / #
# / \ || | | _ |/ \_ | [_ | _ |/ \_ #
# \ || | | | |\ || | | | |\ | #
# \___||__| |__|__| \____||_____| |__|__| \____| #
#############################################################
""")
file_name = input("[Configuration File]: ")
if path.exists(file_name):
main(file_name)
else:
print("invalid file path")