“天猫精灵”
“哎,我在!”
“打开电脑”
上了一天班,回到家就想静静的玩会游戏,吼一句天猫精灵,立马帮你开机,多么舒服装x的体验。然而现实是骨感的,精灵无此功能,网上有人让弄个智能插排+通电自启,这明显无法满足我们的高逼格。
文章目录
前言
准备工作:
- 外部网络可以访问的一个接口,用于逻辑处理,如果你的服务器外网不能访问,就不要整php+nginx,别想着在虚拟机里面搭个,没用!
- 电脑有外网ip,可以外部魔术包唤醒,网卡方面需要支持,一般网卡都支持
- 博主搭建的php+nginx可以提供给有兴趣的小伙伴(之前买的服务器已经到期了,所以暂时无法提供给小伙伴)
- 双11阿里云针对新人的活动真心不错,80元1核2G1M的服务器用来练练手,着实不错
PHP + Nginx 整合CentOS8.0
安装Nginx
dnf install -y nginx #安装nginx
cd /etc/nginx
## 备份nginx.conf
cp nginx.conf nginx.conf_`date +%Y%m%d`
## grep -Ev '#|^%|^$' nginx.conf_`date +%Y%m%d` > nginx.conf
## 新增index.php用于测试
echo -e "<?php \nphpinfo();\n?>" > /usr/share/nginx/html/index.php
## 赋权
chown nginx:nginx -R /usr/share/nginx/html
vi /etc/nginx/nginx.conf
## 参照下面conf进行修改
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
root /usr/share/nginx/html;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 8765 default_server;
listen [::]:8765 default_server;
server_name _;
include /etc/nginx/default.d/*.conf;
location / {
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
安装PHP
dnf install -y php #安装php
dnf install -y php-json #安装php-json
启动服务
## 服务启动php和nginx
systemctl start nginx
systemctl start php-fpm
## 服务器检查
# ps -ef|grep nginx
# ps -ef|grep php-fpm
## 停止命令
# systemctl stop nginx
# systemctl stop php-fpm
## 重启命令
# systemctl restart nginx
# systemctl restart php-fpm
整合结果检查
看到这个页面说明整合完成
编写wol.php用于处理逻辑
cd /usr/share/nginx/html
vi wol.php
<?php
$tmpData = strval(file_get_contents("php://input"));
$dataArray = json_decode($tmpData, true);
if($dataArray == null || $_SERVER['HTTP_HOSTNAME'] == null){
header('location: /404.html');
}else{
$hostName = $_SERVER['HTTP_HOSTNAME'];
$ip = $_SERVER['HTTP_IP'];
if($ip == "null"){
$ip = gethostbyname($hostName);
}
$request = $dataArray["slotEntities"];
$skillName = $dataArray["skillName"];
if($skillName == "打开电脑"){
if($request[0]["intentParameterName"] == "place"){
//查位置
$place = $request[0]["slotValue"];
//确认电脑的其他信息
$customArray = explode("|",$request[1]["slotValue"]);
for($i=0;$i<count($customArray);$i++){
if((explode(",",$customArray[$i]))[0] == $place){
$placeArray = explode(",",$customArray[$i]);
}
}
}else{
//查位置
$place = $request[1]["slotValue"];
//确认电脑的其他信息
$customArray = explode("|",$request[0]["slotValue"]);
for($i=0;$i<count($customArray);$i++){
if((explode(",",$customArray[$i]))[0] == $place){
$placeArray = explode(",",$customArray[$i]);
}
}
}
$placeArray[0] = $ip;
/*
* 生成唤醒包
*/
$addr_byte = explode(':', $placeArray[1]);
$hw_addr = '';
for ($a=0; $a<6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
for ($a=1; $a<=16; $a++) $msg .= $hw_addr;
$handle = stream_socket_client('udp://'.$placeArray[0].':'.$placeArray[2],$errno,$errstr);
if (!$handle) {
die('ERROR:'.$errno. '- '.$errstr.'\n');
}
fwrite($handle,$msg);
fclose($handle);
echo json_encode(array('returnCode'=>'0','returnValue'=>array('reply'=>$place.'的电脑已经打开了','resultType'=>'RESULT','executeCode'=>'SUCCESS')));
}else{
header('location: /404.html');
}
}
?>
前面的操作主要用于逻辑判断,根据个人喜欢,也可以用java,目的很简单,当请求/wol.php页面时候,向目标服务器发送魔术包,wol.php中写了魔术包的生成,以及从Header中获取地址,mac信息
Aligenie 开发者平台
https://iap.aligenie.com/home
需要自行注册为开发者,填写自己的个人信息等等
创建技能
技能调用词,如果选择无调用词,天猫精灵无法语音识别,需要修改为有调用词,名字设置为“打开电脑”。技能创建完成后,能力申请页可以不做调整
创建实体
我们先创建实体,然后创建意图
创建了两个实体custom和place
位置实体中设置两个变量“卧室”和“客厅”,这个可以自己根据自己的情况进行调整,后续会说到意图里面传参的问题,这个要和自定义实体对应上,不要自定义里面有三个位置信息,而这里只有两个,会导致不匹配
自定义实体中没有写任何实体值,因为这个实体目的是用于往逻辑服务器传值,我们只需要在意图里面加上默认值即可
创建意图
创建一个名为唤醒电脑的意图
设置单轮对话
此处是重点:
- 取消精灵追问,因为过程太过简单,只要一次通话即可解决所有问题
- 位置实体设置一个默认值
- 自定义实体的默认值,如果用我的wol.php,请参照这个例子填写,位置不要乱,内容是: 卧室,00:1B:00:40:00,3453|客厅,00:1C:00:04:00:D2,3454
位置信息,mac地址,端口 如果有多条用竖线分开,并且要保证和位置实体对应的上,例如有三个位置电脑:
卧室,00:00:00:00:00,123|客厅,00:00:00:00:00,124|书房,00:00:00:00:00,125 对应的实体取值应该是:卧室|客厅|书房
设置回复逻辑
选择默认逻辑WEBHOOK
下载认证文件,将txt存放至/usr/share/nginx/html/aligenie,具体教程官方文档也有https://www.aligenie.com/doc/357834/xwndex
url:对外开放的接口地址
hostname:需要开机的外网访问地址
ip:没有固定ip可以为null
点击提交后会对接口地址进行认证(如果目前没有外部服务器,可以私信我)(之前买的服务器已经到期了,所以暂时无法提供给小伙伴)
测试
在线测试几次后就可以真机测试,自己使用,无需发布
纯手打,各位看官给个赞吧