Advertisement
Guest User

runMonitor

a guest
Nov 25th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local monitor=nil
  2.  
  3. function setMonitor()
  4.   monitor=peripheral.wrap("top")
  5. end
  6.  
  7. function openNetwork()
  8.   rednet.open("left")
  9. end
  10.  
  11. function split(str, pat)
  12.   local t={}
  13.   local fpat="(.-)"..pat
  14.   local last_end=1
  15.   local s,e,cap = str:find(fpat,1)
  16.   while s do
  17.     if s~= 1 or cap~= "" then
  18.       table.insert(t,cap)
  19.     end
  20.     last_end=e+1
  21.     s,e,cap= str:find(fpat,last_end)
  22.   end
  23.   if last_end <= #str then
  24.     cap= str:sub(last_end)
  25.     table.insert(t,cap)
  26.   end
  27.   return t
  28. end
  29.  
  30. function setStartup()
  31.   if(fs.exists("startup")==false) then
  32.     local startupFile=fs.open("startup","w")
  33.     startupFile.write("os.run({}, \""..shell.getRunningProgram().."\")")
  34.     startupFile.close()
  35.   end
  36. end
  37.  
  38. function __genOrderedIndex(t)
  39.   local orderedIndex={}
  40.   for key,value in pairs(t) do
  41.     table.insert(orderedIndex,key)
  42.   end
  43.   table.sort(orderedIndex)
  44.   return orderedIndex
  45. end
  46.  
  47. function orderedNext(t,state)
  48.   if state==nil then
  49.     t.__orderedIndex=__genOrderedIndex(t)
  50.     key=t.__orderedIndex[1]
  51.     --write(textutils.serialize(t.__orderedIndex))
  52.     return key, t[key]
  53.   end
  54.   key=nil
  55.   for i=1,table.getn(t.__orderedIndex) do
  56.     if t.__orderedIndex[i]==state then
  57.       key= t.__orderedIndex[i+1]
  58.     end
  59.   end
  60.   if key then
  61.     return key, t[key]
  62.   end
  63.   t.__orderedIndex=nil
  64.   return
  65. end
  66.  
  67. function orderedPairs(t)
  68.   return orderedNext, t, nil
  69. end
  70.  
  71. function runMonitor()
  72.   openNetwork()
  73.   setMonitor()
  74.   while true do
  75.     local tableAspect={Free=0}
  76.     rednet.broadcast("howmany")
  77.     local timer=os.startTimer(0.2)
  78.     local continue=true
  79.     while continue do
  80.       local event,param1,message=os.pullEvent()
  81.       if (event=="rednet_message") then
  82.         local messageTable=split(message,"-")
  83.         --write(textutils.serialize(messageTable))
  84.         if (messageTable[1]=="aspectdata") then
  85.           if(messageTable[2]=="untyped") then
  86.             tableAspect["Free"]=tableAspect["Free"]+1
  87.           else
  88.             if(tableAspect[messageTable[2]]==nil) then
  89.               tableAspect[messageTable[2]]=messageTable[3]
  90.             else
  91.               tableAspect[messageTable[2]]=tableAspect[messageTable[2]]+messageTable[3]
  92.             end
  93.           end
  94.         end
  95.       elseif(event=="timer") then
  96.         if(timer==param1) then
  97.           continue=false
  98.         end
  99.       end
  100.     end
  101.     --write(textutils.serialize(tableAspect))
  102.     local x,y=monitor.getSize()
  103.     table.sort(tableAspect)
  104.     monitor.clear()
  105.     local i=1
  106.     for k, v in orderedPairs(tableAspect) do
  107.       --my issue is here
  108.       monitor.setCursorPos(1,i)
  109.       --write(" "..k.." x"..v) --this works fine
  110.       monitor.write(k.." x"..v)-- this dont works
  111.       i=i+1
  112.     end
  113.     os.sleep(5)
  114.   end
  115. end
  116.  
  117. setStartup()
  118. runMonitor()
  119. --write(textutils.serialize(split("test-1","-")))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement