<commit_before>#! /usr/bin/env lua

local io = require("io")
local os = require("os")
local rrd = require("rrd")
local nixio = require("nixio")
nixio.fs = require("nixio.fs")
local uci = require("uci")

local SERIAL_DEVICE = "/dev/ttyS1"
local RRD_FILE = "/tmp/hm.rrd"
local JSON_FILE = "/tmp/json"

local probeNames = { "Pit", "Food Probe1", "Food Probe2", "Ambient" }

function rrdCreate()
 return rrd.create(
   RRD_FILE,
   "--step", "2",
   "DS:sp:GAUGE:30:0:1000",
   "DS:t0:GAUGE:30:0:1000",
   "DS:t1:GAUGE:30:0:1000",
   "DS:t2:GAUGE:30:0:1000",
   "DS:t3:GAUGE:30:0:1000",
   "DS:f:GAUGE:30:-1000:100",
   "RRA:AVERAGE:0.6:5:360",
   "RRA:AVERAGE:0.6:30:360",
   "RRA:AVERAGE:0.6:60:360",
   "RRA:AVERAGE:0.6:90:480"
 )
end

-- This might look a big hokey but rather than build the string
-- and discard it every time, just replace the values to reduce
-- the load on the garbage collector
local JSON_TEMPLATE = {
  '{"time":', 0,
  ',"set":', 0,
  ',"lid":', 0,
  ',"fan":{"c":', 0, ',"a":', 0, 
  '},"temps":[{"n":"', 'Pit', '","c":', 0, -- probe1
  '},{"n":"', 'Food Probe1', '","c":', 0, -- probe2
  '},{"n":"', 'Food Probe2', '","c":', 0, -- probe3
  '},{"n":"', 'Ambient', '","c":', 0, -- probe4
  '}]}'
}
local JSON_FROM_CSV = {2, 4, 14, 18, 22, 26, 8, 10, 6 } 
  
function jsonWrite(vals)
  local i,v
  for i,v in ipairs(vals) do
    JSON_TEMPLATE[JSON_FROM_CSV[i]] = v  
  end
  return nixio.fs.writefile(JSON_FILE, table.concat(JSON_TEMPLATE))
end

