Last active
January 19, 2022 13:31
-
-
Save uranusjr/b5f8e51c86bfbf63996683b3546f5687 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"ambient": { // Ambient sensor. | |
"ambient": [{x: timestamp, y: temperature}, /* more... */] | |
}, | |
"1-1": { // Silo 1, cable 1. | |
"1": [{x: timestamp, y: temperature}, /* more... */], // Node 1. | |
"2": [{x: timestamp, y: temperature}, /* more... */] // Node 2. | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {DateTime} from 'luxon' | |
import xlsx from 'xlsx' | |
import i18n from '@/i18n' | |
function formatSheetName(key) { | |
// TODO: Silo alias. | |
return key === 'ambient' ? i18n.t('ambientLabel') : key | |
} | |
function formatNodeName(key) { | |
return key === 'ambient' ? i18n.t('ambientLabel') : key | |
} | |
const baseDate = new Date(1899, 11, 30, 0, 0, 0) | |
function timestampToExcelDate(timestamp, timezone) { | |
const v = DateTime | |
.fromMillis(timestamp, {zone: 'utc'}) | |
.setZone(timezone) | |
.toJSDate() | |
const dnthresh = baseDate.getTime() + | |
(v.getTimezoneOffset() - baseDate.getTimezoneOffset()) * 60000 | |
return (v.getTime() - dnthresh) / (24 * 60 * 60 * 1000) | |
} | |
export function exportTemperatures(tabularData, timezone, filename) { | |
const book = xlsx.utils.book_new() | |
for (const [key, entry] of Object.entries(tabularData)) { | |
const aoa = [['', ...entry.nodes.map(formatNodeName)]] | |
for (const [ts, data] of Object.entries(entry.data)) { | |
const timestamp = Number(ts) | |
if (Number.isNaN(timestamp)) { | |
continue | |
} | |
const date = timestampToExcelDate(timestamp) | |
const dateCell = {v: date, t: 'n', z: 'yyyy-mm-dd" "hh:MM:ss'} | |
const temperatures = entry.nodes.map((nid) => { | |
const value = data[nid] | |
const temperature = Number(value) | |
return Number.isNaN(temperature) ? (value || '') : temperature | |
}) | |
aoa.push([dateCell, ...temperatures]) | |
} | |
const sheet = xlsx.utils.aoa_to_sheet(aoa) | |
sheet['!cols'] = [{wch: 16}, {wch: 8}] | |
xlsx.utils.book_append_sheet(book, sheet, formatSheetName(key)) | |
} | |
xlsx.writeFile(book, filename) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment