mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-03 11:21:41 +02:00
added Snapclient & Snapserver json objects
This commit is contained in:
parent
5951c123c9
commit
78c9a1c2ad
8 changed files with 139 additions and 42 deletions
|
@ -8,7 +8,7 @@ import org.json.JSONObject;
|
||||||
*/
|
*/
|
||||||
public class Client implements JsonSerialisable {
|
public class Client implements JsonSerialisable {
|
||||||
private Host host;
|
private Host host;
|
||||||
private Snapcast snapclient;
|
private Snapclient snapclient;
|
||||||
private ClientConfig config;
|
private ClientConfig config;
|
||||||
private Time_t lastSeen;
|
private Time_t lastSeen;
|
||||||
private boolean connected;
|
private boolean connected;
|
||||||
|
@ -31,9 +31,9 @@ public class Client implements JsonSerialisable {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.has("snapclient"))
|
if (json.has("snapclient"))
|
||||||
snapclient = new Snapcast(json.getJSONObject("snapclient"));
|
snapclient = new Snapclient(json.getJSONObject("snapclient"));
|
||||||
else {
|
else {
|
||||||
snapclient = new Snapcast();
|
snapclient = new Snapclient();
|
||||||
snapclient.version = json.getString("version");
|
snapclient.version = json.getString("version");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ import org.json.JSONObject;
|
||||||
*/
|
*/
|
||||||
public class Server implements JsonSerialisable {
|
public class Server implements JsonSerialisable {
|
||||||
private Host host;
|
private Host host;
|
||||||
private Snapcast snapserver;
|
private Snapserver snapserver;
|
||||||
|
|
||||||
public Server(JSONObject json) {
|
public Server(JSONObject json) {
|
||||||
fromJson(json);
|
fromJson(json);
|
||||||
|
@ -25,9 +25,9 @@ public class Server implements JsonSerialisable {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.has("snapserver"))
|
if (json.has("snapserver"))
|
||||||
snapserver = new Snapcast(json.getJSONObject("snapserver"));
|
snapserver = new Snapserver(json.getJSONObject("snapserver"));
|
||||||
else {
|
else {
|
||||||
snapserver = new Snapcast();
|
snapserver = new Snapserver();
|
||||||
snapserver.version = json.getString("version");
|
snapserver.version = json.getString("version");
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
|
|
@ -9,8 +9,7 @@ import org.json.JSONObject;
|
||||||
public class Snapcast implements JsonSerialisable {
|
public class Snapcast implements JsonSerialisable {
|
||||||
String name = "";
|
String name = "";
|
||||||
String version = "";
|
String version = "";
|
||||||
int streamProtocolVersion = 1;
|
int protocolVersion = 1;
|
||||||
int controlProtocolVersion = 1;
|
|
||||||
|
|
||||||
public Snapcast() {
|
public Snapcast() {
|
||||||
}
|
}
|
||||||
|
@ -24,8 +23,7 @@ public class Snapcast implements JsonSerialisable {
|
||||||
try {
|
try {
|
||||||
name = json.getString("name");
|
name = json.getString("name");
|
||||||
version = json.getString("version");
|
version = json.getString("version");
|
||||||
streamProtocolVersion = json.getInt("streamProtocolVersion");
|
protocolVersion = json.getInt("protocolVersion");
|
||||||
controlProtocolVersion = json.getInt("controlProtocolVersion");
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -37,8 +35,7 @@ public class Snapcast implements JsonSerialisable {
|
||||||
try {
|
try {
|
||||||
json.put("name", name);
|
json.put("name", name);
|
||||||
json.put("version", version);
|
json.put("version", version);
|
||||||
json.put("streamProtocolVersion", streamProtocolVersion);
|
json.put("protocolVersion", protocolVersion);
|
||||||
json.put("controlProtocolVersion", controlProtocolVersion);
|
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -53,12 +50,8 @@ public class Snapcast implements JsonSerialisable {
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getStreamProtocolVersion() {
|
public int getProtocolVersion() {
|
||||||
return streamProtocolVersion;
|
return protocolVersion;
|
||||||
}
|
|
||||||
|
|
||||||
public int getControlProtocolVersion() {
|
|
||||||
return controlProtocolVersion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -75,16 +68,14 @@ public class Snapcast implements JsonSerialisable {
|
||||||
|
|
||||||
if (name != null ? !name.equals(that.name) : that.name != null) return false;
|
if (name != null ? !name.equals(that.name) : that.name != null) return false;
|
||||||
if (version != null ? !version.equals(that.version) : that.version != null) return false;
|
if (version != null ? !version.equals(that.version) : that.version != null) return false;
|
||||||
if (streamProtocolVersion != that.streamProtocolVersion) return false;
|
return (protocolVersion == that.protocolVersion);
|
||||||
return (controlProtocolVersion == that.controlProtocolVersion);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int result = name != null ? name.hashCode() : 0;
|
int result = name != null ? name.hashCode() : 0;
|
||||||
result = 31 * result + (version != null ? version.hashCode() : 0);
|
result = 31 * result + (version != null ? version.hashCode() : 0);
|
||||||
result = 31 * result + streamProtocolVersion;
|
result = 31 * result + protocolVersion;
|
||||||
result = 31 * result + controlProtocolVersion;
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
package de.badaix.snapcast.control.json;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by johannes on 06.01.16.
|
||||||
|
*/
|
||||||
|
public class Snapclient extends Snapcast {
|
||||||
|
|
||||||
|
public Snapclient() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Snapclient(JSONObject json) {
|
||||||
|
super(json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
package de.badaix.snapcast.control.json;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by johannes on 06.01.16.
|
||||||
|
*/
|
||||||
|
public class Snapserver extends Snapcast {
|
||||||
|
int controlProtocolVersion = 1;
|
||||||
|
|
||||||
|
public Snapserver() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Snapserver(JSONObject json) {
|
||||||
|
super(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fromJson(JSONObject json) {
|
||||||
|
try {
|
||||||
|
super.fromJson(json);
|
||||||
|
controlProtocolVersion = json.getInt("controlProtocolVersion");
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject toJson() {
|
||||||
|
JSONObject json = super.toJson();
|
||||||
|
try {
|
||||||
|
json.put("controlProtocolVersion", controlProtocolVersion);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getControlProtocolVersion() {
|
||||||
|
return controlProtocolVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Snapserver that = (Snapserver) o;
|
||||||
|
|
||||||
|
if (controlProtocolVersion != that.controlProtocolVersion) return false;
|
||||||
|
return super.equals(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = super.hashCode();
|
||||||
|
result = 31 * result + controlProtocolVersion;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -42,7 +42,6 @@ public:
|
||||||
add("ClientName", "Snapclient");
|
add("ClientName", "Snapclient");
|
||||||
add("OS", ::getOS());
|
add("OS", ::getOS());
|
||||||
add("Arch", ::getArch());
|
add("Arch", ::getArch());
|
||||||
add("SnapControlProtocolVersion", "1");
|
|
||||||
add("SnapStreamProtocolVersion", "1");
|
add("SnapStreamProtocolVersion", "1");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,12 +79,7 @@ public:
|
||||||
return get("Arch");
|
return get("Arch");
|
||||||
}
|
}
|
||||||
|
|
||||||
int getControlProtocolVersion()
|
int getProtocolVersion()
|
||||||
{
|
|
||||||
return get("SnapControlProtocolVersion", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int getStreamProtocolVersion()
|
|
||||||
{
|
{
|
||||||
return get("SnapStreamProtocolVersion", 1);
|
return get("SnapStreamProtocolVersion", 1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,31 +137,63 @@ struct ClientConfig
|
||||||
|
|
||||||
struct Snapcast
|
struct Snapcast
|
||||||
{
|
{
|
||||||
Snapcast(const std::string& _name = "", const std::string& _version = "") : name(_name), version(_version), streamProtocolVersion(1), controlProtocolVersion(1)
|
Snapcast(const std::string& _name = "", const std::string& _version = "") : name(_name), version(_version), protocolVersion(1)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void fromJson(const json& j)
|
virtual ~Snapcast()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void fromJson(const json& j)
|
||||||
{
|
{
|
||||||
name = trim_copy(jGet<std::string>(j, "name", ""));
|
name = trim_copy(jGet<std::string>(j, "name", ""));
|
||||||
version = trim_copy(jGet<std::string>(j, "version", ""));
|
version = trim_copy(jGet<std::string>(j, "version", ""));
|
||||||
streamProtocolVersion = jGet<int>(j, "streamProtocolVersion", 1);
|
protocolVersion = jGet<int>(j, "protocolVersion", 1);
|
||||||
controlProtocolVersion = jGet<int>(j, "controlProtocolVersion", 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
json toJson()
|
virtual json toJson()
|
||||||
{
|
{
|
||||||
json j;
|
json j;
|
||||||
j["name"] = trim_copy(name);
|
j["name"] = trim_copy(name);
|
||||||
j["version"] = trim_copy(version);
|
j["version"] = trim_copy(version);
|
||||||
j["streamProtocolVersion"] = streamProtocolVersion;
|
j["protocolVersion"] = protocolVersion;
|
||||||
j["controlProtocolVersion"] = controlProtocolVersion;
|
|
||||||
return j;
|
return j;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
std::string version;
|
std::string version;
|
||||||
int streamProtocolVersion;
|
int protocolVersion;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Snapclient : public Snapcast
|
||||||
|
{
|
||||||
|
Snapclient(const std::string& _name = "", const std::string& _version = "") : Snapcast(_name, _version)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct Snapserver : public Snapcast
|
||||||
|
{
|
||||||
|
Snapserver(const std::string& _name = "", const std::string& _version = "") : Snapcast(_name, _version), controlProtocolVersion(1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void fromJson(const json& j)
|
||||||
|
{
|
||||||
|
Snapcast::fromJson(j);
|
||||||
|
controlProtocolVersion = jGet<int>(j, "controlProtocolVersion", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual json toJson()
|
||||||
|
{
|
||||||
|
json j = Snapcast::toJson();
|
||||||
|
j["controlProtocolVersion"] = controlProtocolVersion;
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
int controlProtocolVersion;
|
int controlProtocolVersion;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -218,7 +250,7 @@ struct ClientInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
Host host;
|
Host host;
|
||||||
Snapcast snapclient;
|
Snapclient snapclient;
|
||||||
ClientConfig config;
|
ClientConfig config;
|
||||||
timeval lastSeen;
|
timeval lastSeen;
|
||||||
bool connected;
|
bool connected;
|
||||||
|
|
|
@ -146,7 +146,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
|
||||||
|
|
||||||
Host host;
|
Host host;
|
||||||
//TODO: Set MAC and IP
|
//TODO: Set MAC and IP
|
||||||
Snapcast snapserver("Snapserver", VERSION);
|
Snapserver snapserver("Snapserver", VERSION);
|
||||||
response = {
|
response = {
|
||||||
{"server", {
|
{"server", {
|
||||||
{"host", host.toJson()},//getHostName()},
|
{"host", host.toJson()},//getHostName()},
|
||||||
|
@ -262,7 +262,7 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
|
||||||
connection->macAddress = helloMsg.getMacAddress();
|
connection->macAddress = helloMsg.getMacAddress();
|
||||||
logO << "Hello from " << connection->macAddress << ", host: " << helloMsg.getHostName() << ", v" << helloMsg.getVersion()
|
logO << "Hello from " << connection->macAddress << ", host: " << helloMsg.getHostName() << ", v" << helloMsg.getVersion()
|
||||||
<< ", ClientName: " << helloMsg.getClientName() << ", OS: " << helloMsg.getOS() << ", Arch: " << helloMsg.getArch()
|
<< ", ClientName: " << helloMsg.getClientName() << ", OS: " << helloMsg.getOS() << ", Arch: " << helloMsg.getArch()
|
||||||
<< ", control version: " << helloMsg.getControlProtocolVersion() << ", Stream version: " << helloMsg.getStreamProtocolVersion() << "\n";
|
<< ", Protocol version: " << helloMsg.getProtocolVersion() << "\n";
|
||||||
|
|
||||||
logD << "request kServerSettings: " << connection->macAddress << "\n";
|
logD << "request kServerSettings: " << connection->macAddress << "\n";
|
||||||
// std::lock_guard<std::mutex> mlock(mutex_);
|
// std::lock_guard<std::mutex> mlock(mutex_);
|
||||||
|
@ -290,8 +290,7 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
|
||||||
client->host.arch = helloMsg.getArch();
|
client->host.arch = helloMsg.getArch();
|
||||||
client->snapclient.version = helloMsg.getVersion();
|
client->snapclient.version = helloMsg.getVersion();
|
||||||
client->snapclient.name = helloMsg.getClientName();
|
client->snapclient.name = helloMsg.getClientName();
|
||||||
client->snapclient.streamProtocolVersion = helloMsg.getStreamProtocolVersion();
|
client->snapclient.protocolVersion = helloMsg.getProtocolVersion();
|
||||||
client->snapclient.controlProtocolVersion = helloMsg.getControlProtocolVersion();
|
|
||||||
client->connected = true;
|
client->connected = true;
|
||||||
gettimeofday(&client->lastSeen, NULL);
|
gettimeofday(&client->lastSeen, NULL);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue