Merge remote-tracking branch 'origin/feature/sd_data' into feature/sd_data

This commit is contained in:
TommyXin
2021-12-02 16:10:21 +08:00
4 changed files with 158 additions and 1135 deletions
+1 -1
View File
@@ -81,7 +81,7 @@ export const IIRFliter = async (ctx, next) => {
function getPromise () {
return new Promise((resolve, reject) => {
const subprocess = childProcess.spawn('python3', [
path.join(homedir(), '/IIRFilter.py'),
path.join(homedir(), 'bioproanalysis', 'filter', 'IIRFilter.py'),
type,
order,
fs,
+154 -131
View File
@@ -2,171 +2,194 @@ import RecordingDataMeta from '../../../models/file/recording_data_meta'
import Controller from '../../../models/product/controller'
import childProcess from 'child_process'
import path from 'path'
import { homedir } from 'os'
const recordingDataMeta = new RecordingDataMeta()
const controller = new Controller()
const sdPath = [homedir(), 'bioproanalysis', 'sdcard']
const sdGetPath = [...sdPath, 'neuLiveSD_C', 'sd_list']
const sdDeletePath = [...sdPath, 'neuLiveSD_C', 'sd_remove']
const sdUploadPath = [...sdPath, 'sd_download.py']
const getParameter = async (metaID, parameterName) => {
const ret = await recordingDataMeta.getByID(metaID)
return ret[0].parameter_set[parameterName]
}
const getPath = async (uuid) => {
return getPathByUuid(uuid, getUuidPathArray(await getUuidPathStringPromise()))
}
const getPathByUuid = (uuid, uuidPathArray) => {
for (const uuidPath of uuidPathArray) {
if (uuidPath[0] === uuid) {
return uuidPath[1]
}
}
}
// [uuid1, uuid2, uuid3...]
const getUuidList = (uuidPathString) => {
return uuidPathString.split('\n').map(val => val.split(' ')[0])
}
// [[uuid1, path1], [uuid2, path2], [uuid3, path3]...]
const getUuidPathArray = (uuidPathString) => {
return uuidPathString.split('\n').map(val => val.split(' '))
}
// 'uuid1 path1\n uuid2 path2\n uuid3 path3\n....'
const getUuidPathStringPromise = async () => {
return new Promise((resolve, reject) => {
const subprocess = childProcess.spawn(path.join(...sdGetPath))
let dataString = ''
subprocess.stdout.on('data', async (data) => {
dataString += data.toString()
})
subprocess.stderr.on('data', (error) => {
console.error(`Error ${error}`)
reject(error)
})
subprocess.on('close', (code) => {
console.log(`sdcard get process exited with code ${code}`)
resolve(dataString)
})
})
}
function uploadPromise (uuidPath, uuid, ampGain) {
return new Promise((resolve, reject) => {
const subprocess = childProcess.spawn('python3', [
path.join(...sdUploadPath),
uuidPath,
uuid,
ampGain
])
let dataString = ''
subprocess.stdout.on('data', async (data) => {
dataString += data.toString()
console.log('upload', dataString)
})
subprocess.stderr.on('data', (error) => {
reject(error)
console.error(`Error ${error}`)
})
subprocess.on('close', (code) => {
console.log(`sd card upload process exited with code ${code}`)
resolve(dataString)
})
})
}
function deletePromise (uuidPath) {
return new Promise((resolve, reject) => {
const subprocess = childProcess.spawn(
path.join(...sdDeletePath),
[uuidPath, 120]
)
let dataString = ''
subprocess.stdout.on('data', async (data) => {
dataString += data.toString()
})
subprocess.stderr.on('data', (error) => {
console.error(`Error ${error}`)
reject(error)
})
subprocess.on('close', (code) => {
console.log(`grep process exited with code ${code}`)
resolve(dataString)
})
})
}
export const exist = async (ctx, next) => {
const request = ctx.request.body
// const controllerID = request.controllerID
const mac = request.mac
const uuid = request.uuid
function existPromise () {
return new Promise((resolve, reject) => {
const subprocess = childProcess.spawn('python3', [
path.join(__dirname, '/sd_card_manipulate.py'),
'exist',
mac,
uuid
])
subprocess.stdout.on('data', async (data) => {
const dataString = data.toString()
resolve(dataString)
})
subprocess.stderr.on('data', (error) => {
reject(error)
console.error(`Error ${error}`)
})
subprocess.on('close', (code) => {
console.log(`grep process exited with code ${code}`)
})
})
}
const result = await existPromise()
const path = await getPath(uuid)
let result
(path !== undefined) ? result = true : result = false
ctx.body = result
next()
}
export const deleted = async (ctx, next) => {
const request = ctx.request.body
// const controllerID = request.controllerID
const mac = request.mac
const uuid = request.uuid
function deletePromise () {
return new Promise((resolve, reject) => {
const subprocess = childProcess.spawn('python3', [
path.join(__dirname, '/sd_card_manipulate.py'),
'delete',
mac,
uuid
])
subprocess.stdout.on('data', async (data) => {
const dataString = data.toString()
if (dataString === 'True') {
resolve(dataString)
}
})
subprocess.stderr.on('data', (error) => {
reject(error)
console.error(`Error ${error}`)
})
subprocess.on('close', (code) => {
console.log(`grep process exited with code ${code}`)
})
})
}
const result = await deletePromise()
ctx.body = result
next()
}
export const upload = async (ctx, next) => {
export const uploadThenDelete = async (ctx, next) => {
const request = ctx.request.body
const controllerID = request.controllerID
const metaID = request.metaID
const mac = request.mac
const uuid = request.uuid
const uuidPath = await getPath(uuid)
const ampGain = await getParameter(metaID, 'AMP_GAIN')
// timeout defualt 120s to no-limit
ctx.req.setTimeout(0)
function uploadPromise () {
return new Promise((resolve, reject) => {
const subprocess = childProcess.spawn('python3', [
path.join(__dirname, '/sd_card_manipulate.py'),
'upload',
mac,
uuid
])
subprocess.stdout.on('data', async (data) => {
const dataString = data.toString()
if (dataString === 'True') {
resolve(dataString)
}
})
subprocess.stderr.on('data', (error) => {
reject(error)
console.error(`Error ${error}`)
})
subprocess.on('close', (code) => {
console.log(`sd card upload process exited with code ${code}`)
})
})
}
// controller column sd_uploading update
await controller.update({
sd_uploading: true
}, controllerID)
const result = await uploadPromise()
const uploadResult = await uploadPromise(uuidPath, uuid, ampGain)
if (uploadResult === 'true') {
// meta sd_card_uploaded true
await recordingDataMeta.update({
sd_data_uploaded: true
}, metaID)
await deletePromise(uuidPath)
}
// controller column sd_uploading close
await controller.update({
sd_uploading: false
}, controllerID)
await recordingDataMeta.update({
sd_data_uploaded: true
}, metaID)
ctx.body = result
ctx.body = uploadResult
next()
}
export const get = async (ctx, next) => {
function getPromise () {
return new Promise((resolve, reject) => {
const subprocess = childProcess.spawn('python3', [
path.join(__dirname, '/sd_card_manipulate.py'),
'get_uuid_list'
])
subprocess.stdout.on('data', async (data) => {
const dataString = data.toString()
resolve(dataString)
})
subprocess.stderr.on('data', (error) => {
reject(error)
console.error(`Error ${error}`)
})
subprocess.on('close', (code) => {
console.log(`sdcard get process exited with code ${code}`)
})
})
}
const result = await getPromise()
ctx.body = result
export const getFileList = async (ctx, next) => {
const result = await getUuidPathStringPromise()
const uuidList = getUuidList(result)
ctx.body = uuidList
next()
}
// export const deleted = async (ctx, next) => {
// const request = ctx.request.body
// const controllerID = request.controllerID
// const uuid = request.uuid
// const uuidPath = await getPath(uuid)
// // timeout defualt 120s to no-limit
// ctx.req.setTimeout(0)
// // controller column sd_uploading update
// await controller.update({
// sd_uploading: true
// }, controllerID)
// const result = await deletePromise(uuidPath)
// // controller column sd_uploading close
// await controller.update({
// sd_uploading: false
// }, controllerID)
// ctx.body = result
// next()
// }
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -5,9 +5,9 @@ import controllers from '../../../controllers'
export default function (_prefix) {
const prefix = _prefix + '/sdcard'
return [
['GET', prefix + '/get', controllers.sdCard.get],
['GET', prefix + '/get', controllers.sdCard.getFileList],
['POST', prefix + '/exist', controllers.sdCard.exist],
['POST', prefix + '/upload', controllers.sdCard.upload],
['POST', prefix + '/delete', controllers.sdCard.deleted]
['POST', prefix + '/upload', controllers.sdCard.uploadThenDelete],
// ['POST', prefix + '/delete', controllers.sdCard.deleted]
]
}