From a19a6b1afa03ed6f521271c5932b2212af3396b8 Mon Sep 17 00:00:00 2001 From: Matthias Hochgatterer Date: Mon, 12 Oct 2020 17:13:15 +0200 Subject: [PATCH] Add public methods to check if permissions --- characteristic/characteristic.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/characteristic/characteristic.go b/characteristic/characteristic.go index 74952f22..6256b8d1 100644 --- a/characteristic/characteristic.go +++ b/characteristic/characteristic.go @@ -94,14 +94,18 @@ func (c *Characteristic) Equal(other interface{}) bool { // Private -func (c *Characteristic) isReadable() bool { +func (c *Characteristic) IsReadable() bool { return readPerm(c.Perms) } -func (c *Characteristic) isWritable() bool { +func (c *Characteristic) IsWritable() bool { return writePerm(c.Perms) } +func (c *Characteristic) IsObservable() bool { + return eventPerm(c.Perms) +} + func (c *Characteristic) getValue(conn net.Conn) interface{} { if c.valueGetFunc != nil { c.updateValue(c.valueGetFunc(), conn, false) @@ -130,12 +134,12 @@ func (c *Characteristic) updateValue(value interface{}, conn net.Conn, checkPerm } // Ignore new values from remote when permissions don't allow write and checkPerms is true - if checkPerms && !c.isWritable() { + if checkPerms && !c.IsWritable() { return } old := c.Value - if c.isReadable() { + if c.IsReadable() { c.Value = value } @@ -223,3 +227,13 @@ func writePerm(permissions []string) bool { } return false } + +// eventPerm returns true when perms include events permission +func eventPerm(permissions []string) bool { + for _, value := range permissions { + if value == PermEvents { + return true + } + } + return false +}