~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Service/PlayerActionService.php

  • Committer: Dan Garner
  • Date: 2015-03-26 14:08:33 UTC
  • Revision ID: git-v1:70d14044444f8dc5d602b99890d59dea46d9470c
Moved web servable files to web folder

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/*
3
 
 * Spring Signage Ltd - http://www.springsignage.com
4
 
 * Copyright (C) 2015 Spring Signage Ltd
5
 
 * (PlayerActionHelper.php)
6
 
 */
7
 
 
8
 
 
9
 
namespace Xibo\Service;
10
 
 
11
 
 
12
 
use Xibo\Entity\Display;
13
 
use Xibo\Exception\ConfigurationException;
14
 
use Xibo\Exception\InvalidArgumentException;
15
 
use Xibo\Helper\Environment;
16
 
use Xibo\XMR\PlayerAction;
17
 
use Xibo\XMR\PlayerActionException;
18
 
 
19
 
/**
20
 
 * Class PlayerActionService
21
 
 * @package Xibo\Service
22
 
 */
23
 
class PlayerActionService implements PlayerActionServiceInterface
24
 
{
25
 
    /**
26
 
     * @var ConfigServiceInterface
27
 
     */
28
 
    private $config;
29
 
 
30
 
    /** @var  LogServiceInterface */
31
 
    private $log;
32
 
 
33
 
    /** @var bool */
34
 
    private $triggerPlayerActions = true;
35
 
 
36
 
    /** @var string  */
37
 
    private $xmrAddress;
38
 
 
39
 
    /** @var array[PlayerAction] */
40
 
    private $actions = [];
41
 
 
42
 
    /**
43
 
     * @inheritdoc
44
 
     */
45
 
    public function __construct($config, $log, $triggerPlayerActions)
46
 
    {
47
 
        $this->config = $config;
48
 
        $this->log = $log;
49
 
        $this->triggerPlayerActions = $triggerPlayerActions;
50
 
    }
51
 
 
52
 
    /**
53
 
     * Get Config
54
 
     * @return ConfigServiceInterface
55
 
     */
56
 
    private function getConfig()
57
 
    {
58
 
        return $this->config;
59
 
    }
60
 
 
61
 
    /**
62
 
     * @inheritdoc
63
 
     */
64
 
    public function sendAction($displays, $action)
65
 
    {
66
 
        if (!$this->triggerPlayerActions)
67
 
            return;
68
 
 
69
 
        // XMR network address
70
 
        if ($this->xmrAddress == null)
71
 
            $this->xmrAddress = $this->getConfig()->GetSetting('XMR_ADDRESS');
72
 
 
73
 
        if (!is_array($displays))
74
 
            $displays = [$displays];
75
 
 
76
 
        // Check ZMQ
77
 
        if (!Environment::checkZmq())
78
 
            throw new ConfigurationException(__('ZeroMQ is required to send Player Actions. Please check your configuration.'));
79
 
 
80
 
        if ($this->xmrAddress == '')
81
 
            throw new InvalidArgumentException(__('XMR address is not set'), 'xmrAddress');
82
 
 
83
 
        // Send a message to all displays
84
 
        foreach ($displays as $display) {
85
 
            /* @var Display $display */
86
 
            if ($display->xmrChannel == '' || $display->xmrPubKey == '')
87
 
                throw new InvalidArgumentException(__('This Player is not configured or ready to receive push commands over XMR. Please contact your administrator.'), 'xmrRegistered');
88
 
 
89
 
            $displayAction = clone $action;
90
 
 
91
 
            try {
92
 
                $displayAction->setIdentity($display->xmrChannel, $display->xmrPubKey);
93
 
            } catch (\Exception $exception) {
94
 
                throw new InvalidArgumentException(__('Invalid XMR registration'), 'xmrPubKey');
95
 
            }
96
 
 
97
 
            // Add to collection
98
 
            $this->actions[] = $displayAction;
99
 
        }
100
 
    }
101
 
 
102
 
    /**
103
 
     * @inheritdoc
104
 
     */
105
 
    public function processQueue()
106
 
    {
107
 
        if (count($this->actions) > 0)
108
 
            $this->log->debug('Player Action Service is looking to send %d actions', count($this->actions));
109
 
        else
110
 
            return;
111
 
 
112
 
        // XMR network address
113
 
        if ($this->xmrAddress == null)
114
 
            $this->xmrAddress = $this->getConfig()->GetSetting('XMR_ADDRESS');
115
 
 
116
 
        $failures = 0;
117
 
 
118
 
        foreach ($this->actions as $action) {
119
 
            /** @var PlayerAction $action */
120
 
            try {
121
 
                // Send each action
122
 
                if ($action->send($this->xmrAddress) === false) {
123
 
                    $this->log->error('Player action refused by XMR (connected but XMR returned false).');
124
 
                    $failures++;
125
 
                }
126
 
 
127
 
            } catch (PlayerActionException $sockEx) {
128
 
                $this->log->error('Player action connection failed. E = ' . $sockEx->getMessage());
129
 
                $failures++;
130
 
            }
131
 
        }
132
 
 
133
 
        if ($failures > 0)
134
 
            throw new ConfigurationException(sprintf(__('%d of %d player actions failed'), $failures, count($this->actions)));
135
 
    }
136
 
}
 
 
b'\\ No newline at end of file'