   return wraped('{', body, '}');
 }
 
+String streamDiOnTask(String query){
+
+  int at = query.indexOf('?');
+  if(at >= 0){
+    String portsQuery = query.substring(at + 1);
+    if(!isInt(portsQuery)){
+      return NgReturnJson("Illegal query.", query);
+    }
+    di_stream_info.ports = strToUInt64(portsQuery);
+  }
+  // queryがない場合はportsを変更せずにstreamをenableにする.
+  di_stream_info.enabled = true;
+
+  return okStreamJson(di_stream_info.enabled, di_stream_info.ports);
+}
+
+String streamDiOffTask(String empty){
+  di_stream_info.enabled = false;
+  return okStreamJson(di_stream_info.enabled, di_stream_info.ports);
+}
+
+String streamAiOnTask(String query){
+  int at = query.indexOf('?');
+  if(at >= 0){
+    String portsQuery = query.substring(at + 1);
+    if(!isInt(portsQuery)){
+      return NgReturnJson("Illegal query.", query);
+    }
+    ai_stream_info.ports = strToUInt64(portsQuery);
+  }
+  // queryがない場合はportsを変更せずにstreamをenableにする.
+  ai_stream_info.enabled = true;
+
+  return okStreamJson(ai_stream_info.enabled, ai_stream_info.ports);
+}
+
+String streamAiOffTask(String empty){
+  ai_stream_info.enabled = false;
+  return okStreamJson(ai_stream_info.enabled, ai_stream_info.ports);
+}
+
+// portsはDI_STREAM_INFOのportsに合わせてuint64_tにしている
+String okStreamJson(bool enabled, uint64_t ports){
+  String body = stringJson("msg", "OK")
+    + "," + boolJson("enabled", enabled)
+    + "," + intJson("ports", ports);
+  return wraped('{', body, '}');
+
+}
 
 // シリアルを閉じる.
 String closeSerial(String empty){

  if(error){
    return error;
  }
  int port = strToInt(portQuery);
  int val = digitalRead(port);
  return ioReturnJson("OK", port, val);
}

     Swith digital pin mode.
     format  => d/mode/{port}?type={type}
     {port}  => port number.
     {type}  => INPUT | OUTPUT | INPUT_PULLUP
     example => d/mode/3?type=INPUT
   */
  {"d/mode/", &switchPinModeTask},

  /*
    DI連続転送ON
    format   => stream/di/on?{ports}
    {ports}  => 有効にするポート.対応するbitを1にする. 指定しない場合は前回と同じポートを使う.Defaultは0
    example  => stream/di/on?240
     240 = 0xf0 = 7 - 4番ポートを有効化


     streamのレスポンス

     streamのレスポンスはeventとして定義する

     {"event":"di", "datas":[
        {"msg":"OK","port":[port],"val":[val]},
        {"msg":"OK","port":[port],"val":[val]},
        ...
        {"msg":"OK","port":[port],"val":[val]}
     ]}
   */
  {"stream/di/on", &streamDiOnTask},

  /*
     DI連続転送OFF
     format => stream/di/off
   */
  {"stream/di/off", &streamDiOffTask},

  /*
    AI連続転送ON
    format   => stream/ai/on?{ports}
    {ports}  => 有効にするポート.対応するbitを1にする. 指定しない場合は前回と同じポートを使う.Defaultは0
    example  => stream/ai/on?15
     15 = 0xf = 3 - 0番ポートを有効化
   */
  {"stream/ai/on", &streamAiOnTask},

  /*
     AI連続転送OFF
     format => stream/ai/off
   */
  {"stream/ai/off", &streamAiOffTask},


  /*
    連続転送間隔設定
    format => stream/delay?{delayMillSec}
    {delayMillSec} => 変換間隔[msec]
   */
  //{"stream/delay", &setStreamDelayTask},

  /*
TODO: 紛らわしいので後で消す
     Close serial port.
     format => system/close
   */
