Пример #1
0
    def _getPointerData(self, moduleID):
        """private function to get pointer data from device and convert to dict

        """
        paramInfo = self.definePointerStructure()

        data = self._parent.client.getPointer(moduleID)
        array = convert.pointer2values(data, paramInfo)
        result = dict()
        for idx, info in enumerate(paramInfo):
            result[info[0]] = array[idx]
        return result
Пример #2
0
    def _getPointerData(self, moduleID):
        """private function to get pointer data from device and convert to dict

        """
        paramInfo = self.definePointerStructure()

        data = self._parent.client.getPointer(moduleID)
        array = convert.pointer2values(data, paramInfo)
        result = dict()
        for idx, info in enumerate(paramInfo):
            result[info[0]] = array[idx]
        return result
Пример #3
0
    def translateDetailedLog(self,
                             moduleID,
                             binaryfile,
                             csvfile,
                             headerNames=None):
        """
        decodes detailed logging
        @param moduleID - module to get log for (Mx, Px, Ax)
        @param binaryfile - filename of binary log
        @param csvfile - filename for csv
        """
        def prepareValue(value, info):
            result = []
            if isinstance(value, list):
                for val in value:
                    result.extend(prepareValue(val, info))
            elif isinstance(value, str):
                result.append('"%s"' % value)
            elif isinstance(value, tuple):
                result.append(str(round(value[0], 3)))
                result.append(
                    '"%s"' %
                    time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(value[1])))
            else:
                if info[1].type == 'TYPE_TIMESTAMP':
                    result.append(
                        '"%s"' %
                        time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(value)))
                else:
                    result.append(str(round(value, 3)))

            return result

        def prepareHeader(info, nr=None):
            hdr = []
            if info[2] > 1 and nr is None:
                for i in range(info[2]):
                    hdr.append(prepareHeader(info, i + 1))
            else:
                tmpl = "%s"
                if nr:
                    tmpl += " (%d)" % nr
                if isinstance(headerNames, dict) and info[0] in headerNames:
                    name = headerNames[info[0]]
                else:
                    name = str(info[0])
                hdr.append(tmpl % name)
                if info[1].type.endswith('_WITH_TS'):
                    hdr.append(tmpl % ("%s timestamp" % name))
            return ", ".join(hdr)

        def calculatePointerSize(structure):
            size = 0
            for elem in structure:
                size += elem[2] * elem[1].size

            return size

        if moduleID[0] == 'M':
            paramInfo = self.master.definePointerStructure()
        elif moduleID[0] == 'P' and self.power:
            paramInfo = self.power.definePointerStructure()
        elif moduleID[0] == 'A' and self.sensor:
            paramInfo = self.sensor.definePointerStructure()
        elif moduleID[0] == 'Q' and self.slave_power:
            paramInfo = self.slave_power.definePointerStructure()
        else:
            raise ValueError('Requested moduleID not found')

        chunk = calculatePointerSize(paramInfo)

        #prepare header information
        lineData = []
        for idx, info in enumerate(paramInfo):
            if info[0] < 3:
                continue
            lineData.append(prepareHeader(info))

        with open(csvfile, "w") as out:
            out.write(", ".join(lineData))
            out.write("\n")
            with open(binaryfile, "rb") as fp:
                while True:
                    data = fp.read(chunk)
                    if not data:
                        break
                    try:
                        array = convert.pointer2values(data, paramInfo)
                    except IndexError:
                        break
                    lineData = []
                    for idx, info in enumerate(paramInfo):
                        if info[0] < 3:
                            continue
                        lineData.extend(prepareValue(array[idx], info))

                    out.write(", ".join(lineData))
                    out.write("\n")
Пример #4
0
    def translateDetailedLog(self, moduleID, binaryfile, csvfile, headerNames=None):
        """
        decodes detailed logging
        @param moduleID - module to get log for (Mx, Px, Ax)
        @param binaryfile - filename of binary log
        @param csvfile - filename for csv
        """

        def prepareValue(value, info):
            result = []
            if isinstance(value, list):
                for val in value:
                    result.extend(prepareValue(val, info))
            elif isinstance(value, str):
                result.append('"%s"' % value)
            elif isinstance(value, tuple):
                result.append(str(round(value[0], 3)))
                result.append('"%s"' % time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(value[1])))
            else:
                if info[1].type == 'TYPE_TIMESTAMP':
                    result.append('"%s"' % time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(value)))
                else:
                    result.append(str(round(value, 3)))

            return result

        def prepareHeader(info, nr=None):
            hdr = []
            if info[2] > 1 and nr is None:
                for i in range(info[2]):
                    hdr.append(prepareHeader(info, i + 1))
            else:
                tmpl = "%s"
                if nr:
                    tmpl += " (%d)" % nr
                if isinstance(headerNames, dict) and info[0] in headerNames:
                    name = headerNames[info[0]]
                else:
                    name = str(info[0])
                hdr.append(tmpl % name)
                if info[1].type.endswith('_WITH_TS'):
                    hdr.append(tmpl % ("%s timestamp" % name))
            return ", ".join(hdr)

        def calculatePointerSize(structure):
            size = 0
            for elem in structure:
                size += elem[2] * elem[1].size

            return size

        if moduleID[0] == 'M':
            paramInfo = self.master.definePointerStructure()
        elif moduleID[0] == 'P' and self.power:
            paramInfo = self.power.definePointerStructure()
        elif moduleID[0] == 'A' and self.sensor:
            paramInfo = self.sensor.definePointerStructure()
        elif moduleID[0] == 'Q' and self.slave_power:
            paramInfo = self.slave_power.definePointerStructure()
        else:
            raise ValueError('Requested moduleID not found')

        chunk = calculatePointerSize(paramInfo)

        #prepare header information
        lineData = []
        for idx, info in enumerate(paramInfo):
            if info[0] < 3:
                continue
            lineData.append(prepareHeader(info))

        with open(csvfile, "w") as out:
            out.write(", ".join(lineData))
            out.write("\n")
            with open(binaryfile, "rb") as fp:
                while True:
                    data = fp.read(chunk)
                    if not data:
                        break
                    try:
                        array = convert.pointer2values(data, paramInfo)
                    except IndexError:
                        break
                    lineData = []
                    for idx, info in enumerate(paramInfo):
                        if info[0] < 3:
                            continue
                        lineData.extend(prepareValue(array[idx], info))

                    out.write(", ".join(lineData))
                    out.write("\n")