function segSplit(line)
  local retVal = {}
  local fieldstart = 1
  line = line .. ','
  repeat
    local nexti = line:find(',', fieldstart)
    retVal[#retVal+1] = line:sub(fieldstart, nexti-1)
    fieldstart = nexti + 1
  until fieldstart > line:len()

  table.remove(retVal, 1) -- remove the segment name
  return retVal
end

function segProbeNames(line)
  local vals = segSplit(line)
  if #vals < 4 then return end

  JSON_TEMPLATE[12] = vals[1]
  JSON_TEMPLATE[16] = vals[2]
  JSON_TEMPLATE[20] = vals[3]
  JSON_TEMPLATE[24] = vals[4]
end

function segRfUpdate(line)
  local vals = segSplit(line)
  local idx = 1
  while (idx < #vals) do
    local nodeId = vals[idx]
    local signalLevel = vals[idx+1]
    local lastReceive = vals[idx+2]
    --nixio.syslog("info", ("RF%s: %d last %s"):format(nodeId, math.floor(signalLevel/2.55), lastReceive))
    idx = idx + 3
  end
end

local lastUpdate = os.time()
function segStateUpdate(line)
    local vals = segSplit(line)
    
    if #vals == 8 then 
      -- If the time has shifted more than 24 hours since the last update
      -- the clock has probably just been set from 0 (at boot) to actual 
      -- time. Recreate the rrd to prevent a 40 year long graph
      local time = os.time()
      if time - lastUpdate > (24*60*60) then
        nixio.syslog("notice", "Time jumped forward by "..(time-lastUpdate)..", restarting database")
        rrdCreate()
      end
      lastUpdate = time

      -- Add the time as the first item
      table.insert(vals, 1, time)
      
      jsonWrite(vals)
      
      local lid = tonumber(vals[9]) or 0
      -- If the lid value is non-zero, it replaces the fan value
      if lid ~= 0 then
        vals[7] = lid
      end
      table.remove(vals, 9) -- lid
      table.remove(vals, 8) -- fan avg
      
      rrd.update(RRD_FILE, table.concat(vals, ":"))
    end
end

local hm = io.open(SERIAL_DEVICE, "rwb")
if hm == nil then
  die("Can not open serial device")
end

nixio.umask("0022")

-- Create database
if not nixio.fs.access(RRD_FILE) then
  rrdCreate()
end

-- Create json file
if not nixio.fs.access("/www/json") then
  if not nixio.fs.symlink(JSON_FILE, "/www/json") then
    print("Can not create JSON file link")
  end
end

local segmentMap = { 
  ["$HMSU"] = segStateUpdate,
  ["$HMPN"] = segProbeNames,
  ["$HMRF"] = segRfUpdate
}

-- Request the current probe names
hm:write("/set?pn@XX\n")

while true do
  local hmline = hm:read("*l") 
  if hmline == nil then break end
 
  local segmentFunc = segmentMap[hmline:sub(1,5)];
  if segmentFunc ~= nil then
    segmentFunc(hmline)
  end
end

hm:close()
nixio.fs.unlink("/var/run/linkmeterd/pid")
<commit_msg>[lm] Remove superflous probeName array and fix serial device file mode<commit_after>#! /usr/bin/env lua

local io = require("io")
local os = require("os")
local rrd = require("rrd")
local nixio = require("nixio")
nixio.fs = require("nixio.fs")
local uci = require("uci")

local SERIAL_DEVICE = "/dev/ttyS1"
local RRD_FILE = "/tmp/hm.rrd"
local JSON_FILE = "/tmp/json"

function rrdCreate()
 return rrd.create(
   RRD_FILE,
   "--step", "2",
   "DS:sp:GAUGE:30:0:1000",
   "DS:t0:GAUGE:30:0:1000",
   "DS:t1:GAUGE:30:0:1000",
   "DS:t2:GAUGE:30:0:1000",
   "DS:t3:GAUGE:30:0:1000",
   "DS:f:GAUGE:30:-1000:100",
   "RRA:AVERAGE:0.6:5:360",
   "RRA:AVERAGE:0.6:30:360",
   "RRA:AVERAGE:0.6:60:360",
   "RRA:AVERAGE:0.6:90:480"
 )
end

-- This might look a big hokey but rather than build the string
-- and discard it every time, just replace the values to reduce
-- the load on the garbage collector
local JSON_TEMPLATE = {
  '{"time":', 0,
  ',"set":', 0,
  ',"lid":', 0,
  ',"fan":{"c":', 0, ',"a":', 0, 
  '},"temps":[{"n":"', 'Pit', '","c":', 0, -- probe1
  '},{"n":"', 'Food Probe1', '","c":', 0, -- probe2
  '},{"n":"', 'Food Probe2', '","c":', 0, -- probe3
  '},{"n":"', 'Ambient', '","c":', 0, -- probe4
  '}]}'
}
local JSON_FROM_CSV = {2, 4, 14, 18, 22, 26, 8, 10, 6 } 
  
function jsonWrite(vals)
  local i,v
  for i,v in ipairs(vals) do
    JSON_TEMPLATE[JSON_FROM_CSV[i]] = v  
  end
  return nixio.fs.writefile(JSON_FILE, table.concat(JSON_TEMPLATE))
end

function segSplit(line)
  local retVal = {}
  local fieldstart = 1
  line = line .. ','
  repeat
    local nexti = line:find(',', fieldstart)
    retVal[#retVal+1] = line:sub(fieldstart, nexti-1)
    fieldstart = nexti + 1
  until fieldstart > line:len()

  table.remove(retVal, 1) -- remove the segment name
  return retVal
end

function segProbeNames(line)
  local vals = segSplit(line)
  if #vals < 4 then return end

  JSON_TEMPLATE[12] = vals[1]
  JSON_TEMPLATE[16] = vals[2]
  JSON_TEMPLATE[20] = vals[3]
  JSON_TEMPLATE[24] = vals[4]
end

function segRfUpdate(line)
  local vals = segSplit(line)
  local idx = 1
  while (idx < #vals) do
    local nodeId = vals[idx]
    local signalLevel = vals[idx+1]
    local lastReceive = vals[idx+2]
    --nixio.syslog("info", ("RF%s: %d last %s"):format(nodeId, math.floor(signalLevel/2.55), lastReceive))
    idx = idx + 3
  end
end

local lastUpdate = os.time()
function segStateUpdate(line)
    local vals = segSplit(line)
    
    if #vals == 8 then 
      -- If the time has shifted more than 24 hours since the last update
      -- the clock has probably just been set from 0 (at boot) to actual 
      -- time. Recreate the rrd to prevent a 40 year long graph
      local time = os.time()
      if time - lastUpdate > (24*60*60) then
        nixio.syslog("notice", "Time jumped forward by "..(time-lastUpdate)..", restarting database")
        rrdCreate()
      end
      lastUpdate = time

      -- Add the time as the first item
      table.insert(vals, 1, time)
      
      jsonWrite(vals)
      
      local lid = tonumber(vals[9]) or 0
      -- If the lid value is non-zero, it replaces the fan value
      if lid ~= 0 then
        vals[7] = lid
      end
      table.remove(vals, 9) -- lid
      table.remove(vals, 8) -- fan avg
      
      rrd.update(RRD_FILE, table.concat(vals, ":"))
    end
end

local hm = io.open(SERIAL_DEVICE, "r+b")
if hm == nil then
  die("Can not open serial device")
end

nixio.umask("0022")

-- Create database
if not nixio.fs.access(RRD_FILE) then
  rrdCreate()
end

-- Create json file
if not nixio.fs.access("/www/json") then
  if not nixio.fs.symlink(JSON_FILE, "/www/json") then
    print("Can not create JSON file link")
  end
end

local segmentMap = { 
  ["$HMSU"] = segStateUpdate,
  ["$HMPN"] = segProbeNames,
  ["$HMRF"] = segRfUpdate
}

-- Request the current probe names
hm:write("/set?pnXXX\n")

while true do
  local hmline = hm:read("*l") 
  if hmline == nil then break end
 
  local segmentFunc = segmentMap[hmline:sub(1,5)];
  if segmentFunc ~= nil then
    segmentFunc(hmline)
  end
end

hm:close()
nixio.fs.unlink("/var/run/linkmeterd/pid")

{'fraction_non_alphanumeric': 0.16488845780795344, 'fraction_numerical': 0.038069835111542195, 'mean_word_length': 3.2151251916198262, 'pattern_counts': {'":': 32, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5621010', 'n_tokens_mistral': 3430, 'n_tokens_neox': 3180, 'n_words': 852}