pimatic
Version:
A home automation server and framework for the Raspberry PI running on node.js
192 lines (168 loc) • 6.54 kB
text/coffeescript
__ = require("i18n-pimatic").__
Promise = require 'bluebird'
assert = require 'cassert'
_ = require('lodash')
S = require('string')
module.exports = (env) ->
class GroupManager
constructor: (, ) -> #nop
addGroup: (id, group) ->
if _.findIndex(, {id: id}) isnt -1
throw new Error('A group with this ID already exists')
unless group.name?
throw new Error('No name given')
.push( group = {
id: id
name: group.name
devices: []
rules: []
variables: []
})
.saveConfig()
._emitGroupAdded(group)
return group
updateGroup: (id, patch) ->
index = _.findIndex(, {id: id})
if index is -1
throw new Error('Group not found')
group = [index]
if patch.name?
group.name = patch.name
if patch.devicesOrder?
group.devices = _.sortBy(group.devices, (deviceId) =>
index = patch.devicesOrder.indexOf deviceId
return if index is -1 then 99999 else index # push it to the end if not found
)
if patch.rulesOrder?
group.rules = _.sortBy(group.rules, (ruleId) =>
index = patch.rulesOrder.indexOf ruleId
return if index is -1 then 99999 else index # push it to the end if not found
)
if patch.variablesOrder
group.variables = _.sortBy(group.variables, (variableName) =>
index = patch.variablesOrder.indexOf variableName
return if index is -1 then 99999 else index # push it to the end if not found
)
.saveConfig()
._emitGroupChanged(group)
return group
getGroupById: (id) -> _.find(, {id: id})
addDeviceToGroup: (groupId, deviceId, position) ->
assert(typeof deviceId is "string")
assert(typeof groupId is "string")
assert(if position? then typeof position is "number" else true)
group =
unless group?
throw new Error('Could not find the group')
oldGroup =
if oldGroup?
#remove rule from all other groups
_.remove(oldGroup.devices, (id) => id is deviceId)
._emitGroupChanged(oldGroup)
unless position? or position >= group.devices.length
group.devices.push(deviceId)
else
group.devices.splice(position, 0, deviceId)
.saveConfig()
._emitGroupChanged(group)
return group
getGroupOfRule: (ruleId) ->
for g in
index = _.indexOf(g.rules, ruleId)
if index isnt -1 then return g
return null
addRuleToGroup: (groupId, ruleId, position) ->
assert(typeof ruleId is "string")
assert(typeof groupId is "string")
assert(if position? then typeof position is "number" else true)
group =
unless group?
throw new Error('Could not find the group')
oldGroup =
if oldGroup?
#remove rule from all other groups
_.remove(oldGroup.rules, (id) => id is ruleId)
._emitGroupChanged(oldGroup)
unless position? or position >= group.rules.length
group.rules.push(ruleId)
else
group.rules.splice(position, 0, ruleId)
.saveConfig()
._emitGroupChanged(group)
return group
getGroupOfVariable: (variableName) ->
for g in
index = _.indexOf(g.variables, variableName)
if index isnt -1 then return g
return null
removeDeviceFromGroup: (groupId, deviceId) ->
group =
unless group?
throw new Error('Device is in no group')
if group.id isnt groupId
throw new Error("Device is not in group #{groupId}")
_.remove(group.devices, (id) => id is deviceId)
.saveConfig()
._emitGroupChanged(group)
return group
removeRuleFromGroup: (groupId, ruleId) ->
group =
unless group?
throw new Error('Rule is in no group')
if group.id isnt groupId
throw new Error("Rule is not in group #{groupId}")
_.remove(group.rules, (id) => id is ruleId)
.saveConfig()
._emitGroupChanged(group)
return group
removeVariableFromGroup: (groupId, variableName) ->
group =
unless group?
throw new Error('Variable is in no group')
if group.id isnt groupId
throw new Error("Variable is not in group #{groupId}")
_.remove(group.variables, (name) => name is variableName)
.saveConfig()
._emitGroupChanged(group)
return group
addVariableToGroup: (groupId, variableName, position) ->
assert(typeof variableName is "string")
assert(typeof groupId is "string")
assert(if position? then typeof position is "number" else true)
group =
unless group?
throw new Error('Could not find the group')
oldGroup =
if oldGroup?
#remove rule from all other groups
_.remove(oldGroup.variables, (name) => name is variableName)
._emitGroupChanged(oldGroup)
unless position? or position >= group.variables.length
group.variables.push(variableName)
else
group.variables.splice(position, 0, variableName)
.saveConfig()
._emitGroupChanged(group)
return group
removeGroup: (id, page) ->
removedGroup = _.remove(, {id: id})
.saveConfig() if removedGroup.length > 0
._emitGroupRemoved(removedGroup[0])
return removedGroup
getGroupOfDevice: (deviceId) ->
for g in
index = _.indexOf(g.devices, deviceId)
if index isnt -1 then return g
return null
getGroups: () ->
return
updateGroupOrder: (groupOrder) ->
assert groupOrder? and Array.isArray groupOrder
.config.groups = = _.sortBy(, (group) =>
index = groupOrder.indexOf group.id
return if index is -1 then 99999 else index # push it to the end if not found
)
.saveConfig()
._emitGroupOrderChanged(groupOrder)
return groupOrder
return exports = { GroupManager }