Skip to content

Instantly share code, notes, and snippets.

@dimified
Created April 16, 2016 15:43
Show Gist options
  • Save dimified/c549a82bcc99e76f48c55ab533547b9a to your computer and use it in GitHub Desktop.
Save dimified/c549a82bcc99e76f48c55ab533547b9a to your computer and use it in GitHub Desktop.
Service: BurningHours (Calculating burning hours related to ecoCalcLight project)
'use strict'
define [
'jquery'
'angular'
], ($, Angular) ->
###*
# Registry of burning hours service
# @namespace App.services.burningHoursSrvc
# @memberof App.services
###
Angular.module 'App.services.burningHoursSrvc', []
.service 'burningHours', ['message', '$translate', (message, $translate) ->
###*
# Weeks in one year with leapyear
# @memberof App.services.burningHoursSrvc
# @property {number}
###
w = 52 + (1/7)
###*
# Calculate minutes from time string
# @function
# @memberof App.services.burningHoursSrvc
# @param {string} time Time string
# @return {number}
###
mm = (time) ->
if !/^([0-9]|0[0-9]|1[0-9]|2[0-4]):[0-5][0-9]$/.test(time)
$translate('message.errors.timePatternMatching', { time: time }).then (translation) ->
message.push('alert', translation)
throw new Error translation
return
time = time.split(':')
hours = Number time[0]
minutes = Number time[1]
inMinutes(hours) + minutes
###*
# Converts minutes to hours
# @function
# @memberof App.services.burningHoursSrvc
# @param {string} minutes Minutes
# @return {number}
###
h = (minutes) ->
Number(minutes / 60)
###*
# Converts hours to minutes
# @function
# @memberof App.services.burningHoursSrvc
# @param {string} hours Hours
# @return {number}
###
inMinutes = (hours) ->
Number(hours * 60)
###*
# Builds time string from minutes
# @function
# @memberof App.services.burningHoursSrvc
# @param {string} minutes Minutes
# @return {string}
###
buildTimeString = (minutes) ->
tmp = minutes / 60
hours = parseInt(tmp)
minutes = Math.round((tmp - hours) * 60, 2)
(if hours < 10 then '0' + hours else hours) + ':' + (if minutes < 10 then '0' + minutes else minutes)
###*
# Calculate total burning hours by working days and start and end time over one year.
# @function
# @memberof App.services.burningHoursSrvc
# @param {number} st Start time of working days given in minutes
# @param {number} et End time of working days given in minutes
# @return {number}
###
b = (wd, st, et) ->
if wd < 0 or 7 < wd
$translate('message.errors.rangeWorkingDays',).then (translation) ->
message.push('alert', translation)
throw new Error translation
wd * w * d(st, et)
###*
# Calculate time span in a day
# @function
# @memberof App.services.burningHoursSrvc
# @param {number} st Start time of working days given in minutes
# @param {number} et End time of working days given in minutes
# @return {number}
###
d = (st, et) ->
if st is et
return inMinutes(24)
if st < et
return et - st
if st > et
return et - st + inMinutes(24)
###*
# Calculate maximum hours of one year based on working days and leap year.
# @function
# @memberof App.services.burningHoursSrvc
# @param {number} wd Working days
# @return {number}
###
max = (wd) ->
wd * w * 24
###*
# Calculate duration of burning hours through working days
# @function
# @memberof App.services.burningHoursSrvc
# @param {number} b Total burning hours
# @param {number} wd Working days
# @return {number}
###
duration = (b, wd) ->
b / (wd * w)
###*
# Calculate end time
# @function
# @memberof App.services.burningHoursSrvc
# @param {number} st Start time
# @param {number} dur Duration
# @return {number}
###
end = (st, dur) ->
span = inMinutes(24) - st
if span < dur
return st + dur - inMinutes(24)
if span > dur
return st + dur
if span is dur
return 0
{
###*
# Calculate minutes from time string
# @property {function}
# @memberof App.services.burningHoursSrvc
###
calculateMinutes: mm
###*
# Converts minutes to hours
# @property {function}
# @memberof App.services.burningHoursSrvc
###
convertHours: h
###*
# Converts hours to minutes
# @property {function}
# @memberof App.services.burningHoursSrvc
###
convertMinutes: inMinutes
###*
# Builds time string from minutes
# @property {function}
# @memberof App.services.burningHoursSrvc
###
getTimeString: buildTimeString
###*
# Calculate burning hours per year based on working days and operating time per day
# @function
# @memberof App.services.burningHoursSrvc
# @param {string|number} obj.workingDays 1 - 7 Working days over one year between
# @param {string} obj.startTime 00:00 - 24:00 Start time of operating time per day
# @param {string} obj.endTime 00:00 - 24:00 End time of operating time per day
# @return {object}
###
calculateBurningHours: (obj) ->
obj.workingDays = parseInt(obj.workingDays)
obj.burningHours = h(b(obj.workingDays, mm(obj.startTime), mm(obj.endTime)))
# Set 24 hours when startTime is endTime
if obj.startTime is obj.endTime
obj.startTime = '00:00'
obj.endTime = '24:00'
obj
###*
# Calculate operating time based on burning hours and specific ecoCALC logic
# @function
# @memberof App.services.burningHoursSrvc
# @param {string|number} obj.workingDays 1 - 7 Working days over one year between
# @param {string} obj.startTime 00:00 - 24:00 Start time of operating time per day
# @param {string} obj.endTime 00:00 - 24:00 End time of operating time per day
# @return {object}
###
calculateOperatingTime: (obj) ->
if obj.burningHours >= 8757
return {
startTime: '00:00'
endTime: '24:00'
workingDays: 7
burningHours: 8760
}
if obj.burningHours >= max(obj.workingDays)
obj.workingDays = 7
# Get the endtime and recalculate burning hours based on rounded values
endTime = end(mm(obj.startTime), duration(inMinutes(obj.burningHours), obj.workingDays))
obj.endTime = buildTimeString(endTime)
this.calculateBurningHours(obj)
obj
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment