Exemplo n.º 1
0
def calibrate_flappers(text):
    txt = text[:4]
    print "** calibrate flappers ", txt
    nc = Netcat(flappers_host, flappers_port)
    nc.write('$iread ' + txt + '\n')
    nc.close()
    return "True"
Exemplo n.º 2
0
def send_to_flappers(text):
    txt = text[:4]
    print "** send to flappers ", txt
    nc = Netcat(flappers_host, flappers_port)
    nc.write(txt + '\n')
    sleep(10)
    nc.write('@@@@\n')
    nc.close()
    return "True"
Exemplo n.º 3
0
def encrypt(msg):
    nc = Netcat(host, port)
    nc.read(1024)
    nc.read(1024)
    nc.write(msg.encode('hex') + '\n')
    data = nc.read(1024)
    nc.read(1024)
    nc.write('n')
    nc.close()
    # print block(data[12:])
    return data[12:]
Exemplo n.º 4
0
def encrypt2(msg):
    nc = Netcat(host, port)
    nc.read_until('\n')
    nc.read_until('\n')
    # print "[1]", nc.read_until('\n')
    # print "[2]", nc.read_until('\n')
    nc.write(msg.encode('hex') + '\n')
    print msg  #.encode('hex')
    data = nc.read_until('\n').strip()
    # print "[+]", data
    nc.read_until('\n')
    # print "[3]", nc.read_until('\n')
    nc.write('n\n')
    nc.close()
    # print block(data[12:])
    return data[12:]
Exemplo n.º 5
0
def main2(argv):

    if len(argv) != 2:
        print "usage: %s url" % argv[0];
        sys.exit(1);

    # Load config file, if available
    cfgfile = ".knxmonitor.cson"
    try:
        print "Trying: %s" %cfgfile
        cfg = cson.loads(open("%s" %cfgfile).read())
        print "Loaded: %s" %cfgfile
    except IOError:
        try:
            print "Trying: ~/%s" %cfgfile
            cfg = cson.loads(open(expanduser("~/%s" % cfgfile)).read())
            print "Loaded: ~/%s" %cfgfile
        except IOError:
            print "No .knxmonitor.cson file found, using default values for config"
            cfg = { 'unitfile' : 'enheter.xml',
                    'groupfile' : 'groupaddresses.csv' }

    #loadGroupAddrs(cfg['groupfile'])
    #loadDeviceAddrs(cfg['unitfile'])
    devDict   = KnxAddressCollection()
    groupDict = KnxAddressCollection()
    dptDict = KnxAddressCollection()

    # Load device and address info
    groupDict.loadGroupAddrs(open(cfg['groupfile']))
    devDict.loadDeviceAddrs(open(cfg['unitfile']))
    if 'dptfile' in cfg.keys():
      dptDict.loadDptTable(open(cfg['dptfile']))

    # Should we push to an InfluxDB instance?
    if 'push2influx' in cfg.keys():
      host, port = cfg['push2influx'].split(":")
      print "Pushing to InfluxDB: %s:%d" %(host,int(port))

    if argv[1] != "simul":

        try:
            con = EIBConnection()
        except:
            print "Could not instantiate EIBConnection";
            sys.exit(1);

        tries = 1
        connected = False
        while (not connected) and (tries < 5):
            try:
                if con.EIBSocketURL(argv[1]) != 0:
                    print "Could not connect to: %s" %argv[1]
                    sys.exit(1)
                else:
                    connected = True
            except socket.error:
                print "failed to connect, retrying in 5 sec..."
                time.sleep(5)
                tries += 1

        if not connected:
            print "Unable to connect, tried %d times, giving up." % tries
            sys.exit(1)

        if con.EIBOpenVBusmonitorText() != 0:
            # For some reason this always "fails" with EBUSY,
            # hence just ignore that particular error
            if con.errno != errno.EBUSY:
                print "Could not open bus monitor";
                sys.exit(1)

        log = KnxLogFileHandler()

        buf = EIBBuffer()
        while 1:
            length = con.EIBGetBusmonitorPacket (buf)

            if length == 0:
                print "Read failed"
                sys.exit(1)

            ts = time.localtime()

            b = ""
            for x in buf.buffer:
                b += chr(x)

            print time.asctime(ts) + ":" + b

            outfile = log.getFileToUse()
            outfile.write(time.asctime(ts) + ":" + b + "\n")
            outfile.flush()

            if 'push2influx' in cfg.keys():
              # Best effort decode...
              try:
                pdu = KnxPdu(devDict, groupDict, b)

                tim = time.mktime(ts)
                to  = pdu.getTo()
                info,typ = dptDict[to]
                val = float(pdu.getValue(typ))

                json_line = json.dumps( { "name" : "KNX",
                                          info : val,
                                          "tim" : tim } )
                print json_line
                #continue

                try:
                  nc = Netcat(host, int(port))
                  nc.write(json_line)
                  nc.close()
                except Exception as e:
                  print "Failed to netcat: %s" %e

              except:
                # Ignore problems for now...
                #print "failed to decode: %s" %b
                pass

        con.EIBClose()