iipRequestSetProperty method

void iipRequestSetProperty(
  1. int callback,
  2. int resourceId,
  3. int index,
  4. DC content,
)

Implementation

void iipRequestSetProperty(
    int callback, int resourceId, int index, DC content) {
  Warehouse.getById(resourceId).then((r) {
    if (r != null) {
      var pt = r.instance?.template.getPropertyTemplateByIndex(index);
      if (pt != null) {
        Codec.parse(content, 0, this).then((value) {
          if (r is DistributedResource) {
            // propagation
            (r as DistributedResource).set(index, value).then<dynamic>((x) {
              sendReply(IIPPacketAction.SetProperty, callback).done();
            }).error((x) {
              sendError(x.type, callback, x.code, x.message);
            });
          } else {
            /*
#if NETSTANDARD1_5
                            var pi = r.GetType().GetTypeInfo().GetProperty(pt.Name);
#else
                            var pi = r.GetType().GetProperty(pt.Name);
#endif*/

            var pi = null; // pt.Info;

            if (pi != null) {
              if (r.instance?.applicable(_session as Session,
                      ActionType.SetProperty, pt, this) ==
                  Ruling.Denied) {
                sendError(ErrorType.Exception, callback,
                    ExceptionCode.SetPropertyDenied.index);
                return;
              }

              if (!pi.CanWrite) {
                sendError(ErrorType.Management, callback,
                    ExceptionCode.ReadOnlyProperty.index);
                return;
              }

              if (pi.propertyType.runtimeType == DistributedPropertyContext) {
                value = new DistributedPropertyContext.setter(this, value);
              } else {
                // cast new value type to property type
                // value = DC.castConvert(value, pi.PropertyType);
              }

              try {
                pi.setValue(r, value);
                sendReply(IIPPacketAction.SetProperty, callback).done();
              } catch (ex) {
                sendError(ErrorType.Exception, callback, 0, ex.toString());
              }
            } else {
              // pt found, pi not found, this should never happen
              sendError(ErrorType.Management, callback,
                  ExceptionCode.PropertyNotFound.index);
            }
          }
        });
      } else {
        // property not found
        sendError(ErrorType.Management, callback,
            ExceptionCode.PropertyNotFound.index);
      }
    } else {
      // resource not found
      sendError(ErrorType.Management, callback,
          ExceptionCode.ResourceNotFound.index);
    }
  });
}