consume

   Private Sub Consume_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim oTranslate As New Translate
        oTranslate.TranslateForm(Me)
        oTranslate = Nothing
    End Sub

    Private Sub ShowScreen()

        MsgBox("Under Consuruction", MsgBoxStyle.Information)
        Exit Sub

        Call SetListViews()
        Me.ShowDialog()
    End Sub

    Private Sub SetListViews()
        Dim oCustList As New CustListView

        oCustList.SetUp(Me.LstVwMaterialsToConsume, "Material to Consume|Quantity Needed|Unit of Measure")
        oCustList.SetUp(Me.LstVwMaterialInventory, "SubLot Key|Available Quantity|Consume Quantity")

        oCustList = Nothing

    End Sub

    Private Sub txtLotID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLotID.KeyPress
        Dim oInQuery As New InTrack_Automation.InQueryClass
        Dim iRetFlag As Integer = 0

        If Asc(e.KeyChar) = 13 Then

            iRetFlag = oInQuery.Query("Sublot", "*", "Where LotID = '" & Me.txtLotID.Text & "'")

            If iRetFlag = 0 Then
                Me.cmbRouteName.Items.Add("")
                Me.cmbOperationName.Items.Add("")
            End If

        End If

    End Sub

    Public Function SetExpirationDate(ByVal spLotID As String, ByVal spNewExpirationDate As Date)
        '
        Dim oInLot As New InTrack_Automation.InLot
        Dim oInDateTime As New InTrack_Automation.InDateTime
        '
        Dim bAllow As Boolean
        Dim iTmp1 As Long
        Dim iTmp2 As Long
        Dim sTmpDate As Date

        Try
            If oKIntrackDB.CheckIntrackConnection <> 1 Then
                Exit Function
            End If

            spLotID = spLotID.ToUpper
            oInLot.LotKey = spLotID
            iErrorNumber = oInLot.Load

            If iErrorNumber <> 0 Then
                InDBConnect.GetErrorMessage(iErrorNumber, sErrorDescription)
                Exit Function
            End If

            If IsDate(spNewExpirationDate) Then
                oInDateTime.SetMonthDayYear(spNewExpirationDate.Month, spNewExpirationDate.Day, spNewExpirationDate.Year)
                iErrorNumber = oInLot.SetExpirationDate(oInDateTime)
                If iErrorNumber <> 0 Then
                    InDBConnect.GetErrorMessage(iErrorNumber, sErrorDescription)
                    Exit Function
                End If

            End If

        Catch ex As Exception
            oKErrors.InternalErrorsHandler(ex, "Consume.SetExpirationDate")

        Finally
            oInLot = Nothing : oInDateTime = Nothing

        End Try

        Return IIf(iErrorNumber = 0, 1, 0)

    End Function


    'Public Function Consume(ByVal spSublotKey As String, ByVal spConsumeItems As String, _
    '                        Optional ByVal bpForceConsumption As Boolean = True, _
    '                        Optional ByVal spMachineName As String = "NONE") As Integer
    '    '
    '    Dim oInSublot As New InTrack_Automation.InSublot
    '    Dim oInPrmQty As New InTrack_Automation.InAmount
    '    Dim oInConsumeItems As New InTrack_Automation.InConsumeItems
    '    Dim oInConsumeItem As New InTrack_Automation.InConsumeItem
    '    '
    '    Dim iRow As Integer = 0
    '    Dim oLstConsume() As String
    '    Dim sConsumeLotID As String
    '    Dim dblConsumeQty As Double = 0
    '    Dim oMaterialQuantities() As String
    '    '
    '    Try

    '        oLstConsume = spConsumeItems.Split("|")
    '        oInSublot.SublotKey = spSublotKey

    '        iErrorNumber = oInSublot.Load

    '        If iErrorNumber <> 0 Then
    '            InDBConnect.GetErrorMessage(iErrorNumber, sErrorDescription)
    '            Exit Function
    '        End If

    '        For iRow = 0 To oLstConsume.Length - 1

    '            oMaterialQuantities = oLstConsume(iRow).Split(",")
    '            sConsumeLotID = oMaterialQuantities(0)
    '            dblConsumeQty = oMaterialQuantities(1)

    '            oInConsumeItem.Sublot = sConsumeLotID & "@NONE[NONE]:NONE"
    '            oInPrmQty.Quantity = dblConsumeQty
    '            oInConsumeItem.PrimaryAmt = oInPrmQty
    '            oInConsumeItem.MachineName = spMachineName
    '            oInConsumeItem.ForceConsumption = bpForceConsumption
    '            '
    '            iErrorNumber = oInSublot.Consume(oInConsumeItem)
    '            '
    '            If iErrorNumber <> 0 Then
    '                InDBConnect.GetErrorMessage(iErrorNumber, sErrorDescription)
    '                Exit Function
    '            End If
    '        Next

    '    Catch ex As Exception
    '        oKErrors.InternalErrorsHandler(ex, "Consume.Consume")

    '    Finally
    '        InDBConnect.Comment = ""
    '        oInSublot = Nothing : oInPrmQty = Nothing : oInConsumeItems = Nothing
    '        oInConsumeItem = Nothing

    '    End Try

    '    Return IIf(iErrorNumber = 0, 1, 0)

    'End Function

    Public Function Consume(ByVal spLotID As String, ByVal spOperationName As String, ByVal spConsumeItems As String, _
                            Optional ByVal bpForceConsumption As Boolean = True, _
                            Optional ByVal spMachineName As String = "NONE") As Integer
        '
        Dim oInSublot As New InTrack_Automation.InSublot
        Dim oInPrmQty As New InTrack_Automation.InAmount
        Dim oInConsumeItems As New InTrack_Automation.InConsumeItems
        Dim oInConsumeItem As New InTrack_Automation.InConsumeItem
        '
        Dim iRow As Integer = 0
        Dim oLstConsume() As String
        Dim sConsumeLotID As String
        Dim dblConsumeQty As Double = 0
        Dim oMaterialQuantities() As String
        '
        Try
            If oKIntrackDB.CheckIntrackConnection <> 1 Then
                Exit Function
            End If

            spLotID = spLotID.ToUpper

            oLstConsume = spConsumeItems.Split("|")
            oInSublot.SublotKey = SetSubLotKey(spLotID, spOperationName)

            iErrorNumber = oInSublot.Load

            If iErrorNumber <> 0 Then
                InDBConnect.GetErrorMessage(iErrorNumber, sErrorDescription)
                Exit Function
            End If

            For iRow = 0 To oLstConsume.Length - 1

                oMaterialQuantities = oLstConsume(iRow).Split(",")
                sConsumeLotID = oMaterialQuantities(0)
                dblConsumeQty = oMaterialQuantities(1)

                oInConsumeItem.Sublot = sConsumeLotID & "@NONE[NONE]:NONE"
                oInPrmQty.Quantity = dblConsumeQty
                oInConsumeItem.PrimaryAmt = oInPrmQty
                oInConsumeItem.MachineName = spMachineName
                oInConsumeItem.ForceConsumption = bpForceConsumption
                '
                iErrorNumber = oInSublot.Consume(oInConsumeItem)
                '
                If iErrorNumber <> 0 Then
                    InDBConnect.GetErrorMessage(iErrorNumber, sErrorDescription)
                    Exit Function
                End If
            Next

        Catch ex As Exception
            oKErrors.InternalErrorsHandler(ex, "Consume.Consume")

        Finally
            InDBConnect.Comment = ""
            oInSublot = Nothing : oInPrmQty = Nothing : oInConsumeItems = Nothing
            oInConsumeItem = Nothing

        End Try

        Return IIf(iErrorNumber = 0, 1, 0)

    End Function

