Hello Guest it is July 19, 2025, 01:29:42 PM

Author Topic: Issue with Mach4 Log Script (http to node-red)  (Read 8563 times)

0 Members and 1 Guest are viewing this topic.

Issue with Mach4 Log Script (http to node-red)
« on: July 02, 2024, 03:19:05 PM »
I have a Lua script for Mach4 that sends and receives messages from a Node-RED server. However, I’m having trouble getting the log to work correctly. The purpose of this script is to read the Mach4 log  send the last message to the server I'm new to this help would be awesome! :)
i want this script to send log data to a Node-RED server. It reads the Mach4 log file and sends the last log message every 60 seconds if it’s different from the last sent message. The script runs indefinitely, checking and sending log updates to make sure that the Node-RED server receives the latest log entries.

Code: [Select]
-- Import necessary libraries
http = require("socket.http")
json = require("json")
wx = require("wx")


local inst = mc.mcGetInstance()
-- Define the server URL for sending log data (Node-RED server)
local serverUrl = "http://192.168.1.64:1880/logs"
-- Variable to store the last sent log message
local lastMessage = ""

-- Function to send log data over HTTP
local function sendLogData(logData)
    local response_body = {}
    -- Log a message in Mach4 to indicate HTTP request preparation
    mc.mcCntlSetLastError(inst, "Preparing HTTP request to " .. serverUrl)
   
    -- Send HTTP POST request with the log data
    local res, code, response_headers = http.request{
        url = serverUrl,
        method = "POST",
        headers = {
            ["Content-Type"] = "application/json",
            ["Content-Length"] = tostring(#logData)
        },
        source = ltn12.source.string(logData),
        sink = ltn12.sink.table(response_body)
    }

    -- Check the response code and log success or failure
    if code ~= 200 then
        mc.mcCntlSetLastError(inst, "HTTP Request Failed: " .. tostring(code))
    else
        mc.mcCntlSetLastError(inst, "Log Data Sent Successfully: " .. table.concat(response_body))
    end
end

-- Function to read the last message from the log file
local function getLastLogMessage()
    -- Path to the Mach4 log file (adjust as necessary)
    local logFilePath = "C:\\Mach4Hobby\\Logs\\Mach4Log.txt"
    local file = io.open(logFilePath, "r")
    local lastLine = ""

    -- Read the log file if it exists
    if file then
        for line in file:lines() do
            lastLine = line
        end
        file:close()
    else
        -- Log an error if the file cannot be opened
        mc.mcCntlSetLastError(inst, "Failed to open log file.")
    end

    return lastLine
end

-- Main loop to read and send log data every 60 seconds
while true do
    local currentMessage = getLastLogMessage()
    -- Send the log message if it's different from the last sent message
    if currentMessage ~= lastMessage then
        local logData = json.encode({message = currentMessage})
        sendLogData(logData)
        lastMessage = currentMessage
    end
    -- Wait for 60 seconds before checking the log again
    wx.wxSleep(60)
end

I just use a button to start the codeH
« Last Edit: July 02, 2024, 03:23:25 PM by TimberTechie »