pub struct Device {
pub serialized_py_object: Vec<u8>,
pub callable_methods: HashMap<String, String>,
}Expand description
Represents a Miio device with its associated properties and Python object.
The Device struct includes data necessary for device communication and method invocation, along with functionalities to serialize/deserialize the device configuration.
Fields§
§serialized_py_object: Vec<u8>A serialized representation of the underlying Python object as bytes.
callable_methods: HashMap<String, String>A map of callable method names to their corresponding Python signatures.
Implementations§
Source§impl Device
impl Device
Sourcepub fn deserialize_json(json_str: &str) -> Result<Device, Error>
pub fn deserialize_json(json_str: &str) -> Result<Device, Error>
Deserializes a JSON string into a Device instance.
This function leverages Serde’s JSON deserializer to parse the provided JSON
string and convert it into a corresponding Device instance. The JSON must
correctly represent the Device struct’s fields.
§Arguments
json_str- A string slice that holds the JSON representation of aDevice.
§Returns
Ok(Device)containing theDeviceinstance if deserialization succeeds.Err(serde_json::Error)if parsing fails due to invalid JSON or type mismatch.
Sourcepub fn serialize_json(&self) -> Result<String, Error>
pub fn serialize_json(&self) -> Result<String, Error>
Serializes the Device instance into a JSON string using pretty formatting.
This function leverages Serde’s JSON serializer with pretty print enabled
to convert the Device struct into a human-readable JSON string. The output
includes appropriate whitespace and line breaks, making it easier to read and
debug.
§Returns
Ok(String)containing the formatted JSON representation of the Device if serialization succeeds.Err(serde_json::Error)if any serialization error occurs.
Sourcepub fn create_device(
ip: &str,
token: &str,
device_type: &str,
) -> Result<Device, PyErr>
pub fn create_device( ip: &str, token: &str, device_type: &str, ) -> Result<Device, PyErr>
Creates a new Device instance by invoking the Python function.
This function calls the Python module to create a device and retrieve its properties, including serialized state and callable methods.
§Arguments
ip- The IP address of the device.token- The token used for authentication.device_type- The type of the device.
§Returns
Ok(Device)on success.Err(PyErr)if any Python call fails.
Sourcepub fn call_method(
&self,
method_name: &str,
args: Vec<&str>,
) -> Result<String, PyErr>
pub fn call_method( &self, method_name: &str, args: Vec<&str>, ) -> Result<String, PyErr>
Calls a method on the device by invoking the corresponding Python function.
This function sends a command to the device through Python and returns the result.
§Arguments
method_name- The name of the method to be called.args- A vector of string arguments for the method.
§Returns
Ok(String)containing the result if successful.Err(PyErr)if the Python call fails.