Skip to content

Commit 6925ba5

Browse files
committed
refactor: 以前のPRによるESLint修正を適用
1 parent ecc74da commit 6925ba5

7 files changed

Lines changed: 192 additions & 136 deletions

File tree

.eslintrc.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ module.exports = {
1212
'no-shadow': 'off',
1313
'@typescript-eslint/no-shadow': 'error',
1414
'no-unused-vars': 'off',
15-
'@typescript-eslint/no-unused-vars': 'error',
15+
'@typescript-eslint/no-unused-vars': [
16+
'error',
17+
{ argsIgnorePattern: '^_|__' },
18+
],
19+
'no-console': 'off',
20+
'consistent-return': 'off',
21+
'no-plusplus': 'off',
22+
'no-alert': 'off',
1623
},
1724
parserOptions: {
1825
ecmaVersion: 2020,
Lines changed: 102 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,166 @@
11
const { SerialPort } = require('serialport');
22

3-
const { ReadlineParser } = require('@serialport/parser-readline')
3+
const { ReadlineParser } = require('@serialport/parser-readline');
44

55
require('date-utils');
6-
const path = require("path");
7-
const fs = require("fs");
8-
const os = require("os");
6+
const path = require('path');
7+
const fs = require('fs');
8+
const os = require('os');
9+
10+
// Helper functions moved to the top to resolve 'no-use-before-define'
11+
const formatDate = (date) => {
12+
return date.toFormat('YYYY-MM-DD_HH24-MI-SS');
13+
};
14+
15+
// ファイルサイズをわかりやすい単位にフォーマットする関数
16+
const formatFileSize = (bytes) => {
17+
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
18+
if (bytes === 0) return '0 B';
19+
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10); // Added radix
20+
return `${(bytes / 1024 ** i).toFixed(2)} ${sizes[i]}`;
21+
};
922

1023
const portMap = new Map();
1124
const dataRecorderMap = new Map();
1225

1326
// handle serial port //
1427
const getSerialPorts = async () => {
1528
return SerialPort.list();
16-
}
29+
};
30+
31+
const closeSerialPort = (webContents) => {
32+
const port = portMap.get(webContents.id);
33+
if (port !== undefined && port.isOpen) {
34+
port.close();
35+
}
36+
};
1737

