#!/usr/bin/perl -w
# Filename : server_iot.pl
#
# look at URL and return different coloured circle. 
# In web broswer use one of these URL
# http://127.0.0.1:7890/on  
# http://127.0.0.1:7890/off
#
# This also writes to log.js
# 
#
# perl server_iot.pl
#
# use web browser to open http://127.0.0.1:7890
#
# This ran on Raspberry Pi, but stopped after I logged out. 
#
# It outputs to terminal and reads from Terminal
#
# 
# HowTo:-
#
# Start with very simple perl tcpip server example
#
# write a very simple web page and post to web server and open it.
# 
# use Wireshark to capture the page being fetched and save text.
#
# Get TCP/IP example to print out captured HTML headers and web page
#
# Modify TCP/IP example to read input from Web Browser
#
# Get Web browser to open one of these
# http://127.0.0.1:7890/on
# http://127.0.0.1:7890/off
#
# Modify TCP/IP example to look for GET and decode "/on" or "/off"
# 
# output different HTML or <SVG>
#
# A simple IOT web example
#
#

use strict;
use Socket;

# use port 7890 as default
my $port = shift || 7890;
my $proto = getprotobyname('tcp');
my $server = "localhost";  # Host IP running the server

   $server = "0.0.0.0";  # Host IP running the server

my $count =1000;

my $mysvg= <<'END_MESSAGE';
<svg width="640" height="480" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">
 <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
 <g>
  <title>Layer 1</title>
  <rect stroke-width="5" stroke="#000000" 
  fill="#808080" 
  id="svg_1" height="299" width="109" y="96.5" x="40.5"/>
  <circle stroke-dasharray="null" stroke-width="5" stroke="#000000" fill="#ff8080" id="svg_2" r="29.960938" cy="290.000001" cx="191.000001"/>
  <text xml:space="preserve" text-anchor="middle" font-family="Sans-serif" font-size="24" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000" id="svg_9" y="179.5" x="311">min</text>
  <line x1="347" y1="179.5" x2="348" y2="326.49999" id="svg_16" stroke="#d31717" stroke-width="5" stroke-dasharray="null" fill="none"/>
  <circle cx="366" cy="181.5" r="2" id="svg_21" fill="#f49aa0" stroke="#d31717" stroke-width="5" stroke-dasharray="null"/>
 </g>
</svg>
END_MESSAGE

# split svg up so fill colour can be selected by the GET statement.
my $mysvg1= <<'END_MESSAGE';
<svg width="640" height="480" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">
 <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
 <g>
  <title>Layer 1</title>
  <circle stroke-width="5" stroke="#000000" 
END_MESSAGE

my $fill = "fill=#808080\n";

my $mysvg2= <<'END_MESSAGE';
  id="svg_1" height="100" width="100" cy="70" cx="70" r="30"/>

  <text xml:space="preserve" text-anchor="middle" font-family="Sans-serif" font-size="24" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000"  
  y="70" x="200">POWER</text>

 </g>
</svg>
END_MESSAGE



my $header="<!DOCTYPE html>\n<body><H1>IOT</H1><A href=http://127.0.0.1:7890/on> on </a> | <A href=http://127.0.0.1:7890/off> off </a> | <A href=./on> on </a> | <A href=./off> off </a> <HR>";
my $tail  ="</body></html>";

#my $mysvglen = length( $mysvg );

# create a socket, make it reusable
socket(SOCKET, PF_INET, SOCK_STREAM, $proto)
   or die "Can't open socket $!\n";
setsockopt(SOCKET, SOL_SOCKET, SO_REUSEADDR, 1)
   or die "Can't set socket option to SO_REUSEADDR $!\n";

# bind to a port, then listen
bind( SOCKET, pack_sockaddr_in($port, inet_aton($server)))
   or die "Can't bind to port $port! \n";

listen(SOCKET, 5) or die "listen: $!";
print "SERVER started on port $port\n";
print "use web browser to open http://127.0.0.1:7890 \n";



# accepting a connection
my $client_addr;
my $buffer ;
my $buffer_len ;
my $name ;
my $value ;
my $colour ;
my $GUESTFJS ;
my $finished ;
my $ContentLen = 0;

while ($client_addr = accept(NEW_SOCKET, SOCKET)) {
   # send them a message, close connection

   #my $name = gethostbyaddr($client_addr, AF_INET );
#   print NEW_SOCKET "Smile from the server";
#   print "Connection recieved from $name\n";
   print "Connection received\n";

   #read( NEW_SOCKET, $buffer, 1000);
   #sleep(0.1);
   
   
   $buffer=<NEW_SOCKET>;
   print "$buffer";
   ($name, $value) = split(/ /, $buffer); 
   print "$buffer : $name ___ $value";
   $fill ="fill=#8080ff\n";
   if ( $value eq "/on" ) { print "Turn on \n";  $fill ="fill=#ffff80\n" }   
   if ( $value eq "/off" ){ print "Turn off \n"; $fill ="fill=#80ff80\n"  }   
  
  open ($GUESTFJS,">>log.js") || &file_error( $GUESTFJS, "log.js" ); 

  #Lock file while updating file to prevent race conddition update.
  # This has not been a big problem as the script does not take long.
  # A simple edit was needed to remove corrupted posts.
  
  # This blocks if file is locked, and removed when script ends.

  flock $GUESTFJS, 2 || &file_error( $GUESTFJS, "lock: log.js" );
  # get time stamp 
  #&setDate();

  # record which page and what browser was used.
  print $GUESTFJS "//". $buffer;
  # This was split earlier.
  print $GUESTFJS "gbF( \"$value\" ) \n";

  close ( $GUESTFJS );
  
  $finished = ( 1==0 );
  while ( !$finished  ) {
     $buffer = <NEW_SOCKET> ;
     print( $buffer ) ;
     ($name, $value) = split(/ /, $buffer); 
     if ( $name eq "Content-Length:" ){ $ContentLen = $value }
     $finished = ( length($buffer) < 3 );
  }


  $finished = ( 1==0 );
  
  $buffer_len = read( NEW_SOCKET , $buffer , $ContentLen ) ;
  print( "\nlength: ".( length $buffer )     . "\n" ) ;
  print( "\nlength: ".( length $buffer_len ) . "\n" ) ;
  print( "\nbuffer: ".  $buffer . "\n");

$mysvg=  $header.$mysvg1.$fill.$mysvg2.$tail;

my $mysvglen = length( $mysvg );

      
   #$buffer=<NEW_SOCKET>;
   #print "$buffer";
   
   print NEW_SOCKET "HTTP/1.0 200 OK\n" ;
   print NEW_SOCKET "Date: Sat, 10 Jul 2021 13:47:51 GMT\n" ;
   print NEW_SOCKET "Last-Modified: Mon, 30 Apr 2022 06:58:50 GMT\n" ;
   print NEW_SOCKET "Accept-Ranges: bytes\n" ;
   print NEW_SOCKET "Content-Length: $mysvglen \n" ;
   print NEW_SOCKET "Connection: close\n" ;
   print NEW_SOCKET "Content-Type: text/html;\n " ;
   print NEW_SOCKET "\n" ;
   print NEW_SOCKET "\n" ;
#   print NEW_SOCKET "<h1>doug</h1>\n" ;
#   print NEW_SOCKET "$count\n" ;
#   print NEW_SOCKET "doug\n" ;
#   print NEW_SOCKET "1234\n" ;
   print NEW_SOCKET $mysvg ;
   
   close NEW_SOCKET ;
   $count = $count+1;
}





