def __init__(self,
              method="post",
              url="/edu-portal-server/api/portal/thing/thing-model/add"):
     super().__init__()
     self.logger = Logger(logging.INFO, logging.DEBUG)
     self.method = method
     self.url = BASE_URL + url
class AddWgModel(Base):
    def __init__(self,
                 method="post",
                 url="/edu-portal-server/api/portal/thing/thing-model/add"):
        self.logger = Logger(logging.INFO, logging.DEBUG)
        super().__init__()
        self.method = method
        self.url = BASE_URL + url

    def add_model_param(self, type="GATEWAY"):
        random_str = str(random.randrange(1, 9999999999, 10))
        add_model_wg = {
            "isUseModel": False,
            "modelType": type,
            "name": "网关" + random_str
        }
        self.logger.info("新建网关参数:{}".format(add_model_wg))
        return add_model_wg

    def post_add_model(self):
        response = Method.send_method(self.method, self.url, self.head,
                                      self.add_model_param())
        self.logger.info("添加网关模型成功,网关模型id为:{}".format(response.json()["data"]))
        if response.status_code == 200:
            dict.append(
                "> 新建网关模型接口:<font color=\"info\">通过</font> {}\n".format(
                    response.json()))
        else:
            dict.append("> 新建网关模型接口:<font color=\"comment\">失败</font> \n")
        return response.json()["data"]
class AddWuModel(Base):
    def __init__(self,
                 method="post",
                 url="/edu-portal-server/api/portal/thing/thing-model/add"):
        super().__init__()
        self.logger = Logger(logging.INFO, logging.DEBUG)
        self.method = method
        self.url = BASE_URL + url

    def add_modelwu_param(self, type="DEVICE"):
        random_str = str(random.randrange(1, 9999999999, 10))
        add_model_wg = {
            "isUseModel": True,
            "modelType": type,
            "name": "物模型" + random_str,
            "templateId": "4",
            "category": "机床,镗床",
            "connectType": "UNCONNECTED"
        }
        self.logger.info("新建物模型参数:{}".format(add_model_wg))
        return add_model_wg

    def post_add_modelwu(self):
        response = Method.send_method(self.method, self.url, self.head,
                                      self.add_modelwu_param())
        self.logger.info("新建物模型成功,物模型id为:{}".format(response.json()["data"]))
        if response.status_code == 200:
            dict.append("> 新建物模型接口:<font color=\"info\">通过</font> {}\n".format(
                response.json()))
        else:
            dict.append("> 新建物模型接口:<font color=\"comment\">失败</font> \n")
        return response.json()["data"]
 def __init__(self, method="post", url="/edu-portal-server/api/portal/thing/thing-instance/add"):
     super().__init__()
     self.logger = Logger(logging.INFO, logging.DEBUG)
     self.method = method
     self.url = BASE_URL + url
     self.modle = AddWuModel()
     self.publish = Publish()
class AddModleInstance(Base):


    def __init__(self, method="post", url="/edu-portal-server/api/portal/thing/thing-instance/add"):
        super().__init__()
        self.logger = Logger(logging.INFO, logging.DEBUG)
        self.method = method
        self.url = BASE_URL + url
        self.modle = AddWuModel()
        self.publish = Publish()


    def data(self, thingModleId, gateWayThingId, type="DEVICE"):
        data = {
            "modelType": type,
            "name": "物模型实例" + self.rand(),
            "modelId": thingModleId,
            "assetId": self.rand(),
            "connectId": self.rand(),
            "connectType": "UNCONNECTED",
            "gatewayId": gateWayThingId
        }
        self.logger.info("新建物实例参数:{}".format(data))
        return data

    def post_add_mxInstance(self, thingModelId, gateWayThingId):
        self.publish.post_publish(thingModelId)
        response = Method.send_method(self.method, self.url, self.head, self.data(thingModelId, gateWayThingId))
        self.logger.info("注册物模型实例成功,物模型实例id为:{}".format(response.json()["data"]))
        if response.status_code == 200:
            dict.append("> 新建物实例接口:<font color=\"info\">通过</font> {}\n".format(response.json()))
        else:
            dict.append("> 新建物实例接口:<font color=\"comment\">失败</font> \n")
        return response.json()["data"]
 def __init__(self,
              method="post",
              url="/device-server/api/device/device/device-info/add"):
     super().__init__()
     self.logger = Logger(logging.INFO, logging.DEBUG)
     self.method = method
     self.url = BASE_URL + url
     self.template = Template()
Exemple #7
0
 def __init__(
         self,
         method="get",
         url="/device-server/api/device/device/device-info/startDevice"):
     super().__init__()
     self.logger = Logger(logging.INFO, logging.DEBUG)
     self.method = method
     self.url = BASE_URL + url
     self.devicesObj = Devices()
Exemple #8
0
 def __init__(
         self,
         method="get",
         url="/device-server/api/device/device/device-info/startDevice"):
     super().__init__()
     self.logger = Logger(logging.INFO, logging.DEBUG)
     self.method = method
     self.url = BASE_URL + url
     self.devices = Devices()
     self.portal = Portal()
     self.gateWayId = AddWgModel().post_add_model()
     self.gateWayInstanceId = AddWgInstance().post_add_wg(self.gateWayId)
     self.modleId = AddWuModel().post_add_modelwu()
     self.modleInstanceId = AddModleInstance().post_add_mxInstance(
         self.modleId, self.gateWayInstanceId)
     self.devicesId = AddDevices().post_add_devices()
Exemple #9
0
 def __init__(self,
              method="post",
              url="/edu-portal-server/api/portal/base/user/login"):
     self.logger = Logger(logging.INFO, logging.DEBUG)
     self.url = BASE_URL + url
     self.head = getHeader(0, 2)
     self.method = method
class Publish(Base):
    def __init__(
            self,
            method="post",
            url="/edu-portal-server/api/portal/thing/thing-model/publishById"):
        super().__init__()
        self.logger = Logger(logging.INFO, logging.DEBUG)
        self.method = method
        self.url = BASE_URL + url

    def data(self, id):
        data = {"id": id}
        self.logger.info("发布模型参数:{}".format(data))
        return data

    def post_publish(self, id):
        response = Method.send_method(self.method, self.url, self.head,
                                      self.data(id))
        self.logger.info("发布模型成功:{}".format(response.status_code))
        return response.status_code
Exemple #11
0
class PublishDevices(Base):
    def __init__(
            self,
            method="get",
            url="/device-server/api/device/device/device-info/startDevice"):
        super().__init__()
        self.logger = Logger(logging.INFO, logging.DEBUG)
        self.method = method
        self.url = BASE_URL + url
        self.devicesObj = Devices()

    def param(self, devicesid):
        data = {"deviceId": devicesid}
        self.logger.info("虚拟设备启动参数:{}".format(data))
        return data

    '''
    启动设备
    '''

    def startDevices(self):
        devices = self.devicesObj.query(BDeviceInfo)
        lists = devices.filter_by(creator_true_name="定时发送",
                                  delete_flag=0,
                                  run_status=0).all()
        array = []
        for i in lists:
            array.append(i.id)
        for j in array:
            self.logger.info("获取虚拟设备id:{}".format(j))
            response = Method.send_method(self.method, self.url, self.head,
                                          self.param(j))
            time.sleep(1)
            self.logger.info("请求url:{}".format(response.url))
            self.logger.info("请求响应结果:{}".format(response.json()))
            if response.json()["code"] == 2000 and response.json(
            )["success"] == True:
                self.logger.info("虚拟设备启动成功")
            else:
                continue
Exemple #12
0
class AddDevices(Base):
    def __init__(self,
                 method="post",
                 url="/device-server/api/device/device/device-info/add"):
        super().__init__()
        self.logger = Logger(logging.INFO, logging.DEBUG)
        self.method = method
        self.url = BASE_URL + url
        self.template = Template()

    def add_devices_param(self):
        param = {
            "name": self.template.getdata()[0],
            "brand": "教学品牌",
            "modelNumber": "Rootcloud-Electric-meter01",
            "deviceSerialNum": self.template.getdata()[1],
            "power": "1.5W",
            "netModelNumber": "Rootcloud-gw01",
            "netBrand": "根云网关",
            "netSerialNum": self.template.getdata()[2],
            "protocolName": "ModbusTCP",
            "imageUrl": "dianbiao.png",
            "templateId": self.template.getdata()[3],
            "protocolId": 1
        }
        self.logger.info("新建虚拟设备参数:{}".format(param))
        return param

    def post_add_devices(self):
        response = Method.send_method(self.method, self.url, self.head,
                                      self.add_devices_param())
        self.logger.info("新建虚拟设备成功,设备id为:{}".format(response.json()["data"]))
        if response.status_code == 200:
            dict.append(
                "> 新建虚拟设备接口:<font color=\"info\">通过</font> {}\n".format(
                    response.json()))
        else:
            dict.append("> 新建虚拟设备接口:<font color=\"comment\">失败</font> \n")
        return response.json()["data"]
class AddWgInstance(Base):
    def __init__(self,
                 method="post",
                 url="/edu-portal-server/api/portal/thing/thing-instance/add"):
        super().__init__()
        self.logger = Logger(logging.INFO, logging.DEBUG)
        self.method = method
        self.url = BASE_URL + url
        self.modle = AddWgModel()
        self.publish = Publish()

    def data(self, gateWayId, type="GATEWAY"):
        data = {
            "modelType": type,
            "name": "网关实例" + self.rand(),
            "modelId": gateWayId,
            "authId": self.rand(),
            "authToken": self.rand(),
            "simNumber": "",
            "assetId": self.rand()
        }
        self.logger.info("新建网关实例参数:{}".format(data))
        return data

    def post_add_wg(self, gateWayModleId):
        self.publish.post_publish(gateWayModleId)
        response = Method.send_method(self.method, self.url, self.head,
                                      self.data(gateWayModleId))
        self.logger.info("注册网关实例成功,网关实例id为:{}".format(response.json()["data"]))
        if response.status_code == 200:
            dict.append(
                "> 新建网关实例接口:<font color=\"info\">通过</font>{} \n".format(
                    response.json()))
        else:
            dict.append("> 新建网关实例接口:<font color=\"comment\">失败</font> \n")
        return response.json()["data"]
