public void Add(Enum id, IOBoardController ioBoard)
        {
            if (ioBoard == null) throw new ArgumentNullException(nameof(ioBoard));

            ExposeToApi(ioBoard);
            _ioBoards.Add(id, ioBoard);
        }
        private void ExposeToApi(IOBoardController ioBoard)
        {
            _httpApiController.Handle(HttpMethod.Get, "device").WithSegment(ioBoard.Id).Using(c => c.Response.Body = new JsonBody(ApiGet(ioBoard)));

            _httpApiController.Handle(HttpMethod.Put, "device")
                .WithSegment(ioBoard.Id)
                .WithRequiredJsonBody()
                .Using(c => ApiPost(ioBoard, c.Request.JsonBody));

            _httpApiController.Handle(HttpMethod.Patch, "device")
                .WithSegment(ioBoard.Id)
                .WithRequiredJsonBody()
                .Using(c => ApplyJSONPortState(ioBoard, c.Request.JsonBody));
        }
        private void ApiPost(IOBoardController ioBoard, JsonObject value)
        {
            JsonArray state = value.GetNamedArray("state", null);
            if (state != null)
            {
                byte[] buffer = JSONValueToByteArray(state);
                ioBoard.SetState(buffer);
            }

            var commit = value.GetNamedBoolean("commit", true);
            if (commit)
            {
                ioBoard.CommitChanges();
            }
        }
 private JsonObject ApiGet(IOBoardController ioBoard)
 {
     var result = new JsonObject();
     result.SetNamedValue("state", ByteArrayToJSONValue(ioBoard.GetState()));
     result.SetNamedValue("committed-state", ByteArrayToJSONValue(ioBoard.GetCommittedState()));
     return result;
 }
        private void ApplyJSONPortState(IOBoardController ioBoard, JsonObject value)
        {
            int port = (int) value.GetNamedNumber("port", 0);
            bool state = value.GetNamedBoolean("state", false);
            bool commit = value.GetNamedBoolean("commit", true);

            ioBoard.SetPortState(port, state ? BinaryState.High : BinaryState.Low);

            if (commit)
            {
                ioBoard.CommitChanges();
            }
        }