public IList <Instruction <G> > Optimize(IList <Instruction <G> > instructions) { bool enableOptimizeOverridingInstructions = EnableOptimizeOverridingInstructions; bool enableOptimizeOpposingInstructions = EnableOptimizeOpposingInstructions; bool enableOptimizeDuplicatesInstructions = EnableOptimizeDuplicatesInstructions; bool enableOptimizeAssignmentInstructions = EnableOptimizeAssignmentInstructions; bool enableOptimizeCopyPushInstructions = EnableOptimizeCopyPushInstructions; bool enableOptimizeMultipleGetPointersInstructions = EnableOptimizeMultipleGetPointersInstructions; _oldInstructions = instructions; _optmized = false; _instructionOffsets = new List <InstructionOffset>(); Stack <GroupPoint> groupPoints = new Stack <GroupPoint>(); bool continueOptimization = true; while (continueOptimization) { int lastGroupIndex = -1; _groupStartIndex = 0; _currentIndex = 0; _innerIndex = 0; _innerOffset = 0; _currentGroupId = "0"; continueOptimization = false; _newInstructions = new List <Instruction <G> >(_oldInstructions.Count); for (; _currentIndex < _oldInstructions.Count; _currentIndex++, _innerIndex++) { Instruction <G> instruction = _oldInstructions[_currentIndex]; bool instructionOptimized = false; switch (instruction.Code) { case InstructionCode.G: groupPoints.Push(new GroupPoint(_currentGroupId, ++lastGroupIndex, _groupStartIndex, _innerIndex, _innerOffset)); _currentGroupId = $"{_currentGroupId}.{lastGroupIndex}"; lastGroupIndex = -1; _groupStartIndex = _currentIndex + 1; _innerOffset = 0; _innerIndex = 0; break; case InstructionCode.GE: GroupPoint groupPoint = groupPoints.Pop(); _innerIndex += groupPoint.InnerIndex; _innerOffset += groupPoint.Offset; _currentGroupId = groupPoint.GroupId; lastGroupIndex = groupPoint.LastGroupIndex; _groupStartIndex = groupPoint.GroupStartIndex; break; default: instructionOptimized = (enableOptimizeOverridingInstructions && OptimizeOverridingInstructions()) || (enableOptimizeDuplicatesInstructions && OptimizeDuplicateInstructions()) || (enableOptimizeOpposingInstructions && OptimizeOpposingInstructions()) || (enableOptimizeAssignmentInstructions && OptimizeAssignmentInstructions()) || (enableOptimizeCopyPushInstructions && OptimizeCopyPushInstructions()) || (enableOptimizeMultipleGetPointersInstructions && OptimizeMultipleGetPointersInstructions()); break; } if (!instructionOptimized) { _newInstructions.Add(_oldInstructions[_currentIndex]); } continueOptimization |= instructionOptimized; } _oldInstructions = _newInstructions; _optmized |= continueOptimization; } if (_optmized) { PublishChanges(); } return(_newInstructions); }
public JObject ReadModbusToJson(int siteId, int DeviceIndex, GroupPoint slaves, out HashEntry[] hashEntries) { JObject datarow = new JObject(); List <HashEntry> entries = new List <HashEntry>(); hashEntries = entries.ToArray(); try { if (master == null) { return(datarow); } ushort startAddr = slaves.StartAddress; ushort pointCnt = (ushort)slaves.AiMaps.Sum(x => x.DataType.Size); ushort[] datas = null; lock (lockerObj) { switch ((modbus_io)slaves.IoType) { case modbus_io.ANALOG_INPUT: datas = master.ReadInputRegisters(_config.SlaveId, startAddr, pointCnt); break; case modbus_io.HOLDING_REGISTER: datas = master.ReadHoldingRegisters(_config.SlaveId, startAddr, pointCnt); break; } } DateTime timeStamp = DateTime.Now; datarow.Add("groupid", slaves.GroupId); datarow.Add("groupname", slaves.GroupName); datarow.Add("deviceId", _config.DeviceName); datarow.Add("siteId", siteId); string normalizedeviceid = slaves.GroupId == 1 ? "PCS" : "BMS"; datarow.Add("normalizedeviceid", normalizedeviceid + DeviceIndex.ToString()); entries.Add(new HashEntry("timestamp", timeStamp.ToString())); datarow.Add("timestamp", DateTime.Now.ToString("yyyyMMddHHmmss")); int bitIdx = 0; foreach (AiMap register in slaves.AiMaps) { if (register.Disable == 1) { continue; } //Console.WriteLine("READ REGISTER: " + register.Name); byte[] buffer = GetReadBytes(register.DataType, bitIdx, datas); bitIdx += register.DataType.Size; dynamic value = 0f; switch ((modbus_type)register.DataType.TypeCode) { case modbus_type.DT_BOOLEAN: value = BitConverter.ToBoolean(buffer); break; case modbus_type.DT_INT32: value = BitConverter.ToInt32(buffer); break; case modbus_type.DT_INT16: value = BitConverter.ToInt16(buffer); break; case modbus_type.DT_UINT32: value = BitConverter.ToUInt32(buffer); break; case modbus_type.DT_UINT16: value = BitConverter.ToUInt16(buffer); break; case modbus_type.DT_FLOAT: value = BitConverter.ToSingle(buffer); break; } value = register.ConvertValue(value); entries.Add(new HashEntry(register.Name, value.ToString())); datarow.Add(register.Name, value.ToString()); //registers.Add(register.ConvertRegister(Convert.ToSingle(value))); } hashEntries = entries.ToArray(); return(datarow); } catch (Exception ex) { logger.LogError(ex, ex.Message); return(null); } }