예제 #1
0
    public void Start(Power firmware)
    {
      var config = new RestApiConfiguration();

      config.AddHandler("GET", "/boot/ping", "is server alive")
        .ReturnNothing()
        .HandledBy(() => {});

      config.AddHandler("GET", "/boot/firmware", "get firmware info")
        .ReturnJson()
        .HandledBy(() => firmware.GetFirmwareInfoAsJson());

      config.AddHandler("PUT", "/boot/firmware", "update firmware")
        .ReturnJson()
        .HandledWithStreamBy((s) => firmware.UpdateFirmware(s));

      config.AddHandler("POST", "/boot/sysinfo", "get system state")
        .ReturnJson()
        .HandledBy(() => HardwareInfo.Instance.GetSystemInfo());

      config.AddHandler("POST", "/boot/diskinfo", "list content of filesystem")
        .ReturnJson()
        .HandledBy(() => HardwareInfo.Instance.GetDiskInfo());

      config.AddHandler("GET", "/boot/networks", "list network information")
        .ReturnJson()
        .HandledBy(() => HardwareInfo.Instance.GetNetworkInterfaceInfo());

      config.AddHandler("GET", "/boot/log/full", "get full log")
        .ReturnJson()
        .HandledBy(() => Logger.Instance.Full.GetLinesAsJson());

      config.AddHandler("GET", "/boot/log/errors", "get error log")
        .ReturnJson()
        .HandledBy(() => Logger.Instance.Errors.GetLinesAsJson());

      config.AddHandler("POST", "/boot/ntp/sync", "sync time with NTP")
        .ReturnJson()
        .HandledBy(() =>
        {
          RealTimeClock.Instance.UpdateFromNTP();
          var status = new JsonObject();
          status.Add("time", DateTime.Now);
          return status;
        });

      config.AddHandler("POST", "/boot/reboot", "reboot the device")
        .SetNotes("This will reboot the device and not return")
        .ReturnNothing()
        .HandledBy(() => firmware.Reboot());

      var credentials = CredentialValidatorFactory.CreateSimpleValidator("ChickenHouse", "admin", "hnsefyk");
      var webService = new WebService(c_servicePort, credentials, config);

      string prefix = Name + ": ";
      webService.Logging.OnLogInfo += message => Logger.Instance.LogInfo(prefix + message);
      webService.Logging.OnLogError += message => Logger.Instance.LogError(prefix + message);
      webService.Logging.OnLogException += (message, ex) => Logger.Instance.LogException(prefix + message, ex);
      webService.Start();
    }