10、监测数据采集物联网应用开发步骤(8.1)

  1. 监测数据采集物联网应用开发步骤(7)

TCP/IP Client开发

com.zxy.common.Com_Para.py中添加如下内容

#socket链接的自动链接时间,定时清除无效tcp连接
dSockList = {}
#本机作为客户端连接socket list
dClientThreadList = {}
#作为客户端接收数据拦截器
ClientREFLECT_IN_CLASS = "com.plugins.usereflect.testClientReflectClass1"

新建接收数据解析类com.zxy.business.Analyse_Data.py

#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''

import importlib
from com.zxy.common import Com_Para
from com.zxy.common.Com_Fun import Com_Fun
from com.zxy.z_debug import z_debug

#监测数据采集物联网应用--接收数据解析
class Analyse_Data(z_debug):
    
    attTemChildNode = ""

    def __init__(self):
        pass
        
    #接收到数据包iServ是否作为服务端接收到数据
    def SubAnalyseRecBytes(self,inputStrResult, inputMServSocket, inputIServ, inputStrIP,inputStrPort):
        #作为客户端接收数据拦截器
        if not inputIServ and Com_Para.ClientREFLECT_IN_CLASS != "":
            try:
                objC = importlib.import_module(Com_Para.ClientREFLECT_IN_CLASS)  #对模块进行导入                
                objName = Com_Para.ClientREFLECT_IN_CLASS.split(".")
                objN = getattr(objC,objName[len(objName) - 1])
                if hasattr(objN,"strResult"):
                    setattr(objN,"strResult",inputStrResult)
                    setattr(objN,"strIP",inputStrIP)
                    setattr(objN,"strPort",inputStrPort)
                    fun_us = getattr(objN,"init_start")
                    fun_us(objN)
                    temResult = getattr(objN,"strResult")
                    temSend = getattr(objN,"strSend")
                    temContinue = getattr(objN,"strContinue")
                    #发送到服务端数据
                    if temSend != "":
                        Com_Fun.SendSocket(temSend,inputMServSocket)
                    #不继续执行操作
                    if temContinue == "0":
                        return ""
            except Exception as e:
                if str(type(self)) == "<class 'type'>":
                    self.debug_in(self,Com_Para.ClientREFLECT_IN_CLASS+"=>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
                else:
                    self.debug_in(Com_Para.ClientREFLECT_IN_CLASS+"=>"+repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
            finally:
                Pass

com.zxy.common.Com_Fun.py中添加代码

    @staticmethod
    def SendSocket(inputValue, inputSc):
        return inputSc.send(inputValue.encode(Com_Para.U_CODE))

新建TCP/IP Client类com.zxy.tcp.TcpClient.py

#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''

import time
from com.zxy.z_debug import z_debug
from com.zxy.adminlog.UsAdmin_Log import UsAdmin_Log
from com.zxy.common import Com_Para
from com.zxy.common.Com_Fun import Com_Fun
from com.zxy.business.Analyse_Data import Analyse_Data

#监测数据采集物联网应用--TCP/IP Client
class TcpClient(z_debug):

    def __init__(self, skey):
        self.skey = skey
        pass
    
    def client_link(self):
        sock = Com_Fun.GetHashTableNone(Com_Para.dClientThreadList,self.skey)
        while True:
            temInit_msg = b''#初始化流
            try:
                temInit_msg = sock.recv(1024*50)
                temValue = temInit_msg.decode(Com_Para.U_CODE)
                if temValue.replace("\r","").replace("\n","") == "Exit":
                    Com_Fun.RemoveHashTable(Com_Para.dClientThreadList,self.skey)
                    if sock is not None:
                        sock.close()
                    uL = UsAdmin_Log(Com_Para.ApplicationPath, Com_Fun.GetTime("%Y-%m-%d %H:%M:%S") + "1 close server","NetWork")
                    uL.WriteLog()
                    break
                elif len(temValue) > 0:
                    ad = Analyse_Data()
                    #解析作为客户端收到数据
                    ad.SubAnalyseRecBytes(temValue,sock,False, sock.getpeername()[0],str(sock.getpeername()[1]))
                elif len(temInit_msg) == 0:
                    Com_Fun.RemoveHashTable(Com_Para.dClientThreadList,self.skey)
                    if sock is not None:
                        sock.close()
                    uL = UsAdmin_Log(Com_Para.ApplicationPath, Com_Fun.GetTime("%Y-%m-%d %H:%M:%S") + "2 close server","NetWork")
                    uL.WriteLog()
                    break
                time.sleep(0.1)
            except Exception as e:
                Com_Fun.RemoveHashTable(Com_Para.dClientThreadList,self.skey)
                if sock is not None:
                    sock.close()
                temLog = ""
                if str(type(self)) == "<class 'type'>":
                    temLog = self.debug_info(self)+repr(e)
                else:
                    temLog = self.debug_info()+repr(e)
                uL = UsAdmin_Log(Com_Para.ApplicationPath, temLog+"=>"+str(e.__traceback__.tb_lineno))
                uL.WriteLog()
                break
  1. 监测数据采集物联网应用开发步骤(8.2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值