Skip to content

Instantly share code, notes, and snippets.

@ecstasy2
Created September 1, 2016 02:19
Show Gist options
  • Save ecstasy2/898225c8cb07ec46da9260fee43f9629 to your computer and use it in GitHub Desktop.
Save ecstasy2/898225c8cb07ec46da9260fee43f9629 to your computer and use it in GitHub Desktop.
Notifiers
var Notifier = require('@ecstasy2/notifier')
//
var DeviceInactive = Notifier.create({
trigger: {
type: 'cron',
scope: 'device',
recurrence: '* */2 * * *',
},
key: function (payload) {
return `devices:${payload.type}:${payload.deviceId}`;
}
backoff: {
type: 'exponential',
delay: '1 day'
},
query: `
query {
device($deviceId) {
userId,
deviceType
garden {
id
name
},
lastReading
}
}
`,
hooks: {
// The `pre` hook is invoked at the begining of the notification and can be used
// to short-circuit the chain and stop any further action by returning `false`.
// NOTE: This needs to return `false` not a `falsy` response. So returning `null` or `undefined`
// won't short-circuit the chain
//
// NOTE: Bellow we are stoping everything if the device has a recent reading
pre: function (ctx) {
var lastReading = ctx.data.device.lastReading
return lastReading && lastReading.timestamp < Math.round(new Date() / 1000) - 60 * 60 * 1000
}
},
// The `predicate` method is invoked after the `pre` hook and before the `post` hook and
// should return `true` in order to trigger a notification
predicate: function (ctx) {
// We have sent this notification in the past 24 hours do not send it again
if (ctx.sentRecently('5 day')) {
return false
}
return true
},
build: function (ctx) {
return {
title: `Valve|Sensor inactive`,
message: `The sensor or valve in you garden "${ctx.data.device.garden.name}" seems to be inactive.`,
data: {
gardenId: ctx.data.device.garden.id,
deviceId: ctx.data.device.id
}
channels: [
`users_${ctx.data.userId}`
]
}
},
});
var Notifier = require('@ecstasy2/notifier')
//
var lowBattery = Notifier.create({
trigger: {
type: 'event',
event: 'waterings:updated',
},
query: `
query {
watering($uuid) {
state,
endTimePlanned
}
}
`,
hooks: {
// The `pre` hook is invoked at the begining of the notification and can be used
// to short-circuit the chain and stop any further action by returning `false`.
// NOTE: This needs to return `false` not a `falsy` response. So returning `null` or `undefined`
// won't short-circuit the chain
//
// NOTE: Bellow we are stoping everything if we don't have reading for the current device
pre: function (ctx) {
}
}
// The `predicate` method is invoked after the `pre` hook and before the `post` hook and
// should return `true` in order to trigger a notification
predicate: function (ctx) {
if (lastReading.data.watering.state !== 'failed') {
return false
}
if (isMoreThanIntervalTimeAgo('1h', lastReading.data.watering.endTimePlanned) ) {
return false
}
},
build: function (ctx) {
return {
title: `Watering failed`,
message: `Your watering planned for ${ctx.data.watering.startTimePlanned} did not complete successfuly`,
silent: true,
channels: [
`users_${ctx.data.watering.valve.userId}`
]
}
},
});
var Notifier = require('@ecstasy2/notifier')
//
var lowBattery = Notifier.create({
trigger: {
type: 'cron',
scope: 'device',
recurrence: '* */2 * * *'
},
query: `
query {
device($deviceId) {
userId,
macAddress,
deviceType
garden {
sensor,
valve,
name
},
lastReading
}
}
`,
hooks: {
// The post hook is invoked at the end of the notification chain,
// it can be used to cache data pertaining to this notification `subject`.
// To store any data to the cache you can use the `ctx.store.set()` function
//
// You can also use the post hook to cancel a notification even if the predicate
// has returned true.
//
// If the post hook returns a promise, the promise will be resolved and the resolved
// value if a `false` will be treated the same way as above.
//
// NOTE: In this particular case we are storing the last know baterry level
// so that we can use it it future runs to determine if we need to send a notification
//
post: function (ctx) {
if (lastReading.data.lastReading) {
ctx.store.set('lastBatteryPercent', ctx.data.lastReading.batteryPercent)
}
},
// The `pre` hook is invoked at the begining of the notification and can be used
// to short-circuit the chain and stop any further action by returning `false`.
// NOTE: This needs to return `false` not a `falsy` response. So returning `null` or `undefined`
// won't short-circuit the chain
//
// NOTE: Bellow we are stoping everything if we don't have reading for the current device
pre: function (ctx) {
return !lastReading.data.lastReading
}
},
// The `predicate` method is invoked after the `pre` hook and before the `post` hook and
// should return `true` in order to trigger a notification
predicate: function (ctx) {
// We have sent this notification in the past 24 hours do not send it again
if (ctx.sentRecently('1 day')) {
return false
}
var lastReading = ctx.data.device.lastReading
if (lastReading.batteryPercent >= 20) {
return false
}
return lastReading && lastReading.timestamp > Math.round(new Date() / 1000) - 60 * 60 * 1000
},
build: function (ctx) {
return {
title: `Low battery`,
message: `The battery of your garden "${ctx.data.device.garden.name}" is running low.`,
silent: true,
channels: [
`users_${ctx.data.userId}`
]
}
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment