2017-02-18 62 views
0

我有寫了一套GPS字符串到一個文本文件中像這樣的應用程序:寫作GPS字符串Ubuntu服務器上的MySQL數據庫

[{「日期」:「2017年2月13日19: 26:00" , 「時間」:1486974360428, 「東經」:151.209900, 「緯度」: - 33.865143} { 「日期」: 「2017年2月13日19時26分十三秒」, 「時間」:1486974373496,「經度「:151.209900,」 緯度 「: - 33.865143} {」 日期 「:」 2017年2月13日19時26分23" 秒, 「時間」:1486974383539, 「東經」:151.209900, 「緯度」: - 33.865143} {」日期 「:」 2017年2月13日19時26分33" 秒, 「時間」:1486974393449, 「東經」:151.209900, 「緯度」: - 33.865143} { 「日期」:「2017年2月13日19:26: 43" , 「時間」:1486974403423, 「經度」:151.209900, 「緯度」: - 33.865143} { 「日期」: 「2017年2月13日19點26分53秒」, 「時間」:1486974413483, 「經度」: 「緯度」: - 33.865143}]

該文件始終以[]開頭和結尾。

該文件被上傳到一個Ubuntu服務器在

'filepath'/uploads/gps/'device ID'/'year-month-day'/'UTC download time'.txt 

例如

/uploads/gps/12/2017-02-12/1486940878.txt 

當文件被上傳到服務器上的文本文件生成,所以每寫多個文件天。

我想用一個方法將值寫入一個MySQL數據庫,標題爲DEVICE(從文件路徑獲得),DATE,TIME,LONGITUDE,LATITUDE。

最初,我可以在服務器上運行一個命令,這樣我最終可以從管理面板上的PHP命令運行。

我從哪裏開始?

+0

您可以使用任何語言編寫腳本c/++,或者使用python可以更簡單快捷。 Php會工作,我猜。但你也可以考慮使用休息服務。所以你不必設置cronjob或自己手動啓動腳本。 –

回答

0

相反上傳的,你可以很容易的文本提交到服務器上的PHP程序。它將使用JSON解碼將其轉換爲數組,然後將每條記錄保存到一個表中。設備ID是腳本的參數之一。

使用這種類型的方法將消除大量的如不導入文件兩次,重命名/移動文件導入後,找到該文件(S)等問題

這也意味着你的數據每次數據發送時都是最新的。

這樣的腳本寫起來很不重要,但它應該內置一些安全類型以防止未授權實體發送數據。

下面是一些將處理文件並將其存儲到數據庫的示例代碼。我刪除了某些您需要編輯的信息(用戶ID /密碼數據庫名稱)。我猜的時間稍長一些,但仍然很短。如果您需要更多信息,請PM我。

<?php 

/* =============================================================== 
    Locate and parse GPS files, then store to MySQL DB. 
    Presumes a folder stucture of gps/device_id/date:YYYY-MM-DD. 
    After a file is processed and stored in the DB table, the 
    file is renamed with a leading "_" so it will be ignored later. 
    =============================================================== 
*/ 

$DS = '/'; // Directory separator character. Use '/' for Linux, '\' for windows. 
// Path to folder containing device folders. 
$base_folder = "./gps"; 
// Today's date foratted like the folders under the devices. If parameter "date" has a value, use it instead of today's date. Parameter MUST be formatted correctly. 
$today = isset($_REQUEST['date']) && $_REQUEST['date'] != '' ? $_REQUEST['date'] : date('Y-m-d'); 

// Get a list of device folders 
$device_folders = get_folders($base_folder); 