Exemple #14
0
class UpdateWg(Base):
    def __init__(
            self,
            method="get",
            url="/device-server/api/device/device/device-info/startDevice"):
        super().__init__()
        self.logger = Logger(logging.INFO, logging.DEBUG)
        self.method = method
        self.url = BASE_URL + url
        self.devices = Devices()
        self.portal = Portal()
        self.gateWayId = AddWgModel().post_add_model()
        self.gateWayInstanceId = AddWgInstance().post_add_wg(self.gateWayId)
        self.modleId = AddWuModel().post_add_modelwu()
        self.modleInstanceId = AddModleInstance().post_add_mxInstance(
            self.modleId, self.gateWayInstanceId)
        self.devicesId = AddDevices().post_add_devices()

    '''
    查询虚拟设备网关与设备序列号
    '''

    def selectId(self):
        devicesInfo = self.devices.query(BDeviceInfo)
        use = devicesInfo.filter_by(id=self.devicesId).first()
        self.logger.info("查询虚拟设备参数,网关序列号:{};设备序列号:{}".format(
            str(use.net_serial_num), str(use.device_serial_num)))
        return use.net_serial_num, use.device_serial_num

    '''
    查询设备实例并更新字段值
    '''

    def selectInstance(self):
        #查询网关实例并更新物标识字段值
        instanceWgObj = self.portal.query(BThingInstance)
        wgObj = instanceWgObj.filter_by(id=self.gateWayInstanceId).first()
        self.logger.info("更新前网关序列号:{}".format(str(wgObj.asset_id)))
        wgObj.asset_id = self.selectId()[0]
        self.portal.commit()
        dict.append(
            "> 更新网关实例序列号:<font color=\"info\">通过</font> 网关序列号:{}\n".format(
                str(wgObj.asset_id)))
        self.logger.info("更新后网关序列号:{}".format(str(wgObj.asset_id)))
        #查询物模型实例更新物标识字段值
        instanceMxObj = self.portal.query(BThingInstance)
        mxObj = instanceMxObj.filter_by(id=self.modleInstanceId).first()
        self.logger.info("更新前设备序列号:{}".format(str(mxObj.asset_id)))
        mxObj.asset_id = self.selectId()[1]
        self.portal.commit()
        dict.append(
            "> 更新物实例设备序列号:<font color=\"info\">通过</font> 设备序列号:{}\n".format(
                str(mxObj.asset_id)))
        self.logger.info("更新后的设备序列号:{}".format(str(mxObj.asset_id)))

    '''查询实例的用户名和密码,更新到虚拟设备'''

    def updateDevices(self):
        '''
        查询到网关实例的用户名密码
        '''
        wggObjQuery = self.portal.query(BThingInstance)
        wgObj = wggObjQuery.filter_by(id=self.gateWayInstanceId).first()
        id = wgObj.auth_id
        pwd = wgObj.auth_token
        self.logger.info("查询网关实例的用户名:{};密码;{}".format(id, pwd))
        '''
        查询到虚拟设备的信息
        '''
        sbObjQuery = self.devices.query(BDeviceNConfig)
        sbObj = sbObjQuery.filter_by(device_id=self.devicesId).first()
        sbObj.client_id = id
        sbObj.user_name = id
        sbObj.password = pwd
        self.devices.commit()
        dict.append(
            "> 更新虚拟设备参数:<font color=\"info\">通过</font> client_id{};用户名{};密码;{}\n"
            .format(str(sbObj.client_id), str(sbObj.user_name),
                    str(sbObj.password)))
        self.logger.info("虚拟设备对象赋值成功:client_id{};用户名{};密码;{}".format(
            str(sbObj.client_id), str(sbObj.user_name), str(sbObj.password)))

    '''
    启动设备接口参数化
    '''

    def param(self, devicesid):
        data = {"deviceId": devicesid}
        self.logger.info("虚拟设备启动参数:{}".format(data))
        return data

    '''
    启动设备
    '''

    def startDevices(self):
        response = Method.send_method(self.method, self.url, self.head,
                                      self.param(self.devicesId))
        self.logger.info("虚拟设备启动成功:{}".format(response.status_code))
        if response.status_code == 200:
            dict.append(
                "> 启动虚拟设备接口:<font color=\"info\">通过</font> {}\n".format(
                    response.json()))
        else:
            dict.append("> 启动虚拟设备接口:<font color=\"comment\">失败</font> \n")
        return response.status_code