Aller au contenu

Messages recommandés

Posté(e)

Hello !

 

Voici une méthode d'un QA qui me permet d'activer ou désactiver un point d'accès wifi.

 

Je pilote cet AP via le port 23 en telnet (CLI).

 

Le côté asynchrone des socket me rend le code HORRIBLE :23:!!! mais fonctionnel...

Je n'avais pas ce soucis avec la HC2 car le tcpSocket:read()  ou tcpSocket:write() n'était pas asycnhrone...

 

y a t il moyen de simplifier cela ?

 

attention, voilà le pavé

 

function QuickApp:SwitchOn()

	-- open connection to the device
	self.sock:connect(self:getVariable("IP"), 23, {  

		success = function(result)     
			print("SUCCES CONNECTION")

			--read response             
			self:Read()

			--wait response
			fibaro.setTimeout(100, function() 
				--wait response (3 s)...
				Timeout = os.time() + 3
				while os.time() < Timeout do 
					res = self:getVariable("Result")
					if res and res ~= "toto" then break end
					fibaro.sleep(1000)   
				end
				print("RESULT - "..res)
			end)

			--WRITE LOGIN --------------------------------------------------------------------------------------------------------------------------
			fibaro.setTimeout(100, function() 
				self.sock:write("admin".."\n", {
					success = function(result)

						--read response
						self:Read()

						--wait response
						fibaro.setTimeout(1, function() 
							--wait response (3 s)...
							Timeout = os.time() + 3
							while os.time() < Timeout do 
								res = self:getVariable("Result")
								if res and res ~= "toto" then break end
								fibaro.sleep(1000)   
							end
							print("RESULT - "..res)

							--WRITE PASSWORD--------------------------------------------------------------------------------------------------------------------------
							fibaro.setTimeout(100, function() 
								self.sock:write("xxxxx".."\n", {
									success = function(result)

										--read response
										self:Read()

										--wait response
										fibaro.setTimeout(1, function() 
											--wait response (3 s)...
											Timeout = os.time() + 3
											while os.time() < Timeout do 
												res = self:getVariable("Result")
												if res and res ~= "toto" then break end
												fibaro.sleep(1000)   
											end
											print("RESULT - "..res)

											--WRITE configure terminal------------------------------------------------------------------------------------------------
											fibaro.setTimeout(100, function() 
												self.sock:write("configure terminal".."\n", {
													success = function(result)

														--read response 
														self:Read()

														--wait response 
														fibaro.setTimeout(1, function() 
															--wait response (3 s)...
															Timeout = os.time() + 3
															while os.time() < Timeout do 
																res = self:getVariable("Result")
																if res and res ~= "toto" then break end
																fibaro.sleep(1000)   
															end
															print("RESULT - "..res)

															--radio 2.4 enable/desable---------------------------------------------------------------------------------
															fibaro.setTimeout(100, function() 
																self.sock:write("radio 2.4 disable".."\n", {
																	success = function(result)

																		--read response
																		self:Read()

																		--wait response
																		fibaro.setTimeout(1, function() 
																			--wait response (3 s)...
																			Timeout = os.time() + 3
																			while os.time() < Timeout do 
																				res = self:getVariable("Result")
																				if res and res ~= "toto" then break end
																				fibaro.sleep(1000)   
																			end
																			print("RESULT - "..res)

																			--WRITE EXIT-------------------------------------------------------------------------------
																			fibaro.setTimeout(100, function() 
																				self.sock:write("exit".."\n", {
																					success = function(result)

																						--read response
																						self:Read()

																						--wait response
																						fibaro.setTimeout(1, function() 
																							--wait response (3 s)...
																							Timeout = os.time() + 3
																							while os.time() < Timeout do 
																								res = self:getVariable("Result")
																								if res and res ~= "toto" then break end
																								fibaro.sleep(1000)   
																							end
																							print("RESULT - "..res)

																							--WRITE save-and-activate-------------------------------------------------
																							fibaro.setTimeout(100, function() 
																								self.sock:write("save-and-activate".."\n", {
																									success = function(result)

																										--read response
																										self:Read()

																										--wait response
																										fibaro.setTimeout(1, function() 
																											--wait response (3 s)...
																											Timeout = os.time() + 3
																											while os.time() < Timeout do 
																												res = self:getVariable("Result")
																												if res and res ~= "toto" then break end
																												fibaro.sleep(1000)   
																											end
																											print("RESULT - "..res)

																											
																										end)
																									end,
																								})
																							end)
																							-------------------------------------------------
																						end)
																					end,
																				})
																			end)
																			-------------------------------------------------
																		end)
																	end,
																})
															end)
															-----------------------------------------------------------------------------------------------------------
														end)
													end,
												})
											end)
											----------------------------------------------------------------------------------------------------------------------------
										end)
									end,
								})
							end)
							----------------------------------------------------------------------------------------------------------------------------
						end)
					end,
				})
			end)
			----------------------------------------------------------------------------------------------------------------------------
		end,
		error = function(err) self:debug(err) end
	})
end

 

et voici le debug : on voit que tout ce passe bien

 

[DEBUG] 26.03.2020 12:12:54: onUIEvent: {"elementName":"button1","eventType":"onReleased","deviceId":133,"values":[null]}
[DEBUG] 26.03.2020 12:12:54: SUCCES CONNECTION
[DEBUG] 26.03.2020 12:12:54: START READ...
[DEBUG] 26.03.2020 12:12:54: RESULT - ������!���� login:
[DEBUG] 26.03.2020 12:12:54: START READ...
[DEBUG] 26.03.2020 12:12:54: RESULT - admin Password:
[DEBUG] 26.03.2020 12:12:54: START READ...
[DEBUG] 26.03.2020 12:12:54: RESULT - NetgearRdc#
[DEBUG] 26.03.2020 12:12:55: START READ...
[DEBUG] 26.03.2020 12:12:55: RESULT - configure terminal NetgearRdc(config)#
[DEBUG] 26.03.2020 12:12:55: START READ...
[DEBUG] 26.03.2020 12:12:55: RESULT - radio 2.4 disable NetgearRdc(config)#
[DEBUG] 26.03.2020 12:12:55: START READ...
[DEBUG] 26.03.2020 12:12:55: RESULT - exit NetgearRdc#
[DEBUG] 26.03.2020 12:12:55: START READ...
[DEBUG] 26.03.2020 12:12:55: RESULT - save-and-activate

 

Posté(e)
il y a 48 minutes, jjacques68 a dit :

Hello !

Voici une méthode d'un QA qui me permet d'activer ou désactiver un point d'accès wifi.

Je pilote cet AP via le port 23 en telnet (CLI).

Le côté asynchrone des socket me rend le code HORRIBLE : 23:!!! mais fonctionnel...

Je n'avais pas ce soucis avec la HC2 car le tcpSocket:read()  ou tcpSocket:write() n'était pas asycnhrone...

y a t il moyen de simplifier cela ?

Like this (incomplete example...)

local SELF,handlers = nil,nil
local function post(ev,time) setTimeout(function() SELF:debug("STEP:",ev.type) handlers[ev.type](ev) end,time or 0) end

handlers={
  start = function(ev)
    SELF.soc.connect(self:getVariable("IP"),23 ,{    
        success = function(result) post({type='connected',res=result}) end,
        error(msg) SELF:debug("Error:",msg) end
      })
  end,

  connected = function(ev) 
    print("SUCCESS CONNECTION")
    post({type='read',tag="CONNECT RESULT:",next='login'})
  end,

  login = function(ev) 
    SELF.soc.write("admin" .. "\ n",{
        success=function(result) post({type='read', tag='LOGIN1:',next='password'}) end,
        error=function() ... end
      })
  end,

  password = function(ev)
    SELF.soc.write("xxxxx" .. "\ n",{
        success=function(result) post({type='read', tag='PASSWORD RESULT',next='whatever'}) end,
        error=function() ... end
      })
  end,

  read = function(ev)
    SELF.soc.read({
        success=function(result)
          SELF:debug(ev.tag,tostring(result))
          post(ev.next)
        end,
        error = function(msg) SELF:debug("Error:",msg) end
      })
  end,
}

function QuickApp:SwitchOn() 
  SELF=self
  post({type='start'}) 
end

See also https://forum.fibaro.com/topic/49113-hc3-quickapps-coding-tips-and-tricks/?do=findComment&amp;comment=201413

 

Posté(e)

hummm :15:

 

I have an error with the code

 

local SELF=self
SELF,handlers = nil,nil

function QuickApp:onInit()     
    SELF.soc = net.TCPSocket({timeout = 2000})
    post({type='start'}) 
end


function post(ev,time)
    setTimeout(function() 
                SELF:debug("STEP:",ev.type) 
                handlers[ev.type](ev)
                end,
    time or 0)
end

handlers={

  start = function(ev)
    SELF.soc.connect("192.168.2.242",23 ,{    
        success = function(result) print("2") post({type='connected',res=result}) end,
        error = function(msg) SELF:debug("Error:",msg) end
      })
  end,

 

ERROR : 

 

attempt to index a nil value (upvalue 'SELF')

 

Posté(e)

ah ok with this : 

 

local handlers = nil  

function QuickApp:onInit()
    SELF = self 
    SELF.soc = net.TCPSocket({timeout = 2000})
    post({type='start'}) 
end

 

pfffffiou !!! 

I'm not used to doing with this methode.

 

I will try...

 

 

Posté(e)

I have a problem with the read function : 

 

read = function(ev)
    SELF.soc:read({
        success=function(result)
          SELF:debug(ev.tag,tostring(result))
          post(ev.next)
        end,
        error = function(msg) SELF:debug("Error:",msg) end
      })
  end,

 

the instruction "post()" seems not be called !

 

 

 

Posté(e)

YEEEES !! it's fine !!

 

thank you very much for your help !

 

it's not natural for me to programming like this but it's very interesting !

 

local handlers = nil  

function QuickApp:onInit()
    SELF = self 
    SELF.soc = net.TCPSocket({timeout = 2000})
    post({type='start'}) 
end

function post(ev,time)
    setTimeout(function() 
                SELF:debug("STEP:",ev.type) 
                handlers[ev.type](ev)
                end,
    time or 100)
end

handlers={

    start = function(ev)
        SELF.soc:connect("192.168.2.242",23 ,{    
            success = function(result) post({type='connected',res=result}) end,
            error = function(msg) SELF:debug("Error:",msg) end
        })
    end,

    connected = function(ev) 
        print("SUCCESS CONNECTION")
        post({type='read',tag="CONNECT RESULT:",next='login'})
    end,

    login = function(ev) 
        SELF.soc:write("admin".."\n",{
            success=function(result) post({type='read', tag='LOGIN : ',next='password'}) end,
            error = function(msg) SELF:debug("Error:",msg) end
        })
    end,

    password = function(ev)
        SELF.soc:write("xxxx".."\n",{
            success=function(result) post({type='read', tag='PASSWORD RESULT : ',next='config'}) end,
            error = function(msg) SELF:debug("Error:",msg) end
        })
    end,

    config = function(ev)
        SELF.soc:write("configure terminal".."\n",{
            success=function(result) post({type='read', tag='CONFIG RESULT : ',next='status'}) end,
            error = function(msg) SELF:debug("Error:",msg) end
        })
    end,


    status = function(ev)
        SELF.soc:write("radio 2.4 disable".."\n",{
            success=function(result) post({type='read', tag='STATUS RESULT : ',next='exitMenu'}) end,
            error = function(msg) SELF:debug("Error:",msg) end
        })
    end,

    exitMenu = function(ev)
        SELF.soc:write("exit".."\n",{
            success=function(result) post({type='read', tag='EXIT RESULT : ',next='save'}) end,
            error = function(msg) SELF:debug("Error:",msg) end
        })
    end,

    save = function(ev)
        SELF.soc:write("save-and-activate".."\n",{
            success=function(result) post({type='read', tag='EXIT RESULT : '}) end,
            error = function(msg) SELF:debug("Error:",msg) end
        })
    end,

    read = function(ev)
    SELF.soc:read({
        success=function(result)
            SELF:debug(ev.tag,tostring(result))
            if ev.next then post({type=ev.next}) end
        end,
        error = function(msg) SELF:debug("Error:",msg) end
        })
    end,
}

 

 

Posté(e) (modifié)
34 minutes ago, jjacques68 said:

but what is the difference between "SELF" and "self"?

You are a fast learner! : 60:

'self' is a local variable inside QuickApp: functions (it's part of the object oriented model)

'self' is not available inside ordinary functions like 'post' or the 'handlers' functions

To have it accessible everywhere we assign it to a local called 'SELF' outside the functions. 

 

Modifié par jang
  • Like 1
Posté(e)

'post' takes a delay in millisecond as second argument (defaults to 0). This allows you to  introduce delays between write/reads in the steps if necessary.

Posté(e)

So I have a problem with the last command : "save"

 

save = function(arg)
        SELF.soc:write("save-and-activate".."\n",{
            success=function(result)
                    post({type='read', tag='SAVE : '},1000)
            end,

            error = function(msg) SELF:debug("Error:",msg) end
        })
    end,

I have not error message, but access point don't react !

 

I do the same instruction with HC2 and it works, there is no difference !!!

Posté(e)

it's OK !  excuse me !

 

I have 3 access points and I did a loop for each AP.

between each AP, I close the socket to open again on an another IP.

And the AP doesn't like that :) 

 

So i don't close the socket and the command :

SELF.soc:connect(SELF.ListeAP[SELF.NumberAP],23 ,{ ... ........

works fine still.

 

PFFFFFFFFFFIIIIIIIIOOOOOOOUUUUUUU !

 

super ! 

 

thanks !

 

I must write any comments to remember this method ;) 

 

×
×
  • Créer...