server.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. <?
  2. require_once 'CheckPoint.php';
  3. require_once "doctrine/bootstrap.php";
  4. use \CheckPoints\CheckPoint;
  5. ini_set('error_reporting', 'E_ALL');
  6. ini_set('display_errors', '1');
  7. ini_set('display_startup_errors', '1');
  8. //defined('YII_DEBUG') or define('YII_DEBUG', false);
  9. //defined('YII_ENV') or define('YII_ENV', 'prod');
  10. require_once 'api/vendor/autoload.php';
  11. require_once 'api/vendor/yiisoft/yii2/Yii.php';
  12. $config = require_once 'api/config/web.php';
  13. (new yii\web\Application($config));
  14. require_once 'auth.php';
  15. require_once 'functions.php';
  16. require_once 'ocs.php';
  17. $varstr = "";
  18. foreach (array_keys($_REQUEST) as $var)
  19. {
  20. ${$var}=$_REQUEST[$var];
  21. $varstr .= " $var: ${$var} ";
  22. //echo "<b>$var:</b> ${$var} <br>";
  23. }
  24. //if ($pnum != '11' )// && $pnum != '38'
  25. // WriteLog("request 2 server.php",$varstr);
  26. include 'problems.php';
  27. $num = intval($pnum);
  28. switch ($num) {
  29. case 1:
  30. Login($num, $id, $did , $tagid);
  31. break;
  32. case 2:
  33. CheckIn($num, $id);
  34. break;
  35. // case 3: requestccordinates position
  36. case 4:
  37. try {
  38. TaskList($num, $id, $verbose);
  39. } catch (\Throwable $e) {
  40. echo $e->getMessage();
  41. }
  42. break;
  43. case 5:
  44. ;
  45. break;
  46. case 6:
  47. try {
  48. TaskConfirm($num, $id, $tid, $confirm_data, $verbose);
  49. } catch (\Throwable $e) {
  50. echo $e->getMessage();
  51. }
  52. break;
  53. case 7:
  54. TaskStatusChange($num, $id, $tid);
  55. break;
  56. case 8:
  57. DeviceIsAuthorized($num, $did);
  58. break;
  59. case 9:
  60. DeviceAdd($num, $did);
  61. break;
  62. case 10:
  63. GetOnline($num, $id);
  64. break;
  65. case 11:
  66. Ping($num, $id);
  67. break;
  68. case 12:
  69. ObjPlace($num, $id, $pos, $scale);
  70. break;
  71. case 13:
  72. RequestObj($num, $id);
  73. break;
  74. //14 SendCMDName
  75. //15 SendJobnames
  76. case 16:
  77. TextNote($num, $taskid, $text, $cpid);
  78. break;
  79. case 17:
  80. //in server_post
  81. //ImageNote($num, $taskid, $img);
  82. break;
  83. case 18:
  84. TaskBegin($num, $tid, $echo, $testing);
  85. break;
  86. case 19:
  87. //in server_post
  88. //AudioNote($num, $taskid, $audio);
  89. break;
  90. case 20:
  91. ARObjectRecognized($num, $tid, $sid);
  92. break;
  93. case 21:
  94. try {
  95. TasksCancel($accid, $maintask, $cause);
  96. } catch (\Throwable $e) {
  97. echo $e->getMessage();
  98. }
  99. break;
  100. case 22:
  101. Quit($accid, $fromsite, $tagid);
  102. break;
  103. //TOERASE
  104. case 23:
  105. AcceptProblem($type, $comment);
  106. break;
  107. case 24:
  108. CheckProblem();
  109. break;
  110. case 25:
  111. ResolveProblemStart($id);
  112. break;
  113. case 26:
  114. EndProblem($id);
  115. break;
  116. case 27:
  117. ProblemsQuit($type);
  118. break;
  119. case 28:
  120. LoginChief($login, $p);
  121. break;
  122. case 29:
  123. CheckList($num, $tid);
  124. break;
  125. case 30:
  126. LoginPassword($num, $login, $p, $did, $ver, $app, $tagid);
  127. break;
  128. case 31:
  129. CheckPointAccept($id, $value);
  130. break;
  131. case 32:
  132. saveBeaconData($num, $location_id, $acc_id, $beacon_id, $rssi, $localtime, $pwr);
  133. break;
  134. case 33:
  135. WriteLog($accid, $text);
  136. break;
  137. case 34:
  138. Token($accid, $token);
  139. break;
  140. case 35:
  141. CheckUpdates($num, $ver, $app);
  142. break;
  143. case 36:
  144. OverPlan($num, $accid);
  145. break;
  146. case 37:
  147. GetMarkers($num, $locid);
  148. break;
  149. case 38:
  150. SendCoordinate($num, $pointx, $pointy, $timeStamp, $accId, $locationId, $zoneId);
  151. break;
  152. case 39:
  153. getTaskTypes($num);
  154. break;
  155. case 40:
  156. getStatus($num);
  157. break;
  158. case 41:
  159. getCheckPointTypes($num);
  160. break;
  161. case 42:
  162. getLocoSeries($num);
  163. break;
  164. case 43:
  165. getUserCompany($num,$id);
  166. break;
  167. case 44:
  168. getUserFaceFeature($num,$id);
  169. break;
  170. case 45:
  171. getCameraInfo($num, $id);
  172. break;
  173. case 46:
  174. getObjectInfo($num, $id);
  175. break;
  176. case 60:
  177. Measurement($num, $taskid, $position, $verbose);
  178. break;
  179. }
  180. function MeasurementHardwareCheck($user, $workid, $taskid, $verbose=0)
  181. {
  182. require_once "curlexec.php";
  183. $params['user_id'] = $user;
  184. $params['work_id'] = $workid;
  185. $url = "http://qaplatform.digitaldepot.ru:8085/Thingworx/Things/2050UserLibraryExp/Services/check_hw_measurements_ready";
  186. $result = CURLrequestPOST($url, ["AppKey: 33cd23bb-3b03-4638-afef-7104a13e9211"], $params);
  187. // $result = $taskAnswer;
  188. $json = json_decode($result);
  189. if ($verbose)
  190. var_dump($result);
  191. WriteLog("check_hw_measurements_ready input ".json_encode($params), $result);
  192. if ($json->result == true) {
  193. \app\models\MappingapiModel::updateMeasurements($taskid);
  194. return true;
  195. }
  196. return false;
  197. }
  198. function Measurement($num, $taskid, $position = 0, $verbose = 0)
  199. {
  200. //создать задание на аппаратные замеры
  201. global $link;
  202. $taskAnswer = '{
  203. "result": true,
  204. "data": {
  205. "hw_measurements_status": "waiting",
  206. "measuring_devices": [
  207. "Доктор - 060RLC"
  208. ],
  209. "task_id": "428f8962-bf47-ce93-6394-d1865c36f4a8"
  210. }
  211. }';
  212. $work_id = 0;
  213. $user_id = 0;
  214. $stage = 0;
  215. $str = "select work_id, worker, measurement_stage, smopp_task_id from asusg_measurements where smopp_task_id=$taskid limit 1";
  216. $result_pos = mysqli_query($link, $str);
  217. while ($obj = mysqli_fetch_object($result_pos)) {
  218. if (!$work_id)
  219. $work_id = $obj->work_id;
  220. if (!$user_id)
  221. $user_id = json_decode($obj->worker)->id;
  222. if (!$stage)
  223. $stage = $obj->measurement_stage;
  224. }
  225. /**
  226. * @var $task Task
  227. */
  228. $task = Task::Find($taskid);
  229. if ($task) {
  230. $user_id = $task->account->uuid;
  231. $currentStatus = $task->status;
  232. if ($currentStatus == 6) {
  233. $task_hw = $task->asusg_task_hw_measurement_id;
  234. if ($task_hw)
  235. {
  236. $buf = "";
  237. $check = MeasurementHardwareCheck($user_id, $work_id, $taskid);
  238. if ($check) {
  239. $buf .= pack("C", 3); //measurements completed
  240. $task->status = 7;
  241. $task->Save();
  242. }
  243. else
  244. {
  245. $buf .= pack("C", 2); //measurements not completed
  246. }
  247. $data = ConstructVariablePacket($num, $buf);
  248. echo base64_encode($data);
  249. }
  250. } else {
  251. require_once "curlexec.php";
  252. $params['user_id'] = $user_id;
  253. $params['work_id'] = $work_id;
  254. $params['section_position_number'] = $position;
  255. $params['measurement_stage'] = $stage;
  256. $url = "http://qaplatform.digitaldepot.ru:8085/Thingworx/Things/2050UserLibraryExp/Services/task_for_hw_measurements";
  257. $result = CURLrequestPOST($url, ["AppKey: 33cd23bb-3b03-4638-afef-7104a13e9211"], $params);
  258. // $result = $taskAnswer;
  259. $json = json_decode($result);
  260. WriteLog("task_for_hw_measurements input " . json_encode($params), $result);
  261. $buf = ""; //заголовок - v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
  262. if ($json->result == true) {
  263. $name = $json->data->measuring_devices[0];
  264. $task_hw_id = $json->data->task_id;
  265. if ($task_hw_id) {
  266. $task = Task::Find($taskid);
  267. if ($task) {
  268. $task->status = 6;
  269. $task->asusg_task_hw_measurement_id = $task_hw_id;
  270. $task->Save();
  271. }
  272. }
  273. $buf .= pack("C", 1);
  274. $buf .= pack("v", strlen($name));
  275. $buf .= $name;
  276. if ($verbose) {
  277. echo " dev: $name stage $stage pos $position user_id $user_id<br>";
  278. }
  279. } else {
  280. $buf .= pack("C", 0);
  281. }
  282. $data = ConstructVariablePacket($num, $buf);
  283. if ($verbose) {
  284. $ans = unpack('C', $data, 3);
  285. echo "answer: <br> ";
  286. var_dump($ans);
  287. }
  288. echo base64_encode($data);
  289. }
  290. }
  291. }
  292. function ARObjectRecognized($num, $tid, $sid)
  293. {
  294. //get scenario id from sid
  295. //get object_id from scenario
  296. //update task object recognized with object_id
  297. global $link;
  298. //$str = "update tasks t JOIN ar_scenarios ars on ars.id=$sid set t.ar_object_id=ars.object_id where t.id = $tid";
  299. $str = "update tasks t JOIN ar_stages ars on ars.scenario_id=$sid and ars.priority = (SELECT MAX(priority) from ar_stages where scenario_id=$sid) set t.ar_object_id=ars.object_id where t.id = $tid";
  300. if (mysqli_query($link, $str))
  301. echo 1;
  302. else
  303. echo 0;
  304. }
  305. function getStatus($num)
  306. {
  307. global $link;
  308. $str = "SELECT id, name, color FROM task_status_names";
  309. $retarr = array();
  310. $query = mysqli_query($link, $str);
  311. while ($res = mysqli_fetch_assoc($query))
  312. {
  313. $retarr[] = $res;
  314. }
  315. echo json_encode($retarr, JSON_UNESCAPED_UNICODE);
  316. }
  317. function getCheckPointTypes($num)
  318. {
  319. global $link;
  320. $str = "SELECT id, name, class FROM checkpoint_types";
  321. $retarr = array();
  322. $query = mysqli_query($link, $str);
  323. while ($res = mysqli_fetch_assoc($query))
  324. {
  325. $retarr[] = $res;
  326. }
  327. //var_dump($retarr);
  328. echo json_encode($retarr, JSON_UNESCAPED_UNICODE);
  329. }
  330. function getLocoSeries($num)
  331. {
  332. global $link;
  333. $str = "SELECT id, name FROM locomotive_series";
  334. $retarr = array();
  335. $query = mysqli_query($link, $str);
  336. while ($res = mysqli_fetch_assoc($query))
  337. {
  338. $retarr[] = $res;
  339. }
  340. //var_dump($retarr);
  341. echo json_encode($retarr, JSON_UNESCAPED_UNICODE);
  342. }
  343. function getTaskTypes($num)
  344. {
  345. global $link;
  346. $str = "SELECT id, name, description, confirmtype, time_to_complete_minutes FROM tasktypes WHERE main_task <> 1";
  347. $retarr = array();
  348. $query = mysqli_query($link, $str);
  349. while ($res = mysqli_fetch_assoc($query))
  350. {
  351. $retarr[] = $res;
  352. }
  353. echo json_encode($retarr, JSON_UNESCAPED_UNICODE);
  354. }
  355. function SendCoordinate($num, $pointx, $pointy, $timeStamp, $accId, $locationId, $zoneId)
  356. {
  357. global $link;
  358. $timeStamp = urldecode($timeStamp);
  359. $newpointx = str_replace(',', '.', $pointx);
  360. $newpointy = str_replace(',', '.', $pointy);
  361. $str = "INSERT INTO user_positions (COORD_X, COORD_Y, TS, ACC_ID, LOCATION_ID, ZONE_ID) VALUES ($newpointx, $newpointy, '$timeStamp', $accId, $locationId, $zoneId)";
  362. // echo $str;
  363. if (mysqli_query($link, $str))
  364. echo base64_encode(pack("CC",$num, 1));
  365. else
  366. echo base64_encode(pack("CC",$num, 0));
  367. }
  368. function GetMarkers($num, $locid)
  369. {
  370. global $link;
  371. $str = "SELECT UUID, X, Y FROM BEACONS WHERE LOCATION_ID=$locid";
  372. $retarr = array();
  373. $query = mysqli_query($link, $str);
  374. while ($res = mysqli_fetch_assoc($query))
  375. {
  376. $retarr[] = $res;
  377. }
  378. //var_dump($retarr);
  379. echo json_encode($retarr);
  380. }
  381. function OverPlan($num, $accid)
  382. {
  383. $account = Account::Find($accid);
  384. if ($account != null) {
  385. if ($account->overplan_mode == 1)
  386. $account->overplan_mode = 0;
  387. else
  388. $account->overplan_mode = 1;
  389. }
  390. Account::ClearTasksFromAccount($accid, false);
  391. $account->Save();
  392. $buf = pack("CC*",$num, $account->overplan_mode);
  393. echo base64_encode($buf);
  394. // MessageToClient("OverPlan!!!1", 2, 1);
  395. }
  396. function CheckUpdates($packetnum, $ver, $app)
  397. {
  398. if (isset($ver) && isset($app))
  399. {
  400. $appdata = GetAppData($ver, $app);
  401. if ($appdata) {
  402. $path = $appdata['app_update_path'];
  403. $ver = $appdata['app_update_filename'];
  404. $md5 = $appdata['md5'];
  405. $answer = 1;
  406. $buf = pack("C*", $answer); //заголовок - v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
  407. $buf .= pack("v*", strlen($path));
  408. $buf .= $path;
  409. $buf .= pack("v*", strlen($ver));
  410. $buf .= $ver;
  411. $buf .= pack("v*", strlen($md5));
  412. $buf .= $md5;
  413. }
  414. else {
  415. $answer = 0;
  416. $buf=pack("C", $answer);
  417. }
  418. $c = ConstructVariablePacket($packetnum, $buf);
  419. echo base64_encode($c);
  420. }
  421. }
  422. function Token($accid, $token)
  423. {
  424. if (!$token)
  425. return;
  426. global $link;
  427. $str = "update accounts set android_token_id = '$token' where id=$accid";
  428. mysqli_query($link, $str);
  429. }
  430. function saveBeaconData($num, $location_id, $acc_id, $beacon_id, $rssi, $localtime, $pwr)
  431. {
  432. global $link;
  433. $localtime = urldecode($localtime);
  434. //$pwr = urldecode($pwr);
  435. $str = "INSERT INTO beacons_scan_data (location_id, acc_id, beacon_id, rssi, local_time, servertime, pwr) VALUES ($location_id, $acc_id, $beacon_id, $rssi, '$localtime', NOW(), '$pwr')";
  436. mysqli_query($link, $str);
  437. // if (mysqli_query($link, $str))
  438. // echo '1';
  439. // else
  440. // echo '0';
  441. //echo "$str";
  442. }
  443. function CheckPointAccept($id, $value, $test=false)
  444. {
  445. $cp = CheckPoint::CreateFromID($id);
  446. $cp->SetValue($value);
  447. $task = Task::Find($cp->parentTaskId);
  448. WriteLog("$id CheckPointAccept $cp->id $cp->typeId ", $cp->parentTaskId);
  449. \app\models\entity\Metrics::log(1, 31, 'taskId = ' . ($task->id ?? 0));
  450. if (isset($task))
  451. {
  452. $acc = $task->account;
  453. $uuid = "";
  454. if (isset($acc))
  455. $uuid = $acc->uuid;
  456. $measurement = IsMeasurement($task->id);
  457. if ($measurement)
  458. {
  459. if ($cp->value || $cp->state)
  460. $cp->WriteValueToMeasurement($measurement, $cp->value, $uuid);
  461. }
  462. if (isset($acc)) {
  463. $task->account->last_action = new \DateTime("now");
  464. $task->account->Save();
  465. }
  466. }
  467. // $cp->AddRemark();
  468. }
  469. function Construct2BytePacket($num, $byte)
  470. {
  471. $buf = pack("CC*", $num, $byte); //заголовок - номер пакета 1б и длина 2б. v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
  472. return $buf;
  473. }
  474. function ConstructVariablePacket($num, $vardatabytes)
  475. {
  476. $buf = pack("C*", $num); //заголовок - номер пакета 1б и длина 2б. v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
  477. $len = strlen($vardatabytes)+3;
  478. $buf .= pack("v*", $len);
  479. $buf .= $vardatabytes;
  480. return $buf;
  481. }
  482. function Quit($accid, $fromsite = false, $tagid)
  483. {
  484. global $link;
  485. /**
  486. * @var $account Account
  487. */
  488. $account = Account::Find($accid);
  489. if ($account != null)
  490. {
  491. $last = new \DateTime("-11minutes");
  492. $account->setLastSeenMobile($last);
  493. // $account->android_token_id = '';
  494. $account->quit_set = 1;
  495. $account->Save();
  496. OCSlogoff($accid);
  497. if ($tagid)
  498. VagrantRequest($accid, $tagid, $account->name, 2);
  499. if ($fromsite)
  500. EnqueueCMD($accid, "22");
  501. }
  502. }
  503. //from client
  504. function TasksCancel($accid, $maintask, $cause)
  505. {
  506. $mappingapiModel = new \app\models\MappingapiModel();
  507. $res = $mappingapiModel->setPause($accid, $maintask);
  508. Account::ClearTasksFromAccount($accid, false, $maintask);
  509. $buf = pack("C", 21);
  510. if ($cause)
  511. {
  512. //TWX work close
  513. // вот список
  514. // Причина1 Отсутствие ТМЦ
  515. // Причина2 Отсутствие оборудования
  516. // Причина3 Отсутствие инструмента
  517. // Причина4 Не мое задание (не моя квалификация)
  518. // Причина5 Прочее
  519. }
  520. echo base64_encode($buf);
  521. }
  522. function TaskControlMapCheck($tid, $echo=0)
  523. {
  524. if ($echo)
  525. echo "TaskControlMapCheck!";
  526. global $link;
  527. $checked = 1;
  528. $cps = CheckPoint::FindAllByTask($tid);
  529. if (sizeof($cps) > 0)
  530. {
  531. foreach ($cps as $cp) {
  532. if ($cp->parentCheckpointId == 0) {
  533. if (0 == $cp->state || null == $cp->state) {
  534. $checked = 0;
  535. break;
  536. }
  537. }
  538. }
  539. }
  540. return $checked;
  541. }
  542. function FindTMCId($tmcType)
  543. {
  544. //найти первый свободный ТМЦ нужного типа
  545. global $link;
  546. // var_dump($tmcType);
  547. $query = mysqli_query($link, "select id from tmc where employee=0 and tmc_type = $tmcType");
  548. if ($res = mysqli_fetch_assoc($query))
  549. {
  550. // var_dump($res);
  551. return $res['id'];
  552. }
  553. return 0;
  554. }
  555. function TaskBegin($num, $tid, $echo, $testing=false)
  556. {
  557. global $link;
  558. $task = Task::Find($tid);
  559. //$exec = \app\models\MappingworksModel::workToExecute($tid);
  560. //if (!$exec) {
  561. // $test = true;
  562. //}
  563. // if ($echo)
  564. // echo "TaskBegin $num $tid task id: $task->id status $task->status<br>";
  565. $exec = \app\models\MappingworksModel::workToExecuteBegin($tid);
  566. if ($task != null && intval($task->status) == 2){
  567. $tasktype = Tasktype::Find($task->type);
  568. $objtype = $tasktype->object_type;
  569. // if ($echo) {
  570. // echo "task not null";
  571. // }
  572. $tmcId = FindTMCId($objtype);
  573. // if ($echo) {
  574. // echo "objtype $objtype tasktype tmcId " . $tmcId;
  575. // }
  576. $proj = $task->project;
  577. $canBegin = true;
  578. // WriteLog("test request tmcId $tmcId ", isset($proj));
  579. if (intval($tmcId) > 0 && isset($proj))
  580. {
  581. // if ($echo)
  582. // echo "$tmcId objtype $objtype";
  583. ////
  584. // $locotype = $proj->getLocoType();
  585. // $loconumber = $proj->loco_number;
  586. $sid = 0;
  587. $section = $task->getSection();
  588. // if ($section)
  589. // $sid = $section->id;
  590. // WriteLog("test request task $task->id ", $sid);
  591. if ($section) {
  592. // if ($echo)
  593. // echo "section ! $section->id";
  594. require_once("curlexec.php");
  595. // $reqstring = "http://oiltest.prmsys.net/api/handler/sessionstart?device=$tmcId&section_id=$section->id&loco_type=$locotype&loco_number=$loconumber";
  596. // $reqstring = "http://oiltest.prmsys.net/api/handler/sessionstart?deviceId=$tmcId&section_id=$section->id";
  597. $reqstring = "http://oiltest.prmsys.net/api/session/start?deviceId=$tmcId&sectionId=$section->id";
  598. $result = CURLrequestGET($reqstring,["uuid-key: 07a13c8907d1-6a7bdba1-a2fz-aatgu4j"]);
  599. // WriteLog("test request", $reqstring);
  600. if ($result == 'false')
  601. $canBegin = false;
  602. }
  603. }
  604. if (!$testing) {
  605. if ($canBegin) {
  606. \app\models\ElasticLog::command((int)$tid, 'Принятие к исполнению');
  607. OCStaskstatus($tid, 3, $task->assignees_arr);
  608. mysqli_query($link, "update tasks set status = 3, accepted_time = NOW() where id=" . $tid);
  609. mysqli_query($link, "update tasks set object_id=$tmcId, status = 3 where id=$task->parent_id");
  610. if ($proj != null) {
  611. if (!$proj->started)
  612. mysqli_query($link, "update projects_locotech set status = 3, started= now() where id=" . $proj->id);
  613. else
  614. mysqli_query($link, "update projects_locotech set status = 3 where id=" . $proj->id);
  615. }
  616. $buf = pack("Cll", $num, $tmcId, $tid);
  617. echo base64_encode($buf);
  618. } else {
  619. $buf = pack("Cll", $num, 0, 0);
  620. echo base64_encode($buf);
  621. }
  622. }
  623. }
  624. }
  625. function RequestObj($num, $id)
  626. {
  627. //echo "RequestObj ".$id;
  628. global $link;
  629. $query = mysqli_query($link, "select * from object_field where id=".$id);
  630. if ($res = mysqli_fetch_array($query))
  631. {
  632. //$buf = $res['position'].'|'.$res['scale'];
  633. //echo $res['position']." ".$res['scale'];
  634. $buf = pack("C", 13);
  635. $buf .= $res['position']." ".$res['scale'];
  636. echo base64_encode($buf);
  637. //echo "RequestObj ".$id;
  638. }
  639. }
  640. function TextNote($num, $taskid, $text, $cpid)
  641. {
  642. $task = Task::Find($taskid);
  643. $answer = 1;
  644. $tasknote = new Tasknote();
  645. if ($task == null)
  646. $answer = 2;
  647. else {
  648. if (isset($task->account)) {
  649. $task->account->last_action = new \DateTime("now");
  650. $task->account->Save();
  651. }
  652. $tasknote->date = new DateTime('now');
  653. $tasknote->text = $text;
  654. $tasknote->task = $task;
  655. if ($cpid)
  656. $tasknote->checkpoint_id = $cpid;
  657. // else
  658. // {
  659. // $result = $task->AddRemark($text);
  660. // $json = json_decode($result);
  661. // if ($json != null) {
  662. // $tasknote->remark_id = $json->RemarkId;
  663. // $tasknote->code1c = $json->Kod;
  664. // $tasknote->guid = $json->Guid;
  665. // $tasknote->twx_result = $result;
  666. // $answer = 4;
  667. // }
  668. // }
  669. $tasknote->Save();
  670. }
  671. $c = Construct2BytePacket($num, $answer);
  672. echo base64_encode($c);
  673. }
  674. function ObjPlace($num, $id, $pos, $scale)
  675. {
  676. //echo "ObjPlace";
  677. global $link;
  678. mysqli_query($link, "update object_field set position = '".$pos."', scale='".$scale."' where id=".$id);
  679. $buf = pack("C", 12);
  680. //$buf .= $res['position']." ".$res['scale'];
  681. echo base64_encode($buf);
  682. //echo "updated id ".$id." coords ".$pos." scale ".$scale;
  683. }
  684. function GetOnline($num, $accid)
  685. {
  686. global $link;
  687. //date(strtotime($date." -3 minutes"));
  688. //echo $time. ' last seen '.$date1.' now '.date('d.m.Y H:i',time());
  689. GetOnlineAccountList(true);
  690. }
  691. function Command($accid)
  692. {
  693. $cmdarr = DequeueCMD($accid);
  694. //пока достаем по одной команде
  695. $ret = false;
  696. foreach ($cmdarr as $cmd) {
  697. if ($cmd == '21') {
  698. $buf = pack("C", 21);
  699. $ret = true;
  700. }
  701. if ($cmd == '22') {
  702. SendPush($accid, "Logout by server!", "");
  703. $buf = pack("C", 22);
  704. $ret = true;
  705. }
  706. if ($cmd == '36_1') {
  707. //SendPush($accid, "Overplan!", "");
  708. $buf = pack("CC*",36, 1);
  709. $ret = true;
  710. }
  711. if ($cmd == '36_0') {
  712. //SendPush($accid, "Overplan!", "");
  713. $buf = pack("CC*",36, 0);
  714. $ret = true;
  715. }
  716. }
  717. echo base64_encode($buf);
  718. return $ret;
  719. }
  720. function MessageToClient($text, $code, $fromId)
  721. {
  722. $buf = pack("Clv", $code, $fromId, strlen($text));
  723. $buf .= pack($text);
  724. $data = ConstructVariablePacket(61, $buf);
  725. echo base64_encode($data);
  726. }
  727. function Ping($num, $accid)
  728. {
  729. if ($accid > 0) {
  730. global $link, $companyID;
  731. $buf = pack("C", 11);
  732. $acc = Account::Find($accid);
  733. $quit = $acc->quit_set;
  734. if ($quit) {
  735. mysqli_query($link, "update accounts set quit_set=0, last_seen_mobile = NOW() where id =" . $accid);
  736. $res = mysqli_query($link, "SELECT MAX(date_entry) as date FROM auth_log where account_id =" . $accid);
  737. mysqli_query($link, "update auth_log set date_finish_task = NOW() where account_id =" . $accid ." and date_entry = '". mysqli_fetch_assoc($res)['date']."'" );
  738. $tagid = GetVagrantTagID($accid);
  739. if ($tagid) {
  740. VagrantRequest($accid, $tagid, $acc->name, 1);
  741. }
  742. }
  743. else
  744. mysqli_query($link, "update accounts set last_seen_mobile = NOW() where id =" . $accid);
  745. \app\models\entity\UserActivity::ping($accid);
  746. // TasksAssign('', true, 0, false, $accid);
  747. if (!Command($accid))
  748. echo base64_encode($buf);
  749. }
  750. }
  751. function DeviceAdd($num, $did)
  752. {
  753. global $link;
  754. $buf = pack("CC*", 9, 1);
  755. mysqli_query($link, "INSERT into devices (id, date) values ('".$did."', NOW())");
  756. echo base64_encode($buf);
  757. // echo "DeviceAdd".$did;
  758. }
  759. function TaskStatusChange($num, $id, $tid)
  760. {
  761. }
  762. function TaskStatusChangeSend($status, $tid)
  763. {
  764. $buf = pack("C", 7);
  765. $buf .= pack("Cl*", $status, $tid);
  766. echo base64_encode($buf);
  767. }
  768. function DeviceIsAuthorized($num, $did)
  769. {
  770. global $link;
  771. $answer = 0;
  772. $buf = pack("C", $num);
  773. $query = mysqli_query($link, "SELECT name FROM devices where id= '".$did."'");
  774. if ($res = mysqli_fetch_row($query))
  775. {
  776. $answer = 1;
  777. }
  778. $buf .= pack("C", $answer);
  779. echo base64_encode($buf);
  780. }
  781. function LoggedIn($myid)
  782. {
  783. return true;
  784. }
  785. function GetDayPlanned($accid)
  786. {
  787. global $link;
  788. $str = "SELECT t.id, t.type, t.input_id, tt.letter, pr.loco_number from tasks as t join projects_locotech as pr on t.input_id=pr.id join tasktypes as tt on t.type = tt.id join asusg_dayplan as ad on tt.letter = ad.letter where assignees_arr=$accid and ad.day = cast((now() - interval 0 day) as date) and t.priority=0 and ad.loco_num=pr.loco_number order by ad.priority asc limit 1";
  789. $query = mysqli_query($link, $str);
  790. if ($res1 = mysqli_fetch_object($query)) {
  791. return Task::Find($res1->id);
  792. }
  793. }
  794. //todo Убрать $control!
  795. function TaskList($numpacket, $account_id, $verbose=false)
  796. {
  797. global $link, $companyID;
  798. global $entityManager;
  799. // $tasks = array();
  800. mysqli_query($link, "update accounts set last_seen_mobile=NOW() where id=".$account_id);
  801. mysqli_query($link, "insert into tmp_lastseenmobile (user_id, last_seen_mobile) values ($account_id, NOW())");
  802. $account = Account::Find($account_id);
  803. if (!$account) {
  804. if ($verbose)
  805. {
  806. echo "no account $account_id";
  807. }
  808. return;
  809. }
  810. $company = $account->getCompany();
  811. $companyID = $company->id;
  812. // TasksAssign('', true, 0, $verbose, $account_id);
  813. $buf="";
  814. $projid = 0;
  815. $loco_num = 0;
  816. $series = "";
  817. /**
  818. * @var $account Account
  819. */
  820. //$t = $account->getAcceptedTasks()->first();
  821. $planned_id = GetDayPlanned($account_id);
  822. if (!$planned_id) {
  823. if ($verbose)
  824. echo "<br>!GetDayPlanned <br>";
  825. $t = $account->getFirstActiveTaskMain();
  826. }
  827. else
  828. $t = $planned_id;
  829. if (!$t) {
  830. if ($verbose)
  831. echo "<br>!getFirstActiveTaskMain <br>";
  832. $t = $account->getActiveTasks()->first();
  833. }
  834. if ($t)
  835. {
  836. if ($verbose) {
  837. echo "<br>Task fisrt found:<br>";
  838. \Doctrine\Common\Util\Debug::dump($t);
  839. }
  840. /**
  841. * @var $pr Project
  842. */
  843. $pr = $t->project;
  844. if ($pr) {
  845. if ($verbose)
  846. echo('project found ' . $pr->id);
  847. $tasks = $pr->getPrimaryTasks($account_id, $planned_id);
  848. if ($verbose)
  849. {
  850. echo('pr ' . $pr->id);
  851. echo('tasks1 size ' . sizeof($tasks));
  852. }
  853. }
  854. }
  855. if (!$tasks) {
  856. // $tasks = $account->getActiveTasks();
  857. if ($verbose)
  858. echo ('!tasks '.sizeof($tasks));
  859. }
  860. //foreach ($account->getActiveTasks() as $task) {
  861. foreach ($tasks as $task) {
  862. $taskid = $task->id;
  863. $asu = ASUSG($task->asusg_job_mapped_id);
  864. // if ($verbose) {
  865. // var_dump($asu);
  866. // }
  867. $outfit = 0;
  868. $time_limit = 0;
  869. if ($asu)
  870. {
  871. $time_limit = $asu[0]['time_limit'];
  872. $outfit = $asu[0]['outfit'];
  873. }
  874. $status = GetTaskStatus($taskid);
  875. $status = $task->status;
  876. if ($status != '2' && $status != '3' && $status != '6' && $status != '7') {
  877. if ($verbose) {
  878. var_dump($task->id);
  879. }
  880. continue;
  881. }
  882. $type = $task->type;
  883. $priority = $task->priority;
  884. if ($verbose)
  885. {
  886. echo "<br> $taskid $priority ".$task->project->id." $task->status<br>";
  887. }
  888. $namedesc = GetTaskNameAndDesc($type);
  889. $tt = GrabTasktype($type);
  890. $timer = $tt['time_to_complete_minutes'];
  891. $record_video = $tt['record_video'];
  892. $objectType = $tt['object_type'];
  893. $letter = $tt['letter'];
  894. if (!$letter)
  895. $letter = " ";
  896. $name = $namedesc[0];
  897. if ($task->name) {
  898. $name = $task->name;
  899. }
  900. $text = $task->text;
  901. $scenario_id = $tt['scenario_id'];
  902. if ( 'Мэппинг.' == $text ) {
  903. $text = explode(':', $task->text)[1];
  904. }
  905. if (!isset ($name) || $name == null || $name == '')
  906. $name = "Empty Task!";
  907. if (!isset ($text) || $text == null || $text == '') {
  908. $text = $tt['description'];
  909. }
  910. if (!isset ($text) || $text == null || $text == '')
  911. $text = "<i><color=green><b>Empty</b></color> Text</i>";
  912. $confirmtype = getConfirmTypebyTask($taskid);
  913. $lenname = strlen($name);
  914. $lentext = strlen($text);
  915. if ($projid == 0)
  916. {
  917. /**
  918. * @var $proj Project
  919. */
  920. /**
  921. * @var $section Section
  922. */
  923. $proj = $task->project;
  924. $projid = $proj->id;
  925. $series = $proj->loco_type;
  926. $loco_num = $proj->loco_number;
  927. if ($verbose) {
  928. echo "series1 $series";
  929. echo "loconum $proj->loco_number";
  930. echo "loco_type $proj->loco_type";
  931. }
  932. $buf .= pack("ll",$projid, $loco_num);
  933. $buf .= pack("v",strlen($series));
  934. $buf .= $series;
  935. $buf .= $letter;
  936. }
  937. $buf .= pack("clcclvvl", $status, $priority, $confirmtype, $timer, $taskid, $lenname, $lentext, $type); //заголовок - номер пакета 1б и длина 2б. v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
  938. $buf .= $name;
  939. $buf .= $text;
  940. $control = "";
  941. //$lencontrol = strlen($control);
  942. //$buf .= pack("v*",$lencontrol);
  943. $buf .= pack("v",$company->GetConfirmTimeout());
  944. $buf .= pack("C",$record_video);
  945. $buf .= pack("l",$objectType);
  946. if ($scenario_id)
  947. $buf .= pack("C", 1);
  948. else
  949. $buf .= pack("C",0);
  950. $buf .= pack("l",$outfit);
  951. $buf .= pack("l",$time_limit);
  952. $buf .= pack("l",$task->object_id);
  953. $buf .= pack ("l", $task->parent_id);
  954. // $buf .= $control;
  955. }
  956. $c = ConstructVariablePacket($numpacket, $buf);
  957. echo base64_encode($c);
  958. \app\models\entity\UserActivity::ping($account_id);
  959. }
  960. function CheckList($numpacket, $task_id)
  961. {
  962. //$buf = pack("C", $numpacket);
  963. $buf = '';
  964. $checkpoints = CheckPoint::Find("all", $task_id);
  965. //echo "checkpoints taks $task_id ".sizeof($checkpoints);
  966. foreach ($checkpoints as $checkpoint)
  967. {
  968. $buf .= $checkpoint->PackToBinary();
  969. $buf .= $checkpoint->PackChildrenToBinary();
  970. }
  971. $c = ConstructVariablePacket($numpacket, $buf);
  972. echo base64_encode($c);
  973. }
  974. function CheckIn($num, $accountId)
  975. {
  976. global $link;
  977. $query = mysqli_query($link, "select text, name from documents where id=1");
  978. $buf="";//pack("c*",$num);
  979. $add = "";
  980. $fail = pack("c",0);
  981. if ($res1 = mysqli_fetch_array($query)) {
  982. $text = $res1['text'];
  983. if (strlen($text) > 0)
  984. {
  985. $did = getCurrentDeviceId($accountId);
  986. $sess = getActiveSession($accountId);
  987. if ($did != $sess["device_id"] && $sess["end"] != NULL)
  988. {
  989. $add .= $fail;
  990. }
  991. else
  992. {
  993. $sid = $sess["id"];
  994. mysqli_query($link, "update sessions set checkin_start = NOW() where account_id=".$accountId." and id=".$sid);
  995. $add .= $text;
  996. }
  997. }
  998. }
  999. $buf .= $add;
  1000. $c = ConstructVariablePacket($num, $buf);
  1001. echo base64_encode($c);
  1002. }
  1003. function Login($packetNum, $id, $did, $tagid)
  1004. {
  1005. global $link;
  1006. $answer = 0;
  1007. $name = "";
  1008. $cmdlevel = 0;
  1009. if (isset($id) && isset($did))
  1010. {
  1011. $query = mysqli_query($link, "select * from accounts where id=" . $id);
  1012. if ($res1 = mysqli_fetch_array($query)) {
  1013. $name = $res1['name'];
  1014. $cmdlevel = intval($res1['cmdlevel']);
  1015. if ($cmdlevel > 0)
  1016. {
  1017. //Login OK!
  1018. //echo $name." id ".$id."act ".$cmdlevel." did ".$did;
  1019. $answer = 1;
  1020. mysqli_query($link, "update accounts set quit_set = 0, overplan_mode=0 where id=$id");
  1021. mysqli_query($link, "insert into sessions (account_id, start, device_id) values ($id, NOW(), '$did')");
  1022. }
  1023. }
  1024. }
  1025. if ($answer == 1) {
  1026. if ($tagid)
  1027. VagrantRequest($id, $tagid, $name, 1);
  1028. $buf = pack("ClC*", $answer, $id, $cmdlevel); //заголовок - v-ushort, c- byte, C- ubyte, s - short, L - int, l - uint
  1029. $buf .= $name;
  1030. OCSlogin($id);
  1031. }
  1032. else
  1033. {
  1034. $buf = pack("C*", $answer);
  1035. }
  1036. $c = ConstructVariablePacket($packetNum, $buf);
  1037. echo base64_encode($c);
  1038. }
  1039. function getUserCompany($pnum,$id)
  1040. {
  1041. global $link;
  1042. $id = (int)$id;
  1043. $result = [0 => ''];
  1044. $str = "SELECT company FROM `accounts_internal` WHERE `id`=". $id;
  1045. $query = mysqli_query($link, $str);
  1046. $result = mysqli_fetch_row($query);
  1047. $buf = pack("Cl", $pnum, $result[0]);
  1048. echo base64_encode($buf);
  1049. }
  1050. function getUserFaceFeature($pnum,$id)
  1051. {
  1052. global $link;
  1053. $id = (int)$id;
  1054. $res = 0;
  1055. $str = "SELECT `id_account`,`face_feature`,`date` FROM `face_feature` WHERE `id_account`=". $id ." ORDER BY `date` DESC LIMIT 1";
  1056. $query = mysqli_query($link, $str);
  1057. $result = mysqli_fetch_assoc($query);
  1058. if ( isset($result['face_feature']) and null != $result['face_feature']) {
  1059. $res = 1;
  1060. }
  1061. $buf = pack("CC",$pnum,$res);
  1062. //var_dump($pnum,'Пользователь '.$id.', ответ из базы = '.$res);
  1063. echo base64_encode($buf);
  1064. }
  1065. function getObjectInfo($num, $id)
  1066. {
  1067. $length_empty = 7;
  1068. $tmc = \app\models\entity\Tmc::findOne(['id' => $id]);
  1069. //title
  1070. if (!$tmc) {
  1071. $buf = pack("Cvl", $num, $length_empty, $id);
  1072. }
  1073. else {
  1074. $buf = pack("lll", $id, $tmc->inventory_num, $tmc->storage);
  1075. $len = sizeof($tmc->desc);
  1076. $buf .= pack("v", $len);
  1077. $buf .= $tmc->desc;
  1078. $buf = ConstructVariablePacket($num, $buf);
  1079. }
  1080. echo base64_encode($buf);
  1081. }
  1082. function getCameraInfo($num, $userId)
  1083. {
  1084. $tmc = \app\models\entity\Tmc::findOne(['employee' => $userId]);
  1085. $length_empty = 3;
  1086. $length_full = 13;
  1087. if (!$tmc) {
  1088. $buf = pack("Cv", $num, $length_empty);
  1089. }
  1090. else {
  1091. $buf = pack("Cv", $num, $length_full);
  1092. //pack ip into 4 bytes array
  1093. foreach (explode('.', $tmc->ip) as $item) {
  1094. $buf .= pack('C', $item);
  1095. }
  1096. //pack mac into 4 bytes array
  1097. foreach (explode(':', $tmc->mac) as $item) {
  1098. $buf .= pack('C', hexdec($item));
  1099. }
  1100. }
  1101. echo base64_encode($buf);
  1102. }
  1103. function packWithSpaces(array $data)
  1104. {
  1105. foreach ($data as $key => $item) {
  1106. $data[$key] = pack("A*", $item);
  1107. }
  1108. $space = pack("A1", ' ');
  1109. return implode($space, $data);
  1110. }