Advertisement
cyber_Ahn

cable server

Dec 26th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.19 KB | None | 0 0
  1. --Bundled Cable Server for Network Control--
  2. --for tow Bundled Cable--
  3.  
  4. -- config--
  5. local channel = 21
  6. local CableSide = "right"                       --set cable side here
  7. local CableSide_1 = "left"                      --set cable side here
  8. modem = peripheral.wrap("back")
  9.  
  10. --vars--
  11. local power = true
  12. --cable state 0
  13. local white_st = 0
  14. local orange_st = 0
  15. local magenta_st = 0
  16. local lightblue_st = 0
  17. local yellow_st = 0
  18. local lime_st = 0
  19. local pink_st = 0
  20. local grey_st = 0
  21. local lightgrey_st = 0
  22. local cyan_st = 0
  23. local purple_st = 0
  24. local blue_st = 0
  25. local brown_st = 0
  26. local green_st = 0
  27. local red_st = 0
  28. local black_st = 0
  29. --cable state 1
  30. local white_st_1 = 0
  31. local orange_st_1 = 0
  32. local magenta_st_1 = 0
  33. local lightblue_st_1 = 0
  34. local yellow_st_1 = 0
  35. local lime_st_1 = 0
  36. local pink_st_1 = 0
  37. local grey_st_1 = 0
  38. local lightgrey_st_1 = 0
  39. local cyan_st_1 = 0
  40. local purple_st_1 = 0
  41. local blue_st_1 = 0
  42. local brown_st_1 = 0
  43. local green_st_1 = 0
  44. local red_st_1 = 0
  45. local black_st_1 = 0
  46.  
  47. --decode--
  48. function decode(msg)
  49. if msg == "off" then
  50. white_st = 0
  51. orange_st = 0
  52. magenta_st = 0
  53. lightblue_st = 0
  54. yellow_st = 0
  55. lime_st = 0
  56. pink_st = 0
  57. grey_st = 0
  58. lightgrey_st = 0
  59. cyan_st = 0
  60. purple_st = 0
  61. blue_st = 0
  62. brown_st = 0
  63. green_st = 0
  64. red_st = 0
  65. black_st = 0
  66. white_st_1 = 0
  67. orange_st_1 = 0
  68. magenta_st_1 = 0
  69. lightblue_st_1 = 0
  70. yellow_st_1 = 0
  71. lime_st_1 = 0
  72. pink_st_1 = 0
  73. grey_st_1 = 0
  74. lightgrey_st_1 = 0
  75. cyan_st_1 = 0
  76. purple_st_1 = 0
  77. blue_st_1 = 0
  78. brown_st_1 = 0
  79. green_st_1 = 0
  80. red_st_1 = 0
  81. black_st_1 = 0
  82. end
  83. if msg == "on" then
  84. white_st = 1
  85. orange_st = 1
  86. magenta_st = 1
  87. lightblue_st = 1
  88. yellow_st = 1
  89. lime_st = 1
  90. pink_st = 1
  91. grey_st = 1
  92. lightgrey_st = 1
  93. cyan_st = 1
  94. purple_st = 1
  95. blue_st = 1
  96. brown_st = 1
  97. green_st = 1
  98. red_st = 1
  99. black_st = 1
  100. white_st_1 = 1
  101. orange_st_1 = 1
  102. magenta_st_1 = 1
  103. lightblue_st_1 = 1
  104. yellow_st_1 = 1
  105. lime_st_1 = 1
  106. pink_st_1 = 1
  107. grey_st_1 = 1
  108. lightgrey_st_1 = 1
  109. cyan_st_1 = 1
  110. purple_st_1 = 1
  111. blue_st_1 = 1
  112. brown_st_1 = 1
  113. green_st_1 = 1
  114. red_st_1 = 1
  115. black_st_1 = 1
  116. end
  117. if msg == "white" then
  118. if white_st == 0 then
  119. white_st = 1
  120. elseif white_st == 1 then
  121. white_st = 0
  122. end
  123. end
  124. if msg == "orange" then
  125. if orange_st == 0 then
  126. orange_st = 1
  127. elseif orange_st == 1 then
  128. orange_st = 0
  129. end
  130. end
  131. if msg == "magenta" then
  132. if magenta_st == 0 then
  133. magenta_st = 1
  134. elseif magenta_st == 1 then
  135. magenta_st = 0
  136. end
  137. end
  138. if msg == "lightblue" then
  139. if lightblue_st == 0 then
  140. lightblue_st = 1
  141. elseif lightblue_st == 1 then
  142. lightblue_st = 0
  143. end
  144. end
  145. if msg == "yellow" then
  146. if yellow_st == 0 then
  147. yellow_st = 1
  148. elseif yellow_st == 1 then
  149. yellow_st = 0
  150. end
  151. end
  152. if msg == "lime" then
  153. if lime_st == 0 then
  154. lime_st = 1
  155. elseif lime_st == 1 then
  156. lime_st = 0
  157. end
  158. end
  159. if msg == "pink" then
  160. if pink_st == 0 then
  161. pink_st = 1
  162. elseif pink_st == 1 then
  163. pink_st = 0
  164. end
  165. end
  166. if msg == "gray" then
  167. if grey_st == 0 then
  168. grey_st = 1
  169. elseif grey_st == 1 then
  170. grey_st = 0
  171. end
  172. end
  173. if msg == "lightgray" then
  174. if lightgrey_st == 0 then
  175. lightgrey_st = 1
  176. elseif lightgrey_st == 1 then
  177. lightgrey_st = 0
  178. end
  179. end
  180. if msg == "cyan" then
  181. if cyan_st == 0 then
  182. cyan_st = 1
  183. elseif cyan_st == 1 then
  184. cyan_st = 0
  185. end
  186. end
  187. if msg == "purple" then
  188. if purple_st == 0 then
  189. purple_st = 1
  190. elseif purple_st == 1 then
  191. purple_st = 0
  192. end
  193. end
  194. if msg == "blue" then
  195. if blue_st == 0 then
  196. blue_st = 1
  197. elseif blue_st == 1 then
  198. blue_st = 0
  199. end
  200. end
  201. if msg == "brown" then
  202. if brown_st == 0 then
  203. brown_st = 1
  204. elseif brown_st == 1 then
  205. brown_st = 0
  206. end
  207. end
  208. if msg == "green" then
  209. if green_st == 0 then
  210. green_st = 1
  211. elseif green_st == 1 then
  212. green_st = 0
  213. end
  214. end
  215. if msg == "red" then
  216. if red_st == 0 then
  217. red_st = 1
  218. elseif red_st == 1 then
  219. red_st = 0
  220. end
  221. end
  222. if msg == "black" then
  223. if black_st == 0 then
  224. black_st = 1
  225. elseif black_st == 1 then
  226. black_st = 0
  227. end
  228. end
  229. if msg == "white-1" then
  230. if white_st_1 == 0 then
  231. white_st_1 = 1
  232. elseif white_st_1 == 1 then
  233. white_st_1 = 0
  234. end
  235. end
  236. if msg == "orange-1" then
  237. if orange_st_1 == 0 then
  238. orange_st_1 = 1
  239. elseif orange_st_1 == 1 then
  240. orange_st_1 = 0
  241. end
  242. end
  243. if msg == "magenta-1" then
  244. if magenta_st_1 == 0 then
  245. magenta_st_1 = 1
  246. elseif magenta_st_1== 1 then
  247. magenta_st_1 = 0
  248. end
  249. end
  250. if msg == "lightblue-1" then
  251. if lightblue_st_1 == 0 then
  252. lightblue_st_1 = 1
  253. elseif lightblue_st_1 == 1 then
  254. lightblue_st_1 = 0
  255. end
  256. end
  257. if msg == "yellow-1" then
  258. if yellow_st_1 == 0 then
  259. yellow_st_1 = 1
  260. elseif yellow_st_1 == 1 then
  261. yellow_st_1 = 0
  262. end
  263. end
  264. if msg == "lime-1" then
  265. if lime_st_1 == 0 then
  266. lime_st_1 = 1
  267. elseif lime_st_1 == 1 then
  268. lime_st_1 = 0
  269. end
  270. end
  271. if msg == "pink-1" then
  272. if pink_st_1 == 0 then
  273. pink_st_1 = 1
  274. elseif pink_st_1 == 1 then
  275. pink_st_1 = 0
  276. end
  277. end
  278. if msg == "gray-1" then
  279. if grey_st_1 == 0 then
  280. grey_st_1 = 1
  281. elseif grey_st_1 == 1 then
  282. grey_st_1 = 0
  283. end
  284. end
  285. if msg == "lightgray-1" then
  286. if lightgrey_st_1 == 0 then
  287. lightgrey_st_1 = 1
  288. elseif lightgrey_st_1 == 1 then
  289. lightgrey_st_1 = 0
  290. end
  291. end
  292. if msg == "cyan-1" then
  293. if cyan_st_1 == 0 then
  294. cyan_st_1 = 1
  295. elseif cyan_st_1 == 1 then
  296. cyan_st_1 = 0
  297. end
  298. end
  299. if msg == "purple-1" then
  300. if purple_st_1 == 0 then
  301. purple_st_1 = 1
  302. elseif purple_st_1 == 1 then
  303. purple_st_1 = 0
  304. end
  305. end
  306. if msg == "blue-1" then
  307. if blue_st_1 == 0 then
  308. blue_st_1 = 1
  309. elseif blue_st_1 == 1 then
  310. blue_st_1 = 0
  311. end
  312. end
  313. if msg == "brown-1" then
  314. if brown_st_1 == 0 then
  315. brown_st_1 = 1
  316. elseif brown_st_1 == 1 then
  317. brown_st_1 = 0
  318. end
  319. end
  320. if msg == "green-1" then
  321. if green_st_1 == 0 then
  322. green_st_1 = 1
  323. elseif green_st_1 == 1 then
  324. green_st_1 = 0
  325. end
  326. end
  327. if msg == "red-1" then
  328. if red_st_1 == 0 then
  329. red_st_1 = 1
  330. elseif red_st_1 == 1 then
  331. red_st_1 = 0
  332. end
  333. end
  334. if msg == "black-1" then
  335. if black_st_1 == 0 then
  336. black_st_1 = 1
  337. elseif black_st_1 == 1 then
  338. black_st_1 = 0
  339. end
  340. end
  341. if msg == "shutdown" then
  342. shell.run("shutdown")
  343. else
  344. shell.run("delete backup.db")
  345. local file = fs.open("backup.db","w")
  346. file.writeLine(tostring(white_st))
  347. file.writeLine(tostring(orange_st))
  348. file.writeLine(tostring(magenta_st))
  349. file.writeLine(tostring(lightblue_st))
  350. file.writeLine(tostring(yellow_st))
  351. file.writeLine(tostring(lime_st))
  352. file.writeLine(tostring(pink_st))
  353. file.writeLine(tostring(grey_st))
  354. file.writeLine(tostring(lightgrey_st))
  355. file.writeLine(tostring(cyan_st))
  356. file.writeLine(tostring(purple_st))
  357. file.writeLine(tostring(blue_st))
  358. file.writeLine(tostring(brown_st))
  359. file.writeLine(tostring(green_st))
  360. file.writeLine(tostring(red_st))
  361. file.writeLine(tostring(black_st))
  362. file.writeLine(tostring(white_st_1))
  363. file.writeLine(tostring(orange_st_1))
  364. file.writeLine(tostring(magenta_st_1))
  365. file.writeLine(tostring(lightblue_st_1))
  366. file.writeLine(tostring(yellow_st_1))
  367. file.writeLine(tostring(lime_st_1))
  368. file.writeLine(tostring(pink_st_1))
  369. file.writeLine(tostring(grey_st_1))
  370. file.writeLine(tostring(lightgrey_st_1))
  371. file.writeLine(tostring(cyan_st_1))
  372. file.writeLine(tostring(purple_st_1))
  373. file.writeLine(tostring(blue_st_1))
  374. file.writeLine(tostring(brown_st_1))
  375. file.writeLine(tostring(green_st_1))
  376. file.writeLine(tostring(red_st_1))
  377. file.writeLine(tostring(black_st_1))
  378. file.close()
  379. set_cable(msg)
  380. end
  381. end
  382.  
  383. --set cable
  384. function set_cable(cable)
  385. color_set = colors.black
  386. if white_st == 1 then
  387. color_set = color_set + colors.white
  388. end
  389. if orange_st == 1 then
  390. color_set = color_set + colors.orange
  391. end
  392. if magenta_st == 1 then
  393. color_set = color_set + colors.magenta
  394. end
  395. if lightblue_st == 1 then
  396. color_set = color_set + colors.lightBlue
  397. end
  398. if yellow_st == 1 then
  399. color_set = color_set + colors.yellow
  400. end
  401. if lime_st == 1 then
  402. color_set = color_set + colors.lime
  403. end
  404. if pink_st == 1 then
  405. color_set = color_set + colors.pink
  406. end
  407. if grey_st == 1 then
  408. color_set = color_set + colors.gray
  409. end
  410. if lightgrey_st == 1 then
  411. color_set = color_set + colors.lightGray
  412. end
  413. if cyan_st == 1 then
  414. color_set = color_set + colors.cyan
  415. end
  416. if purple_st == 1 then
  417. color_set = color_set + colors.purple
  418. end
  419. if blue_st == 1 then
  420. color_set = color_set + colors.blue
  421. end
  422. if brown_st == 1 then
  423. color_set = color_set + colors.brown
  424. end
  425. if green_st == 1 then
  426. color_set = color_set + colors.green
  427. end
  428. if red_st == 1 then
  429. color_set = color_set + colors.red
  430. end
  431. sleep(2)
  432. rs.setBundledOutput(CableSide,color_set)
  433. set_cable_1(cable)
  434. end
  435.  
  436. --set cable 1
  437. function set_cable_1(cable_1)
  438. color_set_1 = colors.black
  439. if white_st_1 == 1 then
  440. color_set_1 = color_set_1 + colors.white
  441. end
  442. if orange_st_1 == 1 then
  443. color_set_1 = color_set_1 + colors.orange
  444. end
  445. if magenta_st_1 == 1 then
  446. color_set_1 = color_set_1 + colors.magenta
  447. end
  448. if lightblue_st_1 == 1 then
  449. color_set_1 = color_set_1 + colors.lightBlue
  450. end
  451. if yellow_st_1 == 1 then
  452. color_set_1 = color_set_1 + colors.yellow
  453. end
  454. if lime_st_1 == 1 then
  455. color_set_1 = color_set_1 + colors.lime
  456. end
  457. if pink_st_1 == 1 then
  458. color_set_1 = color_set_1 + colors.pink
  459. end
  460. if grey_st_1 == 1 then
  461. color_set_1 = color_set_1 + colors.gray
  462. end
  463. if lightgrey_st_1 == 1 then
  464. color_set_1 = color_set_1 + colors.lightGray
  465. end
  466. if cyan_st_1 == 1 then
  467. color_set_1 = color_set_1 + colors.cyan
  468. end
  469. if purple_st_1 == 1 then
  470. color_set_1 = color_set_1 + colors.purple
  471. end
  472. if blue_st_1 == 1 then
  473. color_set_1 = color_set_1 + colors.blue
  474. end
  475. if brown_st_1 == 1 then
  476. color_set_1 = color_set_1 + colors.brown
  477. end
  478. if green_st_1 == 1 then
  479. color_set_1 = color_set_1 + colors.green
  480. end
  481. if red_st_1 == 1 then
  482. color_set_1 = color_set_1 + colors.red
  483. end
  484. sleep(2)
  485. rs.setBundledOutput(CableSide_1,color_set_1)
  486. end
  487.  
  488. --program-
  489. print("start cable Server");
  490. print("start modem on channel "..channel)
  491. print("bundled cable on side "..CableSide.." and "..CableSide_1)
  492. modem.open(channel)
  493. local found = fs.exists("backup.db")
  494. if found == true then
  495. print("load backup")
  496. file = fs.open("backup.db","r")
  497. local fileData = {}
  498. local line = file.readLine()
  499. repeat
  500. table.insert(fileData,line)
  501. line = file.readLine()
  502. until line == nil
  503. file.close()
  504. white_st = tonumber(fileData[1])
  505. orange_st = tonumber(fileData[2])
  506. magenta_st = tonumber(fileData[3])
  507. lightblue_st = tonumber(fileData[4])
  508. yellow_st = tonumber(fileData[5])
  509. lime_st = tonumber(fileData[6])
  510. pink_st = tonumber(fileData[7])
  511. grey_st = tonumber(fileData[8])
  512. lightgrey_st = tonumber(fileData[9])
  513. cyan_st = tonumber(fileData[10])
  514. purple_st = tonumber(fileData[11])
  515. blue_st = tonumber(fileData[12])
  516. brown_st = tonumber(fileData[13])
  517. green_st = tonumber(fileData[14])
  518. red_st = tonumber(fileData[15])
  519. black_st = tonumber(fileData[16])
  520. white_st_1 = tonumber(fileData[17])
  521. orange_st_1 = tonumber(fileData[18])
  522. magenta_st_1 = tonumber(fileData[19])
  523. lightblue_st_1 = tonumber(fileData[20])
  524. yellow_st_1 = tonumber(fileData[21])
  525. lime_st_1 = tonumber(fileData[22])
  526. pink_st_1 = tonumber(fileData[23])
  527. grey_st_1 = tonumber(fileData[24])
  528. lightgrey_st_1 = tonumber(fileData[25])
  529. cyan_st_1 = tonumber(fileData[26])
  530. purple_st_1 = tonumber(fileData[27])
  531. blue_st_1 = tonumber(fileData[28])
  532. brown_st_1 = tonumber(fileData[29])
  533. green_st_1 = tonumber(fileData[30])
  534. red_st_1 = tonumber(fileData[31])
  535. black_st_1 = tonumber(fileData[32])
  536. set_cable(msg)
  537. end
  538. print("cable Server started")
  539.  
  540. --open pull event--
  541. while power == true do
  542. event, modemside, sendchan, replaychan, message, dist = os.pullEvent("modem_message")
  543. print(message)
  544. decode(message)
  545. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement