Giter Site home page Giter Site logo

P115 support about tapo HOT 7 CLOSED

mihai-dinculescu avatar mihai-dinculescu commented on May 24, 2024
P115 support

from tapo.

Comments (7)

mihai-dinculescu avatar mihai-dinculescu commented on May 24, 2024

I don't see why not, but unfortunately, I don't have a P115 to test with.

Can you please try and use it through the P110 interface and see how that goes?

from tapo.

nine-2-five avatar nine-2-five commented on May 24, 2024

I tried cargo run --example tapo_p110 but I get: Couldn't connect to server

from tapo.

nine-2-five avatar nine-2-five commented on May 24, 2024

It's actually working. I haven't written a byte in Rust until now, can you help with serializing tapo_date_format to JSON?

from tapo.

nine-2-five avatar nine-2-five commented on May 24, 2024

I found a way using, not sure if it's the best.

    pub fn serialize<S>(
        date: &OffsetDateTime,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {        
        // let format = format_description::parse(
        //     "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \
        //          sign:mandatory]:[offset_minute]:[offset_second]",
        // )?;
        let s = format!("{}", date);
        serializer.serialize_str(&s)
    }

from tapo.

mihai-dinculescu avatar mihai-dinculescu commented on May 24, 2024

What are you trying to achieve that requires you to serialize a date?

If you're trying to send the tapo responses to another API, which requires serialization, you will probably be better off with building your own structs that #[derive(serde::Serialize)] and map the tapo response into them.

from tapo.

nine-2-five avatar nine-2-five commented on May 24, 2024

I'd like to save them as JSON to docker stdout log, and pick them up with Logstash.
Mapping in Rust is done using From trait? Or is there an automapper lib like in C#?

from tapo.

mihai-dinculescu avatar mihai-dinculescu commented on May 24, 2024

Probably there are some mappers, but they might be overkill for your use case.

You're correct, the From trait would get the job done quite easily.

#[derive(Debug, serde::Serialize)]
pub struct EnergyData {
    pub local_time: time::OffsetDateTime,
    pub data: Vec<u64>,
    pub start_timestamp: u64,
    pub end_timestamp: u64,
    pub interval: u64,
}

impl From<EnergyDataResult> for EnergyData {
    fn from(energy_data: EnergyDataResult) -> Self {
        Self {
            local_time: energy_data.local_time,
            data: energy_data.data,
            start_timestamp: energy_data.start_timestamp,
            end_timestamp: energy_data.end_timestamp,
            interval: energy_data.interval,
        }
    }
}

Then you can use it like this:

let energy_data: EnergyData = energy_data_monthly.into();
let _json = serde_json::to_string_pretty(&energy_data)?;

One gotcha is that you have to bring in the time crate with the serde feature so that you can serialize time::OffsetDateTime automatically.

time = { version = "0.3", features = ["serde"] }

from tapo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.