Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef LIBWEAVE_SRC_COMPONENT_MANAGER_H_ |
| 6 | #define LIBWEAVE_SRC_COMPONENT_MANAGER_H_ |
| 7 | |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 8 | #include <map> |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 11 | #include <base/callback_list.h> |
| 12 | #include <base/time/clock.h> |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 13 | #include <base/values.h> |
| 14 | #include <weave/error.h> |
| 15 | |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 16 | #include "src/commands/command_queue.h" |
| 17 | |
| 18 | namespace weave { |
| 19 | |
| 20 | class CommandInstance; |
| 21 | |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 22 | enum class UserRole { |
| 23 | kViewer, |
| 24 | kUser, |
| 25 | kManager, |
| 26 | kOwner, |
| 27 | }; |
| 28 | |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 29 | // A simple notification record event to track component state changes. |
| 30 | // The |timestamp| records the time of the state change. |
| 31 | // |changed_properties| contains a property set with the new property values |
| 32 | // which were updated at the time the event was recorded. |
| 33 | struct ComponentStateChange { |
| 34 | ComponentStateChange(base::Time time, |
| 35 | const std::string& path, |
| 36 | std::unique_ptr<base::DictionaryValue> properties) |
| 37 | : timestamp{time}, component{path}, |
| 38 | changed_properties{std::move(properties)} {} |
| 39 | base::Time timestamp; |
| 40 | std::string component; |
| 41 | std::unique_ptr<base::DictionaryValue> changed_properties; |
| 42 | }; |
| 43 | |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 44 | class ComponentManager { |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 45 | public: |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 46 | using UpdateID = uint64_t; |
| 47 | using Token = |
| 48 | std::unique_ptr<base::CallbackList<void(UpdateID)>::Subscription>; |
| 49 | struct StateSnapshot { |
| 50 | UpdateID update_id; |
| 51 | std::vector<ComponentStateChange> state_changes; |
| 52 | }; |
| 53 | |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 54 | ComponentManager() {} |
| 55 | virtual ~ComponentManager() {} |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 56 | |
| 57 | // Loads trait definition schema. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 58 | virtual bool LoadTraits(const base::DictionaryValue& dict, |
| 59 | ErrorPtr* error) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 60 | |
| 61 | // Same as the overload above, but takes a json string to read the trait |
| 62 | // definitions from. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 63 | virtual bool LoadTraits(const std::string& json, ErrorPtr* error) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 64 | |
| 65 | // Sets callback which is called when new trait definitions are added. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 66 | virtual void AddTraitDefChangedCallback(const base::Closure& callback) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 67 | |
| 68 | // Adds a new component instance to device. |
| 69 | // |path| is a path to the parent component (or empty string if a root-level |
| 70 | // component is being added). |
| 71 | // |name| is a component name being added. |
| 72 | // |traits| is a list of trait names this component supports. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 73 | virtual bool AddComponent(const std::string& path, |
| 74 | const std::string& name, |
| 75 | const std::vector<std::string>& traits, |
| 76 | ErrorPtr* error) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 77 | |
| 78 | // Adds a new component instance to device, as a part of component array. |
| 79 | // |path| is a path to the parent component. |
| 80 | // |name| is an array root element inside the child components. |
| 81 | // |traits| is a list of trait names this component supports. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 82 | virtual bool AddComponentArrayItem(const std::string& path, |
| 83 | const std::string& name, |
| 84 | const std::vector<std::string>& traits, |
| 85 | ErrorPtr* error) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 86 | |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 87 | // Sets callback which is called when new components are added. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 88 | virtual void AddComponentTreeChangedCallback( |
| 89 | const base::Closure& callback) = 0; |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 90 | |
Alex Vakulenko | d91d625 | 2015-12-05 17:14:39 -0800 | [diff] [blame] | 91 | // Adds a new command instance to the command queue. The command specified in |
| 92 | // |command_instance| must be fully initialized and have its name, component, |
| 93 | // id populated. |
| 94 | virtual void AddCommand( |
| 95 | std::unique_ptr<CommandInstance> command_instance) = 0; |
| 96 | |
| 97 | // Parses the command definition from a json dictionary. The resulting command |
| 98 | // instance is populated with all the required fields and partially validated |
| 99 | // against syntax/schema. |
| 100 | // The new command ID is returned through optional |id| param. |
| 101 | virtual std::unique_ptr<CommandInstance> ParseCommandInstance( |
| 102 | const base::DictionaryValue& command, |
| 103 | Command::Origin command_origin, |
| 104 | UserRole role, |
| 105 | std::string* id, |
| 106 | ErrorPtr* error) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 107 | |
| 108 | // Find a command instance with the given ID in the command queue. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 109 | virtual CommandInstance* FindCommand(const std::string& id) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 110 | |
| 111 | // Command queue monitoring callbacks (called when a new command is added to |
| 112 | // or removed from the queue). |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 113 | virtual void AddCommandAddedCallback( |
| 114 | const CommandQueue::CommandCallback& callback) = 0; |
| 115 | virtual void AddCommandRemovedCallback( |
| 116 | const CommandQueue::CommandCallback& callback) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 117 | |
| 118 | // Adds a command handler for specific component's command. |
| 119 | // |component_path| is a path to target component (e.g. "stove.burners[2]"). |
| 120 | // |command_name| is a full path of the command, including trait name and |
| 121 | // command name (e.g. "burner.setPower"). |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 122 | virtual void AddCommandHandler( |
| 123 | const std::string& component_path, |
| 124 | const std::string& command_name, |
| 125 | const Device::CommandHandlerCallback& callback) = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 126 | |
| 127 | // Finds a component instance by its full path. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 128 | virtual const base::DictionaryValue* FindComponent( |
| 129 | const std::string& path, |
| 130 | ErrorPtr* error) const = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 131 | // Finds a definition of trait with the given |name|. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 132 | virtual const base::DictionaryValue* FindTraitDefinition( |
| 133 | const std::string& name) const = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 134 | |
| 135 | // Finds a command definition, where |command_name| is in the form of |
| 136 | // "trait.command". |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 137 | virtual const base::DictionaryValue* FindCommandDefinition( |
| 138 | const std::string& command_name) const = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 139 | |
| 140 | // Checks the minimum required user role for a given command. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 141 | virtual bool GetMinimalRole(const std::string& command_name, |
| 142 | UserRole* minimal_role, |
| 143 | ErrorPtr* error) const = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 144 | |
| 145 | // Returns the full JSON dictionary containing trait definitions. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 146 | virtual const base::DictionaryValue& GetTraits() const = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 147 | |
| 148 | // Returns the full JSON dictionary containing component instances. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 149 | virtual const base::DictionaryValue& GetComponents() const = 0; |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 150 | |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 151 | // Component state manipulation methods. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 152 | virtual bool SetStateProperties(const std::string& component_path, |
| 153 | const base::DictionaryValue& dict, |
| 154 | ErrorPtr* error) = 0; |
| 155 | virtual bool SetStatePropertiesFromJson(const std::string& component_path, |
| 156 | const std::string& json, |
| 157 | ErrorPtr* error) = 0; |
| 158 | virtual const base::Value* GetStateProperty(const std::string& component_path, |
| 159 | const std::string& name, |
| 160 | ErrorPtr* error) const = 0; |
| 161 | virtual bool SetStateProperty(const std::string& component_path, |
| 162 | const std::string& name, |
| 163 | const base::Value& value, |
| 164 | ErrorPtr* error) = 0; |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 165 | |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 166 | virtual void AddStateChangedCallback(const base::Closure& callback) = 0; |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 167 | |
| 168 | // Returns the recorded state changes since last time this method was called. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 169 | virtual StateSnapshot GetAndClearRecordedStateChanges() = 0; |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 170 | |
| 171 | // Called to notify that the state patch with |id| has been successfully sent |
| 172 | // to the server and processed. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 173 | virtual void NotifyStateUpdatedOnServer(UpdateID id) = 0; |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 174 | |
| 175 | // Returns an ID of last state change update. Each SetStatePropertyNNN() |
| 176 | // invocation increments this value by 1. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 177 | virtual UpdateID GetLastStateChangeId() const = 0; |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 178 | |
| 179 | // Subscribes for device state update notifications from cloud server. |
| 180 | // The |callback| will be called every time a state patch with given ID is |
| 181 | // successfully received and processed by Weave server. |
| 182 | // Returns a subscription token. As soon as this token is destroyed, the |
| 183 | // respective callback is removed from the callback list. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 184 | virtual Token AddServerStateUpdatedCallback( |
| 185 | const base::Callback<void(UpdateID)>& callback) = 0; |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 186 | |
Alex Vakulenko | a3c5e6d | 2015-12-04 17:54:38 -0800 | [diff] [blame] | 187 | // Helper method for legacy API to obtain first component that implements |
| 188 | // the given trait. This is useful for routing commands that have no component |
| 189 | // path specified. |
| 190 | // Returns empty string if no components are found. |
| 191 | // This method only searches for component on the top level of components |
| 192 | // tree. No sub-components are searched. |
Alex Vakulenko | ba98115 | 2015-12-05 13:58:22 -0800 | [diff] [blame] | 193 | virtual std::string FindComponentWithTrait( |
| 194 | const std::string& trait) const = 0; |
Alex Vakulenko | 7b588fc | 2015-12-04 16:03:59 -0800 | [diff] [blame] | 195 | |
Alex Vakulenko | 6b394d1 | 2015-12-05 15:52:54 -0800 | [diff] [blame] | 196 | // Support for legacy APIs. Setting command and state definitions. |
| 197 | // This translates into modifying a trait definition. |
| 198 | virtual bool AddLegacyCommandDefinitions(const base::DictionaryValue& dict, |
| 199 | ErrorPtr* error) = 0; |
| 200 | virtual bool AddLegacyStateDefinitions(const base::DictionaryValue& dict, |
| 201 | ErrorPtr* error) = 0; |
| 202 | // Returns device state for legacy APIs. |
| 203 | virtual const base::DictionaryValue& GetLegacyState() const = 0; |
| 204 | // Returns command definitions for legacy APIs. |
| 205 | virtual const base::DictionaryValue& GetLegacyCommandDefinitions() const = 0; |
| 206 | |
Alex Vakulenko | 44c1dbe | 2015-12-03 15:35:09 -0800 | [diff] [blame] | 207 | DISALLOW_COPY_AND_ASSIGN(ComponentManager); |
| 208 | }; |
| 209 | |
| 210 | } // namespace weave |
| 211 | |
| 212 | #endif // LIBWEAVE_SRC_COMPONENT_MANAGER_H_ |