//TODO: Unsubscribe
private void SetValue(TopicInfo ti, byte[] msgData, bool retained) {
if(ti!=null) {
if(!ti.path.StartsWith(Owner.path)) {
return; // not allow publish
}
object val;
switch(Type.GetTypeCode(ti.topic.valueType)) {
case TypeCode.Boolean:
val=(msgData[0]!=0);
break;
case TypeCode.Int64: {
long rv=(msgData[msgData.Length-1]&0x80)==0?0:-1;
for(int i=msgData.Length-1; i>=0; i--) {
rv<<=8;
rv|=msgData[i];
}
val=rv;
}
break;
case TypeCode.String:
val=Encoding.Default.GetString(msgData);
break;
case TypeCode.Object:
if(ti.topic.valueType==typeof(PLC.ByteArray)) {
val=new PLC.ByteArray(msgData);
break;
} else if(ti.topic.valueType==typeof(SmartTwi)) {
var sa=(ti.topic.GetValue() as SmartTwi);
if(sa==null) {
sa=new SmartTwi(ti.topic);
sa.Recv(msgData);
val=sa;
} else {
sa.Recv(msgData);
return;
}
break;
} else if(ti.topic.valueType==typeof(TWIDriver)) {
var twi=(ti.topic.GetValue() as TWIDriver);
if(twi==null) {
twi=new TWIDriver(ti.topic);
twi.Recv(msgData);
val=twi;
} else {
twi.Recv(msgData);
return;
}
break;
} else {
return;
}
default:
return;
}
ti.topic.saved=retained;
ti.topic.SetValue(val, new TopicChanged(TopicChanged.ChangeArt.Value, Owner));
}
}