Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developing MyTrafficCam

Developing MyTrafficCam

Sharing of how I build a web service for monitoring traffic condition in Malaysia-Singapore border. A community project by HackerspaceJB, in Johor Bahru.

Avatar for Leong Hean Hong

Leong Hean Hong

August 02, 2014
Tweet

More Decks by Leong Hean Hong

Other Decks in Programming

Transcript

  1. About • MTC shows traffic condition for JB-SG travellers •

    MTC aggregates cameras from different sources • I talk about how it works • Share collected data • Describe some of the challenges
  2. System Design • Camera sources ◦ LTA (Singapore) ◦ LLM

    (Malaysia) ◦ Own camera @ Menara Cyberport • Providing feed to ◦ Mobile clients (iOS + Android) ◦ Partner’s website Sponsor: Common Extract
  3. Software Stack • CentOS • Apache • PHP 5.2 ◦

    GluePHP (micro-framework) ◦ Twig Templating library Sponsor: Property.cc
  4. Development Tools • Sublime Text ◦ Programming editor • Filezilla

    ◦ For deployment on production server • Git ◦ Source control • Bitbucket ◦ Free private repo ◦ Code backup
  5. Defining API • Client makes request via HTTP • Response

    in JSON • API specifications documented in Google Doc ◦ Establish standardization across team
  6. Time-sensitive Photo public static function photoURL($camId) { $time = time();

    return "/photo/{$camId}/{$time}/".self::genHash($camId, $time); } Add timestamp to photo URL, expires in 5 seconds
  7. Tie Photo To Client’s IP public static function photoURL($camId) {

    return strtr('http://:host/v1api/photo/:camId/:hash', array( ':host' => $_SERVER['HTTP_HOST'], ':camId' => $camId, ':hash' => self::photoHash($camId) )); } protected static function photoHash($camId) { return sha1(self::HASH_KEY.$camId.$_SERVER['REMOTE_ADDR']); } Prevent reuse of photo across devices
  8. Rate-limiting Control • Log IP address • Allow not more

    than 10 requests per second per IP ◦ Currently we have only 6 cameras