<?php

namespace App\Http\Controllers;

use App\MeasuringMachine;
use App\Package;
use Illuminate\Http\Request;
require_once '../app/library/baidu-api-speech/AipSpeech.php';
class MeasureMonitorController extends Controller
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $measuringMachines=MeasuringMachine::select('id','name','code','status')->get();
        $laravelEchoPrefix = config('database.redis.options.prefix');
        return view('weight.measureMonitor.index',compact('laravelEchoPrefix','measuringMachines'));
    }

    public function data(Request $request){
        $measuring_machine_id=$request->input('id');
        if ($measuring_machine_id){
            $package=Package::with('owner','paperBox','measuringMachine')->where('measuring_machine_id',$measuring_machine_id)->orderBy('id','DESC')->first();
            if (!$package){
                $measuringMachine=MeasuringMachine::where('id',$measuring_machine_id)->first();
                $package=new Package();
                if ($measuringMachine)$package->measuringMachine=$measuringMachine;
            }
            return $package;
        }
        return '';
    }


    public function speech(Request $request){
        $logistic=$request->input('logistic');
        if (!$logistic)return '';
        if (file_exists("storage/".$logistic.'.mp3')){
            return "/storage/".$logistic.'.mp3';
        }
        $client=new \AipSpeech(config('api.baidu.speech.APP_ID'),config('api.baidu.speech.API_KEY'),config('api.baidu.speech.SECRET_KEY'));
        $client->setConnectionTimeoutInMillis('180000');
        $client->setSocketTimeoutInMillis('180000');

        $result = $client->synthesis($logistic, 'zh', 1, array(
            'vol' => 15,
        ));
        // 识别正确返回语音二进制 错误则返回json 参照下面错误码
        if(!is_array($result)){
            file_put_contents('storage/'.$logistic.'.mp3', $result);
        }
        return "/storage/".$logistic.'.mp3';
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
