示例#1
0
    def makePDU(self, segMac, scMac, registerAddr, TH, TL):
        header = struct.pack("!B", 0xCC)#帧头
        scmac = struct.pack("!Q", convertmac(scMac))#采控器地址
        cmd = struct.pack("!B", 0x06)#下发控制命令
        register = struct.pack("<H", registerAddr)#寄存器地址
        
        try:
            data = TH << 8 | TL
            print data
            data = struct.pack("<H", data)#数据
        except:
            data = struct.pack("<H", 0x00)

        tail = struct.pack("!B", 0xDD)#帧尾
        pdu = scmac + cmd + register +  data# 要送去做CRC的字段
        format = "!" + "B" * len(pdu)
        crc = struct.pack("!H", getCRC(list(struct.unpack(format, pdu))))#unpack之后是元组,转成列表

        pdu = header + pdu + crc + tail
        pdu = self.makeMBAPHeader(segMac, pdu) + pdu
        return pdu
示例#2
0
    def makePDU(self, segMac, scMac, registerAddr, TH, TL):
        header = struct.pack("!B", 0xCC)  #帧头
        scmac = struct.pack("!Q", convertmac(scMac))  #采控器地址
        cmd = struct.pack("!B", 0x06)  #下发控制命令
        register = struct.pack("<H", registerAddr)  #寄存器地址

        try:
            data = TH << 8 | TL
            print data
            data = struct.pack("<H", data)  #数据
        except:
            data = struct.pack("<H", 0x00)

        tail = struct.pack("!B", 0xDD)  #帧尾
        pdu = scmac + cmd + register + data  # 要送去做CRC的字段
        format = "!" + "B" * len(pdu)
        crc = struct.pack("!H",
                          getCRC(list(struct.unpack(format,
                                                    pdu))))  #unpack之后是元组,转成列表

        pdu = header + pdu + crc + tail
        pdu = self.makeMBAPHeader(segMac, pdu) + pdu
        return pdu
