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