1838
const setSerialPort = (_portPath, webContents) => {
19-
return new Promise(function (resolve, reject) {
39+
return new Promise(function setSerialPortPromise(resolve, reject) {
40+
// Named function
2041
closeSerialPort(webContents);
21-
const port = new SerialPort({
22-
path: _portPath,
23-
baudRate: 9600
24-
}, function (err) {
25-
if (err) {
26-
reject(err.message);
27-
} else {
28-
resolve("connected");
42+
const port = new SerialPort(
43+
{
44+
path: _portPath,
45+
baudRate: 9600,
46+
},
47+
function portOpenCallback(err) {
48+
// Named function
49+
if (err) {
50+
reject(err.message);
51+
} else {
52+
resolve('connected');
53+
}
2954
}
30-
});
55+
);
3156

3257
portMap.set(webContents.id, port);
3358

34-
const parser = port.pipe(new ReadlineParser({
35-
delimiter: '\r\n'
36-
}));
59+
const parser = port.pipe(
60+
new ReadlineParser({
61+
delimiter: '\r\n',
62+
})
63+
);
3764

38-
parser.on('data', function (_rawData) {
65+
parser.on('data', function parserDataCallback(_rawData) {
66+
// Named function
3967
const currentTime = new Date();
4068
const data = {
4169
timestamp: currentTime,
42-
rawData: _rawData.split(",")
43-
}
44-
webContents.send("newData", data);
70+
rawData: _rawData.split(','),
71+
};
72+
webContents.send('newData', data);
4573
const dataRecorder = dataRecorderMap.get(webContents.id);
4674
if (dataRecorder != null) dataRecorder.saveData(data);
4775
});
4876

49-
port.on("close", function () {
50-
console.log("Port closed!");
77+
port.on('close', function portCloseCallback() {
78+
// Named function
79+
console.log('Port closed!');
5180
});
52-
})
53-
}
54-
55-
const closeSerialPort = (webContents) => {
56-
const port = portMap.get(webContents.id);
57-
if (port !== undefined && port.isOpen) {
58-
port.close();
59-
}
60-
}
81+
});
82+
};
6183

6284
// handle baudRate //
6385
const setBaudRate = async (_baudRate, webContents) => {
6486
const port = portMap.get(webContents.id);
6587
if (port) {
6688
await port.update({ baudRate: Number(_baudRate) });
6789
}
68-
}
90+
};
6991

7092
// handle save //
7193
class DataRecorder {
72-
savePath = ""
94+
savePath = '';
95+
7396
shouldRecord = false;
97+
7498
numSamples = 0;
75-
start_date;
99+
100+
startDate; // Renamed to camelCase
101+
76102
webContents;
103+
77104
constructor(webContents) {
78105
this.webContents = webContents;
79106
// 保存パスの作成
80107
// 2022-08-26_20-13-47
81-
this.start_date = new Date();
82-
const start_date_format = formatDate(this.start_date);
83-
const saveDir = path.join(os.homedir(), "/Documents/PlantAnalysis/Data", start_date_format)
108+
this.startDate = new Date(); // Renamed to camelCase
109+
const startDateFormat = formatDate(this.startDate); // Renamed to camelCase
110+
const saveDir = path.join(
111+
os.homedir(),
112+
'/Documents/PlantAnalysis/Data',
113+
startDateFormat // Renamed to camelCase
114+
);
84115
// 保存先の作成
85116
if (!fs.existsSync(saveDir)) fs.mkdirSync(saveDir, { recursive: true });
86-
this.savePath = path.join(saveDir, `${start_date_format}.csv`);
117+
this.savePath = path.join(saveDir, `${startDateFormat}.csv`); // Renamed to camelCase
87118
// 記録の開始
88119
this.shouldRecord = true;
89120
}
90121

91122
saveData = (data) => {
92123
if (!this.shouldRecord) return;
93-
if (data.rawData[0].includes("*")) return;
124+
if (data.rawData[0].includes('*')) return;
94125
if (this.numSamples < 1) {
95126
this.numSamples += 1;
96-
return; //最初の読み込みはこけることがあるので避ける
127+
return; // 最初の読み込みはこけることがあるので避ける
97128
}
98129
// 保存する
99130
// ファイルが存在するか確認
100131
if (!fs.existsSync(this.savePath)) {
101132
// ない場合headerを書き込み
102-
let headLine = "";
103-
headLine += "timestamp"
104-
for (const rawData of data.rawData) {
133+
let headLine = '';
134+
headLine += 'timestamp';
135+
data.rawData.forEach((rawData) => {
136+
// Changed to forEach
105137
const headName = rawData.split(':')[0].trim();
106-
headLine += `,${headName}`
107-
}
138+
headLine += `,${headName}`;
139+
});
108140
fs.writeFileSync(this.savePath, `${headLine}\n`);
109141
}
110142
// ある場合は追記
111-
let dataLine = "";
143+
let dataLine = '';
112144
dataLine += data.timestamp;
113-
for (const rawData of data.rawData) {
145+
data.rawData.forEach((rawData) => {
146+
// Changed to forEach
114147
const point = rawData.match(/[+-]?(?:\d+\.?\d*|\.\d+)/)[0] || '0';
115-
dataLine += `,${point}`
116-
}
148+
dataLine += `,${point}`;
149+
});
117150
fs.appendFileSync(this.savePath, `${dataLine}\n`);
118151
// 計測時間・容量を通知
119-
const info = {}
152+
const info = {};
120153
/// 計測時間
121-
const elapsed_time = new Date() - this.start_date.getTime();
122-
const elapsedSeconds = Math.floor(elapsed_time / 1000) % 60;
123-
const elapsedMinutes = Math.floor(elapsed_time / 60000) % 60;
124-
const elapsedHour = Math.floor(elapsed_time / 3600000) % 24;
125-
const elapsedDay = Math.floor(elapsed_time / 86400000);
126-
let elapsed_time_format = elapsedDay < 1 ? "" : `${elapsedDay}days`
127-
elapsed_time_format += `${String(elapsedHour).padStart(2, "0")}:${String(elapsedMinutes).padStart(2, "0")}:${String(elapsedSeconds).padStart(2, "0")}`
128-
info.elapsedTime = elapsed_time_format;
154+
const elapsedTime = new Date() - this.startDate.getTime(); // Renamed to camelCase
155+
const elapsedSeconds = Math.floor(elapsedTime / 1000) % 60;
156+
const elapsedMinutes = Math.floor(elapsedTime / 60000) % 60;
157+
const elapsedHour = Math.floor(elapsedTime / 3600000) % 24;
158+
const elapsedDay = Math.floor(elapsedTime / 86400000);
159+
let elapsedTimeFormat = elapsedDay < 1 ? '' : `${elapsedDay}days`; // Renamed to camelCase
160+
elapsedTimeFormat += `${String(elapsedHour).padStart(2, '0')}:${String(
161+
elapsedMinutes
162+
).padStart(2, '0')}:${String(elapsedSeconds).padStart(2, '0')}`;
163+
info.elapsedTime = elapsedTimeFormat;
129164
/// 容量
130165
try {
131166
const stats = fs.statSync(this.savePath);
@@ -136,46 +171,33 @@ class DataRecorder {
136171
console.error(err);
137172
}
138173

139-
this.webContents.send("info", info);
140-
}
174+
this.webContents.send('info', info);
175+
};
141176

142177
stopRecord = () => {
143178
this.shouldRecord = true;
144-
}
179+
};
145180
}
146181

147182
const recordStart = (webContents) => {
148183
const dataRecorder = new DataRecorder(webContents);
149184
dataRecorderMap.set(webContents.id, dataRecorder);
150185
return dataRecorder.savePath;
151-
}
152-
186+
};
153187

154188
const recordStop = (webContents) => {
155189
const dataRecorder = dataRecorderMap.get(webContents.id);
156190
if (dataRecorder) {
157191
dataRecorder.stopRecord();
158192
}
159-
return "sello"
160-
}
161-
162-
const formatDate = (date) => {
163-
return date.toFormat('YYYY-MM-DD_HH24-MI-SS');
164-
}
165-
166-
// ファイルサイズをわかりやすい単位にフォーマットする関数
167-
const formatFileSize = (bytes) => {
168-
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
169-
if (bytes === 0) return '0 B';
170-
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
171-
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${sizes[i]}`;
172-
}
193+
return 'sello';
194+
};
173195

174196
module.exports = {
175197
getSerialPorts,
176198
setSerialPort,
177199
closeSerialPort,
178200
setBaudRate,
179201
recordStart,
180-
recordStop
202+
recordStop,
181203
};

0 commit comments

Comments
 (0)