• Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    rest request result not json conform

    Official EVOK API
    2
    16
    3746
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      juntiedt last edited by tomas_hora

      Hi Tomas,

      did not work :-(
      I have put a debug print into def full(self). It was not printed -> the routine was not executed

      fyi my testprogramm :100:

      #!/usr/bin/python
      
      import urllib.request
      import subprocess
      import time
      
      #----------------------------------------------------------
      
      def restart_evok():
          subprocess.call(['sudo', 'service', 'evok', 'stop'])
          time.sleep(1)
          print("EVOK stoped")
          subprocess.call(['sudo', 'service', 'evok', 'start'])
          time.sleep(1)
          print("EVOK started" + "\n")
          
      #----------------------------------------------------------
      
      restart_evok()
      
      Relay = 1
      url='http://10.0.0.50/rest/relay/'+str(Relay)
      print ('URL   = ' + url)
      unipi = urllib.request.urlopen(url).read()
      print ('R1    = ' + str(unipi) + "\n")
      
      Temp1 = "28B01DC107000045"
      url='http://10.0.0.50/rest/sensor/' + Temp1
      print ('URL   = ' + url)
      unipi = urllib.request.urlopen(url).read()
      print ('Temp1 = ' + str(unipi) + "\n")
      

      it looks like websocket is handled elsewhere in evok.
      any idea?

      1 Reply Last reply Reply Quote 0
      • T
        tomas_hora administrators last edited by

        @juntiedt Are you sure you are editing the full method in right places? I just tried it for DS18B20 sensor and works well:

        class DS18B20(MySensor):  # thermometer
        
            def full(self):
                return {'dev': 'temp', 'circuit': self.circuit, 'address': self.address,
                        'value': self.value, 'lost': self.lost, 'time': str(self.time), 'interval': self.interval, 'typ': self.type}
        

        And the output was as expected:

        >>> print urllib.urlopen("http://192.168.1.107/rest/sensor/28A83B60070000FD").read()
        {"interval": 15, "value": 22.4, "circuit": "28A83B60070000FD", "address": "28A83B60070000FD", "time": "1480336515.68", "typ": "DS18B20", "lost": false, "dev": "temp"}
        
        1 Reply Last reply Reply Quote 0
        • J
          juntiedt last edited by

          sorry, but is this the right place to edit on my Raspi?
          /home/pi/evok/evok/owclient.py

          1 Reply Last reply Reply Quote 0
          • T
            tomas_hora administrators last edited by

            That is only a temporary folder where the source was downloaded. The application is located in /opt/evok/

            1 Reply Last reply Reply Quote 0
            • J
              juntiedt last edited by

              Hallo Tomas,

              Problem solved!
              Thanks for your support.

              Hannes

              1 Reply Last reply Reply Quote 0
              • J
                juntiedt last edited by

                Hallo Tomas,

                the problem above is solved but now the unipi control panel does not work correctly with the above suggested changes.
                The web application needs also to be changed to work with strings. But where to change?

                Hannes

                1 Reply Last reply Reply Quote 0
                • T
                  tomas_hora administrators last edited by

                  @juntiedt The place to look at is here: https://github.com/UniPiTechnology/evok/blob/master/www/evok/js/wsbase.js#L11-L45

                  Located in /var/www/evok/

                  1 Reply Last reply Reply Quote 0
                  • J
                    juntiedt last edited by

                    I am not familiar with java script.
                    Due to my changes in the python code msg.circuit and msg.value should be in string format which I have to reformat in java script to int or float .....?
                    Please have a look at my code below.
                    control panel only shows relay 1 and input 1

                     function SyncDevice(msg) {
                    //todo: add name as parameter
                    var name = "";
                    var circuit = parseInt(msg.circuit);     <-----
                    var dev_type = msg.dev;
                    var value = parseInt(msg.value);    <------
                    var unit = "";
                    var ns = "unipi_" + dev_type + "_" + circuit;
                    
                    if (dev_type == 'ai') {
                        name = "Analog input " + circuit;
                        value = value.toFixed(3);     <------
                        unit = "V";
                    }
                    else if (dev_type == 'ao') {
                        name = "Analog output " + circuit;
                        unit = "V";
                        value = value.toFixed(1);     <------
                    }
                    else if (dev_type == 'relay') {
                        name = "Relay " + circuit;
                    }
                    else if (dev_type == 'input') {
                        name = "Input " + circuit;
                    }
                    else if (dev_type == 'temp') {
                        name = "Sensor " + msg.typ + " - " + circuit;
                        if value == null) {
                            value = "null";
                        }
                        else {
                            value = value.toFixed(1);
                            unit = "°C";
                        }
                    }
                    
                    1 Reply Last reply Reply Quote 0
                    • T
                      tomas_hora administrators last edited by tomas_hora

                      You should parse the string according to the expected data type. Eg. if you make integer from float right at the beginning you wont be able to get a float from that later on. So it should be something like this:

                      function SyncDevice(msg) {
                      //todo: add name as parameter
                      var name = "";
                      var circuit = parseInt(msg.circuit);
                      var dev_type = msg.dev;
                      var value = msg.value;       <------
                      var unit = "";
                      var ns = "unipi_" + dev_type + "_" + circuit;
                      
                      if (dev_type == 'ai') {
                          name = "Analog input " + circuit;
                          value = parseFloat(value).toFixed(3);     <------
                          unit = "V";
                      }
                      else if (dev_type == 'ao') {
                          name = "Analog output " + circuit;
                          unit = "V";
                          value = parseFloat(value).toFixed(1);     <------
                      }
                      else if (dev_type == 'relay') {
                          name = "Relay " + circuit;
                          value = parseInt(value);    <------
                      }
                      else if (dev_type == 'input') {
                          name = "Input " + circuit;
                          value = parseInt(value);    <------
                      }
                      else if (dev_type == 'temp') {
                          name = "Sensor " + msg.typ + " - " + circuit;
                          if (value == null || value == "") {
                              value = "null";
                          }
                          else {
                              value = parseFloat(value).toFixed(1);     <------
                              unit = "°C";
                          }
                      }
                      
                      1 Reply Last reply Reply Quote 0
                      • J
                        juntiedt last edited by

                        super!
                        everything is ok now
                        great support
                        thank you

                        1 Reply Last reply Reply Quote 0
                        • J
                          juntiedt last edited by tomas_hora

                          Hi Tomas,

                          I still have a little problem with the control panel.
                          After my changes in wsbase.js my 1-wire sensor circuits are cut off at the first letter. See picture:

                          0_1481284880886_upload-2ede6770-4a69-4a1e-a7d8-7aff93d1e313

                          I changed my code in wsbase as follows:

                          function SyncDevice(msg) {
                          	//todo: add name as parameter
                          	var name = "";
                          	var circuit_a = msg.circuit;                            <------------------------
                          	var circuit = parseInt(msg.circuit);
                          	var dev_type = msg.dev;
                          	var value = msg.value;       
                          	var unit = "";
                          	var ns = "unipi_" + dev_type + "_" + circuit;
                          	if (dev_type == 'ai') {
                          		name = "Analog input " + circuit;
                          		value = parseFloat(value).toFixed(3);
                          		unit = "V";
                          	}
                          	else if (dev_type == 'ao') {
                          		name = "Analog output " + circuit;
                          		unit = "V";
                          		value = parseFloat(value).toFixed(1);
                          	}
                          	else if (dev_type == 'relay') {
                          		name = "Relay " + circuit;
                          		value = parseInt(value);
                          	}
                          	else if (dev_type == 'input') {
                          		name = "Input " + circuit;
                          		value = parseInt(value);
                          	}
                          	else if (dev_type == 'temp') {
                          		name = "Sensor " + msg.typ + " - " + circuit_a;     <------------------------
                          		if (value == null || value == ""){
                          			value = "null";
                              	}
                                   else {
                                  	value = parseFloat(value).toFixed(1);
                                  	unit = "°C";
                          		}
                          	}
                          

                          unfortunately no success.
                          What have I done wrong?

                          1 Reply Last reply Reply Quote 0
                          • J
                            juntiedt last edited by

                            hi Tomas,

                            sorry for my recent post!
                            I have solved the problem!
                            I had to delete the cache of my chrom browser and now it works!

                            have a nice Weekend

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post