1- const {
2- SerialPort
3- } = require ( 'serialport' ) ;
1+ const { SerialPort } = require ( 'serialport' ) ;
42
5- const {
6- ReadlineParser
7- } = require ( '@serialport/parser-readline' )
3+ const { ReadlineParser } = require ( '@serialport/parser-readline' )
84
95require ( 'date-utils' ) ;
106const path = require ( "path" ) ;
117const fs = require ( "fs" ) ;
128const os = require ( "os" ) ;
139
10+ const portMap = new Map ( ) ;
11+ const dataRecorderMap = new Map ( ) ;
12+
1413// handle serial port //
1514const getSerialPorts = async ( ) => {
16- closeSerialPort ( ) ;
1715 return SerialPort . list ( ) ;
1816}
1917
20-
21- let port ;
22- let portPath ;
23- let baudRate = 9600 ;
24- let webContents ;
25-
26- const setSerialPort = ( _portPath , _webContents ) => {
18+ const setSerialPort = ( _portPath , webContents ) => {
2719 return new Promise ( function ( resolve , reject ) {
28- closeSerialPort ( ) ;
29- portPath = _portPath
30- port = new SerialPort ( {
31- path : portPath ,
32- baudRate : baudRate
20+ closeSerialPort ( webContents ) ;
21+ const port = new SerialPort ( {
22+ path : _portPath ,
23+ baudRate : 9600
3324 } , function ( err ) {
3425 if ( err ) {
3526 reject ( err . message ) ;
@@ -38,7 +29,7 @@ const setSerialPort = (_portPath, _webContents) => {
3829 }
3930 } ) ;
4031
41- webContents = _webContents ;
32+ portMap . set ( webContents . id , port ) ;
4233
4334 const parser = port . pipe ( new ReadlineParser ( {
4435 delimiter : '\r\n'
@@ -51,6 +42,7 @@ const setSerialPort = (_portPath, _webContents) => {
5142 rawData : _rawData . split ( "," )
5243 }
5344 webContents . send ( "newData" , data ) ;
45+ const dataRecorder = dataRecorderMap . get ( webContents . id ) ;
5446 if ( dataRecorder != null ) dataRecorder . saveData ( data ) ;
5547 } ) ;
5648
@@ -60,34 +52,37 @@ const setSerialPort = (_portPath, _webContents) => {
6052 } )
6153}
6254
63- const closeSerialPort = ( ) => {
55+ const closeSerialPort = ( webContents ) => {
56+ const port = portMap . get ( webContents . id ) ;
6457 if ( port !== undefined && port . isOpen ) {
6558 port . close ( ) ;
6659 }
6760}
6861
6962// handle baudRate //
70- const setBaudRate = async ( _baudRate , _webContents ) => {
71- baudRate = Number ( _baudRate ) ;
72- await setSerialPort ( portPath , _webContents ) ;
63+ const setBaudRate = async ( _baudRate , webContents ) => {
64+ const port = portMap . get ( webContents . id ) ;
65+ if ( port ) {
66+ await port . update ( { baudRate : Number ( _baudRate ) } ) ;
67+ }
7368}
7469
7570// handle save //
76- let dataRecorder ;
7771class DataRecorder {
7872 savePath = ""
7973 shouldRecord = false ;
8074 numSamples = 0 ;
81- constructor ( ) {
75+ start_date ;
76+ webContents ;
77+ constructor ( webContents ) {
78+ this . webContents = webContents ;
8279 // 保存パスの作成
8380 // 2022-08-26_20-13-47
84- const start_date = new Date ( ) ;
85- const start_date_format = formatDate ( start_date ) ;
81+ this . start_date = new Date ( ) ;
82+ const start_date_format = formatDate ( this . start_date ) ;
8683 const saveDir = path . join ( os . homedir ( ) , "/Documents/PlantAnalysis/Data" , start_date_format )
8784 // 保存先の作成
88- if ( ! fs . existsSync ( saveDir ) ) fs . mkdirSync ( saveDir , {
89- recursive : true
90- } ) ;
85+ if ( ! fs . existsSync ( saveDir ) ) fs . mkdirSync ( saveDir , { recursive : true } ) ;
9186 this . savePath = path . join ( saveDir , `${ start_date_format } .csv` ) ;
9287 // 記録の開始
9388 this . shouldRecord = true ;
@@ -123,7 +118,7 @@ class DataRecorder {
123118 // 計測時間・容量を通知
124119 const info = { }
125120 /// 計測時間
126- const elapsed_time = new Date ( ) - start_date . getTime ( ) ;
121+ const elapsed_time = new Date ( ) - this . start_date . getTime ( ) ;
127122 const elapsedSeconds = Math . floor ( elapsed_time / 1000 ) % 60 ;
128123 const elapsedMinutes = Math . floor ( elapsed_time / 60000 ) % 60 ;
129124 const elapsedHour = Math . floor ( elapsed_time / 3600000 ) % 24 ;
@@ -141,21 +136,26 @@ class DataRecorder {
141136 console . error ( err ) ;
142137 }
143138
144- webContents . send ( "info" , info ) ;
139+ this . webContents . send ( "info" , info ) ;
145140 }
146141
147142 stopRecord = ( ) => {
148143 this . shouldRecord = true ;
149144 }
150145}
151146
152- const recordStart = ( ) => {
153- dataRecorder = new DataRecorder ( ) ;
147+ const recordStart = ( webContents ) => {
148+ const dataRecorder = new DataRecorder ( webContents ) ;
149+ dataRecorderMap . set ( webContents . id , dataRecorder ) ;
154150 return dataRecorder . savePath ;
155151}
156152
157- const recordStop = ( ) => {
158- dataRecorder . stopRecord ( ) ;
153+
154+ const recordStop = ( webContents ) => {
155+ const dataRecorder = dataRecorderMap . get ( webContents . id ) ;
156+ if ( dataRecorder ) {
157+ dataRecorder . stopRecord ( ) ;
158+ }
159159 return "sello"
160160}
161161
0 commit comments