-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdjustProjectFile.js
More file actions
160 lines (129 loc) · 5.33 KB
/
AdjustProjectFile.js
File metadata and controls
160 lines (129 loc) · 5.33 KB
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
const sdkadapter = require('/steady/neeo/cp6/sdkadapter.json');
const fs = require('fs');
var discoveryBuffer = __dirname + '/resultsDiscovery.json';
const path = require('path');
const {JSONPath} = require('jsonpath-plus');
var ProjectFileName;
var ProjectJSON, RawProjectJSON;
var OldSource;
var OldAdapter;
var NewSource;
var NewAdapter;
var DeviceDrivers;
var NewJson;
function Read_SDKAdapter()
{
var MySdkAdapter = sdkadapter;
}
function FindLatestProjectFileName(Directory){
var filter = /^project-home.+json$/;
var Result = "";
var files = fs.readdirSync(Directory);
files.forEach(function (file, index) {
var fromPath = path.join(Directory, file);
if (filter.test(file)) {
console.log("file",file);
Result = file;
}
});
return Result;
}
function Read_Projectfile(FileName) {
console.log("Reading latest project-home file:",FileName);
data = fs.readFileSync("/steady/neeo/cp6/"+ FileName);
return data;
}
function Backup_Projectfile(Filename,Content) {
var BackupFilename = __dirname + "/"+ Filename + '.bkp'
if (fs.existsSync(BackupFilename)) {
console.log("Backup-file already exists, not overwriting it")
return true;
}
try {
fs.writeFileSync(BackupFilename, Content);
}
catch (err)
{console.log("Error creating backup, aborting",err);
return false;
}
return true;
}
function GetAllDeviceDrivers(ProjectJSON) {
var PossibleDups = [];
var filter = / \(2\)$/
NewJson = JSON.stringify(ProjectJSON) // prepare a new JSON-structure that can be modified and written out
DeviceDrivers = JSONPath({path: "$.project.rooms.*.devices.*", json: ProjectJSON});
var SplitOn = " (2)"
console.log("")
console.log("Overview of all custom drivers:")
for(var i = 0; i < DeviceDrivers.length; i++)
if (DeviceDrivers[i].details.sourceName.substring(0,4)=="src-"){
console.log("Driver: ",DeviceDrivers[i].name,DeviceDrivers[i].details.sourceName,DeviceDrivers[i].details.adapterName)
if (filter.test(DeviceDrivers[i].name)) // IS this the "Duplicate devicedriver" we are looking for
PossibleDups.push({name:DeviceDrivers[i].name,sourceName:DeviceDrivers[i].details.sourceName,adapterName:DeviceDrivers[i].details.adapterName})
};
console.log("")
if (!PossibleDups.length)
{console.log("We cannot find a duplicate device-driver (marked by '"+SplitOn+"' at the end of it's name; aborting")
throw "Error"
}
let FoundOne = 0;
for(var New = 0; New < PossibleDups.length; New++) {
let LookforName = PossibleDups[New].name.split(SplitOn)[0]
console.log("Possible new driver name:",PossibleDups[New].name,"Checking if we have a matching driver with name",LookforName)
for(var Old = 0; Old < DeviceDrivers.length; Old++) {
if (LookforName==DeviceDrivers[Old].name) {
if (DeviceDrivers[Old].details.sourceName != PossibleDups[New].sourceName||DeviceDrivers[Old].details.adapterName!=PossibleDups[New].adapterName)
{console.log("Yes, we have a matching device driver and it has different source/adaptername");
MakeChanges(ProjectFileName,ProjectJSON,DeviceDrivers[Old],PossibleDups[New])
FoundOne++;
}
else
console.log("Yes, we have a maching pair, but it has already been updated; you can safely remove this duplicate device name");
console.log("")
}
}
}
if (!FoundOne) {
console.log("Nothing we can do right now")
return false
}
else {console.log("We are good to go!")
return true
}
}
function MakeChanges(FileName,ProjectJSON,OldDeviceDriver,PossibleDup) {
var OldSource_term = new RegExp(OldDeviceDriver.details.sourceName, "g");
var OldAdapter_term = new RegExp(OldDeviceDriver.details.adapterName, "g");
if (OldDeviceDriver.details.sourceName!=PossibleDup.sourceName)
console.log("replacing",OldDeviceDriver.details.sourceName,"into", PossibleDup.sourceName)
console.log("replacing",OldDeviceDriver.details.adapterName,"into",PossibleDup.adapterName)
NewJson = NewJson.replace(OldSource_term,PossibleDup.sourceName)
NewJson = NewJson.replace(OldAdapter_term,PossibleDup.adapterName)
}
function Write_NewProjectFile(FileName,NewJson) {
let NewFileName = "/steady/neeo/cp6/"+ FileName;
console.log("And writing contents to file",NewFileName)
try {
fs.writeFileSync(NewFileName, NewJson)
}
catch (err) {
console.log("Error writing file ",NewFileName,"aborting",err);
return false}
console.log("Outputfile written:",NewFileName);
return true
}
function MyMain() {
ProjectFileName = FindLatestProjectFileName("/steady/neeo/cp6");
if (ProjectFileName) {
ProjectJSON = Read_Projectfile(ProjectFileName)
if (!Backup_Projectfile(ProjectFileName,ProjectJSON))
console.log("Error creating backup, aborting")
else
{ProjectJSON = JSON.parse(ProjectJSON);
if (GetAllDeviceDrivers(ProjectJSON))
Write_NewProjectFile(ProjectFileName,NewJson)
}
}
}
MyMain();