Terned Around About Nullables
by Remy Porter
in CodeSOD
on 2021-09-27
John H works with some industrial devices. After a recent upgrade at the falicity, the new control software just felt like it was packed with WTFs. Fortunately, John was able to get at the C# source code for these devices, which lets us see some of the logic used…
public bool SetCrossConveyorDoor(CrossConveyorDoorInfo ccdi, bool setOpen)
{
if (!ccdi.PowerBoxId.HasValue)
return false;
ulong? powerBoxId = ccdi.PowerBoxId;
ulong pbid;
ulong ccId;
ulong rowId;
ulong targetIdx;
PBCrossConveyorConfiguration.ExtractIdsFromPowerboxId(powerBoxId.Value, out pbid, out ccId, out rowId, out targetIdx);
TextWriter textWriter = Console.Out;
object[] objArray1 = new object[8];
objArray1[0] = (object) pbid;
objArray1[1] = (object) ccId;
objArray1[2] = (object) setOpen;
object[] objArray2 = objArray1;
powerBoxId = ccdi.PowerBoxId;
ulong local = powerBoxId.Value;
objArray2[3] = (object) local;
objArray1[4] = (object) pbid;
objArray1[5] = (object) ccId;
objArray1[6] = (object) rowId;
objArray1[7] = (object) targetIdx;
object[] objArray3 = objArray1;
textWriter.WriteLine(
"Sending CCD command to pbid = {0}, ccdId = {1}, Open={2}, orig PowerBoxId: {3} - divided:{4}/{5}/{6}/{7}",
objArray3);
bool? nullable1 = this.CopyDeviceToRegisters((int) (ushort) ccId);
if ((!nullable1.GetValueOrDefault() ? 1 : (!nullable1.HasValue ? 1 : 0)) != 0)
return false;
byte? nullable2 = this.ReadDeviceRegister(19, "CrossConvDoor");
byte num = nullable2.HasValue ? nullable2.GetValueOrDefault() : (byte) 0;
byte registerValue = setOpen ? (byte) ((int) num & -225 | 1 << (int) targetIdx) : (byte) ((int) num & -225 | 16);
Console.Out.WriteLine("ccdid = {0} targetIdx = {1}, b={2:X2}", (object) ccId, (object) targetIdx, (object) registerValue);
this.WriteDeviceRegister(19, registerValue, "CrossConvDoor");
nullable1 = this.CopyRegistersToDevice();
return nullable1.GetValueOrDefault() && nullable1.HasValue;
}