package org.myrobotlab.service;

import org.myrobotlab.framework.Service;
import org.myrobotlab.framework.ServiceType;
import org.myrobotlab.logging.Level;
import org.myrobotlab.logging.LoggerFactory;
import org.myrobotlab.logging.LoggingFactory;
import org.slf4j.Logger;
import org.myrobotlab.service.interfaces.ServoControl;

public class Intro extends Service {

  private static final long serialVersionUID = 1L;

  public final static Logger log = LoggerFactory.getLogger(Intro.class);

  public Intro(String n, String id) {
    super(n, id);
  }

  /**
   * This static method returns all the details of the class without it having
   * to be constructed. It has description, categories, dependencies, and peer
   * definitions.
   * 
   * @return ServiceType - returns all the data
   * 
   */
  static public ServiceType getMetaData() {

    ServiceType meta = new ServiceType(Intro.class);
    meta.addDescription("Introduction to MyRobotlab");
    meta.setAvailable(true); 
    meta.addCategory("general");
    meta.addPeer("servo", "Servo", "servo");
    return meta;
  }

  public static void main(String[] args) {
    try {

      LoggingFactory.init(Level.INFO);

      Runtime.start("intro", "Intro");
      Runtime.start("servo", "Servo");
      Runtime.start("gui", "SwingGui");

    } catch (Exception e) {
      log.error("main threw", e);
    }
  }

  transient Servo servo;

  boolean isServoActivated = false;

  public boolean isServoActivated() {
        return isServoActivated;
        }



  public Servo startServo(String port) {
    return startServo(port, 3);
  }

  public Servo startServo(String port, int pin) {
  
    if (servo == null) {
      speakBlocking("starting servo");
      isServoActivated = true;

      servo = (Servo) startPeer("servo");

      if (port != null) {
        try {
          speakBlocking(port);
          Arduino controller = (Arduino) startPeer("controller");
          controller.connect(port);
          controller.attach(controller, pin);
        } catch (Exception e) {
          error(e);
        }
      }
    }
    return servo;
  }

  public void stopServo() {
    speakBlocking("stopping servo");
    releasePeer("servo");
    isServoActivated = false;
  }

}

@@ -1086,24 +1086,105 @@
  %22servo%22);%0D%0A
+    meta.addPeer(%22controller%22, %22Arduino%22, %22Arduino controller for this servo%22);%0D%0A
     return m
@@ -1189,16 +1189,16 @@
  meta;%0D%0A
-
   %7D%0D%0A%0D%0A 
@@ -1513,16 +1513,23 @@
 nt Servo
+Control
  servo;%0D
@@ -1617,44 +1617,1244 @@
     
-    return isServoActivated;%0D%0A      
+return isServoActivated;%0D%0A  %7D%0D%0A  %0D%0A%0D%0A  public Servo(String n, String id) throws Exception %7B%0D%0A    super(n, id);%0D%0A%0D%0A    startPeers();%0D%0A%0D%0A    servo.setPin(3);%0D%0A%0D%0A    servo.setMinMax(0.0, 180.0);%0D%0A%0D%0A    servo.setRest(90.0);%0D%0A%0D%0A    servo.setPosition(90.0);%0D%0A%0D%0A    setSpeed(100.0);%0D%0A  %7D%0D%0A%0D%0A  public void broadcastState() %7B%0D%0A    super.broadcastState();%0D%0A    servo.broadcastState();%0D%0A  %7D%0D%0A%0D%0A  public void disable() %7B%0D%0A    servo.disable();%0D%0A  %7D%0D%0A%0D%0A  public void enable() %7B%0D%0A    sleep(Intro.attachPauseMs);%0D%0A    servo.enable();%0D%0A  %7D%0D%0A%0D%0A  public void fullSpeed() %7B%0D%0A    servo.fullSpeed();%0D%0A  %7D%0D%0A%0D%0A  public ServoControl getServo() %7B%0D%0A    return servo;%0D%0A  %7D%0D%0A%0D%0A  @Override%0D%0A  public boolean save() %7B%0D%0A    super.save();%0D%0A    servo.save();%0D%0A    return true;%0D%0A  %7D%0D%0A%0D%0A  public void setAutoDisable(Boolean idleTimeoutMs) %7B%0D%0A    servo.setAutoDisable(idleTimeoutMs);%0D%0A  %7D%0D%0A%0D%0A  public void setLimits(double servoMin, double servoMax) %7B%0D%0A    servo.setMinMax(servoMin, servoMax);%0D%0A  %7D%0D%0A%0D%0A  public void setServo(ServoControl servo) %7B%0D%0A    this.servo = servo;%0D%0A  %7D%0D%0A%0D%0A  public void setSpeed(Double servo) %7B%0D%0A    this.servo.setSpeed(servo);%0D%0A  %7D%0D%0A%0D%0A    public void stop() %7B%0D%0A    servo.stop();%0D%0A  %7D%0D%0A%0D%0A  public void waitTargetPos() %7B%0D%0A    servo.waitTargetPos();%0D%0A
   %7D%0D
@@ -2930,17 +2930,19 @@
 o(port, 
-3
+pin
 );%0D%0A  %7D%0D

add servo to Intro2