Pages.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 04/12/2018
  5. * @time 15:22
  6. */
  7. namespace OpenMind\Admin;
  8. use Exception;
  9. use OpenMind\FindBox\FindBoxCoreSystem;
  10. use OpenMind\FindBox\SmartESL;
  11. use OpenMind\System\Notifications;
  12. use OpenMind\Web\Controller;
  13. use Template;
  14. class Pages extends Controller
  15. {
  16. private $findBoxCore = null;
  17. /**
  18. * Pages constructor.
  19. * @param \Base $f3
  20. */
  21. public function __construct($f3)
  22. {
  23. parent::__construct($f3);
  24. try {
  25. $this->findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb')));
  26. } catch (\Exception $e) {
  27. $ns = new Notifications();
  28. $ns->notify(Notifications::FB_ADMIN, $f3->clean($e->getMessage()));
  29. // IF can't connect to FINDBOX, reroute to OMLINK Page Settings to fast change settings
  30. if ($f3->get('PATTERN') != '/admin/omlsettings')
  31. $f3->reroute('/admin/omlsettings');
  32. }
  33. // Set Admin F3 UI folder
  34. $f3->set('UI', 'ui/admin/');
  35. }
  36. public function Admin($f3)
  37. {
  38. echo Template::instance()->render('masterpage.tpl');
  39. }
  40. public function ESLStatus($f3)
  41. {
  42. $eslStatus = $this->findBoxCore->GetAllESLStatus();
  43. $f3->set('STATUS_BADGE_COLOR', [
  44. 'OFFLINE' => 'warning',
  45. 'BLOCKED,FC' => 'danger',
  46. 'NO_STATUS' => 'light',
  47. 'OK' => 'success'
  48. ]);
  49. $f3->set('BATLEV_ICONS', [
  50. 'GOOD' => 'full', // 100% - 60%
  51. 'LOW' => 'half', // 60% - 30%
  52. 'BAD' => 'empty', // 30% - 0%
  53. ]);
  54. $f3->set('ESL_STATUS', $eslStatus);
  55. $f3->set('PAGENAME', 'Dashboard');
  56. $f3->set('CONTENT', 'labelstatus.tpl');
  57. echo Template::instance()->render('masterpage.tpl');
  58. }
  59. public function OML_Settings($f3)
  60. {
  61. global $frameworkUtils;
  62. $path = 'cfg/config.bin';
  63. if (!file_exists($path))
  64. throw new Exception('Config file not found');
  65. $data = file_get_contents($path);
  66. $config = $frameworkUtils->Decrypt($data);
  67. $f3->set('PAGENAME', 'OM-Link Settings');
  68. $f3->set('CONTENT', 'omlsettings.tpl');
  69. $f3->set('CONFIG_TEXT', $config);
  70. echo Template::instance()->render('masterpage.tpl');
  71. }
  72. public function Config($f3)
  73. {
  74. global $frameworkUtils;
  75. $config = $f3->get('BODY');
  76. if (strlen($config) == 0) {
  77. echo json_encode([
  78. "err" => "Empty body command"
  79. ]);
  80. return;
  81. }
  82. $configOBJ = json_decode($config);
  83. switch ($configOBJ->method) {
  84. case 'saveconfig':
  85. $newConfig = $frameworkUtils->Encrypt($configOBJ->value);
  86. @rename('cfg/config.bin', 'cfg/config.bin.old');
  87. @file_put_contents('cfg/config.bin', $newConfig);
  88. echo json_encode([
  89. "err" => "Configuration saved"
  90. ]);
  91. break;
  92. case 'loadconfig':
  93. $config = $frameworkUtils->Decrypt(file_get_contents('cfg/config.bin'));
  94. echo json_encode([
  95. "err" => "Configuration reloaded successfully",
  96. "value" => $config
  97. ]);
  98. break;
  99. case 'geteslstatus':
  100. $labelID = $configOBJ->label_id;
  101. $esl = new SmartESL($this->findBoxCore, [$labelID]);
  102. $status = $esl->GetStatus(true);
  103. $svgPath = $this->findBoxCore->GetTemplateImage($status[$configOBJ->label_id]['data']['template_id']);
  104. echo json_encode([
  105. "err" => "Status reloaded successfully",
  106. "value" => $status[$configOBJ->label_id],
  107. "svgPath" => $svgPath
  108. ]);
  109. break;
  110. case 'blinklabels':
  111. $esl = new SmartESL($this->findBoxCore, [$configOBJ->labelid]);
  112. $ret = $esl->BlinkSpecial(2, 1, 1000, 5, 0);
  113. if ($ret['err'] == 0 && $ret['value']['code'] == 0) {
  114. if ($ret['value']['value'][$configOBJ->labelid])
  115. echo json_encode([
  116. "err" => 0,
  117. "value" => "{$configOBJ->labelid} blink successfull"
  118. ]);
  119. else
  120. echo json_encode([
  121. "err" => 1,
  122. "value" => "{$configOBJ->labelid} blink error"
  123. ]);
  124. }
  125. break;
  126. case 'unbind':
  127. $ret = $this->findBoxCore->UnBind($configOBJ->labelid);
  128. if ($ret['err'] != 0) {
  129. echo json_encode($ret);
  130. return;
  131. }
  132. echo json_encode([
  133. "err" => 0,
  134. "value" => "{$configOBJ->labelid} unbind successfull"
  135. ]);
  136. break;
  137. case 'resetfailcounter':
  138. $URI = "/1.0.0/index.php/smartesl.resetfailcounter/1.0.0/[[\"{$configOBJ->labelid}\"]]";
  139. $this->findBoxCore->CallAPI($URI, "");
  140. echo json_encode([
  141. "err" => 0,
  142. "value" => "{$configOBJ->labelid} Reset Fail Counter successfull"
  143. ]);
  144. break;
  145. case 'TemplateSelector':
  146. $this->TemplateSelector($f3, $configOBJ);
  147. break;
  148. case 'SetDisplay':
  149. foreach($configOBJ->labels as $labelID) {
  150. $esl = new SmartESL($this->findBoxCore, [$labelID]);
  151. $esl->Display($configOBJ->id, $labelID);
  152. }
  153. echo json_encode([
  154. "err" => "Display command successfull"
  155. ]);
  156. break;
  157. }
  158. return;
  159. }
  160. private function TemplateSelector($f3, $Command)
  161. {
  162. global $f3, $frameworkUtils;
  163. $fb_template = $this->findBoxCore->CallAPI('/1.0.0/index.php/smartesl.gettemplates/1.0.0/', '[]');
  164. $templates = $this->findBoxCore->getLabelsFromResponse($fb_template);
  165. if ($Command->clearCache == 1)
  166. $frameworkUtils->rrmdir("fbtpl");
  167. $tplFileNames = [];
  168. if ($templates != null) {
  169. foreach($templates as $item) {
  170. if (!in_array($item['resolution'], $Command->resolution))
  171. continue;
  172. if (!file_exists("fbtpl/{$item['template_id']}.svg"))
  173. $this->findBoxCore->GetTemplateImage($item['template_id']);
  174. $tplFileNames[] = [
  175. "id" => $item['template_id'],
  176. "name" => $item['name'],
  177. "resolution" => $item['resolution'],
  178. "title" => $item['name'],
  179. "svg" => file_get_contents("fbtpl/{$item['template_id']}.svg")
  180. ];
  181. }
  182. }
  183. $f3->set('TPLOBJ', $tplFileNames);
  184. $tpl = Template::instance()->render('templates.tpl');
  185. echo json_encode([
  186. "err" => 0,
  187. "value" => $tpl
  188. ]);
  189. }
  190. }