View difference between Paste ID: kQ79jeRH and fGfQ3kXy
SHOW: | | - or go back to the newest paste.
1
local tableColorsToSiphon={white="tilesiphon_1",orange="tilesiphon_2",magenta="tilesiphon_3",lightBlue="tilesiphon_4",yellow="tilesiphon_5"}
2
local colors ={}
3
for  k,v in pairs(tableColorsToSiphon) do table.insert(colors,k) end
4
5
--write(textutils.serialize(colors))
6
7
function getColoredSiphon(color)
8
  return peripheral.wrap(tableColorsToSiphon[color])
9
end
10
11
function getAspect(color)
12
  local siphon=getColoredSiphon(color)
13
  local rawresults=siphon.getAspects()
14
  if (rawresults[1]==nil)then
15
    return nil
16
  else
17
    return rawresults[1]["name"]
18
  end
19
end
20
21
function getAspectAmount(color)
22
  local siphon=getColoredSiphon(color)
23
  local rawresults=siphon.getAspects()
24
  if (rawresults[1]==nil) then
25
    return nil
26
  else
27
    return rawresults[1]["quantity"]
28
  end
29
end
30
31
function isStable(color)
32
    local value1=getAspectAmount(color)
33
    if(value1==nil) then
34
      return false
35
    end
36
    os.sleep(0.85)
37
    if(getAspectAmount(color)==value1) then
38
      return true
39
    else
40
      return false
41
    end
42
end
43
44
function openNetwork()
45
  rednet.open("front")
46
end
47
48
function filtredWaitingMessage(time,filtre)
49
  openNetwork()
50
  local timer=os.startTimer(time)
51
  local continue=true
52
  while (continue) do
53
    local event,param1,message=os.pullEvent()
54
    if(event=="rednet_message") then
55
      if(message==filtre) then
56
        return param1, message
57
      end
58
    elseif(event=="timer") then
59
      if(param1==timer) then
60
        return nil, nil
61
      end
62
    end
63
  end
64
end
65
66
function setStartup()
67
  if(fs.exists("startup")==false) then
68
    local startupFile=fs.open("startup","w")
69
    startupFile.write("os.run({}, \""..shell.getRunningProgram().."\")")
70
    startupFile.close()
71
  end
72
end
73
74
function runProvider()
75
  openNetwork()
76
  while(true)do
77
    for k, v in pairs(colors) do
78
      write(" "..v)
79
      if(isStable(v))then
80
        write(" is stable\n")
81
        rednet.broadcast("wherestore-"..getAspect(v).."-"..getAspectAmount(v))
82
        local senderId, message=filtredWaitingMessage(0.2,"here")
83
        if (message=="here") then
84
          rednet.send(senderId,"store-"..getAspect(v))
85
          rednet.broadcast("open-"..v)
86
          while(getAspectAmount(v)~=nil) do
87
            os.sleep(0.05)
88
          end
89
          rednet.broadcast("close-"..v)
90
          os.sleep(0.15)
91
          rednet.send(senderId,"stop")          
92
       end
93
      end
94
    end
95
  end
96
end
97
98
setStartup()
99
runProvider()
100
--local senderId, message=filtredWaitingMessage(20,"here")
101
--write(textutils.serialize(message))
102
--write(textutils.serialize(isStable("white")))