<commit_before><?php

    namespace couchServe\Service\Lib;
    use couchServe\Service\Lib\WebSocket\Container;
    use couchServe\Service\Lib\Registries\ModuleRegistry;
    use couchServe\Service\Lib\Registries\SensorRegistry;
    use couchServe\Service\Lib\Registries\ControllerRegistry;

    class App {

        /**
         * @var ModuleRegistry
         */
        protected $moduleRegistry;

        /**
         * @var SensorRegistry
         */
        protected $sensorRegistry;

        /**
         * @var ControllerRegistry
         */
        protected $controllerRegistry;

        /**
         * @var Container
         */
        protected $webSocketContainer;

        /**
         * @param ModuleRegistry $moduleRegistry
         * @param SensorRegistry $sensorRegistry
         * @param ControllerRegistry $controllerRegistry
         * @param Container $webSocketContainer
         */
        public function __construct(
            ModuleRegistry $moduleRegistry,
            SensorRegistry $sensorRegistry,
            ControllerRegistry $controllerRegistry,
            Container $webSocketContainer
        ) {
            $this->moduleRegistry = $moduleRegistry;
            $this->sensorRegistry = $sensorRegistry;
            $this->controllerRegistry = $controllerRegistry;
            $this->webSocketContainer = $webSocketContainer;
        }

        public function run() {
            while ($this->stayAlive()) {
                $this->tick(new CommandPool);
            }
        }

        protected function tick(CommandPool $commandPool) {
            $this->collectSenses($commandPool);
            $this->collectStreamCommands($commandPool);
            $this->processSenses($commandPool);
            $this->processCommands($commandPool);
            $this->broadcastCommands($commandPool);
        }

        protected function collectSenses(CommandPool $commandPool) {
            foreach ($this->sensorRegistry->getSensors() as $sensor) {
                $sensor->sense();
                if ($sensor->valueHasChangedSinceLatestSense()) {
                    $commandPool->addSense($sensor->getCurrentSense());
                }
            }
        }

        protected function collectStreamCommands($commandPool) {
            $this->webSocketContainer->getHandler()->setCommandPool($commandPool);
            $this->webSocketContainer->getServer()->tick();
        }

        protected function processSenses(CommandPool $commandPool) {
            foreach ($this->controllerRegistry->getControllers() as $controller) {
                $controller->react($commandPool);
            }
        }

        protected function processCommands(CommandPool $commandPool) {
            $modules = $this->moduleRegistry->getModules();
            foreach ($commandPool->getCommands() as $command) {
                if (!isset($modules[$command->getTargetId()])) {
                    continue;
                }
                $modules[$command->getTargetId()]->act($command);
            }
        }

        protected function broadcastCommands(CommandPool $commandPool) {
            foreach ($commandPool->getSenses() as $sense) {
                $this->webSocketContainer->getHandler()->broadcast([
                    'type' => 'UPDATE',
                    'component' => 'SENSOR',
                    'target' => $sense->getSensor()->getId(),
                    'data' => [
                        'value' => $sense->getValue(),
                        'time' => $sense->getSenseTime()->format('c')
                    ]
                ]);
            }
        }

        protected function stayAlive() {
            return true;
        }
    }<commit_msg>adds command broadcast routine<commit_after><?php

    namespace couchServe\Service\Lib;
    use couchServe\Service\Lib\WebSocket\Container;
    use couchServe\Service\Lib\Registries\ModuleRegistry;
    use couchServe\Service\Lib\Registries\SensorRegistry;
    use couchServe\Service\Lib\Registries\ControllerRegistry;

    class App {

        /**
         * @var ModuleRegistry
         */
        protected $moduleRegistry;

        /**
         * @var SensorRegistry
         */
        protected $sensorRegistry;

        /**
         * @var ControllerRegistry
         */
        protected $controllerRegistry;

        /**
         * @var Container
         */
        protected $webSocketContainer;

        /**
         * @param ModuleRegistry $moduleRegistry
         * @param SensorRegistry $sensorRegistry
         * @param ControllerRegistry $controllerRegistry
         * @param Container $webSocketContainer
         */
        public function __construct(
            ModuleRegistry $moduleRegistry,
            SensorRegistry $sensorRegistry,
            ControllerRegistry $controllerRegistry,
            Container $webSocketContainer
        ) {
            $this->moduleRegistry = $moduleRegistry;
            $this->sensorRegistry = $sensorRegistry;
            $this->controllerRegistry = $controllerRegistry;
            $this->webSocketContainer = $webSocketContainer;
        }

        public function run() {
            while ($this->stayAlive()) {
                $this->tick(new CommandPool);
            }
        }

        protected function tick(CommandPool $commandPool) {
            $this->collectSenses($commandPool);
            $this->collectStreamCommands($commandPool);
            $this->processSenses($commandPool);
            $this->processCommands($commandPool);
            $this->broadcastCommands($commandPool);
        }

        protected function collectSenses(CommandPool $commandPool) {
            foreach ($this->sensorRegistry->getSensors() as $sensor) {
                $sensor->sense();
                if ($sensor->valueHasChangedSinceLatestSense()) {
                    $commandPool->addSense($sensor->getCurrentSense());
                }
            }
        }

        protected function collectStreamCommands($commandPool) {
            $this->webSocketContainer->getHandler()->setCommandPool($commandPool);
            $this->webSocketContainer->getServer()->tick();
        }

        protected function processSenses(CommandPool $commandPool) {
            foreach ($this->controllerRegistry->getControllers() as $controller) {
                $controller->react($commandPool);
            }
        }

        protected function processCommands(CommandPool $commandPool) {
            $modules = $this->moduleRegistry->getModules();
            foreach ($commandPool->getCommands() as $command) {
                if (!isset($modules[$command->getTargetId()])) {
                    continue;
                }
                $modules[$command->getTargetId()]->act($command);
            }
        }

        protected function broadcastCommands(CommandPool $commandPool) {
            foreach ($commandPool->getSenses() as $sense) {
                $this->webSocketContainer->getHandler()->broadcast([
                    'type' => 'UPDATE',
                    'component' => 'SENSOR',
                    'target' => $sense->getSensor()->getId(),
                    'data' => [
                        'value' => $sense->getValue(),
                        'time' => $sense->getSenseTime()->format('c')
                    ]
                ]);
            }

            foreach ($commandPool->getCommands() as $command) {
                $this->webSocketContainer->getHandler()->broadcast([
                    'type' => 'UPDATE',
                    'component' => 'MODULE',
                    'target' => $command->getModule()->getId(),
                    'options' => $command->getModule()->getModuleValue()->getExportableOptions()
                ]);
            }
        }

        protected function stayAlive() {
            return true;
        }
    }
{'fraction_non_alphanumeric': 0.11626130141347256, 'fraction_numerical': 0.0, 'mean_word_length': 1.771347918136909, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4954965', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1806, 'n_words': 298}