diff --git a/application/controllers/Dashboard.php b/application/controllers/Dashboard.php index e315a4cab..32813d366 100644 --- a/application/controllers/Dashboard.php +++ b/application/controllers/Dashboard.php @@ -172,7 +172,8 @@ class Dashboard extends CI_Controller { } $data['total_countries_needed'] = count($dxcc->result()) - $current; - + $this->load->library('Mh'); + $this->mh->wl_event('web/dashboard/'.$this->session->userdata('user_id'), json_encode($data)); $this->load->view('interface_assets/header', $data); $this->load->view('dashboard/index'); $this->load->view('interface_assets/footer', $footerData); diff --git a/application/libraries/Mh.php b/application/libraries/Mh.php new file mode 100644 index 000000000..5181a4c76 --- /dev/null +++ b/application/libraries/Mh.php @@ -0,0 +1,62 @@ +ci = & get_instance(); + $this->mqsettings['server']=($this->ci->config->item('mqtt_server') ?? ''); + $this->mqsettings['port']=($this->ci->config->item('mqtt_port') ?? 1883); + $this->mqsettings['prefix']=($this->ci->config->item('mqtt_prefix') ?? 'wavelog/'); + } + + public function connect() { + $server = $this->mqsettings['server']; + $port = $this->mqsettings['port']; + $clientId = uniqid('wl_'); + + if ($this->mqsettings['server'] != '') { + try { + $this->mqtt = @new Wavelog\phpMQTT($server, $port, $clientId); + + if (!@$this->mqtt->connect(true, NULL)) { + throw new Exception('Failed to connect to MQTT broker'); + } + register_shutdown_function([$this, 'disconnect']); + return true; + } catch (Exception $e) { + log_message('error','Error while trying to connect to MQTT: '.$e->getMessage()); + $this->mqtt=false; + } + } + } + + public function disconnect() { + if ($this->mqtt) { + log_message('error', 'disconnect from MQTT broker'); + $this->mqtt->close(); + } + } + + public function wl_event($topic, $message) { + if ($this->mqsettings['server'] != '') { + if (!($this->mqtt)) { + $this->connect(); + } + $this->publish($this->mqsettings['prefix'].$topic, $message); + } + } + + private function publish($topic, $message) { + if ($this->mqtt) { + $this->mqtt->publish($topic, $message, 0); + } else { + log_message('error', 'Cannot publish message: MQTT connection not established'); + } + } +}