Skip to content

Commit

Permalink
Merge branch 'master' into 35-queries-detalhe-reporte
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasAndrad committed Jun 26, 2019
2 parents 0395c01 + d014f3a commit a9c0ec9
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 32 deletions.
16 changes: 16 additions & 0 deletions migrations/20190625032209_add_neural_net_state_to_analysis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function up (knex) {
return knex.schema.alterTable('analysis', (table) => {
table.string('neural_net_state')
})
}

function down (knex) {
return knex.schema.alterTable('analysis', t => {
t.dropColumn('neural_net_state')
})
}

export {
up,
down
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"multer": "^1.4.1",
"node-zip": "^1.1.1",
"pg": "^7.10.0",
"readline": "^1.3.0",
"unzipper": "^0.9.12",
"uuid": "^3.3.2"
},
Expand Down
28 changes: 4 additions & 24 deletions src/helpers/analysis.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
import { knexInstance } from '../db'
import uuid from 'uuid/v4'

let state = {
pubsub: null,
currentCable: null,
currentReport: null
}

async function setCable (cable) {
state.currentCable = cable
}

async function setReport (report) {
state.currentReport = report
}

async function setPubSub (pubsub) {
state.pubsub = pubsub
}
import { state } from './state'

async function createAnalysis (analysisObject) {
let { positionStart, positionEnd, imagePath } = analysisObject
let { positionStart, positionEnd, imagePath, cableState } = analysisObject
let reportId = state.currentReport.id
let cableId = state.currentCable.id
try {
Expand All @@ -29,7 +12,8 @@ async function createAnalysis (analysisObject) {
position_start: positionStart,
position_end: positionEnd,
report_id: reportId,
cable_id: cableId
cable_id: cableId,
state: cableState
}
let path = analysisObject.image_path || imagePath
if (path) {
Expand All @@ -47,9 +31,5 @@ async function createAnalysis (analysisObject) {
}

export {
state,
setCable,
setReport,
setPubSub,
createAnalysis
}
24 changes: 24 additions & 0 deletions src/helpers/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let state = {
pubsub: null,
currentCable: null,
currentReport: null
}

async function setCable (cable) {
state.currentCable = cable
}

async function setReport (report) {
state.currentReport = report
}

async function setPubSub (pubsub) {
state.pubsub = pubsub
}

export {
state,
setCable,
setReport,
setPubSub
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fsx.ensureDirSync(publicDir)
import { initFtpServer } from './ftp-server'

// Set global pubsub
import { setPubSub } from './helpers/analysis'
import { setPubSub } from './helpers/state'

const sleep = (timeout) => (new Promise((resolve, reject) => (setTimeout(resolve, timeout))))

Expand Down
52 changes: 47 additions & 5 deletions src/info-explorer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import nodeZip from 'node-zip'
import fsx from 'fs-extra'
import Path from 'path'
import unzip from 'unzipper'
import { exec } from 'child_process'
import util from 'util'
import { createAnalysis } from './helpers/analysis'
import { state } from './helpers/state'
import readline from 'readline'

const DISPLACEMENT = 50 // 50 means 5cm on real life
const SENSOR_ERROR_VALUE = 20

async function unzipFile (filename) {
let path = Path.resolve(__dirname, '../public', filename)
Expand All @@ -19,26 +21,66 @@ async function unzipFile (filename) {
return folder
}

async function checkCableState (PATH) {
return new Promise((resolve, reject) => {
let rl = readline.createInterface({
input: fsx.createReadStream(PATH)
})
let cableState = 'Normal'
let damageCount = 0
rl.on('line', (line) => {
let values = line.split(',')
let intValues = Array.from(values, (v) => parseInt(v))
let sum = intValues.reduce((a,b) => a + b)
let average = Math.floor(sum / intValues.length)
if (average <= SENSOR_ERROR_VALUE) {
damageCount++
}
})
rl.on('close', () => {
// Averages:
// - <= 3: Normal
// - > 3 <= 5: Danificado
// - > 5: Muito danificado
if (damageCount > 3 && damageCount <= 5) {
cableState = 'Danificado'
} else if (damageCount > 5) {
cableState = 'Muito danificado'
}
resolve(cableState)
})
})
}

async function infoControll (filename = '1557707265663-1.zip') {
if (filename) {
let folder = await unzipFile(filename)

// Executing python script to cocatenate images
let pythonScript = Path.resolve(__dirname, './lib/concat-images.py')
const PYTHON_SCRIPT_PATH = Path.resolve(__dirname, './lib/concat-images.py')
let execPromise = util.promisify(exec)
let { stdout, stderr } = await execPromise(`python3 ${pythonScript} ${folder}`)
let { stdout, stderr } = await execPromise(`python3 ${PYTHON_SCRIPT_PATH} ${folder}`)
if (stderr) {
throw new Error(`------> Erro inesperado ao gerar imagem concatenada: ${stderr} <------`)
}
// TODO: use image size to calculate poitionStart and positionEnd
let treatedFilename = filename.replace('.zip', '') // remove .zip sufix
let robotPosition = parseInt(treatedFilename.split('-')[1])
let splits = treatedFilename.split('-')
let robotPosition = parseInt(splits[1])
let place = robotPosition * DISPLACEMENT
let position = {
positionStart: place - DISPLACEMENT,
positionEnd: place
}
await createAnalysis({ ...position, image_path: folder + '/merged-image.png' })
if (splits.length === 3) { // check 'end' sufix
if (splits[2] === 'end') {
state.pubsub.publish('endCable', { endCable: place })
}
}

const SENSORS_REPORT_PATH = Path.resolve(folder, 'data.txt')
let cableState = await checkCableState(SENSORS_REPORT_PATH)
await createAnalysis({ ...position, image_path: folder + '/merged-image.png', cableState })
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mutations/reports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { knexInstance } from '../db'
import { setReport, setCable } from '../helpers/analysis'
import { setReport, setCable } from '../helpers/state'

import uuid from 'uuid/v4'

Expand Down
7 changes: 6 additions & 1 deletion src/subscriptions/analysis.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { state } from '../helpers/analysis'
import { state } from '../helpers/state'

export default {
analysisWasCreated: {
subscribe: (parent, args) => {
return state.pubsub.asyncIterator('analysisWasCreated')
}
},
endCable: {
subscribe: (parent, args) => {
return state.pubsub.asyncIterator('endCable')
}
}
}
2 changes: 2 additions & 0 deletions src/type-defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Analysis {
image_path: String
state: String
manual_state: String
neural_net_state: String
report_id: ID
cable_id: ID
created_at: DateTime
Expand Down Expand Up @@ -80,5 +81,6 @@ type Mutation {
# Subscriptions
type Subscription {
analysisWasCreated: Analysis!
endCable: Int!
}
`

0 comments on commit a9c0ec9

Please sign in to comment.