Skip to content

Commit

Permalink
salto
Browse files Browse the repository at this point in the history
  • Loading branch information
rohsyl committed Oct 24, 2022
1 parent feb55b0 commit 9764b76
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 111 deletions.
19 changes: 0 additions & 19 deletions .scrutinizer.yml

This file was deleted.

5 changes: 0 additions & 5 deletions .styleci.yml

This file was deleted.

19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

All notable changes to `:package_name` will be documented in this file
All notable changes to `rohsyl/salto` will be documented in this file

## 1.0.0 - 201X-XX-XX
## 1.0.0 - 2022-11-10

- initial release
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
}
],
"require": {
"php": ">=7.4",
"ext-sockets": "*"
"php": ">=8.0",
"ext-sockets": "*",
"nesbot/carbon": "^2.62"
},
"require-dev": {
"symfony/var-dumper": "^4.3",
Expand Down Expand Up @@ -47,7 +48,7 @@
"rohsyl\\Salto\\SaltoServiceProvider"
],
"aliases": {
"Skeleton": "rohsyl\\Salto\\Salto"
"Salto": "rohsyl\\Salto\\Salto"
}
}
}
Expand Down
54 changes: 0 additions & 54 deletions configure-skeleton.sh

This file was deleted.

39 changes: 39 additions & 0 deletions src/Commands/SaltoClientCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace rohsyl\Salto\Commands;

use Illuminate\Console\Command;
use rohsyl\Salto\Message\EncodeMobileMessage;
use rohsyl\Salto\SaltoClient;
use Carbon\Carbon;

class SaltoClientCommand extends Command
{

protected $signature = 'salto:client';

public function handle() {

$this->info('Salto Socket Client [Started]');

$client = new SaltoClient('127.0.0.1', 10001);
$client->openSocketConnection();

if(!$client->isReady()) return; // TODO wait if not ready ?
echo "ready\n";

$message = (new EncodeMobileMessage())
->phone('+41774539943') // #1
->for('101'); // #2

echo "Message : " . $message->toString() . "\n";

$response = $client->sendMessage(
$message
);

// ... do something with
// $response ...
}
}
83 changes: 83 additions & 0 deletions src/Commands/SaltoServerCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace rohsyl\Salto\Commands;

use Illuminate\Console\Command;

class SaltoServerCommand extends Command
{

protected $signature = 'salto:server';

public function handle() {

$this->info('Salto Socket Server [Started]');

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
* as it comes in. */
ob_implicit_flush();

$address = '127.0.0.1';
$port = 10001;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}
/* Send instructions.
$msg = "\nWelcome to the PHP Test Server. \n" .
"To quit, type 'quit'. To shut down the server type 'shutdown'.\n";
socket_write($msgsock, $msg, strlen($msg));*/

do {
if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "\n";
break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 0x05) {
socket_write($msgsock, 0x06);
}
if($buf[0] == 0x02) {

echo 'STX' . "\n";

socket_write($msgsock, 0x06);

socket_write($msgsock, join(0xB3, [
0x02, 'CNM', '+41774539943', '101', 0x03
]));
}
if ($buf == 'shutdown') {
socket_close($msgsock);
break 2;
}
//$talkback = "PHP: You said '$buf'.\n";
//socket_write($msgsock, $talkback, strlen($talkback));
echo "$buf\n";
} while (true);
} while (true);

socket_close($sock);
}
}
1 change: 1 addition & 0 deletions src/Message/EncodeMobileMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace rohsyl\Salto\Message;

use rohsyl\Salto\SaltoClient;
use Carbon\Carbon;

class EncodeMobileMessage extends Message
{
Expand Down
8 changes: 8 additions & 0 deletions src/Message/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,12 @@ public function getFrame() : array {

return [SaltoClient::STX, ...$this->getMessageArray(), SaltoClient::ETX, $lrc];
}

public function toString() {
$out = 'STX|';
foreach($this->trimEmptyFields($this->getFields()) as $field) {
$out .= $field . '|';
}
return $out . 'ETX ' . $this->getLrcChecksum();
}
}
42 changes: 42 additions & 0 deletions src/Response/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace rohsyl\Salto\Response;

use rohsyl\Salto\Salto;
use rohsyl\Salto\SaltoClient;

class Response
{
public static function Ack() {
return new self(SaltoClient::ACK);
}

public static function Nak() {
return new self(SaltoClient::NAK);
}

protected $rawResponse;
protected $checksum;

public function __construct($rawResponse, $checksum = null)
{
$this->rawResponse = $rawResponse;
$this->checksum = $checksum;
}

public function isAck() {
return $this->rawResponse === SaltoClient::ACK;
}

public function isNak() {
return $this->rawResponse === SaltoClient::NAK;
}

public function isMessage() {
return $this->rawResponse[0] === SaltoClient::STX;
}

public function raw() {
return $this->rawResponse;
}
}
21 changes: 16 additions & 5 deletions src/SaltoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace rohsyl\Salto;

use rohsyl\Salto\Message\Message;
use rohsyl\Salto\Response\Response;

class SaltoClient
{
Expand Down Expand Up @@ -44,10 +45,15 @@ public function openSocketConnection() {
}

public function isReady() {
return $this->sendRequest(self::ENQ) === self::ACK;
echo 'send enq';
return $this->sendRequest([self::ENQ, "\n"])->isAck();
}

public function sendRequest($frame) {
public function sendRequest(string|array $frame) : Response {

if(is_array($frame)) {
$frame = join($frame);
}

$this->socket->write($frame);

Expand All @@ -57,6 +63,8 @@ public function sendRequest($frame) {
return $responseAcknowledgement;
}

if($frame === self::ENQ) return $responseAcknowledgement;

if($responseAcknowledgement->isAck()) {
// server got the request and will process it
$requestResponse = $this->readResponse();
Expand Down Expand Up @@ -84,16 +92,19 @@ public function readResponse() {
$isChecksum = false;
$checksum = null;
do {
$byte = $this->socket->readByte();
$string = $this->socket->readByte();
$byte = intval($string);


if($byte === SaltoClient::ACK) {
echo 'ack';
// request will be processed
// return Response::Ack();
return Response::Ack();
}
if($byte === SaltoClient::NAK) {
echo 'nak';
// request wont be processed
// return Response::Nak();
return Response::Nak();
}

// are we already processing a frame ?
Expand Down
9 changes: 9 additions & 0 deletions src/SaltoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace rohsyl\Salto;

use Illuminate\Support\ServiceProvider;
use rohsyl\Salto\Commands\SaltoClientCommand;
use rohsyl\Salto\Commands\SaltoServerCommand;

class SaltoServiceProvider extends ServiceProvider
{
Expand All @@ -21,5 +23,12 @@ public function boot()
public function register()
{

if(!$this->app->isProduction() && $this->app->runningInConsole()) {
$this->commands([
SaltoServerCommand::class,
SaltoClientCommand::class
]);
}

}
}
Loading

0 comments on commit 9764b76

Please sign in to comment.