<commit_before><?php
namespace Ehome\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Http\Client;
use Zend\Http\Request;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use Zend\Json\Json;

class RaspController extends AbstractActionController
{

    const ROUTE_LOGIN = 'zfcuser/login';

    protected $rasp_external_ip = '192.168.3.11';

    public function indexAction ()
    {
        if (! $this->zfcUserAuthentication()->hasIdentity()) { // check for
                                                               // valid
                                                               // session
            return $this->redirect()->toRoute(static::ROUTE_LOGIN);
        }
        
        $userEmail = $this->zfcUserAuthentication()
            ->getIdentity()
            ->getEmail();
        
        $apartment = $this->getApartmentState();
        
        return new ViewModel(
                array(
                        'apartment' => $apartment,
                        'useremail' => $userEmail
                ));
    }
    
    
    public function changeStateAction(){
        $room = $this->params()->fromRoute('room', '');
        $entry = $this->params()->fromRoute('entry', '');
        $state = $this->params()->fromRoute('state','');
        
        $apartment = $this->getApartmentState();
        $apartment[$room][$entry] = $state;
        
        $this->setApartmentState($apartment);
        
        $this->redirect()->toRoute('rasp');
        
        
    }
    
    private function setApartmentState($apartment){
        $client = new Client();
        
        $request = new Request();
        $request->setUri("http://" . $this->rasp_external_ip . ':8080');
        $request->setMethod(Request::METHOD_POST);
        
        $json = Json::encode($apartment);
        
        $request->setContent($json);
        
        $client->dispatch($request);
        
        
    }

    private function getApartmentState ()
    {
        $client = new Client();
        
        $request = new Request();
        $request->setUri("http://" . $this->rasp_external_ip . ':8080');
        $response = $client->dispatch($request);
        
        $json = $response->getBody();
        
        $apartment = Json::decode($json, Json::TYPE_ARRAY);
        return $apartment;
    }
}

?><commit_msg>added the interface for mobile clients<commit_after><?php
namespace Ehome\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Http\Client;
use Zend\Http\Request;
use Zend\View\Model\JsonModel;
use Zend\View\Model\ViewModel;
use Zend\Json\Json;

class RaspController extends AbstractActionController
{

    const ROUTE_LOGIN = 'zfcuser/login';

    protected $user = '<EMAIL>';

    protected $password = '<PASSWORD>';

    protected $rasp_external_ip = '192.168.127.12';

    public function indexAction ()
    {
        if (! $this->zfcUserAuthentication()->hasIdentity()) { // check for
                                                               // valid
                                                               // session
            return $this->redirect()->toRoute(static::ROUTE_LOGIN);
        }
        
        $userEmail = $this->zfcUserAuthentication()
            ->getIdentity()
            ->getEmail();
        
        $apartment = $this->getApartmentState(false);
        
        return new ViewModel(
                array(
                        'apartment' => $apartment,
                        'useremail' => $userEmail
                ));
    }

    public function mobileInterfaceAction ()
    {
        if ($_POST['src'] != 'mobile') {
            throw new \Exception('Connection attempt from unknown source.');
        }
        
        $pw = $_POST['pw_user'];
        $u = $_POST['email'];
        
        if ($pw != $this->password or $u != $this->user) {
            throw new \Exception('Connection attempt from unknown source.');
        }
        
        $func = $_POST['func'];
        
        switch ($func) {
            
            case 'login':
                return new JsonModel(
                        
                        array(
                                'response' => 'success'
                        ));
            
            case 'getState':
                return $this->getApartmentState(true);
            
            default:
                throw new \Exception('Connection attempt from unknown source.');
        }
    }

    public function changeStateAction ()
    {
        $request = $this->getRequest();
        
        if ($request->isPost()) {
            $pw = $_POST['pw_user'];
            $u = $_POST['email'];
            
            if ($pw != $this->password or $u != $this->user) {
                throw new \Exception('Connection attempt from unknown source.');
            }
        } else {
            if (! $this->zfcUserAuthentication()->hasIdentity()) { // check for
                                                                   // valid
                                                                   // session
                return $this->redirect()->toRoute(static::ROUTE_LOGIN);
            }
        }
        
        $room = $this->params()->fromRoute('room', '');
        $entry = $this->params()->fromRoute('entry', '');
        $state = $this->params()->fromRoute('state', '');
        
        $apartment = $this->getApartmentState(false);
        $apartment[$room][$entry] = $state;
        
        if ($this->setApartmentState($apartment)) {
            
            if ($request->isPost()){
                return new JsonModel($apartment);
            }else{
            return new JsonModel(
                    
                    array(
                            'response' => 'success'
                    ));}
        } else {
            return new JsonModel(
                    
                    array(
                            'response' => 'failure'
                    ));
        }
    }

    private function setApartmentState ($apartment)
    {
        $client = new Client();
        
        $request = new Request();
        $request->setUri("http://" . $this->rasp_external_ip . ':8080');
        $request->setMethod(Request::METHOD_POST);
        
        $apartment['Password'] = $<PASSWORD>;
        $apartment['User'] = $this->user;
        
        $json = Json::encode($apartment);
        
        $request->setContent($json);
        
        $response = $client->dispatch($request);
        
        $json = $response->getBody();
        
        $resp = Json::decode($json, Json::TYPE_ARRAY);
        
        return $resp['response'] == 'success';
    }

    private function getApartmentState ($mobile)
    {
        $client = new Client();
        
        $request = new Request();
        $request->getQuery()->set('Password', $<PASSWORD>);
        $request->getQuery()->set('User', $this->user);
        $request->setUri("http://" . $this->rasp_external_ip . ':8080');
        $request->setMethod(Request::METHOD_GET);
        $response = $client->dispatch($request);
        
        $json = $response->getBody();
        $apartment = Json::decode($json, Json::TYPE_ARRAY);
        if (! $mobile) {
            
            return $apartment;
        } else {
            return new JsonModel($apartment);
        }
    }
}

?>
{'fraction_non_alphanumeric': 0.13856969041503311, 'fraction_numerical': 0.0048668379072597, 'mean_word_length': 1.4431968295904887, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 88, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2008458', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1858, 'n_words': 346}