built-in exceptions needed \
This commit is contained in:
+29
-32
@@ -7,16 +7,13 @@ use Menking\Meshcore\Model\AdvertPathResponse;
|
|||||||
use Menking\Meshcore\Model\AppStartResponse;
|
use Menking\Meshcore\Model\AppStartResponse;
|
||||||
use Menking\Meshcore\Model\BatteryStorageResponse;
|
use Menking\Meshcore\Model\BatteryStorageResponse;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use Menking\Meshcore\Model\BinaryResponse;
|
|
||||||
use Menking\Meshcore\Model\ChannelResponse;
|
use Menking\Meshcore\Model\ChannelResponse;
|
||||||
use Menking\Meshcore\Model\CodeSentResponse;
|
use Menking\Meshcore\Model\CodeSentResponse;
|
||||||
use Menking\Meshcore\Model\CodeStatusResponse;
|
use Menking\Meshcore\Model\CodeStatusResponse;
|
||||||
use Menking\Meshcore\Model\CodeStatusTypeCoreResponse;
|
|
||||||
use Menking\Meshcore\Model\CodeStatusTypePacketResponse;
|
|
||||||
use Menking\Meshcore\Model\CodeStatusTypeRadioResponse;
|
|
||||||
use Menking\Meshcore\Model\CurrentTimeResponse;
|
use Menking\Meshcore\Model\CurrentTimeResponse;
|
||||||
use Menking\Meshcore\Model\DeviceInfoResponse;
|
use Menking\Meshcore\Model\DeviceInfoResponse;
|
||||||
use Menking\Meshcore\Model\MessageResponse;
|
use Menking\Meshcore\Model\MessageResponse;
|
||||||
|
use Menking\Meshcore\Model\NoMoreMessagesResponse;
|
||||||
use Menking\Meshcore\Model\OkResponse;
|
use Menking\Meshcore\Model\OkResponse;
|
||||||
|
|
||||||
class Meshcore {
|
class Meshcore {
|
||||||
@@ -48,7 +45,7 @@ class Meshcore {
|
|||||||
public function appStart(string $app_name): ?AppStartResponse {
|
public function appStart(string $app_name): ?AppStartResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_APP_START) . chr(0x00) . " " . $app_name);
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_APP_START) . chr(0x00) . " " . $app_name);
|
||||||
|
|
||||||
return $this->waitForResponse(AppStartResponse::class);
|
return $this->waitForResponse([AppStartResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -58,7 +55,7 @@ class Meshcore {
|
|||||||
public function getBatteryAndStorage(): ?BatteryStorageResponse {
|
public function getBatteryAndStorage(): ?BatteryStorageResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_BATT_AND_STORAGE));
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_BATT_AND_STORAGE));
|
||||||
|
|
||||||
return $this->waitForResponse(BatteryStorageResponse::class);
|
return $this->waitForResponse([BatteryStorageResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,7 +73,7 @@ class Meshcore {
|
|||||||
public function getDeviceInfo(): ?DeviceInfoResponse {
|
public function getDeviceInfo(): ?DeviceInfoResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_DEVICE_QUERY) . 0x03);
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_DEVICE_QUERY) . 0x03);
|
||||||
|
|
||||||
return $this->waitForResponse(DeviceInfoResponse::class);
|
return $this->waitForResponse([DeviceInfoResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,7 +84,7 @@ class Meshcore {
|
|||||||
public function getChannel(int $channel_idx): ?ChannelResponse {
|
public function getChannel(int $channel_idx): ?ChannelResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_CHANNEL) . chr($channel_idx));
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_CHANNEL) . chr($channel_idx));
|
||||||
|
|
||||||
return $this->waitForResponse(ChannelResponse::class);
|
return $this->waitForResponse([ChannelResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -108,7 +105,7 @@ class Meshcore {
|
|||||||
|
|
||||||
CoreProtocol::writeFrame($this->serial, $payload);
|
CoreProtocol::writeFrame($this->serial, $payload);
|
||||||
|
|
||||||
return $this->waitForResponse(OkResponse::class);
|
return $this->waitForResponse([OkResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -140,7 +137,7 @@ class Meshcore {
|
|||||||
public function getNextMessage(): ?MessageResponse {
|
public function getNextMessage(): ?MessageResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SYNC_NEXT_MESSAGE));
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SYNC_NEXT_MESSAGE));
|
||||||
|
|
||||||
return $this->waitForResponse(MessageResponse::class);
|
return $this->waitForResponse([MessageResponse::class, NoMoreMessagesResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -153,7 +150,7 @@ class Meshcore {
|
|||||||
$payload = chr(CoreProtocol::CMD_SEND_CHANNEL_TXT_MSG) . chr(0x00) . chr($channel_idx) . pack('V', time()) . "$message\0";
|
$payload = chr(CoreProtocol::CMD_SEND_CHANNEL_TXT_MSG) . chr(0x00) . chr($channel_idx) . pack('V', time()) . "$message\0";
|
||||||
CoreProtocol::writeFrame($this->serial, $payload);
|
CoreProtocol::writeFrame($this->serial, $payload);
|
||||||
|
|
||||||
return $this->waitForResponse(CodeSentResponse::class);
|
return $this->waitForResponse([CodeSentResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -163,7 +160,7 @@ class Meshcore {
|
|||||||
public function getDeviceTime(): ?CurrentTimeResponse {
|
public function getDeviceTime(): ?CurrentTimeResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_DEVICE_TIME));
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_DEVICE_TIME));
|
||||||
|
|
||||||
return $this->waitForResponse(CurrentTimeResponse::class);
|
return $this->waitForResponse([CurrentTimeResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -173,7 +170,7 @@ class Meshcore {
|
|||||||
public function setDeviceTime(): ?OkResponse {
|
public function setDeviceTime(): ?OkResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SET_DEVICE_TIME) . pack('V', time()));
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SET_DEVICE_TIME) . pack('V', time()));
|
||||||
|
|
||||||
return $this->waitForResponse(OkResponse::class);
|
return $this->waitForResponse([OkResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -187,7 +184,7 @@ class Meshcore {
|
|||||||
|
|
||||||
CoreProtocol::writeFrame($this->serial, $payload);
|
CoreProtocol::writeFrame($this->serial, $payload);
|
||||||
|
|
||||||
return $this->waitForResponse(CodeSentResponse::class);
|
return $this->waitForResponse([CodeSentResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -198,7 +195,7 @@ class Meshcore {
|
|||||||
public function disconnect(string $contact_pk): ?OkResponse {
|
public function disconnect(string $contact_pk): ?OkResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_LOGOUT) . base64_decode($contact_pk) . "\0");
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_LOGOUT) . base64_decode($contact_pk) . "\0");
|
||||||
|
|
||||||
return $this->waitForResponse(OkResponse::class);
|
return $this->waitForResponse([OkResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -211,7 +208,7 @@ class Meshcore {
|
|||||||
|
|
||||||
CoreProtocol::writeFrame($this->serial, $payload);
|
CoreProtocol::writeFrame($this->serial, $payload);
|
||||||
|
|
||||||
return $this->waitForResponse(CodeSentResponse::class);
|
return $this->waitForResponse([CodeSentResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -222,7 +219,7 @@ class Meshcore {
|
|||||||
public function connected(string $contact_pk): ?OkResponse {
|
public function connected(string $contact_pk): ?OkResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_HAS_CONNECTION) . base64_decode($contact_pk) . "\0");
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_HAS_CONNECTION) . base64_decode($contact_pk) . "\0");
|
||||||
|
|
||||||
return $this->waitForResponse(OkResponse::class);
|
return $this->waitForResponse([OkResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -233,7 +230,7 @@ class Meshcore {
|
|||||||
public function getTelemetryRequest(string $contact_pk): ?OkResponse {
|
public function getTelemetryRequest(string $contact_pk): ?OkResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SEND_TELEMETRY_REQ) . base64_decode($contact_pk));
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SEND_TELEMETRY_REQ) . base64_decode($contact_pk));
|
||||||
|
|
||||||
return $this->waitForResponse(OkResponse::class);
|
return $this->waitForResponse([OkResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -244,7 +241,7 @@ class Meshcore {
|
|||||||
public function sendAnonRequest(string $contact_pk): ?OkResponse {
|
public function sendAnonRequest(string $contact_pk): ?OkResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SEND_ANON_REQ) . base64_decode($contact_pk) . "\0");
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SEND_ANON_REQ) . base64_decode($contact_pk) . "\0");
|
||||||
|
|
||||||
return $this->waitForResponse(OkResponse::class);
|
return $this->waitForResponse([OkResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -255,7 +252,7 @@ class Meshcore {
|
|||||||
public function sendBinaryRequest(string $request): ?CodeSentResponse {
|
public function sendBinaryRequest(string $request): ?CodeSentResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SEND_BINARY_REQ) . $request);
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_SEND_BINARY_REQ) . $request);
|
||||||
|
|
||||||
return $this->waitForResponse(CodeSentResponse::class);
|
return $this->waitForResponse([CodeSentResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -266,7 +263,7 @@ class Meshcore {
|
|||||||
public function getAdvertPath(string $contact_pk): ?AdvertPathResponse {
|
public function getAdvertPath(string $contact_pk): ?AdvertPathResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_ADVERT_PATH) . chr(0x00) . base64_decode($contact_pk));
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_ADVERT_PATH) . chr(0x00) . base64_decode($contact_pk));
|
||||||
|
|
||||||
return $this->waitForResponse(AdvertPathResponse::class);
|
return $this->waitForResponse([AdvertPathResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -277,7 +274,7 @@ class Meshcore {
|
|||||||
public function getStats(int $type): ?CodeStatusResponse {
|
public function getStats(int $type): ?CodeStatusResponse {
|
||||||
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_STATS) . chr($type));
|
CoreProtocol::writeFrame($this->serial, chr(CoreProtocol::CMD_GET_STATS) . chr($type));
|
||||||
|
|
||||||
return $this->waitForResponse(CodeStatusResponse::class);
|
return $this->waitForResponse([CodeStatusResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setChannel(int $idx, string $name, string $secret): ?OkResponse {
|
public function setChannel(int $idx, string $name, string $secret): ?OkResponse {
|
||||||
@@ -292,7 +289,7 @@ class Meshcore {
|
|||||||
|
|
||||||
CoreProtocol::writeFrame($this->serial, $payload);
|
CoreProtocol::writeFrame($this->serial, $payload);
|
||||||
|
|
||||||
return $this->waitForResponse(OkResponse::class);
|
return $this->waitForResponse([OkResponse::class]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -330,7 +327,7 @@ class Meshcore {
|
|||||||
$this->serial = fopen(Environment::getDevice(), 'r+b');
|
$this->serial = fopen(Environment::getDevice(), 'r+b');
|
||||||
|
|
||||||
if( !$this->serial ) {
|
if( !$this->serial ) {
|
||||||
throw new RuntimeException("Could not open " . Environment::getDevice());
|
throw new \RuntimeException("Could not open " . Environment::getDevice());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,9 +337,11 @@ class Meshcore {
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
private function waitForResponse(string $class_expected): mixed {
|
private function waitForResponse(array $classes_expected): mixed {
|
||||||
if( !class_exists($class_expected) ) {
|
foreach($classes_expected as $class) {
|
||||||
throw new InvalidArgumentException("Class $class_expected does not exist");
|
if( !class_exists($class) ) {
|
||||||
|
throw new \InvalidArgumentException("Class $class does not exist");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$mark = time();
|
$mark = time();
|
||||||
@@ -351,14 +350,12 @@ class Meshcore {
|
|||||||
$response = CoreProtocol::readFrame($this->serial);
|
$response = CoreProtocol::readFrame($this->serial);
|
||||||
$obj = CoreParser::parseResponse($response);
|
$obj = CoreParser::parseResponse($response);
|
||||||
|
|
||||||
echo "D: got a " . get_class($obj) . ", was expecting $class_expected\n";
|
if( !in_array(get_class($obj), $classes_expected) ) {
|
||||||
|
|
||||||
if( get_class($obj) != $class_expected ) {
|
|
||||||
array_push($this->msg_queue, $obj);
|
array_push($this->msg_queue, $obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while((time() - $mark) < 5 && get_class($obj) != $class_expected);
|
while((time() - $mark) < 5 && !in_array(get_class($obj), $classes_expected));
|
||||||
|
|
||||||
return (get_class($obj) == $class_expected)?$obj:null;
|
return in_array(get_class($obj), $classes_expected)?$obj:null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user