Welcome !!

最新網誌

編輯 關閉
網誌分類:Programming |
圖片
網誌日期:2008-08-23 09:35
上次用到 Google Calendar Send SMS. 今日遇到打風. 就寫個 Program 去 Send 天氣
 
天文台的文字版係有 Spec. 去講.
 
結果就整了這個啦 ^^ 時間就一定唔係天文台的時間. 時間會係個 Program Detect 到 Change 之後 6 分鐘啦 ^^
引用(0)
網誌標籤:SMS, Weather
  • 檢舉

    eddie 2008-08-28 21:10

    可不可以分享你的source code....我都想收到天文台的訊息...

    Thank you

    檢舉

    Alex2008-10-23 08:15

    Code Here. 我係用 PHP 寫. 用 Linux Server 行 Cron Jobs To Check. 如果你要用. 用改 Username 同 Password. 同埋 Call GoogleCalendar.exe 個 Path. 有個 cyclone.txt 去 Store 最後 Check 的台風 Signal No.

    #!/usr/bin/php
    <?php

    $w = new weather();


    class weather
    {

     private $url = "http://www.weather.gov.hk/textonly/forecast/englishwx.htm";
     private $urlw = "http://www.hko.gov.hk/textonly/warning/warn.htm";
     private $file = "/usr/local/weather/cyclone.txt";
     private $rawData = "";
     private $googleUsername = "YOURUSERNAME";
     private $googlePassword = "YOURPASSWORD";

     private $_temperature = "ERROR";
     private $_humidity = "ERROR";
     private $_weatherCartoon = "50";
     private $_cyclone = "";
     private $_uvIndex = 0;
     private $_lastUpdate = "ERROR";

     public function __construct()
     {
      $this->getData();
      $this->parseData();
      //$this->generateImage();

      //$this->sendSMS();
      $this->checkChange();
     }

     public function checkChange()
     {
      $cyclone = file_get_contents($this->file);
      if ($cyclone != $this->_cyclone)
      {
       $this->sendSMS2();
      }
      file_put_contents($this->file, $this->_cyclone);
     }

     public function sendSMS2()
     {
      if ($this->_cyclone != "")
      {
       $cmd = "/opt/mono/2.0/bin/mono /root/GoogleCalendar/GoogleCalendar.exe " . $this->googleUsername . " " . $this->googlePassword . " default \"Typhoon No.: " . $this->_cyclone . "\" \"Tropical Cyclone Signal No.: " . $this->_cyclone . ", Temperature: " . $this->_temperature . ", Humidity: " . $this->_humidity . "\" HK";
      }
      else
      {
       $cmd = "/opt/mono/2.0/bin/mono /root/GoogleCalendar/GoogleCalendar.exe " . $this->googleUsername . "  " . $this->googlePassword . " default \"Temperature: " . $this->_temperature . ", Humidity: " . $this->_humidity . "\" Dummy HK";
      }
      system($cmd);
     }

     public function sendSMS()
     {
      echo "SEND SMS";
      $cmd = "/opt/mono/2.0/bin/mono /root/GoogleCalendar/GoogleCalendar.exe " . $this->googleUsername . " " . $this->googlePassword . " default \"Temperature: " . $this->_temperature . ", Humidity: " . $this->_humidity . "\" Dummy HK";
      system($cmd);
      
     }

     public function getXML()
     {

     }

     // Get Weather Data From Web
     private function getData()
     {
      $fh = fopen($this->url, 'r') or die("Error on Connect");
      $this->rawData = stream_get_contents($fh);
      fclose($fh);
     }

     // Parse Weather Data
     private function parseData()
     {

      $start = strpos($this->rawData, "<pre>");
      $end = strpos($this->rawData, "</pre>");

      if(preg_match('/Bulletin updated at (.*)\</i', $this->rawData, $matches) == 1)
      {
       $this->_lastUpdate = $matches[1];
      }

      $tempString = substr($this->rawData, $start + 5, $end - $start - 5);
      //echo $tempString;
      
      if(preg_match('/AIR TEMPERATURE : (.*) DEGREES CELSIUS/i', $tempString, $matches) == 1)
      {
       $this->_temperature = $matches[1];
      }

      if(preg_match('/RELATIVE HUMIDITY : (.*) PER CENT/i', $tempString, $matches)== 1)
      {
       $this->_humidity = $matches[1];
      }
      
      if(preg_match('/WEATHER CARTOON : NO.(.*) - /i', $tempString, $matches) == 1)
      {
       $this->_weatherCartoon = $matches[1];
      }

      if(preg_match('/THE TROPICAL CYCLONE SIGNAL NO. (\d)/i', $tempString, $matches) == 1)
      { 
       $this->_cyclone = $matches[1];
       if ($this->_cyclone == "8")
       {
        $this->getWarn();
       }
      }

      
      if(preg_match("/DURING THE PAST HOUR THE MEAN UV INDEX RECORDED AT KING'S PARK : (.*)/i", $tempString, $matches) == 1)
      {
       $this->_uvIndex = $matches[1];
      }
     }

            private function getWarn()
            {
                    $fh = fopen($this->urlw, 'r');
                    $warn = stream_get_contents($fh);
                    fclose($fh);

                    if(preg_match('/NO. 8 (.*) GALE/i', $warn, $matches) == 1)
                    {
                            switch ($matches[1])
                            {
                                    case "SOUTHWEST":
                                            $this->_cyclone = "8sw";
                                            break;
                                    case "NORTHWEST":
                                            $this->_cyclone = "8nw";
                                            break;
                                    case "SOUTHEAST":
                                            $this->_cyclone = "8se";
                                            break;
                                    case "NORTHEAST":
                                            $this->_cyclone = "8ne";
                                            break;
                            }
                    }

            }

    }

    ?>

喜愛此BLOG的人同時都愛看

  • 沒有相關訂閱

更多網誌

網誌分類

瀏覽日期

2010
S M T W T F S
  1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31