forked from thex-at/pimatic-holidays
-
Notifications
You must be signed in to change notification settings - Fork 0
/
holidays.coffee
121 lines (97 loc) · 3.96 KB
/
holidays.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module.exports = (env) ->
# Require the bluebird promise library
Promise = env.require 'bluebird'
# Require the [cassert library](https://github.com/rhoot/cassert).
assert = env.require 'cassert'
# Include you own depencies with nodes global require function:
#
# someThing = require 'someThing'
#
dateholidays = require 'date-holidays'
# ###MyPlugin class
# Create a class that extends the Plugin class and implements the following functions:
class holidays extends env.plugins.Plugin
# ####init()
# The `init` function is called by the framework to ask your plugin to initialise.
#
# #####params:
# * `app` is the [express] instance the framework is using.
# * `framework` the framework itself
# * `config` the properties the user specified as config for your plugin in the `plugins`
# section of the config.json file
#
#
init: (app, @framework, @config) =>
env.logger.info("HolidaySensor initialized...")
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("HolidaySensor", {
configDef: deviceConfigDef.HolidaySensor,
createCallback: (config) => new HolidaySensor(config)
})
class HolidaySensor extends env.devices.Sensor
# ####constructor()
# Your constructor function must assign a name and id to the device.
constructor: (@config) ->
@name = @config.name
@id = @config.id
@country = @config.country ? @plugin.config.country
@state = @config.state ? @plugin.config.state
@debug = @config.debug ? @plugin.config.debug
@_holidayname = "none"
@_holidaytype = "none"
@_presence = false
# ...
# update the presence value every 5 seconds
setInterval( ( =>
@_setPresence()
), 5000)
super()
attributes:
presence:
description: "holiday yes/no"
type: "boolean"
labels: ['Holiday', 'noHoliday']
holidayname:
description: "holidays name"
type: "string"
holidaytype:
description: "holidays type"
type: "string"
_setPresence: (value) ->
if @debug is true then env.logger.info("debug", @debug)
if @debug is true then env.logger.info("country", @country)
if @debug is true then env.logger.info("state", @state)
dh = new dateholidays(@country, @state)
# check if date is a holiday while respecting timezones
today = new Date()
todaystring = today.getFullYear() + "-" + (today.getMonth()+1) + "-" + today.getDate()
# fakedate for testing
#todaystring = '2017-12-24'
if @debug is true then env.logger.info("actualdate", todaystring )
istodayholiday = dh.isHoliday( new Date(todaystring) )
@_holidayname = 'none'
if istodayholiday then @_holidayname = istodayholiday["name"]
@emit 'holidayname', @_holidayname
if @debug is true then env.logger.info("holidayname variable:", @_holidayname)
@_holidaytype = "none"
if istodayholiday then @_holidaytype = istodayholiday["type"]
@emit 'holidaytype', @_holidaytype
if @debug is true then env.logger.info("holidaytype variable:", @_holidaytype)
if @debug is true then env.logger.info("istodayholiday:", istodayholiday)
if istodayholiday == false then value = false else value = true
if @debug is true then env.logger.info("presencevalue:", value)
if @_presence is value then return
@_presence = value
@emit 'presence', value
getPresence: -> Promise.resolve(@_presence)
getHolidayname: -> Promise.resolve(@_holidayname)
getHolidaytype: -> Promise.resolve(@_holidaytype)
template: "presence"
destroy: () ->
clearInterval @_setPresence if @_setPresence?
super()
# ###Finally
# Create a instance of my plugin
holidays = new holidays
# and return it to the framework.
return holidays