,"* array_push($data, array("id" => $zkl_data_array[$i], "code" => $zkl_data_array[$i + 1])); } // Store intermediates $stream_data .= "Intermediate result: !FF, \"" . $zkl_data_array[$i] . "," . $zkl_data_array[$i + 1] . "\"*; "; } } } // Store result $stream_data .= "Result: !" . dechex($result) . ",\""; if (is_array($zkl_data_array)) { $offset = (((int)$result < 0x10) && (in_array($reason, $intermediates))) ? $device_size : 0; for($i=($offset * 2); $i < sizeof($zkl_data_array); $i++) { if ($i != ($offset * 2)) { $stream_data .= ","; } // Get sequence number if ($i == (($offset * 2) + 1)) { $seqnr = $zkl_data_array[$i]; } // Store extra result info $stream_data .= $zkl_data_array[$i]; } } $stream_data .= "\"*; "; } // Valid result and return $result; } /** * Store/update/handle work instruction using the secure server * * Inputs: * - project_id: Project id * - Parent: Parent project or child (period) * - data array containing intermediate info * - original: Array containing original project info (users/equip/general) * - reason WI update reason * - required_state WI required state * - equipment Equipment (overrules project settings) * - code Verification code * - emulated_status Emulated project status, this to make it possible to store the complete db * - seqnr Work instruction sequence number * * Return: Ok(0)/Error(1) */ function sc_wi($project_id, $parent = TRUE, &$data = null, $original = "", $reason = "", $required_state = "", $equipment = "", $code = "", $emulated_status = "", &$seqnr = null) { GLOBAL $_PAGE_INFO; GLOBAL $_SERVER; // Initial values $result = TCPSTAT_OK; $sign_data = array(); $stream_data = ""; $seqnr = ""; $debug_info = array(); // Start time $start = microtime(TRUE); // Create result array if (!is_array($data)) { $data = array(); } // Get project info $project['general'] = db_fetch_project($project_id, "", 1); $project['equip'] = db_fetch_project_lances($project_id, "", array(array("gsm","gprs"))); $project['users'] = db_fetch_project_users($project_id, "", "schakelen"); // Fix project name (remove trailing '_#NEW' when available), only for project change!! if (($_SESSION[$_PAGE_INFO['id']]['action'] == "project_design") && (is_array($original))) { if ((strpos($project['general']['naam'], '_#NEW') !== FALSE)) { $project['general']['naam'] = (strpos($project['general']['naam'], '_#NEW') == (strlen($project['general']['naam']) - strlen('_#NEW'))) ? substr($project['general']['naam'], 0, (strlen($project['general']['naam']) - strlen('_#NEW'))) : $project['general']['naam']; } } // Debug info array_push($debug_info, "Action: " . $_SESSION[$_PAGE_INFO['id']]['action']); if (strlen($reason)) { array_push($debug_info, "Changes: " . $reason . "(forced)"); } // Fix sstatus => 'request for release' equals 'non-released' => Needed to power the devices if (is_array($original)) { $original['general']['sstatus'] = ($original['general']['sstatus'] == "verzoek tot vrijgeven") ? "niet vrijgegeven" : $original['general']['sstatus']; } $project['general']['sstatus'] = ($project['general']['sstatus'] == "verzoek tot vrijgeven") ? "niet vrijgegeven" : $project['general']['sstatus']; // Get non-switching equipment from parent (including RS3000) if ((!$parent) && (!is_array($equipment))) { $project['equip_non_switching'] = db_fetch_project_lances($project['general']['parent'], "", array(array("gsm","gprs"))); if (is_array($project['equip_non_switching'])) { foreach($project['equip_non_switching'] as $item) { // non-switching? if (!db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) { // valid array? if (!is_array($project['equip'])) { $project['equip'] = array(); } array_push($project['equip'], $item); } } } } // Overruled equipment? Be aware that the reason must be known, otherwhise the original // equipment probably differs from the project equipment and the wrong update reason will be chosen! $project['equip'] = (is_array($equipment)) ? $equipment : $project['equip']; // Find out the update reason (this array includes priority) $changes = array("type","status","sstatus","naam","ostatus","pstatus"); // Emulate project status? if ((is_array($emulated_status)) && (is_array($changes))) { foreach ($changes as $change) { if (array_key_exists($change, $emulated_status)) { $project['general'][$change] = $emulated_status[$change]; } } } // Reason known? if (!strlen($reason)) { // Regular project? => Skip, or the type must be changed if (($project['general']['type'] != "normaal") || ((is_array($original)) && (!empty($original)) && ($original['general']['type'] != "normaal"))) { // New project? if (((is_array($original)) && (empty($original))) || ((!is_array($original)) && (!strlen($original)))) { $reason = "nieuw"; } else { // Something changed on the planning? if (is_array($changes)) { foreach ($changes as $change) { // Already reason found? if (!strlen($reason)) { // Key exists? if ((array_key_exists($change, $project['general'])) && (array_key_exists($change, $original['general']))) { // Key changes? if ($project['general'][$change] != $original['general'][$change]) { // Debug info array_push($debug_info, "Changes: " . $change . ", old: " . $original['general'][$change] . ", new: " . $project['general'][$change]); switch($change) { // Something changed on the project type? case "type": switch($project['general']['type']) { case "normaal": $reason = "verwijderen"; break; case "rc": $reason = "nieuw"; break; default: break; } break; // Something changed on the project status? case "status": switch($project['general']['status']) { case "afgesloten": $reason = "verwijderen"; break; default: break; } break; // Something changed on the project release status? case "sstatus": switch($project['general']['sstatus']) { case "niet vrijgegeven": $reason = "teruggegeven"; break; case "vrijgegeven": $reason = "vrijgave"; break; default: break; } break; // Something changed on the design? case "ostatus": switch($project['general']['ostatus']) { case "concept": case "gereed": $reason = "ontwerp gewijzigd"; break; case "geverifieerd": $reason = "ontwerp geverifieerd"; break; case "gevalideerd": $reason = "ontwerp gevalideerd"; break; default: break; } break; // Something changed on the planning? case "pstatus": switch($project['general']['pstatus']) { case "concept": // Be aware! // When a safety critical change has been made to the design, the ostatus can be unchanged (stays on "gereed") // But the pstatus is changed to "concept" (because of period changes). // So first check if switching equipment has been changed! before accepting this update reason if (($project['general']['ostatus'] == "concept") || ($project['general']['ostatus'] == "gereed")) { if ((is_array($project['equip'])) && (is_array($original['equip']))) { // Equipment added? foreach($project['equip'] as $item) { $found = FALSE; foreach($original['equip'] as $search_item) { $found = ($search_item['id'] == $item['id']) ? TRUE : $found; } if (!$found) { // Switching equipment? if (db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) { $reason = "ontwerp gewijzigd"; } } } // Equipment removed? foreach($original['equip'] as $item) { $found = FALSE; foreach($project['equip'] as $search_item) { $found = ($search_item['id'] == $item['id']) ? TRUE : $found; } if (!$found) { // Switching equipment? if (db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) { $reason = "ontwerp gewijzigd"; } } } } } // This was not the reason? => then the planning was changed if (!strlen($reason)) { $reason = "planning gewijzigd"; } break; case "gereed": $reason = "planning gewijzigd"; break; case "geverifieerd": $reason = "planning geverifieerd"; break; case "gevalideerd": $reason = "planning gevalideerd"; break; default: break; } break; // Project name changed? case "naam": $reason = "ontwerp gewijzigd"; break; default: break; } } } } } } // Check only when no parent => equipment/users not stored in wi on secure server if (!$parent) { // Found reason? => Check equipment on project if (!strlen($reason)) { // We do not have to check when there is no old equip info or no new equip info // Because this is handled by adding/removing periods or in the parent (which has no equipment for the secure server) if ((is_array($project['equip'])) && (is_array($original['equip']))) { // Equipment added? foreach($project['equip'] as $item) { $found = FALSE; foreach($original['equip'] as $search_item) { $found = ($search_item['id'] == $item['id']) ? TRUE : $found; } if (!$found) { // Debug info array_push($debug_info, "Changes: equipment added (" . $item['idcode'] . ")"); // Switching equipment? if (db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) { $reason = "ontwerp gewijzigd"; } else { $reason = "planning gewijzigd"; } } } // Equipment removed? foreach($original['equip'] as $item) { $found = FALSE; foreach($project['equip'] as $search_item) { $found = ($search_item['id'] == $item['id']) ? TRUE : $found; } if (!$found) { // Debug info array_push($debug_info, "Changes: equipment removed (" . $item['idcode'] . ")"); // Switching equipment? if (db_check_system_device_capabilities($item['device'], array("kortsluiting schakelen"))) { $reason = "ontwerp gewijzigd"; } else { $reason = "planning gewijzigd"; } } } } } // Found reason? => Check users on project if (!strlen($reason)) { if (((is_array($project['users'])) && (!is_array($original['users']))) || ((!is_array($project['users'])) && (is_array($original['users'])))) { // Debug info array_push($debug_info, "Changes: users changed"); $reason = "planning gewijzigd"; } else if ((is_array($project['users'])) && (is_array($original['users']))) { // Users added? foreach($project['users'] as $item) { $found = FALSE; foreach($original['users'] as $search_item) { $found = ($search_item['id'] == $item['id']) ? TRUE : $found; } if (!$found) { // Debug info array_push($debug_info, "Changes: user added (" . $item['gebruikersnaam'] . ")"); $reason = "planning gewijzigd"; } } // Users removed? foreach($original['users'] as $item) { $found = FALSE; foreach($project['users'] as $search_item) { $found = ($search_item['id'] == $item['id']) ? TRUE : $found; } if (!$found) { // Debug info array_push($debug_info, "Changes: user removed (" . $item['gebruikersnaam'] . ")"); $reason = "planning gewijzigd"; } } } } } } } } // Find legitimate reason? if (strlen($reason)) { // Setup connection with secure server $socket = sc_connect(); // Valid connection? if ($socket) { // Send WI start if (!$parent) { $result = sc_wi_start($socket, $project, $reason, $required_state, $sign_data, $stream_data, $device_size); } else { // Skip users and equipment for parent $result = sc_wi_start($socket, array("general" => $project['general'], "equip" => array(), "users" => array()), $reason, $required_state, $sign_data, $stream_data, $device_size); } // Send WI timeout $result = sc_wi_timeout($socket, (($reason == "vrijgave") ? TIMEOUT_RELEASE : TIMEOUT_DEFAULT), $sign_data, $stream_data); // Send WI user (only for periods) if ($result) { if (!$parent) { if (is_array($project['users'])) { foreach($project['users'] as $user) { sc_wi_user($socket, $user, $sign_data, $stream_data); } } } } // Send WI device (Only for periods, the non-switching equipment from the parent has been stored (needed for the RS3000)) if ($result) { if (!$parent) { if (is_array($project['equip'])) { foreach($project['equip'] as $equip) { sc_wi_device($socket, $equip, $sign_data, $stream_data); } } } } // Send WI request (only for request for switch action) if ($result) { if (($reason == "verzoek tot inschakelen") || ($reason == "verzoek tot uitschakelen")) { if (is_array($project['users'])) { foreach($project['users'] as $user) { // Current user? if ($user['id'] == $_SESSION[$_PAGE_INFO['id']]['login']['user']['id']) { // Check if RS3000 has been used? => rs3000 used as verification method => overrule sms/pin $user['verificatie'] = ((is_dev("RS3000")) || (is_dev("APP") && (stristr($_SERVER['HTTP_USER_AGENT'],"_RS")))) ? "rs3000" : $user['verificatie']; sc_wi_req($socket, $user, $sign_data, $stream_data); } } } } } // Send WI verify (only for for switch action and user verification) if ($result) { if (($reason == "inschakelen") || ($reason == "uitschakelen") || ($reason == "controle verificatie")) { if (is_array($project['users'])) { foreach($project['users'] as $user) { // Current user? if ($user['id'] == $_SESSION[$_PAGE_INFO['id']]['login']['user']['id']) { // Check if RS3000 has been used? => rs3000 used as verification method => overrule sms/pin $user['verificatie'] = ((is_dev("RS3000")) || (is_dev("APP") && (stristr($_SERVER['HTTP_USER_AGENT'],"_RS")))) ? "rs3000" : $user['verificatie']; sc_wi_ver($socket, $user, $code, $sign_data, $stream_data); } } } } } // Send WI end if ($result) { $result = sc_wi_end($socket, $reason, $data, $sign_data, $device_size, $stream_data, $seqnr); } // Store total time $stream_data .= " Time: " . (microtime(TRUE) - $start); // Split up debug stream $stream_array = explode(';', $stream_data); // Add extra debug info to debug stream if (is_array($debug_info)) { foreach($debug_info as $item) { array_push($stream_array, $item); } } if (is_array($stream_array)) { foreach($stream_array as $line) { if (intval($result,16) != TCPSTAT_OK) { DBG("wi error (" . $seqnr . "): " . trim($line)); } else if (LOG_SC) { DBG("wi debug (" . $seqnr . "): " . trim($line)); } } } // Close secure server socket sc_disconnect($socket); } else { DBG("wi error: invalid socket"); } } else { DBG("wi debug: no legitimate reason"); } // Return value return $result; } ?>