-
Notifications
You must be signed in to change notification settings - Fork 4
/
FileConverter.py
executable file
·160 lines (141 loc) · 6.52 KB
/
FileConverter.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
"""
FileConverter.py
Copyright (C) 2024 - 2025 Marc Postema (mpostema09 -at- gmail.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Or, point your browser to http://www.gnu.org/copyleft/gpl.html
"""
import csv
import json
import sys
class FileConverter:
def __init__(self):
print("Converting...")
# pathIn = ./json/test_nac_original.json
# pathOut = ./json/test_nac_conv.json
def convertNAC(self, pathIn, pathOut):
if pathIn == pathOut:
print("In and out paths are the same... ERROR")
return
file = open(pathIn, 'r', encoding='utf-8')
jsonFile = file.read()
self.ecuObjectList = json.loads(jsonFile.encode("utf-8"))
# Converter
for zoneID in self.ecuObjectList["NAC"]["zones"]:
zone = self.ecuObjectList["NAC"]["zones"][zoneID]
if "params" in zone:
zone["tab"] = "telemat"
zone["type"] = "raw"
zone["form_type"] = "multi"
zone["params"] = zone.pop("params")
for param in zone["params"]:
param.pop("size")
param["name_original"] = param.pop("name")
param["name"] = param["detail"]["en"]
param.pop("detail")
param["byte"] = int(param["pos"]) - 4
param.pop("pos")
param.pop("mask")
param["mask"] = param.pop("maskBinary")
if "listbox" in param:
param["form_type"] = "combobox"
param["params"] = param.pop("listbox")
for item in param["params"]:
item["mask"] = '{0:08b}'.format(int(item["value"], 16))
item["name"] = item["text"]["en"]
item.pop("text")
item.pop("value")
else:
mask = int(param["mask"], 2)
if mask.bit_count() > 1:
param["type"] = "raw"
param["form_type"] = "string"
else:
param["form_type"] = "checkbox"
param["available_logic"] = "active_high"
else:
print("Need to handle this!!")
self.ecuObjectList = self.ecuObjectList.pop("NAC")
self.ecuObjectList.pop("VIN")
self.ecuObjectList.pop("SN")
self.ecuObjectList["name"] = "telemat_nac_rcc"
self.ecuObjectList["tx_id"] = "764"
self.ecuObjectList["rx_id"] = "664"
self.ecuObjectList["protocol"] = "uds"
self.ecuObjectList["key_type"] = "multi"
keys = {"NAC": "D91C", "RCC": "D91C" }
tabs = {"telemat": "Telematic Unit (BTEL)"}
self.ecuObjectList["keys"] = keys
self.ecuObjectList["tabs"] = tabs
self.ecuObjectList["zones"] = self.ecuObjectList.pop("zones")
# Converter
wFile = open(pathOut, 'w', encoding='utf-8')
json.dump(self.ecuObjectList, wFile, ensure_ascii=False, indent=2)
# pathIn = ./json/test_CIROCCO_original.json
# pathOut = ./json/test_CIROCCO_conv.json
def convertCIROCCO(self, pathIn, pathOut):
if pathIn == pathOut:
print("In and out paths are the same... ERROR")
return
file = open(pathIn, 'r', encoding='utf-8')
jsonFile = file.read()
self.ecuObjectList = json.loads(jsonFile.encode("utf-8"))
# Converter
for zoneID in self.ecuObjectList["CIROCCO"]["zones"]:
zone = self.ecuObjectList["CIROCCO"]["zones"][zoneID]
if "params" in zone:
zone["tab"] = "cmb"
zone["type"] = "raw"
zone["form_type"] = "multi"
zone["params"] = zone.pop("params")
for param in zone["params"]:
param.pop("size")
param["name_original"] = param.pop("name")
param["name"] = param["detail"]["en"]
param.pop("detail")
param["byte"] = int(param["pos"]) - 4
param.pop("pos")
param.pop("mask")
param["mask"] = param.pop("maskBinary")
if "listbox" in param:
param["form_type"] = "combobox"
param["params"] = param.pop("listbox")
for item in param["params"]:
item["mask"] = '{0:08b}'.format(int(item["value"], 16))
item["name"] = item["text"]["en"]
item.pop("text")
item.pop("value")
else:
mask = int(param["mask"], 2)
if mask.bit_count() > 1:
param["type"] = "raw"
param["form_type"] = "string"
else:
param["form_type"] = "checkbox"
param["available_logic"] = "active_high"
else:
print("Need to handle this!!")
self.ecuObjectList = self.ecuObjectList.pop("CIROCCO")
self.ecuObjectList.pop("SN")
self.ecuObjectList["name"] = "combine"
self.ecuObjectList["tx_id"] = "75F"
self.ecuObjectList["rx_id"] = "65F"
self.ecuObjectList["protocol"] = "uds"
self.ecuObjectList["key_type"] = "multi"
keys = {"CIROCCO": "FAFA" }
tabs = {"cmb": "Instrument Panel CIROCCO"}
self.ecuObjectList["keys"] = keys
self.ecuObjectList["tabs"] = tabs
self.ecuObjectList["zones"] = self.ecuObjectList.pop("zones")
# Converter
wFile = open(pathOut, 'w', encoding='utf-8')
json.dump(self.ecuObjectList, wFile, ensure_ascii=False, indent=2)