Exemple #1
0
def send_plan():

    Utils.log("send plan start")
    # 获取设配配置
    devices = Utils.readJsonFile('config/device_config_plan.json')
    # 迭代设备
    data = {}
    # 时间
    ts = Utils.getTS()
    for name, device in devices.items():

        for k, v in device.items():
            if 'I' in k:
                device[k] = v + random.randint(-100, 200) / 10
            elif 'P' == k:
                device[k] = v + random.randint(-300, 500) / 10

        # 组装设备数据结构
        data[name] = [{'ts': ts, 'devic': name, 'values': device}]
        #status = device_connection
        # 维护设备状态
        #device_status(name, status)
    if data:
        # mqtt发送数据
        tb_client.SendTelemetry(json.dumps(data), False)

        data_mod = Utils.readFile('config/th_data.json')
        data_mod = data_mod.replace("{time}", str(Utils.getTS()))
        data_mod = data_mod.replace("{h}", str(234 / 10))
        data_mod = data_mod.replace("{t}", str(178 / 10))

        tb_client.SendTelemetry(data_mod, False)

    Utils.log("send plan end")
Exemple #2
0
def send2():
    try:
        data = master.execute(5, 4, 0, 6)
        data2 = master.execute(5, 1, 0, 6)
        Utils.log('')
        Utils.log('send2 data {data}'.format(data = data))
        Utils.log('')

        data_mod = Utils.readJsonFile('config/wd_data.json')

        data_mod = data_mod.replace("{time}", str(Utils.getTS()))
        data_mod = data_mod.replace("{1A}", str(data[0]))
        data_mod = data_mod.replace("{1B}", str(data[1]))
        data_mod = data_mod.replace("{1C}", str(data[2]))
        data_mod = data_mod.replace("{2A}", str(data[3]))
        data_mod = data_mod.replace("{2B}", str(data[4]))
        data_mod = data_mod.replace("{2C}", str(data[5]))

        data_mod = data_mod.replace("{1A_p}", str(data2[0]))
        data_mod = data_mod.replace("{1B_p}", str(data2[1]))
        data_mod = data_mod.replace("{1C_p}", str(data2[2]))
        data_mod = data_mod.replace("{2A_p}", str(data2[3]))
        data_mod = data_mod.replace("{2B_p}", str(data2[4]))
        data_mod = data_mod.replace("{2C_p}", str(data2[5]))

        #Utils.log('data2 JSON: {data}'.format(data = data_mod))
        if data:
            tb_client.SendTelemetry(data_mod, False)
            Utils.log('send success')
    except BaseException as e:
        Utils.log('get error','error')
        Utils.log('except:{s}'.format(s = str(e)))
        Utils.log('traceback.print_exc():{s}'.format(s = str(traceback.print_exc())))
Exemple #3
0
def send():
    Utils.log("send test start")
    ts = Utils.getTS()
    data_mod = Utils.readFile('config/test_data.json')
    data_mod = data_mod.replace("{time}", str(ts))
    tb_client.SendTelemetry(data_mod, False)
    Utils.log("send test end")
Exemple #4
0
def send():
    Utils.log("send start")
    send_data = defaultdict(init_dict)
    for master in masters:
        ts = Utils.getTS()
        # 获取此master 对应串口的设备s
        devices = device_info_map[master.port]
        for device in devices:
            device_name = device['device_name']
            address = device['address']
            # 采集
            data = device['collect_func'](master)
            # 组装设备数据结构
            if data:
                send_data[device_name][0]['ts'] = ts
                send_data[device_name][0]['values'] = data
                status = device_connection
            else:
                # 异常常处理
                error_handler(master.port, address, device_name)
                status = device_disconnection
            # 维护设备状态
            device_status_handler(master.port, address, device_name, status)
    if send_data.keys():
        # mqtt发送数据
        tb_client.SendTelemetry(data, True)
    Utils.log("send end")
Exemple #5
0
def send():
    # my_data = Utils.readJsonFile('config/test_data.json')
    values = {}
    try:
        data = master.execute(1, cst.READ_HOLDING_REGISTERS, 23, 58)
        if data:
            data = data.__iter__()

            for key in parms[:-3]:
                try:
                    values[key] = fc.pars(next(data), next(data))
                except ValueError:
                    values[key] = 0
            # 最后4个寄存器值
            last_data = list(data)
            try:
                values[parms[-3]] = fc.pars(last_data[0], last_data[1])
                values[parms[-2]] = fc.pars(last_data[1], last_data[2])
                values[parms[-1]] = fc.pars(last_data[2], last_data[3])
            except:
                pass
    except Exception as e:
        Utils.log('获取数据失败' + e.__str__(), 'error')

    if values:
        my_data['test'][0]['ts'] = Utils.getTS()
        my_data['test'][0]['values'] = values
        tb_client.SendTelemetry(my_data, True)
        Utils.log("数据推送")
Exemple #6
0
def send():
    data = ()
    try:
        #data2 = master.execute(2, cst.READ_HOLDING_REGISTERS, 0, 2)
        data = master.execute(1, cst.WRITE_SINGLE_COIL, 0, 2)
        Utils.log('')
        Utils.log('获取温湿度数据 data1 {data}'.format(data = data))
        #Utils.log('获取温湿度数据 data2 {data}'.format(data=data2))
        Utils.log('')

        data_mod = Utils.readJsonFileFile('config/th_data.json')
        data_mod = data_mod.replace("{time}",str(Utils.getTS()))
        data_mod = data_mod.replace("{h}", str(data[0]/10))
        data_mod = data_mod.replace("{t}", str(data[1]/10))
    except BaseException:
        Utils.log('温湿度获取失败','error')

    if data:
        tb_client.SendTelemetry(data_mod,False)
        Utils.log("数据推送 '湿度':{humidity},‘温度’:{temperature} ".format(humidity = data[0]/10, temperature = data[1]/10))
Exemple #7
0
def send():
    """
    发送遥测数据
    :return: None
    """
    Utils.log("send start")
    # 获取设配配置
    devices = Utils.readJsonFile('config/device_config.json')
    # 获取设备数据
    devices_data = modbus_data.get_devices_data()
    # 迭代设备
    data = {}
    # 时间
    ts = Utils.getTS()
    for name, device in devices.items():
        devices_param = {}
        # 匹配设备参数
        for k, v in device.items():
            if isinstance(v, str):
                continue
            if v in devices_data:
                devices_param[k] = devices_data[v]

                if name == '1D1' or name == '3D1':
                    if '-' not in k and len(k) < 4:
                        Utils.log(
                            '***********   ***********   ***********  *********** {name}  {k}={v}'
                            .format(name=name, k=k, v=devices_data[v]), 'info')

        if devices_param:
            # 组装设备数据结构
            data[name] = [{'ts': ts, 'devic': name, 'values': devices_param}]
            status = device_connection
        else:
            status = device_disconnection
        # 维护设备状态
        device_status(name, status)
    if data:
        # mqtt发送数据
        tb_client.SendTelemetry(json.dumps(data), False)
    Utils.log("send end")
Exemple #8
0
def send():
    Utils.log("send test start")
    ts = Utils.getTS()

    try:
        data_mod = Utils.readFile('/root/tb-gateway/config/test_data.json')

        data = master.execute(1, cst.READ_HOLDING_REGISTERS, 3, 3)
        Utils.log("data[] : {d}".format(d=data))
        Utils.log("data: w: {data}   w1:{data1}".format(data=data[0] / 10,
                                                        data1=data[1] / 10))
        data_mod = data_mod.replace("{time}", str(ts))
        data_mod = data_mod.replace("{ATC400}", str(data[0] / 10))
        if data[1] == 65036:
            data_mod = data_mod.replace("{ATE300}", str(0))
        else:
            data_mod = data_mod.replace("{ATE300}", str(data[1] / 10))

        tb_client.SendTelemetry(data_mod, False)
        Utils.log("send test end")
    except Exception:
        logging.exception(" w send exception :")
Exemple #9
0
def send_frt():
    try:

        CT_Var_AH10 = 30
        PT_Var_AH10 = 100
        CT_Var_1D5_8 = 80
        PT_Var_1D5_8 = 1
        CT_Var_5D3_2 = 30
        PT_Var_5D3_2 = 1

        time = Utils.getTS()
        #AH10
        data = master.execute(4, 3, 0, 15)
        Utils.log('data_frt_AH10 {data}'.format(data=data))
        #1D5-8
        data1 = master.execute(2, 3, 0, 15)
        Utils.log('data_frt_1D5-8 {data}'.format(data=data1))
        #HZ-GZDW
        data2 = master1.execute(1, 3, 0, 7)
        Utils.log('data_frt_HZ-GZDW {data}'.format(data=data2))
        #W1 C
        data3 = master2.execute(5, 4, 0, 6)
        Utils.log('data_frt_W1 C {data}'.format(data=data3))
        #W1 D
        data4 = master2.execute(5, 1, 0, 6)
        Utils.log('data_frt_W1 D {data}'.format(data=data4))
        # 5D3-2
        data5 = master.execute(3, 3, 0, 15)
        Utils.log('data_frt_5D3-2 {data}'.format(data=data5))

        data_mod = Utils.readFile('config/frt_data.json')
        data_mod = data_mod.replace("{AH10_time}", str(time))
        data_mod = data_mod.replace("{AH10_Ua}",   str(data[0]/10 * PT_Var_AH10))
        data_mod = data_mod.replace("{AH10_Ub}",   str(data[1]/10 * PT_Var_AH10))
        data_mod = data_mod.replace("{AH10_Uc}",   str(data[2]/10 * PT_Var_AH10))
        data_mod = data_mod.replace("{AH10_Ia}",   str( '%.3f' % (data[3]/1000 * CT_Var_AH10) ))
        data_mod = data_mod.replace("{AH10_Ib}",   str( '%.3f' % (data[4]/1000 * CT_Var_AH10) ))
        data_mod = data_mod.replace("{AH10_Ic}",   str( '%.3f' % (data[5]/1000 * CT_Var_AH10) ))
        data_mod = data_mod.replace("{AH10_P}",    str( '%.3f' % (data[6]/1000 * PT_Var_AH10 * CT_Var_AH10) ))
        data_mod = data_mod.replace("{AH10_Q}",    str( '%.3f' % (data[7]/1000 * PT_Var_AH10 * CT_Var_AH10) ))
        data_mod = data_mod.replace("{AH10_S}",    str( '%.3f' % (data[8]/1000 * PT_Var_AH10 * CT_Var_AH10) ))
        data_mod = data_mod.replace("{AH10_F}",    str(data[9]/100))
        data_mod = data_mod.replace("{AH10_Pf}",   str(data[10]/10))
        data_mod = data_mod.replace("{AH10_Wh}",   str( '%.3f' % ( wvar(data[11],data[12]) /1000 * PT_Var_AH10 * CT_Var_AH10) ))
        data_mod = data_mod.replace("{AH10_Varh}", str( '%.3f' % ( wvar(data[13],data[14]) /1000 * PT_Var_AH10 * CT_Var_AH10) ))

        data_mod = data_mod.replace("{1D5-8_time}", str(time))
        data_mod = data_mod.replace("{1D5-8_Ua}",   str(data1[0] / 10 * PT_Var_1D5_8))
        data_mod = data_mod.replace("{1D5-8_Ub}",   str(data1[1] / 10 * PT_Var_1D5_8))
        data_mod = data_mod.replace("{1D5-8_Uc}",   str(data1[2] / 10 * PT_Var_1D5_8))
        data_mod = data_mod.replace("{1D5-8_Ia}",   str( '%.3f' % (data1[3] / 1000 * CT_Var_1D5_8) ))
        data_mod = data_mod.replace("{1D5-8_Ib}",   str( '%.3f' % (data1[4] / 1000 * CT_Var_1D5_8) ))
        data_mod = data_mod.replace("{1D5-8_Ic}",   str( '%.3f' % (data1[5] / 1000 * CT_Var_1D5_8) ))
        data_mod = data_mod.replace("{1D5-8_P}",    str( '%.3f' % (data1[6] / 1000 * PT_Var_1D5_8 * CT_Var_1D5_8) ))
        data_mod = data_mod.replace("{1D5-8_Q}",    str( '%.3f' % (data1[7] / 1000 * PT_Var_1D5_8 * CT_Var_1D5_8) ))
        data_mod = data_mod.replace("{1D5-8_S}",    str( '%.3f' % (data1[8] / 1000 * PT_Var_1D5_8 * CT_Var_1D5_8) ))
        data_mod = data_mod.replace("{1D5-8_F}",    str(data1[9] / 100))
        data_mod = data_mod.replace("{1D5-8_Pf}",   str(data1[10] / 10))
        data_mod = data_mod.replace("{1D5-8_Wh}",   str( '%.3f' % ( wvar(data[11],data[12]) / 1000 * PT_Var_1D5_8 * CT_Var_1D5_8) ))
        data_mod = data_mod.replace("{1D5-8_Varh}", str( '%.3f' % ( wvar(data[13],data[14]) / 1000 * PT_Var_1D5_8 * CT_Var_1D5_8) ))

        data_mod = data_mod.replace("{HZ-GZDW_time}",   str(time))
        data_mod = data_mod.replace("{HZ-GZDW_Ua}",     str(data2[0] / 10))
        data_mod = data_mod.replace("{HZ-GZDW_Ub}",     str(data2[1] / 10))
        data_mod = data_mod.replace("{HZ-GZDW_Uc}",     str(data2[2] / 10))
        data_mod = data_mod.replace("{HZ-GZDW_Uhm}",    str(data2[3] / 10))
        data_mod = data_mod.replace("{HZ-GZDW_Ukm}",    str(data2[4] / 10))
        data_mod = data_mod.replace("{HZ-GZDW_Idc}",    str(data2[5] / 10))
        data_mod = data_mod.replace("{HZ-GZDW_Ikm}",    str(data2[6] / 10))

        data_mod = data_mod.replace("{W1_time}", str(time))
        data_mod = data_mod.replace("{1A}",      str(data3[0]))
        data_mod = data_mod.replace("{1B}",      str(data3[1]))
        data_mod = data_mod.replace("{1C}",      str(data3[2]))
        data_mod = data_mod.replace("{2A}",      str(data3[3]))
        data_mod = data_mod.replace("{2B}",      str(data3[4]))
        data_mod = data_mod.replace("{2C}",      str(data3[5]))

        data_mod = data_mod.replace("{1A_p}",    str(data4[0]))
        data_mod = data_mod.replace("{1B_p}",    str(data4[1]))
        data_mod = data_mod.replace("{1C_p}",    str(data4[2]))
        data_mod = data_mod.replace("{2A_p}",    str(data4[3]))
        data_mod = data_mod.replace("{2B_p}",    str(data4[4]))
        data_mod = data_mod.replace("{2C_p}",    str(data4[5]))

        data_mod = data_mod.replace("{5D3-2_time}", str(time))
        data_mod = data_mod.replace("{5D3-2_Ua}",   str(data5[0] / 10 * PT_Var_5D3_2))
        data_mod = data_mod.replace("{5D3-2_Ub}",   str(data5[1] / 10 * PT_Var_5D3_2))
        data_mod = data_mod.replace("{5D3-2_Uc}",   str(data5[2] / 10 * PT_Var_5D3_2))
        data_mod = data_mod.replace("{5D3-2_Ia}",   str( '%.3f' % (data5[3] / 1000 * CT_Var_5D3_2) ))
        data_mod = data_mod.replace("{5D3-2_Ib}",   str( '%.3f' % (data5[4] / 1000 * CT_Var_5D3_2) ))
        data_mod = data_mod.replace("{5D3-2_Ic}",   str( '%.3f' % (data5[5] / 1000 * CT_Var_5D3_2) ))
        data_mod = data_mod.replace("{5D3-2_P}",    str( '%.3f' % (data5[6] / 1000 * PT_Var_5D3_2 * CT_Var_5D3_2) ))
        data_mod = data_mod.replace("{5D3-2_Q}",    str( '%.3f' % (data5[7] / 1000 * PT_Var_5D3_2 * CT_Var_5D3_2) ))
        data_mod = data_mod.replace("{5D3-2_S}",    str( '%.3f' % (data5[8] / 1000 * PT_Var_5D3_2 * CT_Var_5D3_2) ))
        data_mod = data_mod.replace("{5D3-2_F}",    str(data5[9] / 100))
        data_mod = data_mod.replace("{5D3-2_Pf}",   str(data5[10] / 10))
        data_mod = data_mod.replace("{5D3-2_Wh}",   str( '%.3f' % ( wvar(data[11],data[12]) / 1000 * PT_Var_5D3_2 * CT_Var_5D3_2) ))
        data_mod = data_mod.replace("{5D3-2_Varh}", str( '%.3f' % ( wvar(data[13],data[14]) / 1000 * PT_Var_5D3_2 * CT_Var_5D3_2) ))

        Utils.log(data_mod)
        if data_mod:
            tb_client.SendTelemetry(data_mod, False)
            Utils.log('send success')
    except BaseException as e:
        Utils.log('get error','error')
        Utils.log('except:{s}'.format(s=str(e)))
        Utils.log('traceback.print_exc():{s}'.format(s=str(traceback.print_exc())))