示例#3
0
文件: ctmb.py 项目: Zouyiran/Adapter
    def regularReport(self, mbapHeader, data):
        gathertime = datetime.datetime.now()
        dataInfoList = []
        forcrc = data[:-3]
        forcrc = forcrc[1:]
        format = "!" + "B" * len(forcrc)
        calculate_crc = getCRC(list(struct.unpack(format, forcrc)))
        recv_crc = struct.unpack("!H", data[-3:-1])[0]
        if calculate_crc == recv_crc:
            #边缘网关标识符Mac
            segmac = mbapHeader[6:10]
            segmac = struct.unpack("!I", segmac)[0]
            segmac = convertmac(hex(segmac), 4)
                       
            #采控器地址 Mac   
            scmac = data[1:9]
            scmac = struct.unpack("!Q", scmac)[0]
            scmac = convertmac(hex(scmac), 8)
              
            #起始寄存器地址Address
            reg = data[10:12]
            reg = struct.unpack("<H", reg)[0]
            
            #数据长度
            datalength = data[12:14] #N * 4
            datalength = struct.unpack("<H", datalength)[0]
            datalength = datalength / 4 # N
            
            #每个数据值以及其所包含的信息
            for n in range(0, datalength):
                reg = reg + n * 4
                getConfigInfo = ConfigManager(XMLFILEPATH)
                try:
                    eachDataInfoDict, configFile = getConfigInfo.get(segmac,scmac,reg)
                except:
                    continue
                value = data[(14 + n * 4) : (18 + n * 4)]
                value = struct.unpack("<I", value)[0]
                TH = float((value >> 16) >> 8)
                TL = float((value >> 16) & 0x00FF)
                try:
                    realvalue = eval(eachDataInfoDict['Rule']) #算映射值  
                    del eachDataInfoDict['Rule'] 
                except Exception, e:
                    #如果原始数据转换规则出错则默认原始数据为0
                    realvalue = 0
                    logger.info("未找到原始数据转换规则或者规则错误,原因%s" % str(e))

                try:
                    SValue = self.getSValue(realvalue, eachDataInfoDict['SRule'])
                    del eachDataInfoDict['SRule']
                except Exception, e:
                    #如果查询显示转换规则出错则默认显示数据与原始数据一致
                    SValue = realvalue
                    logger.info("未找到显示转换规则或者规则错误,原因%s" % str(e))

                eachDataInfoDict['Value'] = realvalue
                eachDataInfoDict['SValue'] = SValue
                #以收到上报数据的时间作为采集时间
                eachDataInfoDict['GatherTime'] = gathertime
                #处理是否告警
                rangeRequester = GetDevInsRangeList(eachDataInfoDict['InsID'])
                high, low = rangeRequester.run()
                entityName = eachDataInfoDict['MonitorName'].encode("UTF-8")
                deviceName = eachDataInfoDict['Name'].encode("UTF-8")
                
                insid = eachDataInfoDict["InsID"]
                #本地内存中没有该设备的记录,则新建一个,默认告警状态为正常
                if insid not in globals.deviceMap.keys():
                    eachDataInfoDict['AlertType'] = 0
                    eachDataInfoDict['AlertDesc'] = "正常"
                    globals.deviceMap[insid] = eachDataInfoDict
                #有记录则当前应沿用之前的告警状态
                else:
                    eachDataInfoDict['AlertType'] = globals.deviceMap[insid]['AlertType']
                    eachDataInfoDict['AlertDesc'] = globals.deviceMap[insid]['AlertDesc']

                if high != -1 and float(SValue) > float(high):  
                    #若已经处于告警状态,不再重复告警
                    if globals.deviceMap[insid]['AlertType'] != 2:               
                        msg = "%s%s(%s)在%s超过阈值(%d,%d)" % (entityName, deviceName, SValue, datetime.datetime.now(), low, high)
                        eachDataInfoDict['AlertType'] = 2
                        eachDataInfoDict['AlertDesc'] = msg
                        alertRequester = AlertToApp()
                        alertRequester.insid = eachDataInfoDict['InsID']
                        alertRequester.workstatus = eachDataInfoDict['WorkStatus']
                        alertRequester.alertType = 1
                        alertRequester.contents = msg
                        alertRequester.run()

                elif low != -1 and float(SValue) < float(low):
                    #若已经处于告警状态,不再重复告警
                    if globals.deviceMap[insid]['AlertType'] != 1:               
                        msg = "%s%s(%s)在%s超过阈值(%d,%d)" % (entityName, deviceName, SValue, datetime.datetime.now(), low, high)
                        eachDataInfoDict['AlertType'] = 1
                        eachDataInfoDict['AlertDesc'] = msg
                        alertRequester = AlertToApp()
                        alertRequester.insid = eachDataInfoDict['InsID']
                        alertRequester.workstatus = eachDataInfoDict['WorkStatus']
                        alertRequester.alertType = 1
                        alertRequester.contents = msg
                        alertRequester.run()
                else:
                    #若处于告警状态,发出恢复正常告警指令,若已为正常状态,不再发出告警
                    eachDataInfoDict['AlertType'] = 0
                    eachDataInfoDict['AlertDesc'] = "正常"
                    if globals.deviceMap[insid]['AlertType'] != 0:  
                        msg = "%s%s(%s)在%s恢复正常(%d,%d)" % (entityName, deviceName, SValue, datetime.datetime.now(), low, high)
                        alertRequester = AlertToApp()
                        alertRequester.insid = eachDataInfoDict['InsID']
                        alertRequester.workstatus = eachDataInfoDict['WorkStatus']
                        alertRequester.alertType = 0
                        alertRequester.contents = msg
                        alertRequester.run()

                #更新内存中设备状态表信息
                globals.deviceMap[eachDataInfoDict["InsID"]] = eachDataInfoDict
                dataInfoList.append(eachDataInfoDict)
                
                storeip(self.ip, segmac, configFile, self.segIP)    
