-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
274 lines (243 loc) · 9.38 KB
/
index.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Require express and create an instance of it
const Fritzing = require ('./node_modules/fritzing-js/fritzing.js')
const {FritzingPartsAPIClient} = require('fritzing-parts-api-client-js')
const express = require('express');
const cors = require('cors')
const fs = require('fs');
const path = require('path');
const _ = require('lodash')
const app = express();
// const {parse, stringify} = require('flatted/cjs')
const FZPUtils = require('fzp-js')
const fzConnections = require('./fzConnections.js')
const partNames = require('./partNames.js')
let mysketch
const FritzingAPI = 'https://fritzing.github.io/fritzing-parts';
const FritzingAPISVGCore = FritzingAPI+'/svg/core/';
app.use(cors())
const SketchBundle = Fritzing.SketchBundle
const Sketch = Fritzing.Sketch
let tempBlockConnections = []
let tempAimConnections = []
let filename = 'test.fzz'
const dataBaseFolder = './examples'
const dataBase = []
let newTemp = {}
const loadDataBase = (res) => fs.readdir(dataBaseFolder, (err, files) => {
console.log(files);
files.forEach((file,key) => {
let temp = {name:path.parse(file).name, ext:path.parse(file).ext, data: {} }
// console.log("this is temp",temp);
if (_.find(dataBase,el => el.name === temp.name))
{
console.log('we have it in database',temp.name);
_.find(dataBase, function(el, key) {
if (el.name === temp.name) {
// console.log('database is empty',dataBase[key],el,temp);
if (_.isEmpty(el.data)) {
// console.log('before extension check',temp);
if (temp.ext === '.json') {
console.log('have a json for that',temp.name, temp.ext);
let thePath = './examples/' + temp.name + temp.ext
console.log(thePath);
fs.readFile(thePath ,function(err,data){
// console.log('storing',key, dataBase[key],data);
newTemp = JSON.parse(data)
// console.log('OUR NEW TEMP FROM READFILE',newTemp);
dataBase[key].data = newTemp
// console.log('NEW TEMP OUTSIDE READFILE',dataBase)
})
// console.log('the element', dataBase);
// dataBase.push(temp)
}
}
return el === temp}}
)
console.log('Known:',dataBase, file);
}
else {
if (temp.ext !== '.json') {
dataBase.push(temp)
// console.log('NEW FILE:', temp);
}
}
});
res.json(dataBase);
})
function download(filename,res) {
let dbDataName = filename.slice(0, -4)
console.log(dbDataName);
if (_.find(dataBase,el =>
{
if (!_.isEmpty(el.data)){
return el.name === dbDataName}
}
)){
_.find(dataBase,el => {
(el.name === dbDataName) ? (
// console.log("the data we use",el),
res.json(el.data)):null
})
console.log('USE FROM DATABASE',dbDataName)
}
else {
console.log('READ NEW FILE',filename)
return fs.readFile('./examples/'+filename,function(err,data){
if (data) {
SketchBundle.fromFZZ(data)
.then((sketchBundle) => {
console.log("from sketchBundle", filename,filename.slice(0, -1));
let mytest = sketchBundle.primary
fzFilename = filename.slice(0, -1)
let myInstances = sketchBundle.primary[fzFilename].instances
/*
get all the parts that are not wires
*/
let allParts = _.filter(myInstances, function(el) {
// if (!el.moduleIdRef.includes('Wire') && !el.moduleIdRef.includes('Via') && !el.moduleIdRef.includes('Hole') ) {
return el
// }
})
/*
get all the partnames from the fz
*/
let iLoveTest = partNames.partNames(allParts)
// fs.writeFileSync('./data/out.json', JSON.stringify(partNames));
let myConnections = fzConnections.fzConnections('ardu',iLoveTest)
// fs.writeFileSync('./data/endConnections.json', JSON.stringify(myConnections));
/*
get the arduino instance
*/
let newArduino = _.find(iLoveTest, function(el) {
if (el.name.includes('arduino') && !el.name.includes('shield')){
return el
}
})
console.log("we have a new Arduino found",newArduino);
arduinoConnected = []
myConnections.mainBoardEndConnection.map(connector => {
arduinoConnected.push({connector:connector.connections[0].startId, type: '',connected: true, breadSvgId: ''})
})
/*
get the fzp file of the board and the svg
*/
FZPUtils.FZPUtils.loadFZPandSVGs('core/'+ newArduino.name)
.then(fzp => {
let test = fzp
let returnObject = {
moduelId: test.moduleId,
partName: test.title,
connected: arduinoConnected,
connectors: test.connectors,
breadSvg: test.views.breadboard.svg,
// pcbSvg: test.views.pcb.svg,
}
returnObject.connected.map((con,key) => {
// console.log(con);
let temp = _.find(returnObject.connectors, (el) => {
// console.log('EL',el.id, 'CON', con.connector);
return el.id === con.connector
})
// console.log("this is my temps from what i have found",temp);
if (temp) {
console.log("this is the temp",temp, temp.views.breadboard.svgId);
switch (true) {
case temp.name.includes('gnd') || temp.name.includes('GND') :
console.log("a ground connector");
returnObject.connected[key].type = 'GND'
returnObject.connected[key].name = temp.name
returnObject.connected[key].breadSvgId = temp.views.breadboard.svgId
break;
case temp.name.includes('5V') || temp.name.includes('3v3') || temp.name.includes('3V') :
console.log("a ground connector");
returnObject.connected[key].type = 'V'
returnObject.connected[key].name = temp.name
returnObject.connected[key].breadSvgId = temp.views.breadboard.svgId
break;
case temp.name.includes('A'):
console.log("an analog connector");
returnObject.connected[key].type = 'out'
returnObject.connected[key].name = temp.name
returnObject.connected[key].breadSvgId = temp.views.breadboard.svgId
break;
default:
returnObject.connected[key].type = 'unknown'
returnObject.connected[key].name = temp.name
returnObject.connected[key].breadSvgId = temp.views.breadboard.svgId
}
// temp.name
}
})
jsonFilename = filename.slice(0, -4)
console.log('the jsonFilename', jsonFilename);
_.find(dataBase,el => {
if (el.name === jsonFilename){
if (_.isEmpty(el.data)) {
el.data = returnObject
fs.writeFileSync('./examples/'+jsonFilename+'.json', JSON.stringify(returnObject))
return
}
}
else {
fs.writeFileSync('./examples/'+jsonFilename+'.json', JSON.stringify(returnObject))
}
return el.name === filename
})
// console.log(returnObject);
res.json(returnObject);
})
.catch((err) => {
console.log("we have an error in READ FZP",err);
res.json({err: {show: true,
message: 'err from READ FZP'}
})
})
})
.catch((err) => {
console.log("we have an error in READ FZZ",err);
res.json({err: {show: true,
message: 'err from sketchBundle'}
})
})
}
else {
console.log("err,readFile",err);
res.json({err: {show: true,
message: err}
})
}
})
}
}
app.get('/database', function (req, res) {
if(_.isEmpty(dataBase)) {
loadDataBase(res)
}
else {
res.json(dataBase)
}
// console.log("get the fzz",tempBlockConnections);
});
app.get('/fzz', function (req, res) {
loadDataBase(res)
// console.log("get the fzz",tempBlockConnections);
});
app.get('/fzz/:filename', function (req, res) {
// req.query.filename
download(req.params.filename,res)
// console.log("get the fzz",req.params.filename);
// res.json(JSON.stringify(tempBlockConnections));
// tempBlockConnections = []
});
// On localhost:3000/welcome
app.get('/welcome', function (req, res) {
res.send('<b>Hello</b> welcome to my http server made with express');
});
// Change the 404 message modifing the middleware
app.use(function(req, res, next) {
res.status(404).send("Sorry, that route doesn't exist. Have a nice day :)");
});
// start the server in the port 3000 !
app.listen(3005, function () {
console.log('Example app listening on port 3005.');
});