Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: begin() #36

Open
DigiHzData opened this issue Nov 6, 2022 · 4 comments
Open

New feature: begin() #36

DigiHzData opened this issue Nov 6, 2022 · 4 comments

Comments

@DigiHzData
Copy link

As it is now of OctoPrintAPI (V1.1.15) it is only possible to initialize it before setup.
This makes problems for dynamic variables.

But i changed the source so that OctoPrintAPI will work from within setup now with success.

Here is what needs to be changed:

In OctoPrintAPI.h

class OctoprintApi {
public:
//OctoprintApi(Client &client, IPAddress octoPrintIp, int octoPrintPort, String apiKey);//DigiHzData removed.
//OctoprintApi(Client &client, char *octoPrintUrl, int octoPrintPort, String apiKey);//DigiHzData removed.
OctoprintApi(Client &client);//DigiHzData added.
void begin(IPAddress octoPrintIp, int octoPrintPort, String apiKey);//DigiHzData added.
void begin(char *octoPrintUrl, int octoPrintPort, String apiKey);//DigiHzData added.
...

In OctoPrintAPI.cpp

#include "OctoPrintAPI.h"

#include "Arduino.h"

OctoprintApi::OctoprintApi(Client &client){//DigiHzData added.
_client = &client;//DigiHzData added.
}//DigiHzData added.

/** OctoprintApi()

  • IP address version of the client connect function
  • */
    //OctoprintApi::OctoprintApi(Client &client, IPAddress octoPrintIp, int octoPrintPort, String apiKey) {//DigiHzData removed.
    void OctoprintApi::begin(IPAddress octoPrintIp, int octoPrintPort, String apiKey) {//DigiHzData added.
    //_client = &client;//DigiHzData removed.
    _apiKey = apiKey;
    _octoPrintIp = octoPrintIp;
    _octoPrintPort = octoPrintPort;
    _usingIpAddress = true;
    }

/** OctoprintApi()

  • Hostname version of the client connect function
  • */
    //OctoprintApi::OctoprintApi(Client &client, char *octoPrintUrl, int octoPrintPort, String apiKey) {//DigiHzData removed.
    void OctoprintApi::begin(char *octoPrintUrl, int octoPrintPort, String apiKey) {//DigiHzData added.
    //_client = &client;//DigiHzData removed.
    _apiKey = apiKey;
    _octoPrintUrl = octoPrintUrl;
    _octoPrintPort = octoPrintPort;
    _usingIpAddress = false;
    }

/** GET YOUR ASS TO OCTOPRINT...
*

  • **/
    ...

After these changes has been made, then you initiate OctoPrintApi before setup with:
OctoprintApi api(client);

And in setup you do either:
api.begin(ip, octoprint_httpPort, octoprint_apikey);//If using IP address
OR
api.begin(octoprint_host, octoprint_httpPort, octoprint_apikey);//If using hostname.

This way, we can pull ip, octoprint_host, octoprint_httpPort and octoprint_apikey from eeprom or sd card.

@chunkysteveo
Copy link
Owner

This library needs some new love(!) Thanks for the update, I'll take a look and see what I can integrate in.

@jcassel
Copy link
Contributor

jcassel commented Nov 25, 2022

@chunkysteveo
I also needed something similar for my project. I left in the existing calls and added in 2 new init() methods and a simple create method that takes no parameters. Figuring its better to not break this as it would break every single existing use case.

I will put in a pull request later this week.

Headers would look like below for a quick understanding on how I implemented it.

  OctoprintApi(void);
  OctoprintApi(Client &client, IPAddress octoPrintIp, int octoPrintPort, String apiKey);
  OctoprintApi(Client &client, char *octoPrintUrl, int octoPrintPort, String apiKey);
  void init(Client &client, char *octoPrintUrl, int octoPrintPort, String apiKey);
  void init(Client &client, IPAddress octoPrintIp, int octoPrintPort, String apiKey);

The original create methods now just call the associated init method.

Doing it this way allows existing Examples to continue to be valid. A minor update to include how to use the init methods would be another update I would do.

A minor update to the Keywords file is also needed.

@DigiHzData
Copy link
Author

@jcassel
I will check your different approach when i get the time.
I will notify here later about the outcoming results.
Thank you for your implementation for now,

@jcassel
Copy link
Contributor

jcassel commented Dec 29, 2022

@DigiHzData
The pull request I made for this was merged in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants