Update Wiz controller to use JSON library, receive firmware information from device

This commit is contained in:
Adam Honse 2020-11-04 00:42:31 -06:00
parent 5c5ed6f8bd
commit 87c99d31cf
4 changed files with 25664 additions and 14 deletions

View file

@ -5,12 +5,32 @@
\*---------------------------------------------------------*/
#include "PhilipsWizController.h"
#include "json.hpp"
using json = nlohmann::json;
using namespace std::chrono_literals;
PhilipsWizController::PhilipsWizController(std::string ip)
{
/*-----------------------------------------------------------------*\
| Fill in location string with device's IP address |
\*-----------------------------------------------------------------*/
location = "IP: " + ip;
/*-----------------------------------------------------------------*\
| Open a UDP client sending to the device's IP, port 38899 |
\*-----------------------------------------------------------------*/
port.udp_client(ip.c_str(), "38899");
/*-----------------------------------------------------------------*\
| Start a thread to handle responses received from the Wiz device |
\*-----------------------------------------------------------------*/
ReceiveThread = new std::thread(&PhilipsWizController::ReceiveThreadFunction, this);
/*-----------------------------------------------------------------*\
| Request the system config (name, firmware version, MAC address) |
\*-----------------------------------------------------------------*/
RequestSystemConfig();
}
PhilipsWizController::~PhilipsWizController()
@ -25,35 +45,122 @@ std::string PhilipsWizController::GetLocation()
std::string PhilipsWizController::GetName()
{
return("");
return("Wiz");
}
std::string PhilipsWizController::GetVersion()
{
return("");
return(module_name + " " + firmware_version);
}
std::string PhilipsWizController::GetManufacturer()
{
return("");
return("Philips");
}
std::string PhilipsWizController::GetUniqueID()
{
return("");
return(module_mac);
}
void PhilipsWizController::SetColor(unsigned char red, unsigned char green, unsigned char blue)
{
if(red == 0 && green == 0 && blue == 0)
{
std::string message = "{\"method\":\"setPilot\",\"params\":{\"r\":" + std::to_string(red) + ",\"g\":" + std::to_string(green) + ",\"b\":" + std::to_string(blue) + ",\"state\":0}}";
port.udp_write((char *)message.c_str(), message.length() + 1);
}
else
{
std::string message = "{\"method\":\"setPilot\",\"params\":{\"r\":" + std::to_string(red) + ",\"g\":" + std::to_string(green) + ",\"b\":" + std::to_string(blue) + ",\"state\":1}}";
port.udp_write((char *)message.c_str(), message.length() + 1);
}
json command;
/*-----------------------------------------------------------------*\
| Fill in the setPilot command with RGB information. |
| The bulb will not respond to 0, 0, 0, so if all channels are zero,|
| set the state to off. Otherwise, set it to on. |
\*-----------------------------------------------------------------*/
command["method"] = "setPilot";
command["params"]["r"] = red;
command["params"]["g"] = green;
command["params"]["b"] = blue;
command["params"]["state"] = !((red == 0) && (green == 0) && (blue == 0));
/*-----------------------------------------------------------------*\
| Convert the JSON object to a string and write it |
\*-----------------------------------------------------------------*/
std::string command_str = command.dump();
port.udp_write((char *)command_str.c_str(), command_str.length() + 1);
}
void PhilipsWizController::ReceiveThreadFunction()
{
char recv_buf[1024];
while(1)
{
/*-----------------------------------------------------------------*\
| Receive up to 1024 bytes from the device |
\*-----------------------------------------------------------------*/
int size = port.udp_listen(recv_buf, 1024);
/*-----------------------------------------------------------------*\
| Responses are not null-terminated, so add termination |
\*-----------------------------------------------------------------*/
recv_buf[size] = '\0';
/*-----------------------------------------------------------------*\
| Convert null-terminated response to JSON |
\*-----------------------------------------------------------------*/
json response = json::parse(recv_buf);
/*-----------------------------------------------------------------*\
| Check if the response contains the method name |
\*-----------------------------------------------------------------*/
if(response.contains("method"))
{
/*-------------------------------------------------------------*\
| Handle responses for getSystemConfig method |
| This method's response should contain a result object |
| containing fwVersion, moduleName, and mac, among others. |
\*-------------------------------------------------------------*/
if(response["method"] == "getSystemConfig")
{
if(response.contains("result"))
{
json result = response["result"];
if(result.contains("fwVersion"))
{
firmware_version = result["fwVersion"];
}
if(result.contains("moduleName"))
{
module_name = result["moduleName"];
}
if(result.contains("mac"))
{
module_mac = result["mac"];
}
}
}
}
}
}
void PhilipsWizController::RequestSystemConfig()
{
json command;
/*-----------------------------------------------------------------*\
| Fill in the getSystemConfig command |
\*-----------------------------------------------------------------*/
command["method"] = "getSystemConfig";
/*-----------------------------------------------------------------*\
| Convert the JSON object to a string and write it |
\*-----------------------------------------------------------------*/
std::string command_str = command.dump();
port.udp_write((char *)command_str.c_str(), command_str.length() + 1);
/*-----------------------------------------------------------------*\
| Sleep for 100ms to give it time to receive and process response |
\*-----------------------------------------------------------------*/
std::this_thread::sleep_for(100ms);
}

View file

@ -8,6 +8,7 @@
#include "net_port.h"
#include <string>
#include <thread>
#include <vector>
#pragma once
@ -26,7 +27,14 @@ public:
void SetColor(unsigned char red, unsigned char green, unsigned char blue);
void ReceiveThreadFunction();
void RequestSystemConfig();
private:
std::string firmware_version;
std::string module_name;
std::string module_mac;
std::string location;
net_port port;
std::thread* ReceiveThread;
};

View file

@ -42,6 +42,7 @@ DEFINES +=
INCLUDEPATH += \
dependencies/ColorWheel \
dependencies/CRCpp/ \
dependencies/json/ \
dependencies/libe131/src/ \
i2c_smbus/ \
i2c_tools/ \
@ -106,6 +107,7 @@ INCLUDEPATH +=
HEADERS += \
dependencies/ColorWheel/ColorWheel.h \
dependencies/json/json.hpp \
NetworkClient.h \
NetworkProtocol.h \
NetworkServer.h \

25533
dependencies/json/json.hpp vendored Normal file

File diff suppressed because it is too large Load diff