websocket send command



  • I am experimenting with websocket send. I want to replace the hard coded command with variables. Unfortunately without success. See code:

    import websocket
    import json
    from time import *
    
    
    #sending messages ------------------------------------
    
    def send_data(CMD,DEV,CIRCUIT,VALUE):
        print("sending data : ",CMD,DEV,CIRCUIT,VALUE)
        x = ("%s" %('"cmd"' + ":" + '"' + CMD + '"' + "," + '"dev"' + ":" + '"' + DEV + '"' + "," + '"circuit"' + ":" + '"' + CIRCUIT + '"' + "," + '"value"' + ":" + '"' + VALUE + '"'))
        print(x)
        ws = websocket.WebSocket()
        ws.connect("ws://localhost/ws")
        #ws.send('{"cmd":"all"}')
        #ws.send('{"cmd":"set","dev":"relay","circuit":"3","value":"0"}')
        ws.send('{("%s" %('"cmd"' + ":" + '"' + CMD + '"' + "," + '"dev"' + ":" + '"' + DEV + '"' + "," + '"circuit"' + ":" + '"' + CIRCUIT + '"' + "," + '"value"' + ":" + '"' + VALUE + '"'))}')    
        #ws.send('{"%s" %(x)}')
        ws.close()
    
    #main program -----------------------------------------
    #
    send_data("set","relay","5","1")
    sleep(10)
    

    Any idea what I am doing wrong here?
    Also command "cmd":"all" is not working.

    I am running evok on Stretch. Are there still problems with the new os?
    I find it difficult to find the needed libraries for stretch and Python3. It would be helpfull to have some tested links for download.

    appriciate your help.



  • I found it:

    #sending messages
    def ws_send(CMD, DEV, CIRCUIT, VALUE):
        ws = websocket.WebSocket()
        ws.connect(url)
        ws.send('{"cmd":"%s","dev":"%s","circuit":"%s","value":"%s"}' %(CMD,DEV,CIRCUIT,VALUE))    
        ws.close()