Documentation ¶
Index ¶
- Variables
- func AddLogEvent(name string, description string, level int) error
- func AddNotification(receiverUsername string, name string, description string, priority uint8) error
- func AddUser(user FullUser) error
- func AddUserCameraPermission(username string, cameraId string) error
- func AddUserPermission(username string, permission PermissionType) error
- func AddUserSwitchPermission(username string, switchId string) (bool, error)
- func CheckDatabase() error
- func CreateCamera(data Camera) error
- func CreateHardwareNode(node HardwareNode) error
- func CreateNewAutomation(automation Automation) (uint, error)
- func CreateNewHomescript(homescript Homescript) error
- func CreateNewReminder(name string, description string, dueDate time.Time, owner string, ...) (uint, error)
- func CreateNewSchedule(schedule Schedule) (uint, error)
- func CreateRoom(room RoomData) error
- func CreateSwitch(id string, name string, roomId string, watts uint16) error
- func DeleteAllAutomationsFromUser(username string) error
- func DeleteAllHomescriptsOfUser(username string) error
- func DeleteAllNotificationsFromUser(username string) error
- func DeleteAllRemindersFromUser(username string) error
- func DeleteAllSchedulesFromUser(username string) error
- func DeleteAutomationById(id uint) error
- func DeleteCamera(id string) error
- func DeleteHardwareNode(url string) error
- func DeleteHomescriptById(homescriptId string) error
- func DeleteNotificationFromUserById(notificationId uint, username string) error
- func DeleteRoom(id string) error
- func DeleteRoomCameras(roomId string) error
- func DeleteRoomQuery(id string) error
- func DeleteRoomSwitches(roomId string) error
- func DeleteScheduleById(id uint) error
- func DeleteSwitch(switchId string) error
- func DeleteTables() error
- func DeleteUser(username string) error
- func DeleteUserReminderById(owner string, id uint) error
- func DoesHomescriptExist(homescriptId string) (bool, error)
- func DoesPermissionExist(permission string) bool
- func FlushAllLogs() error
- func FlushOldLogs() error
- func GetAvatarPathByUsername(username string) (string, error)
- func GetUserNotificationCount(username string) (uint16, error)
- func GetUserPasswordHash(username string) (string, error)
- func GetUserPermissions(username string) ([]string, error)
- func GetUserSwitchPermissions(username string) ([]string, error)
- func Init(databaseConfig DatabaseConfig, adminPassword string) error
- func InitLogger(logger *logrus.Logger)
- func InsertUser(user FullUser) error
- func ModifyAutomation(id uint, newItem AutomationWithoutIdAndUsername) error
- func ModifyCamera(id string, newName string, newUrl string) error
- func ModifyHardwareNode(url string, node HardwareNode) error
- func ModifyHomescriptById(id string, homescript HomescriptFrontend) error
- func ModifyReminder(id uint, name string, description string, dueDate time.Time, ...) error
- func ModifyRoomData(id string, newName string, newDescription string) error
- func ModifySchedule(id uint, newItem ScheduleWithoudIdAndUsername) error
- func ModifySwitch(id string, name string, watts uint16) error
- func RemoveAllCameraPermissionsOfUser(username string) error
- func RemoveAllPermissionsOfUser(username string) error
- func RemoveAllSwitchPermissionsOfUser(username string) error
- func RemoveCameraFromPermissions(cameraId string) error
- func RemoveSwitchFromPermissions(switchId string) error
- func RemoveUserCameraPermission(username string, cameraId string) error
- func RemoveUserPermission(username string, permission PermissionType) error
- func RemoveUserSwitchPermission(username string, switchId string) (bool, error)
- func SetAutomationSystemActivation(enabled bool) error
- func SetNodeOnline(nodeUrl string, online bool) error
- func SetPowerState(switchId string, isPoweredOn bool) (bool, error)
- func SetReminderUserWasNotified(id uint, wasNotified bool, wasNotifiedAt time.Time) error
- func SetServerConfiguration(config ServerConfig) error
- func SetUserAvatarPath(username string, avatarPath string) error
- func SetUserDarkThemeEnabled(username string, useDarkTheme bool) error
- func SetUserSchedulerEnabled(username string, enabled bool) error
- func Shutdown() error
- func UpdateLocation(lat float32, lon float32) error
- func UpdateUserMetadata(username string, forename string, surname string, primaryColorDark string, ...) error
- func UserHasCameraPermission(username string, cameraId string) (bool, error)
- func UserHasCameraPermissionQuery(username string, cameraId string) (bool, error)
- func UserHasPermission(username string, permission PermissionType) (bool, error)
- func UserHasSwitchPermission(username string, switchId string) (bool, error)
- func UserHasSwitchPermissionQuery(username string, switchId string) (bool, error)
- type Automation
- type AutomationWithoutIdAndUsername
- type Camera
- type DBStatus
- type DatabaseConfig
- type FullUser
- type HardwareNode
- type Homescript
- type HomescriptFrontend
- type LogEvent
- type Notification
- type NotificationPriority
- type Permission
- type PermissionType
- type PowerState
- type Reminder
- type Room
- type RoomData
- type Schedule
- type ScheduleWithoudIdAndUsername
- type ServerConfig
- type Switch
- type TimingMode
- type User
- type UserDetails
Constants ¶
This section is empty.
Variables ¶
var ( Permissions = []Permission{ { Permission: PermissionPower, Name: "Power", Description: "Interact with switches", }, { Permission: PermissionLogs, Name: "Manage Logging", Description: "Use and manage the internal logging system", }, { Permission: PermissionDebug, Name: "Debug Features", Description: "Obtain debug information about the system", }, { Permission: PermissionManageUsers, Name: "Manage Users", Description: "Create / remove and manage users and manage their permissions", }, { Permission: PermissionHomescript, Name: "Use Homescript", Description: "List, add, delete, run, and modify Homescripts", }, { Permission: PermissionAutomation, Name: "Use Automations", Description: "List, add, delete, and modify automations", }, { Permission: PermissionScheduler, Name: "Use the Scheduler", Description: "List, add, delete, and modify schedules", }, { Permission: PermissionReminder, Name: "Use Reminders", Description: "List, add, delete, and modify reminders", }, { Permission: PermissionModifyRooms, Name: "Manage Rooms", Description: "View, add, modify and delete rooms and room like switches and cameras. If enabled, the user also has access to every switch of the system.", }, { Permission: PermissionViewCameras, Name: "View Cameras", Description: "View camera video feed. However, which camera can be viewed still depends on the camera permissions.", }, { Permission: PermissionModifyServerConfig, Name: "Manage Server Config", Description: "Change global server configuration values", }, { Permission: PermissionWildCard, Name: "Permission Wildcard *", Description: "Allows all permissions", }, } )
Functions ¶
func AddLogEvent ¶
Add a logged internal event based on `name, description, and level`
func AddNotification ¶
func AddNotification(receiverUsername string, name string, description string, priority uint8) error
Adds a new notification to a user's `inbox`, can return an error if the database fails
func AddUser ¶
Helper function to create a User which is given a set of basic permissions Will return an error if the database fails TODO: Remove business logic from here, move to core/user
func AddUserCameraPermission ¶
Adds a given cameraId to an arbitrary user The existence of the camera and the user should be validated beforehand
func AddUserPermission ¶
func AddUserPermission(username string, permission PermissionType) error
Adds a permission to a user, if database fails, then an error is returned Does not check for username so additional checks should be completed beforehand
func AddUserSwitchPermission ¶
Adds a given switchId to a given user The existence of the switch should be validated beforehand If this permission already resides inside the table, it is ignored and modified=false, error=nil is returned TODO: Remove useless check if user already has permission
func CheckDatabase ¶
func CheckDatabase() error
Executes a ping to the database in order to check if it is online
func CreateCamera ¶
Creates a new camera Checks, for example if the camera already exists should be completed beforehand
func CreateHardwareNode ¶
func CreateHardwareNode(node HardwareNode) error
Adds a new hardware node to the database, if the node already exists (same url), its name will be updated
func CreateNewAutomation ¶
func CreateNewAutomation(automation Automation) (uint, error)
Creates a new automation item, does not check the validity of the user or the homescript Id
func CreateNewHomescript ¶
func CreateNewHomescript(homescript Homescript) error
Creates a new homescript entry
func CreateNewReminder ¶
func CreateNewReminder(name string, description string, dueDate time.Time, owner string, priority NotificationPriority) (uint, error)
Creates a new reminder in the database
func CreateNewSchedule ¶
Creates a new schedule which represents a job of the scheduler
func CreateRoom ¶
Creates a new room given an arbitrary, non-existing id
func CreateSwitch ¶
Creates a new switch Will return an error if the database fails
func DeleteAllAutomationsFromUser ¶
Deletes all automations from a given user
func DeleteAllHomescriptsOfUser ¶
Deletes all Homescripts of a given user
func DeleteAllNotificationsFromUser ¶
If the user requests to empty their notification area, all hist notifications will be deleted
func DeleteAllRemindersFromUser ¶
Deletes all reminders of a given user
func DeleteAllSchedulesFromUser ¶
Deletes all schedules from a given user
func DeleteAutomationById ¶
Deletes an automation item given its Id Does not validate the validity of the provided Id
func DeleteCamera ¶
Deletes a camera, removes dependencies, such as camera-permission first Used in deleting all room cameras and deleting one camera
func DeleteHomescriptById ¶
Deletes a homescript by its Id, does not check if the user has access to the homescript
func DeleteNotificationFromUserById ¶
Deletes a given notification, can return an error
func DeleteRoom ¶
func DeleteRoomCameras ¶
Deletes all cameras in an arbitrary room Uses `DeleteCamera` in order to remove dependencies beforehand
func DeleteRoomQuery ¶
Deletes a room and all entities that depend on the room
func DeleteRoomSwitches ¶
Deletes all switches from an arbitrary room Before deleting the switch, dependencies like switch-permissions are deleted
func DeleteScheduleById ¶
Deletes a schedule item given its Id Does not validate the validity of the provided Id
func DeleteSwitch ¶
Delete a given switch after all data which depends on this switch has been deleted
func DeleteTables ¶
func DeleteTables() error
func DeleteUser ¶
Deletes a User based on a given Username, can return an error if the database fails The function does not validate the existence of this username itself, so additional checks should be done beforehand The avatar is removed in `core/user/user`
func DeleteUserReminderById ¶
Delete a single reminder, for example if its task is finished
func DoesHomescriptExist ¶
TODO: remove and intigrate into get user homescript Checks if a Homescript with an id exists in the database
func DoesPermissionExist ¶
Checks the validity of a given permission string
func FlushAllLogs ¶
func FlushAllLogs() error
func FlushOldLogs ¶
func FlushOldLogs() error
Deletes log events older than 30 days in order to save storage space This function will later be used by a scheduler for daily jobs
func GetAvatarPathByUsername ¶
Returns the path of the avatar image of a given user, does not check if the user exists, additional checks needed beforehand
func GetUserNotificationCount ¶
Used for displaying the notification count for a given user Used in the frontend before the actual permissions are fetched
func GetUserPasswordHash ¶
Returns the password of a given user
func GetUserPermissions ¶
Returns a list of permissions assigned to a given user, if it exists
func GetUserSwitchPermissions ¶
Returns a list of strings which resemble switch permissions
func Init ¶
func Init(databaseConfig DatabaseConfig, adminPassword string) error
func InitLogger ¶
func InsertUser ¶
Creates a new user based on a the supplied `User` struct Won't panic if user already exists, but will change password
func ModifyAutomation ¶
func ModifyAutomation(id uint, newItem AutomationWithoutIdAndUsername) error
Modifies the metadata of a given automation item Does not validate the provided metadata
func ModifyHardwareNode ¶
func ModifyHardwareNode(url string, node HardwareNode) error
Changes the metadata of a given node Does not affect the online boolean For changing the online status, use `SetNodeOnline`
func ModifyHomescriptById ¶
func ModifyHomescriptById(id string, homescript HomescriptFrontend) error
Modifies the metadata of a given homescript Does not check the validity of the homescript's id
func ModifyReminder ¶
func ModifyReminder(id uint, name string, description string, dueDate time.Time, priority NotificationPriority) error
Modifies a given reminder to possess the new metadata
func ModifyRoomData ¶
Updates the room's name and description
func ModifySchedule ¶
func ModifySchedule(id uint, newItem ScheduleWithoudIdAndUsername) error
Modifies the metadata of a given schedule Does not validate the provided metadata
func ModifySwitch ¶
Modifies the metadata of a given switch
func RemoveAllCameraPermissionsOfUser ¶
Removes all camera permissions of a given user, used when deleting a user
func RemoveAllPermissionsOfUser ¶
Removes all permissions of a given user, used when deleting a user in order to prevent foreign key failure Does not validate username, additional checks required, returns an error if the database fails
func RemoveAllSwitchPermissionsOfUser ¶
Removes all switch permissions of a given user, used when deleting a user Does not validate the existence of said user
func RemoveCameraFromPermissions ¶
Deletes all occurences of a given camera, used if a camera is deleted
func RemoveSwitchFromPermissions ¶
Deletes all occurrences of a given switch, used if a certain switch is deleted
func RemoveUserCameraPermission ¶
Removes a camera permission of an arbitrary user
func RemoveUserPermission ¶
func RemoveUserPermission(username string, permission PermissionType) error
Attempts to remove a provided permission from a provided user Fails if permission does not exist or if the database fails Warns and returns `false` for the `modified` boolean the user does not have the permission
func RemoveUserSwitchPermission ¶
TODO: Remove useless check if user already has permission TODO: check naming consistency of `ADD / CREATE` and `DELETE / REMOVE` Removes a switch permission from a user, but does not delete if from the switch permission list
func SetAutomationSystemActivation ¶
Change the state of the automation system
func SetNodeOnline ¶
Updates the online / offline state of a given node (url)
func SetPowerState ¶
Used when marking a power state of a switch Does not check the validity of the switch Id The returned boolean indicates if the power state had changed
func SetReminderUserWasNotified ¶
Modifies the reminders status its owner has been informed about urgency
func SetServerConfiguration ¶
func SetServerConfiguration(config ServerConfig) error
Updates the servers configuration
func SetUserAvatarPath ¶
Sets the path of the avatar for a given user, does not check if the user exists, additional checks needed beforehand
func SetUserDarkThemeEnabled ¶
Set whether the user uses the dark theme or the light theme
func SetUserSchedulerEnabled ¶
Set whether the scheduler is enabled for the current user
func UpdateLocation ¶
Changes the location of the server
func UpdateUserMetadata ¶
func UpdateUserMetadata(username string, forename string, surname string, primaryColorDark string, primaryColorLight string) error
Sets the users primary colors
func UserHasCameraPermission ¶
Returns a boolean indicating whether a user has a camera permission
func UserHasCameraPermissionQuery ¶
Used in userHasCameraPermission
func UserHasPermission ¶
func UserHasPermission(username string, permission PermissionType) (bool, error)
Checks if a provided user is in possession of a provided permission, can return an error, if the database fails
func UserHasSwitchPermission ¶
Returns a boolean if a user has a switch permission
Types ¶
type Automation ¶
type Automation struct { Id uint `json:"id"` Name string `json:"name"` Description string `json:"description"` CronExpression string `json:"cronExpression"` HomescriptId string `json:"homescriptId"` Owner string `json:"owner"` Enabled bool `json:"enabled"` TimingMode TimingMode `json:"timingMode"` }
func GetAutomationById ¶
func GetAutomationById(id uint) (Automation, bool, error)
Returns a Automation struct which matches the given Id If the id does not match a struct, a `false“ is returned
func GetAutomations ¶
func GetAutomations() ([]Automation, error)
Returns a list with automations of all users Used for activating persistent automations when the server starts
func GetUserAutomations ¶
func GetUserAutomations(username string) ([]Automation, error)
Returns a list containing automations of a given user Does not check the validity of the user
type Camera ¶
type Camera struct { Id string `json:"id"` Name string `json:"name"` Url string `json:"url"` RoomId string `json:"roomId"` }
func GetCameraById ¶
Returns the metadata of a given camera, wheter it could be found and a potential error
func ListCameras ¶
Returns a list containing all cameras Used when deleting all cameras in a room
func ListUserCameras ¶
Combines the logic from `ListUserCamerasQuery()` With an exception: if the user has the permission to modify rooms, all cameras are granted
func ListUserCamerasQuery ¶
Like `ListCameras()` but takes a user string as a filter Only returns camerass to wich the user has access to Used in `ListUserCameras()`
type DBStatus ¶
type DBStatus struct { OpenConnections int `json:"openConnections"` InUse int `json:""` Idle int `json:""` }
func GetDatabaseStats ¶
func GetDatabaseStats() DBStatus
type DatabaseConfig ¶
type FullUser ¶
type FullUser struct { Username string `json:"username"` Forename string `json:"forename"` Surname string `json:"surname"` PrimaryColorDark string `json:"primaryColorDark"` PrimaryColorLight string `json:"primaryColorLight"` Password string `json:"password"` AvatarPath string `json:"avatarPath"` SchedulerEnabled bool `json:"schedulerEnabled"` DarkTheme bool `json:"darkTheme"` }
Identified by a username, has a password and an avatar path
type HardwareNode ¶
type HardwareNode struct { Name string `json:"name"` Online bool `json:"online"` Enabled bool `json:"enabled"` // Can be used to temporarely deactivate a node in case of maintenance Url string `json:"url"` Token string `json:"token"` }
Hardware node
func GetHardwareNodeByUrl ¶
func GetHardwareNodeByUrl(url string) (HardwareNode, bool, error)
Returns a hardware node given its url
func GetHardwareNodes ¶
func GetHardwareNodes() ([]HardwareNode, error)
Returns a list of hardware nodes
type Homescript ¶
type Homescript struct { Id string `json:"id"` Owner string `json:"owner"` Name string `json:"name"` Description string `json:"description"` QuickActionsEnabled bool `json:"quickActionsEnabled"` SchedulerEnabled bool `json:"schedulerEnabled"` Code string `json:"code"` }
func GetUserHomescriptById ¶
func GetUserHomescriptById(homescriptId string, username string) (Homescript, bool, error)
Returns a Homescript given its id Returns Homescript, has been found, error TODO: replace with query row
func ListHomescriptFiles ¶
func ListHomescriptFiles() ([]Homescript, error)
Lists all Homescript files in the database
func ListHomescriptOfUser ¶
func ListHomescriptOfUser(username string) ([]Homescript, error)
Returns a list of homescripts owned by a given user
type HomescriptFrontend ¶
type LogEvent ¶
type LogEvent struct { Id uint `json:"id"` Name string `json:"name"` Description string `json:"description"` Level int `json:"level"` Date time.Time `json:"date"` }
internal logging-related
type Notification ¶
type Notification struct { Id uint `json:"id"` Priority uint8 `json:"priority"` // Includes 1: info, 2: warning, 3: alert Name string `json:"name"` Description string `json:"description"` Date time.Time `json:"date"` }
User notification
func GetUserNotifications ¶
func GetUserNotifications(username string) ([]Notification, error)
Used when requesting the user's permissions in the frontend Returns a list containing the permissions of a given user
type NotificationPriority ¶
type NotificationPriority uint
const ( Low NotificationPriority = iota Normal Medium High Urgent )
type Permission ¶
type Permission struct { Permission PermissionType `json:"permission"` Name string `json:"name"` Description string `json:"description"` }
This file defines which permissions exists and describes their attributes
type PermissionType ¶
type PermissionType string
const ( PermissionPower PermissionType = "setPower" PermissionViewCameras PermissionType = "viewCameras" PermissionLogs PermissionType = "logs" PermissionManageUsers PermissionType = "manageUsers" PermissionDebug PermissionType = "debug" PermissionHomescript PermissionType = "homescript" PermissionAutomation PermissionType = "automation" PermissionScheduler PermissionType = "scheduler" PermissionReminder PermissionType = "reminder" PermissionModifyServerConfig PermissionType = "modifyServerConfig" PermissionModifyRooms PermissionType = "modifyRooms" // Dangerous PermissionWildCard PermissionType = "*" )
Different types of permissions
type PowerState ¶
Contains the switch id and a matching boolean Used when requesting global power states
func GetPowerStates ¶
func GetPowerStates() ([]PowerState, error)
Returns a list of PowerStates Can return a database error
type Reminder ¶
type Reminder struct { Id uint `json:"id"` Name string `json:"name"` Description string `json:"description"` Priority NotificationPriority `json:"priority"` CreatedDate time.Time `json:"createdDate"` DueDate time.Time `json:"dueDate"` Owner string `json:"owner"` UserWasNotified bool `json:"userWasNotified"` // Saves if the ownere has been notified about the current urgency of the task UserWasNotifiedAt time.Time `json:"userWasNotifiedAt"` }
func GetReminderById ¶
Given its id and owner, the function returns a reminder, if it was found and an error
func GetUserReminders ¶
Returns a slice of reminders which were set up by a given user
type Room ¶
type Room struct { Data RoomData `json:"data"` Switches []Switch `json:"switches"` Cameras []Camera `json:"cameras"` }
func ListAllRoomsWithData ¶
Returns a complete list of rooms, includes its metadata like switches and cameras
func ListPersonalRoomsWithData ¶
Returns a complete list of rooms to which a user has access to, includes its metadata like switches and cameras
type RoomData ¶
type RoomData struct { Id string `json:"id"` Name string `json:"name"` Description string `json:"description"` }
func GetRoomDataById ¶
Returns an arbitrary room given its id, whether it exists an a possible error
type Schedule ¶
type Schedule struct { Id uint `json:"id"` Name string `json:"name"` Owner string `json:"owner"` Hour uint `json:"hour"` Minute uint `json:"minute"` HomescriptCode string `json:"homescriptCode"` // Will be executed if the scheduler runs the job }
func GetScheduleById ¶
Returns a schedule struct which matches the given id If the id does not match a struct, a `false“ is returned
func GetSchedules ¶
Returns a list of schedules of all users, used for activating schedules at the start of the server
func GetUserSchedules ¶
Returns a list containing schedules of a given user
type ServerConfig ¶
type ServerConfig struct { AutomationEnabled bool `json:"automationEnabled"` // Sets the global state of the server's automation system LockDownMode bool `json:"lockDownMode"` // If enabled, the server is unable to change power states and will not allow power actions Latitude float32 `json:"latitude"` // Used for calculating the sunset / sunrise and for openweathermap Longitude float32 `json:"longitude"` }
func GetServerConfiguration ¶
func GetServerConfiguration() (ServerConfig, bool, error)
Retrieves the servers configuration
type Switch ¶
type Switch struct { Id string `json:"id"` Name string `json:"name"` RoomId string `json:"roomId"` PowerOn bool `json:"powerOn"` Watts uint16 `json:"watts"` }
Identified by a Switch Id, has a name and belongs to a room
func GetSwitchById ¶
Returns an arbitrary switch given its id
func ListSwitches ¶
Returns a list of all available switches with their attributes
func ListUserSwitches ¶
func ListUserSwitchesQuery ¶
Like `ListSwitches()` but takes a user string as a filter Only returns switches which are contained in the switch-permission table with the given user
type TimingMode ¶
type TimingMode string
const ( TimingNormal TimingMode = "normal" // Will not change, automation will always execute based on this time TimingSunrise TimingMode = "sunrise" // Uses the local time for sunrise, each run of a set automation will update the actual time and regenerate a cron expression TimingSunset TimingMode = "sunset" // Same as above, just for sunset )
type User ¶
type User struct { Username string `json:"username"` Forename string `json:"forename"` Surname string `json:"surname"` PrimaryColorDark string `json:"primaryColorDark"` PrimaryColorLight string `json:"primaryColorLight"` SchedulerEnabled bool `json:"schedulerEnabled"` DarkTheme bool `json:"darkTheme"` }
func GetUserByUsername ¶
Returns a user struct based on a username, does not check if the user exists, additional checks needed beforehand
type UserDetails ¶
func GetUserDetails ¶
func GetUserDetails(username string) (UserDetails, bool, error)
Returns the users information and their permissions