• Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
Welcome to OpenComputers

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more!

This message will be removed once you have signed in.

  • Announcements

    • Lizzy Trickster

      Latest Stable OpenComputers Version   11/26/16

      The latest released version of OpenComputers is version 1.6.1 for MC 1.7.10, 1.8.9, 1.9.4 & 1.10.2. See more information here! Beta/Dev builds can be found at the Jenkins Build Server (ci.cil.li)
  • 0
squall2044

SGCraft and wireless

Question

Hi all I am trying to write a program to control sgcraft gates and
am having some trouble with the server side of things heres what I have
so far:
##########################################################################
local c = require("component")

local e = require("event")

local m = c.modem

local s = c.stargate

local computer = require("computer")

()
m.open(20)

m.broadcast(20, "Online.")

local _, _, f1, port, _, message1 = e.pull("modem_message")

if message1 == "s.dial('s.localAddress()')" then

  m.send(f1, 20, io.stderr:write("Cannot connect to self."))

 else if message1 == "s.dial('???-??????')" and s.energyAvailable() < s.energyToDial('????-??????') then

   m.send(f1, 10, io.stderr:write("Not enough energy to dial that gate."))

  else if message1 == "s.dial('????-??????')" and s.stargateState() == "Idle" then

    m.send(f1, 20, "Dialing.")

   else if message1 == "s.closeIris()" and s.irisState() ~= "offline" then

     m.send(f1, 20, "Closing Iris.")

    else if message1 == "s.openIris()" and s.irisState() ~= "offline" then

      m.send(f1, 20, "Opening Iris.")

     else if message1 == "s.disconnect()" and s.stargateState() ~= "Idle" then

       m.send(f1, 20, "Disconnecting.")

----    else if message1 ==

    else

      m.send(f1, 20, io.stderr:write("Command Not Recognized. PLease wait 30 SECONDS for shutdown"))

     end

    end

   end

  end

 end

end

-----print("Hello")


(message1) ----This is where I am having trouble
computer.shutdown()
#########################################################################

Right now the computer is hooked up to a stargate with an Iris and I cam call the methods
directly from the computer but I want to run them from a tablet so I can 
bury the computer and leave it with no screen and a wake message.

what I want it to do is if I send s.closeIris() the Iris will close but I am just getting a
/home/dial:37: syntax error near 'computer'

Can anyone help me?

Thank you in advance.
 

Edited by squall2044
Added more info

Share this post


Link to post
Share on other sites

3 answers to this question

  • 0

Well, the error says the syntax is invalid, and it is. If you want to execute the message, then you need to do something like this:

 

load(message1)()

 

This loads the code and compiles it, then the empty parenthesis afterwards executes it.

Share this post


Link to post
Share on other sites
  • 0

Thanks Brisingr that worked like a charm.   Can you help with the addressing part as well I cant seem to get it to recognize the dial command

 

 

thanks

Share this post


Link to post
Share on other sites
  • 0

I would recommend not executing strings sent down a network.. If you're just in a SP world then fine, have at it. But, if you're on multiplayer then this is a big security problem. Just saying..

A better solution would be to design commands / responses with parameters that can be sent between the client and server. i.e.

-- # On the server

-- # Other code implied up hurr...

local commands = {
	openIris = function()
		if s.irisState() ~= 'offline' then
			m.send( f1, 20, 'Opening Iris.' )
		end
	end,
	dial = function( address )
		if s.irisState() == 'idle' then
			m.send( f1, 20, string.format( 'Dialing %s', address )
		end
	end
	-- ... more commands go here
}

-- # on message...
-- # cmd should look like: { 'dial', 'kjai2sdiwdSOME_ADDRESSksjdlaks' }
local cmd = { string.match( message1, '(%a%d_)' ) }

local command, args = table.remove( cmd, 1 ), { table.unpack( cmd ) }

-- # This calls our command with any args sent with it. This is safer than load('msg') because 'msg' could be malicious.
commands[ command ]( table.unpack( args ) )

-- # DISCLAIMER: I haven't run this code. Could have bugs. But this is generally how I handle commands over the net.

 

Let me know if I need to clarify anything... :)

Edited by Molinko
formatting..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now