Created
January 7, 2023 20:05
-
-
Save billerby/605dc8f5955fafb7f640900d0614b6df to your computer and use it in GitHub Desktop.
Conver to ToU pattern
This file contains 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
// Get day of the week (7 = Sunday, 1 = Monday, etc.) | |
function getDayAsNumber(day) { | |
return day === 0 ? 7 : day; | |
} | |
const values = msg.payload.schedule | |
.filter((e) => e.activity != 0) | |
.reduce((total, e) => { | |
const toTimeString = (date) => { | |
const HH = date.getHours().toString().padStart(2, '0') | |
const mm = date.getMinutes().toString().padStart(2, '0') | |
return `${HH}:${mm}` | |
} | |
const touPattern = (start, end, charge) => { | |
let pattern = toTimeString(start) | |
pattern += '-' | |
pattern += toTimeString(end) | |
pattern += '/' | |
pattern += getDayAsNumber(start.getDay()); | |
pattern += '/' | |
pattern += charge | |
return pattern | |
} | |
const startDate = new Date(e.start) | |
const endDate = new Date(startDate.getTime() + (e.duration - 1) * 60000) | |
const charge = e.activity == 1 ? '+' : '-' | |
if (startDate.getDay() == endDate.getDay()) { | |
total.push(touPattern(startDate, endDate, charge)) | |
} else { | |
const endDateDay1 = new Date(startDate) | |
endDateDay1.setHours(23) | |
endDateDay1.setMinutes(59) | |
total.push(touPattern(startDate, endDateDay1, charge)) | |
const startDateDay2 = new Date(endDate) | |
startDateDay2.setHours(0) | |
startDateDay2.setMinutes(0) | |
total.push(touPattern(startDateDay2, endDate, charge)) | |
} | |
return total | |
}, []) | |
msg.payload = values.join('\n') | |
return msg; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment