Skip to content

Commit

Permalink
Merge branch 'testing'
Browse files Browse the repository at this point in the history
  • Loading branch information
hfedcba committed Jul 27, 2018
2 parents 00ba7bb + 3e246c2 commit f9d084d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 73 deletions.
3 changes: 3 additions & 0 deletions misc/Config Directory/kodi.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ ___________________________________________________________________________
---------------------------------- Kodi ----------------------------------
___________________________________________________________________________

[General]

moduleEnabled = true
82 changes: 18 additions & 64 deletions src/MyCentral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,9 @@ void MyCentral::deletePeer(uint64_t id)
if(_peersById.find(id) != _peersById.end()) _peersById.erase(id);
}

if(_currentPeer && _currentPeer->getID() == id) _currentPeer.reset();

int32_t i = 0;
while(peer.use_count() > 1 && i < 600)
{
if(_currentPeer && _currentPeer->getID() == id) _currentPeer.reset();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
i++;
}
Expand Down Expand Up @@ -261,15 +258,6 @@ std::string MyCentral::handleCliCommand(std::string command)
try
{
std::ostringstream stringStream;
if(_currentPeer)
{
if(command == "unselect" || command == "u")
{
_currentPeer.reset();
return "Peer unselected.\n";
}
return _currentPeer->handleCliCommand(command);
}
if(command == "help" || command == "h")
{
stringStream << "List of commands:" << std::endl << std::endl;
Expand Down Expand Up @@ -392,7 +380,6 @@ std::string MyCentral::handleCliCommand(std::string command)
if(!peerExists(peerID)) stringStream << "This peer is not paired to this central." << std::endl;
else
{
if(_currentPeer && _currentPeer->getID() == peerID) _currentPeer.reset();
stringStream << "Removing peer " << std::to_string(peerID) << std::endl;
deletePeer(peerID);
}
Expand Down Expand Up @@ -601,47 +588,6 @@ std::string MyCentral::handleCliCommand(std::string command)
}
return stringStream.str();
}
else if(command.compare(0, 12, "peers select") == 0 || command.compare(0, 2, "ps") == 0)
{
uint64_t id = 0;

std::stringstream stream(command);
std::string element;
int32_t offset = (command.at(1) == 's') ? 0 : 1;
int32_t index = 0;
while(std::getline(stream, element, ' '))
{
if(index < 1 + offset)
{
index++;
continue;
}
else if(index == 1 + offset)
{
if(element == "help") break;
id = BaseLib::Math::getNumber(element, false);
if(id == 0) return "Invalid id.\n";
}
index++;
}
if(index == 1 + offset)
{
stringStream << "Description: This command selects a peer." << std::endl;
stringStream << "Usage: peers select PEERID" << std::endl << std::endl;
stringStream << "Parameters:" << std::endl;
stringStream << " PEERID:\tThe id of the peer to select. Example: 513" << std::endl;
return stringStream.str();
}

_currentPeer = getPeer(id);
if(!_currentPeer) stringStream << "This peer is not paired to this central." << std::endl;
else
{
stringStream << "Peer with id " << std::hex << std::to_string(id) << " and device type 0x" << _bl->hf.getHexString(_currentPeer->getDeviceType()) << " selected." << std::dec << std::endl;
stringStream << "For information about the peer's commands type: \"help\"" << std::endl;
}
return stringStream.str();
}
else return "Unknown command.\n";
}
catch(const std::exception& ex)
Expand Down Expand Up @@ -750,10 +696,16 @@ PVariable MyCentral::deleteDevice(BaseLib::PRpcClientInfo clientInfo, std::strin
try
{
if(serialNumber.empty()) return Variable::createError(-2, "Unknown device.");
std::shared_ptr<MyPeer> peer = getPeer(serialNumber);
if(!peer) return PVariable(new Variable(VariableType::tVoid));

return deleteDevice(clientInfo, peer->getID(), flags);
uint64_t peerId = 0;

{
std::shared_ptr<MyPeer> peer = getPeer(serialNumber);
if(!peer) return PVariable(new Variable(VariableType::tVoid));
peerId = peer->getID();
}

return deleteDevice(clientInfo, peerId, flags);
}
catch(const std::exception& ex)
{
Expand All @@ -770,18 +722,20 @@ PVariable MyCentral::deleteDevice(BaseLib::PRpcClientInfo clientInfo, std::strin
return Variable::createError(-32500, "Unknown application error.");
}

PVariable MyCentral::deleteDevice(BaseLib::PRpcClientInfo clientInfo, uint64_t peerID, int32_t flags)
PVariable MyCentral::deleteDevice(BaseLib::PRpcClientInfo clientInfo, uint64_t peerId, int32_t flags)
{
try
{
if(peerID == 0) return Variable::createError(-2, "Unknown device.");
std::shared_ptr<MyPeer> peer = getPeer(peerID);
if(!peer) return PVariable(new Variable(VariableType::tVoid));
uint64_t id = peer->getID();
if(peerId == 0) return Variable::createError(-2, "Unknown device.");

{
std::shared_ptr<MyPeer> peer = getPeer(peerId);
if(!peer) return PVariable(new Variable(VariableType::tVoid));
}

deletePeer(id);
deletePeer(peerId);

if(peerExists(id)) return Variable::createError(-1, "Error deleting peer. See log for more details.");
if(peerExists(peerId)) return Variable::createError(-1, "Error deleting peer. See log for more details.");

return PVariable(new Variable(VariableType::tVoid));
}
Expand Down
26 changes: 17 additions & 9 deletions src/MyPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,10 @@ void MyPeer::connected(bool connected)
std::shared_ptr<std::vector<std::string>> valueKeys(new std::vector<std::string>{"CONNECTED"});
std::shared_ptr<std::vector<PVariable>> rpcValues(new std::vector<PVariable>{value});

std::string eventSource = "device-" + std::to_string(_peerID);
std::string address(_serialNumber + ":" + std::to_string(channel));
raiseEvent(_peerID, channel, valueKeys, rpcValues);
raiseRPCEvent(_peerID, channel, address, valueKeys, rpcValues);
raiseEvent(eventSource, _peerID, channel, valueKeys, rpcValues);
raiseRPCEvent(eventSource, _peerID, channel, address, valueKeys, rpcValues);
}
catch(const std::exception& ex)
{
Expand Down Expand Up @@ -630,12 +631,13 @@ void MyPeer::packetReceived(std::shared_ptr<MyPacket> packet)

if(!rpcValues.empty())
{
for(std::map<uint32_t, std::shared_ptr<std::vector<std::string>>>::const_iterator j = valueKeys.begin(); j != valueKeys.end(); ++j)
for(std::map<uint32_t, std::shared_ptr<std::vector<std::string>>>::iterator j = valueKeys.begin(); j != valueKeys.end(); ++j)
{
if(j->second->empty()) continue;
std::string address(_serialNumber + ":" + std::to_string(j->first));
raiseEvent(_peerID, j->first, j->second, rpcValues.at(j->first));
raiseRPCEvent(_peerID, j->first, address, j->second, rpcValues.at(j->first));
std::string eventSource = "device-" + std::to_string(_peerID);
std::string address(_serialNumber + ":" + std::to_string(j->first));
raiseEvent(eventSource, _peerID, j->first, j->second, rpcValues.at(j->first));
raiseRPCEvent(eventSource, _peerID, j->first, address, j->second, rpcValues.at(j->first));
}
}
}
Expand Down Expand Up @@ -795,7 +797,12 @@ PVariable MyPeer::setValue(BaseLib::PRpcClientInfo clientInfo, uint32_t channel,
valueKeys->push_back(valueKey);
values->push_back(value);
}
if(!valueKeys->empty()) raiseRPCEvent(_peerID, channel, _serialNumber + ":" + std::to_string(channel), valueKeys, values);
if(!valueKeys->empty())
{
std::string address(_serialNumber + ":" + std::to_string(channel));
raiseEvent(clientInfo->initInterfaceId, _peerID, channel, valueKeys, values);
raiseRPCEvent(clientInfo->initInterfaceId, _peerID, channel, address, valueKeys, values);
}
return PVariable(new Variable(VariableType::tVoid));
}
else if(rpcParameter->physical->operationType != IPhysical::OperationType::Enum::command) return Variable::createError(-6, "Parameter is not settable.");
Expand Down Expand Up @@ -860,8 +867,9 @@ PVariable MyPeer::setValue(BaseLib::PRpcClientInfo clientInfo, uint32_t channel,

if(!valueKeys->empty())
{
raiseEvent(_peerID, channel, valueKeys, values);
raiseRPCEvent(_peerID, channel, _serialNumber + ":" + std::to_string(channel), valueKeys, values);
std::string address(_serialNumber + ":" + std::to_string(channel));
raiseEvent(clientInfo->initInterfaceId, _peerID, channel, valueKeys, values);
raiseRPCEvent(clientInfo->initInterfaceId, _peerID, channel, address, valueKeys, values);
}

return PVariable(new Variable(VariableType::tVoid));
Expand Down

0 comments on commit f9d084d

Please sign in to comment.