/*
 * Project IIC
 * Team 13 
 * Jaar: 2006 - 2007
 * Klas: E203
 *
 *
 * Met dank aan Wilbert Li team 14,
 * 
 */

import javax.swing.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.io.*;
import javax.comm.*;
import java.awt.*;
import java.util.*;


public class meddespencer extends JFrame{
 static CommPortIdentifier portID;
  static SerialPort serialPort;
  static OutputStream outStream;
  MyPanel pane;

  public meddespencer(){
    pane = new MyPanel();
    this.setContentPane(pane);
  }

  public static void main(String args[]){
    try{
      portID = CommPortIdentifier.getPortIdentifier("COM6");
      serialPort = (SerialPort) portID.open("Meddespencer toolkit",1000);
      serialPort.setSerialPortParams(115200,
                  SerialPort.DATABITS_8,
                  SerialPort.STOPBITS_1,
                  SerialPort.PARITY_NONE);
      serialPort.setInputBufferSize(4096);
      serialPort.setOutputBufferSize(4096);
      outStream  = serialPort.getOutputStream();
      meddespencer myApp = new meddespencer();
      	 myApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	  myApp.setSize(640,424);
   		   myApp.setTitle("Meddespencer toolkit");
   myApp.setVisible(true);
  } catch(Exception e){
   System.out.println("Fout : COM6 bestaat niet of is al in gebruik");
     System.exit(0);
   }
  }

  public static void write(String str) {
   	 try {
   	   outStream.write(str.getBytes());
   	 } catch (IOException e) {}
  }

  private void slaap() {
    try { Thread.sleep( 15000 ); } //timer 1000 = 1sec
    catch( InterruptedException e ) { }

}

class MyPanel extends JPanel {
    int i=0;
    int m2muit;
    int sms=0;
//    int sim=0638441055; // nummer van de simkaart
    char a;
    String str;
    JButton sms2,test, aan, sms1, uit, sentsms;
    MyPanel myself;
    private ImageIcon gif5 = new ImageIcon( "4.jpg");
    
    

    public MyPanel(){
      sms2   = new JButton("sms2");
      test = new JButton("M2M test");
      aan = new JButton("aan");
      sms1 = new JButton("sms1");
      uit = new JButton("uit" );
      sentsms = new JButton("stuur sms" );
      this.add(aan);
      this.add(uit);
      this.add(sentsms);
      this.add(test);
      this.add(sms1);
      this.add(sms2);
      
      sms2.addActionListener( new KnopHandler());
      sms1.addActionListener( new KnopHandler());
      test.addActionListener( new KnopHandler());
      aan.addActionListener( new KnopHandler());
      uit.addActionListener( new KnopHandler());
      sentsms.addActionListener( new KnopHandler());
	  myself = this;
    }

    public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.drawString("Data van PC naar M2M-bord",20,60);
      g.drawString("De decimale waarde  wordt naar COM6 geschreven",20,80);
      gif5.paintIcon( this, g, 0, 0 );
    }

    class KnopHandler implements ActionListener {
      public void actionPerformed (ActionEvent e){
        JComponent c = (JComponent) e.getSource();
        
        if (c == aan) {
        str = "at+wopen=1\n";
        meddespencer.write(str);
         System.out.print("_at+wopen=1 start m2m_");
         m2muit =0;
        }
        
        
        if (c==uit) {
        	str = "at+wopen=0\n";
        	meddespencer.write(str);
        	System.out.print("_at+wopen=0 stop m2m_ ");
        	m2muit =1;
        }
        
        
        
        if (c == sentsms) {
        	if (m2muit==1)
        	{System.out.print("_FOUT!!! m2m staat uit_ ");
        	}
        	else{	
        	System.out.print("_sms is verstuur ^_^ ");
        	str = "at+cmss="+sms+",0638343996\n";
        	meddespencer.write(str);}
        	
        }
        
        if (c == sms1) {
        	sms =3;
        	System.out.print("_smsbericht 1 mr X_");
        }
        
        if (c == sms2 ) {
           sms =2;
        	System.out.print("_smsberichtt 2 paul_");
        }
        
        if (c == test) {
        
         
            str = "atd0638343996\n";
            meddespencer.write(str);
        	System.out.print("_bel_");
        	slaap(); // 15sec
        	System.out.print("_hang op_");
            str = "ath\n";
            meddespencer.write(str);
        
        }
	/*	maak je eigen sms
	 *if (c==?) {
				str = "at+cmgw ="+sim+"\n";
				str = // voer bericht in en dan ctrl+z is gelijk aan \1A 
				(read +cmgw:nummer )
				teller vanaf 3 ++i; == sms 
		}*/

        myself.repaint();
    }
   }
}
}