cxfoot/commands/HelloController.php
2023-10-27 14:25:12 +08:00

248 lines
7.3 KiB
PHP

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace app\commands;
use app\models\BallCart;
use app\models\BallMark;
use app\modules\api\components\Mqtt;
use yii\console\Controller;
use yii\console\ExitCode;
use \PhpMqtt\Client\MQTTClient;
use \PhpMqtt\Client\ConnectionSettings;
use app\models\Store;
/**
* This command echoes the first argument that you have entered.
*
* This command is provided as an example for you to learn how to create console commands.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class HelloController extends Controller
{
/**
* This command echoes what you have entered as the message.
* @param string $message the message to be echoed.
* @return int Exit code
*/
public function actionIndex($message = 'hello world')
{
$query = BallCart::find()->alias('ball')
->leftJoin(['store' => Store::tableName()], 'ball.store_id=store.id')
->leftJoin(['mark' => BallMark::tableName()], 'ball.mark_id=mark.id')
->select('ball.id,ball.cover_pic,ball.money,mark.name as mark_name,mark.id as mard_id,ball.ball_number,ball.status,store.name')
->where([
'ball.is_delete' => 0,
])->andWhere(['!=', 'ball.status', 2])
->andFilterWhere([
'ball.store_id' => 1
]);
var_dump($query->createCommand()->getRawSql());
exit();
/*$min = 1750; # 目标积分
$time = 356; # 剩余分钟
$a_g = 403;# 当前积分
$b_g = 1080;# 当前积分
$a = 1.5*1.2; # a位置积分
$b = 4*1.2; # b位置积分
$c = ($b-$a)/$b; # 分差
$b_t = ($min - $b_g)/$b;# 所需时间
$b_t_t = $time-$b_t;# 剩余时间
$d = ($b_t_t - $b_t)/$b_t_t);
// $c
$res = $b_t-(($b_t_t*(($c)));
var_dump($res);
if($res < 0){
$res = 0;
}
var_dump($b_t);
var_dump($b_t_t);
var_dump("b:".(($b*$res)+($a*$time-$res)+$b_g));
var_dump("a:".(($a*$res)+($b*$time-$res)+$a_g));
exit();*/
$obj = new Mqtt();
$res = $obj->sendOpen('20221000898',1,1);
// $res = $obj->getInfo('20221000898',1);
var_dump($res);
}
public function x_o($data){
$t = 0;
for($i=0;$i<strlen($data);$i++){
if($i){
$t ^= ord($data[$i]);
}else{
$t = ord($data[$i]) ^ 0;
}
}
return $t;
}
/**
* @param $string
* @param int $len
* @return array
* 字符串根据字节长度拆分为数组
*/
public function mbStrSplit($string, $len = 1)
{
$start = 0;
$strlen = strlen($string);
while ($strlen) {
$array[] = ord(strtoupper(substr($string, $start, $len)));
$strlen--;
}
return $array;
}
/**
* # +========================================================================
* # | - @name hex数据BBC异或校验(两两比较)
* # | - @author cq <just_leaf@foxmail.com>
* # | - @copyright zmtek 2020-11-24
* # +------------------------------------------------------------------------
* # | - 返回结果
* # +========================================================================
*/
public function hexOr($byte1, $byte2)
{
$result = '';
$byte1 = str_pad(base_convert($byte1, 16, 2), '8', '0', STR_PAD_LEFT);
$byte2 = str_pad(base_convert($byte2, 16, 2), '8', '0', STR_PAD_LEFT);
$len1 = strlen($byte1);
for ($i = 0; $i < $len1 ; $i++) {
$result .= $byte1[$i] == $byte2[$i] ? '0' : '1';
}
return strtoupper(base_convert($result, 2, 16));
}
/**
* # +========================================================================
* # | - @name hex数据BBC异或校验(多个hex数据进行校验)
* # | - @author cq <just_leaf@foxmail.com>
* # | - @copyright zmtek 2020-11-24
* # +------------------------------------------------------------------------
* # | - 返回结果
* # +========================================================================
*/
public function hexOrArr($data)
{
$result = $data[0];
for ($i = 0; $i < count($data) - 1; $i++) {
$result = $this->hexOr($result, $data[$i + 1]);
}
return $result;
}
/**
* 转换一个String字符串为byte数组
* @param $str 需要转换的字符串
* @param $bytes 目标byte数组
* @author Zikie
*/
public function getBytes(string $str)
{
$len = strlen($str);
$bytes = [];
for ($i = 0; $i < $len; $i++) {
if (ord($str[$i]) >= 128) {
$byte = ord($str[$i]) - 256;
} else {
$byte = ord($str[$i]);
}
$bytes[] = $byte;
}
return $bytes;
}
/**
* 将字节数组转化为String类型的数据
* @param $bytes 字节数组
* @param $str 目标字符串
* @return 一个String类型的数据
*/
public function toStr($bytes)
{
$str = '';
foreach ($bytes as $ch) {
$str .= chr($ch);
}
return $str;
}
/**
* 转换一个int为byte数组
* @param $byt 目标byte数组
* @param $val 需要转换的字符串
* @author Zikie
*/
public function integerToBytes($val)
{
$byt = [];
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
$byt[2] = ($val >> 16 & 0xff);
$byt[3] = ($val >> 24 & 0xff);
return $byt;
}
/**
* 从字节数组中指定的位置读取一个Integer类型的数据
* @param $bytes 字节数组
* @param $position 指定的开始位置
* @return 一个Integer类型的数据
*/
public function bytesToInteger($bytes, $position)
{
$val = 0;
$val = $bytes[$position + 3] & 0xff;
$val <<= 8;
$val |= $bytes[$position + 2] & 0xff;
$val <<= 8;
$val |= $bytes[$position + 1] & 0xff;
$val <<= 8;
$val |= $bytes[$position] & 0xff;
return $val;
}
/**
* 转换一个shor字符串为byte数组
* @param $byt 目标byte数组
* @param $val 需要转换的字符串
* @author Zikie
*/
public static function shortToBytes($val)
{
$byt = [];
$byt[0] = ($val & 0xff);
$byt[1] = ($val >> 8 & 0xff);
return $byt;
}
/**
* 从字节数组中指定的位置读取一个Short类型的数据。
* @param $bytes 字节数组
* @param $position 指定的开始位置
* @return 一个Short类型的数据
*/
public static function bytesToShort($bytes, $position)
{
$val = 0;
$val = $bytes[$position + 1] & 0xFF;
$val = $val << 8;
$val |= $bytes[$position] & 0xFF;
return $val;
}
}