示例#4
0
    def regularReport(self, mbapHeader, data):
        gathertime = datetime.datetime.now()
        dataInfoList = []
        forcrc = data[:-3]
        forcrc = forcrc[1:]
        format = "!" + "B" * len(forcrc)
        calculate_crc = getCRC(list(struct.unpack(format, forcrc)))
        recv_crc = struct.unpack("!H", data[-3:-1])[0]
        if calculate_crc == recv_crc:
            #边缘网关标识符Mac
            segmac = mbapHeader[6:10]
            segmac = struct.unpack("!I", segmac)[0]
            segmac = convertmac(hex(segmac), 4)

            #采控器地址 Mac
            scmac = data[1:9]
            scmac = struct.unpack("!Q", scmac)[0]
            scmac = convertmac(hex(scmac), 8)

            #起始寄存器地址Address
            reg = data[10:12]
            reg = struct.unpack("<H", reg)[0]

            #数据长度
            datalength = data[12:14]  #N * 4
            datalength = struct.unpack("<H", datalength)[0]
            datalength = datalength / 4  # N

            #每个数据值以及其所包含的信息
            for n in range(0, datalength):
                reg = reg + n * 4
                getConfigInfo = ConfigManager(XMLFILEPATH)
                try:
                    eachDataInfoDict, configFile = getConfigInfo.get(
                        segmac, scmac, reg)
                except:
                    continue
                value = data[(14 + n * 4):(18 + n * 4)]
                value = struct.unpack("<I", value)[0]
                TH = float((value >> 16) >> 8)
                TL = float((value >> 16) & 0x00FF)
                try:
                    realvalue = eval(eachDataInfoDict['Rule'])  #算映射值
                    del eachDataInfoDict['Rule']
                except Exception, e:
                    #如果原始数据转换规则出错则默认原始数据为0
                    realvalue = 0
                    logger.info("未找到原始数据转换规则或者规则错误,原因%s" % str(e))

                try:
                    SValue = self.getSValue(realvalue,
                                            eachDataInfoDict['SRule'])
                    del eachDataInfoDict['SRule']
                except Exception, e:
                    #如果查询显示转换规则出错则默认显示数据与原始数据一致
                    SValue = realvalue
                    logger.info("未找到显示转换规则或者规则错误,原因%s" % str(e))

                eachDataInfoDict['Value'] = realvalue
                eachDataInfoDict['SValue'] = SValue
                #以收到上报数据的时间作为采集时间
                eachDataInfoDict['GatherTime'] = gathertime
                #处理是否告警
                rangeRequester = GetDevInsRangeList(eachDataInfoDict['InsID'])
                high, low = rangeRequester.run()
                entityName = eachDataInfoDict['MonitorName'].encode("UTF-8")
                deviceName = eachDataInfoDict['Name'].encode("UTF-8")

                insid = eachDataInfoDict["InsID"]
                #本地内存中没有该设备的记录,则新建一个,默认告警状态为正常
                if insid not in globals.deviceMap.keys():
                    eachDataInfoDict['AlertType'] = 0
                    eachDataInfoDict['AlertDesc'] = "正常"
                    globals.deviceMap[insid] = eachDataInfoDict
                #有记录则当前应沿用之前的告警状态
                else:
                    eachDataInfoDict['AlertType'] = globals.deviceMap[insid][
                        'AlertType']
                    eachDataInfoDict['AlertDesc'] = globals.deviceMap[insid][
                        'AlertDesc']

                if high != -1 and float(SValue) > float(high):
                    #若已经处于告警状态,不再重复告警
                    if globals.deviceMap[insid]['AlertType'] != 2:
                        msg = "%s%s(%s)在%s超过阈值(%d,%d)" % (
                            entityName, deviceName, SValue,
                            datetime.datetime.now(), low, high)
                        eachDataInfoDict['AlertType'] = 2
                        eachDataInfoDict['AlertDesc'] = msg
                        alertRequester = AlertToApp()
                        alertRequester.insid = eachDataInfoDict['InsID']
                        alertRequester.workstatus = eachDataInfoDict[
                            'WorkStatus']
                        alertRequester.alertType = 1
                        alertRequester.contents = msg
                        alertRequester.run()

                elif low != -1 and float(SValue) < float(low):
                    #若已经处于告警状态,不再重复告警
                    if globals.deviceMap[insid]['AlertType'] != 1:
                        msg = "%s%s(%s)在%s超过阈值(%d,%d)" % (
                            entityName, deviceName, SValue,
                            datetime.datetime.now(), low, high)
                        eachDataInfoDict['AlertType'] = 1
                        eachDataInfoDict['AlertDesc'] = msg
                        alertRequester = AlertToApp()
                        alertRequester.insid = eachDataInfoDict['InsID']
                        alertRequester.workstatus = eachDataInfoDict[
                            'WorkStatus']
                        alertRequester.alertType = 1
                        alertRequester.contents = msg
                        alertRequester.run()
                else:
                    #若处于告警状态,发出恢复正常告警指令,若已为正常状态,不再发出告警
                    eachDataInfoDict['AlertType'] = 0
                    eachDataInfoDict['AlertDesc'] = "正常"
                    if globals.deviceMap[insid]['AlertType'] != 0:
                        msg = "%s%s(%s)在%s恢复正常(%d,%d)" % (
                            entityName, deviceName, SValue,
                            datetime.datetime.now(), low, high)
                        alertRequester = AlertToApp()
                        alertRequester.insid = eachDataInfoDict['InsID']
                        alertRequester.workstatus = eachDataInfoDict[
                            'WorkStatus']
                        alertRequester.alertType = 0
                        alertRequester.contents = msg
                        alertRequester.run()

                #更新内存中设备状态表信息
                globals.deviceMap[eachDataInfoDict["InsID"]] = eachDataInfoDict
                dataInfoList.append(eachDataInfoDict)

                storeip(self.ip, segmac, configFile, self.segIP)