<think> 我们正在讨论的是Lua中的`nixio.util.consume`函数。由于`nixio`是OpenWrt平台上的一个Lua网络库,并非标准Lua的一部分,因此我们需要在OpenWrt环境下使用它。 功能:`nixio.util.consume`通常用于从源(如文件描述符、套接字等)中读取所有可用数据,直到遇到文件结束符(EOF)或发生错误。它类似于连续读取直到读完所有数据。 使用方法:一般用于从一个文件描述符(fd)中读取所有数据并返回一个字符串。其基本语法如下: data = nixio.util.consume(fd) 参数: fd: 一个已经打开的文件描述符对象(例如,通过`nixio.fd.open`打开的文件,或者通过`socket:accept`得到的客户端套接字)。 返回值:读取到的所有数据组成的字符串。如果读取过程中出错,则可能抛出错误。 示例:假设我们有一个从网络套接字接收数据的场景,我们可以使用`consume`来读取客户端发送的所有数据直到连接关闭。 ```lua local nixio = require "nixio" -- 创建一个TCP服务器套接字 local server = nixio.socket("inet", "stream") server:bind("0.0.0.0", 8080) server:listen(5) while true do local client = server:accept() if client then -- 使用consume读取客户端发送的所有数据 local data = nixio.util.consume(client) print("Received data: ", data) client:close() end end ``` 注意:`consume`函数会一直读取直到遇到EOF(对于套接字来说,就是对方关闭连接),所以它适用于知道对方会关闭连接的场景。如果协议是持续连接(如HTTP长连接),则不能使用`consume`,因为它会一直等待直到连接关闭。 此外,由于`consume`会一次性读取所有数据,可能会占用大量内存,因此不适用于处理非常大的数据流。对于大文件或大数据流,应使用循环读取固定大小的块来处理。 替代方法:如果不希望一次性读取所有数据,可以使用循环读取固定大小: ```lua local chunk_size = 4096 local data = "" while true do local chunk, err = client:read(chunk_size) if not chunk then break end data = data .. chunk end ``` 但是,使用`consume`更加简洁,适用于数据量不大的情况。 相关问题: 1. `nixio.util.consume`与`fd:read`有什么区别? 2. 在OpenWrt的Lua环境中,如何处理大文件读取? 3. 除了`nixio.util.consume`,还有哪些常用的nixio工具函数? 4. 如何安装nixio库? </think> ### `nixio.util.consume` 功能与使用方法 `nixio.util.consume` 是 OpenWrt/LEDE 系统中 `nixio` 库的核心工具函数,用于高效处理 I/O 流中的数据读取。其主要功能是将文件描述符(fd)中的**所有可用数据一次性读取到 Lua 字符串**中。 #### 功能特性 1. **完整数据读取** 自动读取 fd 中所有可读数据,直到遇到 EOF(文件结束符)或错误 $$ \text{consume}(fd) \rightarrow \text{data} $$ 2. **高效内存管理** 内部使用动态缓冲区优化,避免反复分配内存 3. **阻塞式操作** 在数据未完全就绪时会阻塞当前协程 #### 使用示例 ```lua -- 加载 nixio 库 local nixio = require "nixio" -- 打开文件(需已安装 nixio) local fd = nixio.fd.open("/tmp/data.log", nixio.O_RDONLY) -- 一次性读取全部内容 local data = nixio.util.consume(fd) print("File size:", #data) -- 关闭文件描述符 fd:close() ``` #### 典型应用场景 1. **网络套接字处理** ```lua local client = server_socket:accept() local request = nixio.util.consume(client) ``` 2. **管道数据读取** ```lua local pipe = nixio.pipe() pipe:write("Hello from Lua") local msg = nixio.util.consume(pipe[1]) -- 读取管道内容 ``` 3. **日志文件处理** ```lua local log_fd = nixio.fd.open("/var/log/system.log") local logs = nixio.util.consume(log_fd) ``` #### 注意事项 1. **缓冲区限制** 默认最大读取 65536 字节,超大文件需分段处理: ```lua local CHUNK = 4096 while true do local chunk = fd:read(CHUNK) if not chunk then break end -- 处理分段数据 end ``` 2. **错误处理** 需捕获可能的 I/O 错误: ```lua local ok, data = pcall(nixio.util.consume, fd) if not ok then print("Error:", data) end ``` 3. **资源释放** 操作后必须显式关闭 fd:`fd:close()` > 该函数在 OpenWrt 文档中定义为:"Read all data from fd until EOF"[^1]。适用于处理小型或中等规模数据流(如配置文件、HTTP 请求),不适合 GB 级大文件。 --- ### 相关问题 1. 如何在 Lua 中处理超过 64KB 的文件? 2. `nixio.fd.read` 和 `nixio.util.consume` 有何区别? 3. 如何捕获 nixio 操作中的具体错误信息? 4. 在 OpenWrt 中如何安装 nixio 库? [^1]: 参见 OpenWrt 官方文档:`nixio.util` 模块说明
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值