// GET api/run/{IPアドレス("."を"_"に置き換えたもの)}/{コマンド("*"と"\r\n"を抜いたもの)}
        public string Get(string id, string comand)
        {
            string ip = id.Replace('_','.');
            comand = "*"+ comand;
            string res;

            Common common = new Common();
            try { 
                res = common.ConnectRemocon(ip, comand); 
            } catch {
                return "iRemocon(" + ip + ") に接続できませんでした。";
            }

            return res;
        }
        public ActionResult ChangeCodeInfo(ChangeCodeModel model) {

            var target = db.iRemocons.Where(p => p.IPAddress.Equals(model.IPAddress)).Single();
            var targetcode = target.RegistrationCodes.Where(p=>p.RegistrationCode1 == model.Code).Single();

            if (ModelState.IsValid) {

                if (!model.Check) {
                    Common common = new Common();
                    string res;
                    try {
                        res = common.ConnectRemocon(model.IPAddress, "*au\r\n");
                    } catch {
                        ModelState.AddModelError("", "iRemocon(" + model.IPAddress + ") に接続できませんでした");
                        return View(model);
                    }
                    if (!res.Equals("ok")) {
                        ModelState.AddModelError("", "iRemocon(" + model.IPAddress + ") に接続できませんでした");
                        return View(model);
                    }

                    try {
                        res = common.ConnectRemocon(model.IPAddress, "*ic;" + model.Code + "\r\n");
                    } catch {
                        ModelState.AddModelError("", "iRemocon(" + model.IPAddress + ") に接続できませんでした");
                        return View(model);
                    }
                    if (!res.Equals("ic;ok")) {
                        ModelState.AddModelError("", "iRemocon(" + model.IPAddress + ") からのエラー: " + res);
                        return View(model);
                    }
                }


                targetcode.Detail = model.Detail;
                db.SaveChanges();

                return RedirectToAction("ShowCodes", "Home", new { id = target.ID });

            }

            // ここで問題が発生した場合はフォームを再表示します
            return View(model);
        }
        public ActionResult RegisterCode(RegisterCodeModel model) {

            var target = db.iRemocons.Where(p => p.IPAddress.Equals(model.IPAddress)).Single();

            if (ModelState.IsValid) {

                if (model.IPAddress.Equals("192.168.10.80")) {
                    if (model.Code < 100 || 1500 < model.Code) {
                        ModelState.AddModelError("", "登録可能なコード番号は100~1500です。");
                        return View(model);
                    }
                } else {
                    if (model.Code < 1 || 1500 < model.Code) {
                        ModelState.AddModelError("", "登録可能なコード番号は1~1500です。");
                        return View(model);
                    }
                }
                
                foreach (var x in target.RegistrationCodes) {
                    if (model.Code == x.RegistrationCode1) {
                        ModelState.AddModelError("", "既に登録済みのコード番号です。");
                        return View(model);
                    }
                }

                Common common = new Common();
                string res;
                try {
                    res = common.ConnectRemocon(model.IPAddress, "*au\r\n");
                } catch {
                    ModelState.AddModelError("", "iRemocon("+model.IPAddress+") に接続できませんでした");
                    return View(model);
                }
                if (!res.Equals("ok")) {
                    ModelState.AddModelError("", "iRemocon(" + model.IPAddress + ") に接続できませんでした");
                    return View(model);
                }

                try {
                    res = common.ConnectRemocon(model.IPAddress, "*ic;"+ model.Code +"\r\n");
                } catch {
                    ModelState.AddModelError("", "iRemocon(" + model.IPAddress + ") に接続できませんでした");
                    return View(model);
                }
                if (!res.Equals("ic;ok")) {
                    ModelState.AddModelError("", "iRemocon(" + model.IPAddress + ") からのエラー: " + res);
                    return View(model);
                }

                RegistrationCode insert = new RegistrationCode();
                insert.RegistrationCode1 = model.Code;
                insert.Detail = model.Detail;
                target.RegistrationCodes.Add(insert);
                db.SaveChanges();

                return RedirectToAction("ShowCodes", "Home", new { id = target.ID });

            }

            // ここで問題が発生した場合はフォームを再表示します
            return View(model);
        }