diff --git a/lib/www/server/lib/db/plan/lib/index.js b/lib/www/server/lib/db/plan/lib/index.js new file mode 100644 index 0000000..a48b6e8 --- /dev/null +++ b/lib/www/server/lib/db/plan/lib/index.js @@ -0,0 +1,102 @@ +const configuration = require('../../configuration'); + +async function getDistance (client, payload) { + const text = ` + SELECT ST_Distance(pp0.geometry, pp1.geometry) distance + FROM preplot_points pp0, + preplot_points pp1 + WHERE + pp0.line = $1 AND pp1.line = $1 + AND pp0.class = $4 AND pp1.class = $4 + AND pp0.point = $2 AND pp1.point = $3; + `; + + const p = payload; + const res = await client.query(text, [p.line, p.fsp, p.lsp, p.class || "V"]); + if (res.rows.length) { + return res.rows[0].distance; + } // else undefined +} + +async function getSequence (client) { + const text = ` + SELECT max(sequence)+1 AS sequence + FROM ( + SELECT sequence + FROM raw_lines + UNION SELECT sequence + FROM planned_lines + ) t; + `; + + const res = await client.query(text); + return res.rows[0] && res.rows[0].sequence; +} + +async function getTimestamps (client, projectId, payload) { + + const defaultLineChangeDuration = (await configuration.get(projectId, "planner/defaultLineChangeDuration") || 30) * 60*1000; // minutes to milliseconds + + const defaultAcquisitionSpeed = (await configuration.get(projectId, "planner/defaultAcquisitionSpeed") || 4.8) * 1.852 / 3.6; // Knots to m/s + + const distance = await getDistance(client, payload); + + const text = ` + SELECT * FROM planned_lines; + `; + + const res = await client.query(text); + + const ts0 = new Date( + (res.rows.length + ? res.rows.map(r => r.ts1).reduce( (a, b) => Math.max(a, b) ) + : Date.now() + ) + defaultLineChangeDuration + ); + + const ts1 = new Date(ts0.valueOf() + (distance / defaultAcquisitionSpeed)*1000); + + return {ts0, ts1}; +} + +async function getSequencesForLine (client, line) { + const text = ` + SELECT * from sequences_summary WHERE line = $1 ORDER BY sequence; + `; + + const res = await client.query(text, [line]); + return res.rows; +} + +async function getPlanned (client) { + const text = ` + SELECT * FROM planned_lines ORDER BY sequence; + `; + + const res = await client.query(text); + return res.rows; +} + + +async function getLineName (client, projectId, payload) { + // FIXME TODO Get line name script from configuration + + const planned = await getPlanned(client); + const previous = await getSequencesForLine(client, payload.line); + const attempt = planned.filter(r => r.line == payload.line).concat(previous).length; + const p = payload; + const incr = p.lsp > p.fsp; + const sequence = p.sequence; + const line = p.line; + return `${incr?"1":"2"}0${line}${attempt}${sequence.toString().padStart(3, "0")}S00000`; +} + + +module.exports = { + getDistance, + getSequence, + getTimestamps, + getSequencesForLine, + getPlanned, + getLineName +}; diff --git a/lib/www/server/lib/db/plan/post.js b/lib/www/server/lib/db/plan/post.js index 2349f08..5669682 100644 --- a/lib/www/server/lib/db/plan/post.js +++ b/lib/www/server/lib/db/plan/post.js @@ -1,96 +1,5 @@ const { setSurvey, transaction } = require('../connection'); -const configuration = require('../configuration'); - -async function getDistance (client, payload) { - const text = ` - SELECT ST_Distance(pp0.geometry, pp1.geometry) distance - FROM preplot_points pp0, - preplot_points pp1 - WHERE - pp0.line = $1 AND pp1.line = $1 - AND pp0.class = $4 AND pp1.class = $4 - AND pp0.point = $2 AND pp1.point = $3; - `; - - const p = payload; - const res = await client.query(text, [p.line, p.fsp, p.lsp, p.class || "V"]); - if (res.rows.length) { - return res.rows[0].distance; - } // else undefined -} - -async function getSequence (client) { - const text = ` - SELECT max(sequence)+1 AS sequence - FROM ( - SELECT sequence - FROM raw_lines - UNION SELECT sequence - FROM planned_lines - ) t; - `; - - const res = await client.query(text); - return res.rows[0] && res.rows[0].sequence; -} - -async function getTimestamps (client, projectId, payload) { - - const defaultLineChangeDuration = (await configuration.get(projectId, "planner/defaultLineChangeDuration") || 30) * 60*1000; // minutes to milliseconds - - const defaultAcquisitionSpeed = (await configuration.get(projectId, "planner/defaultAcquisitionSpeed") || 4.8) * 1.852 / 3.6; // Knots to m/s - - const distance = await getDistance(client, payload); - - const text = ` - SELECT * FROM planned_lines; - `; - - const res = await client.query(text); - - const ts0 = new Date( - (res.rows.length - ? res.rows.map(r => r.ts1).reduce( (a, b) => Math.max(a, b) ) - : Date.now() - ) + defaultLineChangeDuration - ); - - const ts1 = new Date(ts0.valueOf() + (distance / defaultAcquisitionSpeed)*1000); - - return {ts0, ts1}; -} - -async function getSequencesForLine (client, line) { - const text = ` - SELECT * from sequences_summary WHERE line = $1 ORDER BY sequence; - `; - - const res = await client.query(text, [line]); - return res.rows; -} - -async function getPlanned (client) { - const text = ` - SELECT * FROM planned_lines ORDER BY sequence; - `; - - const res = await client.query(text); - return res.rows; -} - -async function getLineName (client, projectId, payload) { - // FIXME TODO Get line name script from configuration - - const planned = await getPlanned(client); - const previous = await getSequencesForLine(client, payload.line); - console.log("Previous", previous); - const attempt = planned.filter(r => r.line == payload.line).concat(previous).length; - const p = payload; - const incr = p.lsp > p.fsp; - const sequence = p.sequence; - const line = p.line; - return `${incr?"1":"2"}0${line}${attempt}${sequence.toString().padStart(3, "0")}S00000`; -} +const lib = require('./lib'); async function post (projectId, payload, opts = {}) { @@ -98,12 +7,10 @@ async function post (projectId, payload, opts = {}) { try { if (!payload.sequence) { - payload.sequence = await getSequence(client); - console.log("Sequence", payload.sequence); + payload.sequence = await lib.getSequence(client); } if (!payload.ts0 || !payload.ts1) { - const ts = await getTimestamps(client, projectId, payload); - console.log("ts", ts); + const ts = await lib.getTimestamps(client, projectId, payload); if (!payload.ts0) { payload.ts0 = ts.ts0; } @@ -112,7 +19,7 @@ async function post (projectId, payload, opts = {}) { } } if (!payload.name) { - payload.name = await getLineName(client, projectId, payload); + payload.name = await lib.getLineName(client, projectId, payload); } const p = Object.assign({ @@ -127,7 +34,6 @@ async function post (projectId, payload, opts = {}) { `; const values = [ p.sequence, p.line, p.fsp, p.lsp, p.ts0, p.ts1, p.name, p.remarks, p.meta ]; - console.log(text, values); await client.query(text, values); } catch (err) { if (err.code && Math.trunc(err.code/1000) == 23) {