// Loop through all of the device folders 
$num_file_processed = 0; 
foreach($device_folders as $dev_folder) { 
    // Check to see if there is a folder in the device folder for today. 
    $folder_path = $base_folder.$DS.$dev_folder.$DS.$today; 
    // Check if the device/date folder exists. 
    if(file_exists($folder_path) && is_dir($folder_path)) { 
     // Folder exists, get a list of files that haven't been processed. 
     $file_list = get_files($folder_path); 
     // Process the files (if any) 
     foreach($file_list as $filename) { 
      $f_path = $folder_path.$DS.$filename; 
      $json = file_get_contents($f_path); 
      // Fix the JSON -- missing "," between records. 
      $json = str_replace("}{","},{",$json); 
      $data = json_decode($json); 
      // Process each row of data and save to DB. 
      $num_saved = 0; 
      $rec_num = 0; 
      foreach($data as $recno => $rec_data) { 
       if(save_GPS($dev_folder,$rec_data->date,$rec_data->time,$rec_data->longitude,$rec_data->latitude)) { 
        $num_saved++; 
       } 
       $rec_num++; 
      } 
      // Rename file so we can ignore it if processing is done again. 
      if($num_saved > 0) { 
       $newName = $folder_path.$DS."_".$filename; 
       rename($f_path,$newName); 
       $num_file_processed++; 
      } 
     } 
    } else { 
     echo "<p>" . $folder_path . " not found.</p>\n"; 
    } 
} 
echo "Processing Complete. ".$num_file_processed." files processed. ".$num_saved." records saved to db.\n"; 



function save_GPS($dev_id,$rec_date,$rec_time,$long,$lat) { 
    $server = "localhost"; 
    $uid = "your_db_user_id"; 
    $pid = "your_db_password"; 
    $db_name = "your_database_name"; 

    $qstr = ""; 
    $qstr .= "INSERT INTO `gps_log`\n"; 
    $qstr .= "(`device`,`date`,`time`,`longitude`,`latitude`)\n"; 
    $qstr .= "VALUES\n"; 
    $qstr .= "('".$dev_id."','".$rec_date."','".$rec_time."','".$long."','".$lat."');\n"; 

    $db = mysqli_connect($server,$uid,$pid,$db_name); 
    if(mysqli_connect_errno()) { 
     echo "Failed to connect to MySQL server: " . mysqli_connect_errno() . " " . mysqli_connect_error() . "\n"; 
     return false; 
    } 
    // Connected to DB, so save the record 
    mysqli_query($db,$qstr); 
    mysqli_close($db); 
    return true; 
} 

function get_folders($base_folder) { 
    $rslts = array(); 
    $folders = array_map("htmlspecialchars", scandir($base_folder)); 
    foreach($folders as $folder) { 
     // Ignore files and folders that start with "." (ie. current folder and parent folder references) 
     if(is_dir($base_folder."/".$folder) && substr($folder,0,1) != '.') { 
      $rslts[] = $folder; 
     } 
    } 
    return $rslts; 
} 

function get_files($base_folder) { 
    $rslts = array(); 
    $files = array_map("htmlspecialchars", scandir($base_folder)); 
    foreach($files as $file) { 
     // Ignore files and folders that start with "." (ie. current folder and parent folder references), or "_" (files already processed). 
     if(!is_dir($file) && substr($file,0,1) != '.' && substr($file,0,1) != '_') { 
      $rslts[] = $file; 
     } 
    } 
    return $rslts; 
} 
+0

不幸的是,文本文件是由客戶端設備創建的。我真的沒有辦法將它作爲創建的txt文件來處理... – noob

+0

客戶端如何將其上傳到服務器?該文件可以提交給腳本進行處理。如果不是,則需要定期運行腳本來處理文件。這使事情變得複雜一點。該腳本將掃描文件,處理它們,以某種方式將它們標記爲已處理(更改文件名,刪除文件,將文件移動到不同的文件夾等)。仍然相當簡單的編碼,PHP中20-40行。你在尋找代碼示例嗎? –

+0

我真的很感激一些示例代碼。我的客戶端設備實際上具有在設備上加密的txt文件,並且在上載過程中它被服務器解密。未加密的文件位於服務器上進行處理。你可以提供的任何幫助都很好。 – noob

相關問題