Exemplo n.º 1
0
    def add_device(self, dev_type_node, device, gate, inputs, outputs,
                   commands):
        dev_node = self.objects.add_object(self.idx, device, dev_type_node)
        self.devices[device] = dev_node

        hs = redis_rtdb.hgetall(device)
        for input in inputs:
            input = _dict(input)
            s = hs.get(input.name + "/value")
            if s:
                val = json.loads(s)

                varid = '%d:' % self.idx + input.name
                var = dev_node.get_child(varid)
                if var:
                    datavalue = ua.DataValue(val[1])
                    datavalue.SourceTimestamp = datetime.datetime.utcfromtimestamp(
                        val[0])
                    #self.server.set_attribute_value(var.nodeid, datavalue)
                    #var.set_value(datavalue)
                    self.server.iserver.aspace._nodes[var.nodeid].attributes[
                        ua.AttributeIds.Value].value = datavalue

        handle = self.server.create_subscription(500, OutputHandler(device))
        output_nodes = []
        for output in outputs:
            output = _dict(output)
            varid = '%d:' % self.idx + output.name
            var = dev_node.get_child(varid)
            if var:
                output_nodes.append(var)
        if len(output_nodes) > 0:
            handle.subscribe_data_change(output_nodes)
            self.devices_sub_handle[device] = handle
        return
Exemplo n.º 2
0
    def run(self):
        now = int(self.time / (60 * 5)) * 60 * 5  # five minutes
        session = self.create_get_session(self.auth_code)

        r = session.get(self.api_srv + ".list_devices")
        if r.status_code != 200:
            logging.warning(r.text)
            return
        msg = _dict(r.json())
        if not msg or not msg.message.get('company_devices'):
            logging.warning('Result is not json!!')
            return

        online_count = 0
        offline_count = 0
        company_devices = msg.message.get('company_devices')
        for group in company_devices:
            group = _dict(group)
            for i in range(0, len(group.devices), 64):
                devs = group.devices[i:i + 64]
                sts = self.redis_sts.mget(devs)
                for status in sts:
                    if status == 'ONLINE':
                        online_count = online_count + 1
                    else:
                        offline_count = offline_count + 1
        self.tsdb_worker.append_statistics('device_status_statistics',
                                           self.owner, None, now, {
                                               'online': online_count,
                                               'offline': offline_count
                                           })
Exemplo n.º 3
0
 def list_devices(self):
     devices = _dict(self.user_api.list_devices())
     results = set(devices.private_devices)
     for g in devices.shared_devices:
         results = results.union(set(_dict(g).devices))
     for g in devices.company_devices:
         results = results.union(set(_dict(g).devices))
     return results
Exemplo n.º 4
0
    def device(self, device, gate, info):
        self.del_device(device, gate)
        meta = info.get('meta')
        if not meta:
            return
        meta = _dict(meta)
        dev = self.device_types.get(meta.name)
        if dev:
            inputs = info.get('inputs') or []
            outputs = info.get('outputs') or []
            commands = info.get('commands') or []
            return self.add_device(dev, device, gate, inputs, outputs,
                                   commands)

        dev = self.objects.add_object_type(self.idx, meta.name)

        outputs = info.get('outputs') or []
        output_names = []
        for output in outputs:
            output = _dict(output)
            idv = 0.0
            if output.vt == 'int':
                idv = 0
            if output.vt == 'string':
                idv = ""

            node = dev.add_variable(self.idx, output.name, None)
            node.set_modelling_rule(True)
            node.set_writable(True)
            output_names.append(output.name)

        inputs = info.get('inputs') or []
        for input in inputs:
            input = _dict(input)
            if input.name not in output_names:
                idv = 0.0
                if input.vt == 'int':
                    idv = 0
                if input.vt == 'string':
                    idv = ""

                node = dev.add_variable(self.idx, input.name, None)
                node.set_modelling_rule(True)
                node.set_writable(False)

        commands = info.get('commands') or []
        for command in commands:
            command = _dict(command)
            ctrl = dev.add_object(self.idx, command.name)
            ctrl.set_modelling_rule(True)
            #ctrl.add_property(0, "state", "Idle").set_modelling_rule(True)

        self.device_types[meta.name] = dev

        return self.add_device(dev, device, gate, inputs, outputs, commands)
Exemplo n.º 5
0
 def start(self):
     server = Server()
     server.set_endpoint("opc.tcp://0.0.0.0:4840/thingsroot/server")
     server.set_server_name("ThingsRoot Example OpcUA Server")
     self.idx = server.register_namespace("http://opcua.thingsroot.com")
     self.objects = server.get_objects_node()
     self.server = server
     self.devices = _dict({})
     self.devices_sub_handle = _dict({})
     self.device_types = _dict({})
     # self.load_redis_db()
     server.start()
Exemplo n.º 6
0
def run_statistics_tasks():
    logging.debug("run_statistics_tasks")
    cloud_statistics = []
    companines = cloud_api.list_companies()
    if not companines:
        logging.warning("Companies is None")
        return

    for comp in companines:
        comp = _dict(comp)
        if comp.enable is not None and (comp.enable is True
                                        or int(comp.enable) != 0):
            cloud_statistics.append(comp)

    for value in cloud_statistics:
        logging.debug("run_statistics_tasks for company %s", value.company)
        worker = create_statistics_worker(value.company)
        tsdb = create_tsdb_worker(value.database)
        # worker.create_dss_task(tsdb, redis_sts, api_srv, val.company, val.auth_code)
        worker.create_dts_task(redis_statistics, api_srv, value.company,
                               value.auth_code)
        worker.create_dets_task(tsdb, create_tsdb_client(value.database),
                                api_srv, value.company, value.auth_code)
        worker.create_des_task(create_tsdb_client(value.database),
                               redis_statistics, api_srv, value.company,
                               value.auth_code)
        worker.create_dscs_task(tsdb, create_tsdb_client(value.database),
                                api_srv, value.company, value.auth_code)
Exemplo n.º 7
0
def device_event():
    if not 'AuthorizationCode' in request.headers:
        logging.warning("AuthorizationCode is requied in headers")
        return

    auth_code = request.headers.get('AuthorizationCode')
    data = _dict(json.loads(request.data))
    logging.debug("Received device event %s %s", auth_code, request.data)

    return "OK!"
Exemplo n.º 8
0
 def __init__(self, config):
     threading.Thread.__init__(self)
     self.thread_stop = False
     self.config = config
     self.api_srv = config.get(
         'iot', 'url',
         fallback='http://127.0.0.1:8000') + "/api/method/iot."
     self.auth_code = config.get('iot', 'auth_code', fallback='1234567890')
     self.user_api = UserApi(self.api_srv, self.auth_code)
     self.apps = _dict({})
Exemplo n.º 9
0
    def run(self):
        now = int(self.time / (60 * 5)) * 60 * 5  # five minutes
        start_time = datetime.datetime.fromtimestamp(now - (5 * 60)).strftime(
            DATETIME_FORMAT)
        end_time = datetime.datetime.fromtimestamp(now).strftime(
            DATETIME_FORMAT)
        session = self.create_get_session(self.auth_code)

        r = session.get(self.api_srv + ".list_devices")
        if r.status_code != 200:
            logging.warning(r.text)
            return
        msg = _dict(r.json())
        if not msg or not msg.message.get('company_devices'):
            logging.warning('Result is not json!!')
            return

        company_devices = msg.message.get('company_devices')
        logging.debug('Event Type Count Start {0}'.format(self.owner))
        total = {}
        for group in company_devices:
            group = _dict(group)
            for dev in group.devices:
                try:
                    val = self.tsdb_client.query_event_type_count(
                        dev, start_time, end_time)
                    logging.debug('Event Type Count {0}-{1} [{2}]: {3}'.format(
                        start_time, end_time, dev, json.dumps(val)))
                    if val:
                        self.tsdb_worker.append_statistics(
                            'single_device_event_type_statistics', self.owner,
                            dev, now, val)
                        for k in val:
                            total[k] = (total.get(k) or 0) + val[k]
                except Exception as ex:
                    logging.exception(ex)
        logging.debug('Event Type Count End {0}: {1}'.format(
            self.owner, json.dumps(total)))
        if total:
            self.tsdb_worker.append_statistics('device_event_type_statistics',
                                               self.owner, None, now, total)
Exemplo n.º 10
0
 def list_mqtt_apps(self):
     applist = self.user_api.list_user_apps() or {}
     apps = _dict({})
     for app in applist:
         app = _dict(app)
         if app.device_data == 1 and app.device_data_mqtt_host:
             user = app.device_data_mqtt_user
             if user and len(user) == 0:
                 user = None
             password = app.device_data_mqtt_passwd
             if password and len(password) == 0:
                 password = None
             apps[app.name] = _dict({
                 'name': app.name,
                 'host': app.device_data_mqtt_host,
                 'user': user,
                 'password': password,
                 'modified': app.modified,
                 'auth_code': app.auth_code
             })
     return apps
Exemplo n.º 11
0
    def run(self):
        start_time = (int(self.time /
                          (60 * 60 * 24)) * 60 * 60 * 24) - (7 * 24 * 60 * 60)
        start_time = datetime.date.fromtimestamp(start_time).strftime(
            DATETIME_FORMAT)
        # end_time = datetime.datetime.fromtimestamp(int(self.time / (60 * 5)) * 60 * 5).strftime(DATETIME_FORMAT)
        end_time = datetime.datetime.fromtimestamp(
            self.time).strftime(DATETIME_FORMAT)
        session = self.create_get_session(self.auth_code)

        r = session.get(self.api_srv + ".list_devices")
        if r.status_code != 200:
            logging.warning(r.text)
            return
        msg = _dict(r.json())
        if not msg or not msg.message.get('company_devices'):
            logging.warning('Result is not json!!')
            return

        company_devices = msg.message.get('company_devices')
        for group in company_devices:
            group = _dict(group)
            for dev in group.devices:
                '''
				query one week event counts, and then got the total count and today's
				'''
                val = self.tsdb_client.query_event_count(
                    dev, start_time, end_time, '1d')
                data = {
                    'points': val,
                    'today': 0,
                    'total': 0,
                }
                if len(val) > 0:
                    data['today'] = val[len(val) - 1].get('count') or 0
                    total = 0
                    for v in val:
                        total = total + (v.get('count') or 0)
                    data['total'] = total
                self.redis_statistics.hmset('event_count.' + dev, data)
Exemplo n.º 12
0
    def run(self):
        now = int(self.time / (60 * 5)) * 60 * 5  # five minutes
        session = self.create_get_session(self.auth_code)

        r = session.get(self.api_srv + ".list_devices")
        if r.status_code != 200:
            logging.warning(r.text)
            return
        msg = _dict(r.json())
        if not msg or not msg.message.get('company_devices'):
            logging.warning('Result is not json!!')
            return

        company_devices = msg.message.get('company_devices')

        q102_count = 0
        q204_count = 0
        t1_3000_count = 0
        vm_count = 0

        for group in company_devices:
            group = _dict(group)
            for dev in group.devices:
                if dev[0:8] == '2-30002-':
                    q102_count = q102_count + 1
                elif dev[0:8] == '2-30102-':
                    q204_count = q204_count + 1
                elif dev[0:6] == 'TRTX01':
                    t1_3000_count = t1_3000_count + 1
                else:
                    vm_count = vm_count + 1

        self.redis_statistics.hmset(
            'device_type.' + self.owner, {
                "Q102": q102_count,
                "Q204": q204_count,
                "T1-3000": t1_3000_count,
                "VBOX": vm_count
            })
Exemplo n.º 13
0
    def run(self):
        now = int(self.time / (60 * 5)) * 60 * 5  # five minutes
        start_time = datetime.datetime.fromtimestamp(now - (5 * 60)).strftime(
            DATETIME_FORMAT)
        end_time = datetime.datetime.fromtimestamp(now).strftime(
            DATETIME_FORMAT)
        session = self.create_get_session(self.auth_code)

        r = session.get(self.api_srv + ".list_devices")
        if r.status_code != 200:
            logging.warning(r.text)
            return
        msg = _dict(r.json())
        if not msg or not msg.message.get('company_devices'):
            logging.warning('Result is not json!!')
            return

        company_devices = msg.message.get('company_devices')

        online_count = 0
        offline_count = 0

        for group in company_devices:
            group = _dict(group)
            for dev in group.devices:
                val = self.tsdb_client.query_device_status(
                    dev, start_time, end_time)
                for v in val:
                    if v.get('online') is True:
                        online_count = online_count + 1
                    else:
                        offline_count = offline_count + 1
        # TODO: using end time or start time
        self.tsdb_worker.append_statistics('device_status_statistics',
                                           self.owner, None, now, {
                                               'online': online_count,
                                               'offline': offline_count
                                           })
Exemplo n.º 14
0
	def list_companies(self):
		session = self.create_get_session()

		try:
			r = session.get(self.api_srv + ".list_statistics_companies")
			if r.status_code != 200:
				logging.warning(r.text)
				return

			msg = _dict(r.json())
			if not msg or not msg.message:
				logging.warning('Result is not json!!')
				return

			return msg.message
		except Exception as ex:
			logging.exception(ex)
			return
Exemplo n.º 15
0
    def start(self):
        host = self.config.get('mqtt', 'host', fallback='127.0.0.1')
        port = self.config.getint('mqtt', 'port', fallback=1883)
        keepalive = self.config.getint('mqtt', 'keepalive', fallback=60)
        clientid = self.config.get(
            'mqtt', 'clientid_base',
            fallback="IOT_CLOUD_APPS") + ".BRIDGE." + self.remote.name
        user = self.config.get('mqtt', 'user', fallback="root")
        password = self.config.get('mqtt',
                                   'password',
                                   fallback="bXF0dF9pb3RfYWRtaW4K")
        mqttc = IOTMQTTClient(subclient=self,
                              host=host,
                              port=port,
                              keepalive=keepalive,
                              clientid=clientid,
                              user=user,
                              password=password)
        mqttc.start()
        self.mqttc = mqttc

        remote = _dict(self.remote)
        bridge_host = remote.host
        bridge_port = 1883
        bridge_clientid = 'THINGSROOT_MQTT_BRIDGE'
        uri = urlparse(remote.host)
        if uri:
            bridge_host = uri.hostname or remote.host
            bridge_port = uri.port or 1883
            bridge_clientid = uri.username or 'THINGSROOT_MQTT_BRIDGE'
        mqtt_bridge = MQTTClient(host=bridge_host,
                                 port=bridge_port,
                                 clientid=bridge_clientid,
                                 user=remote.user,
                                 password=remote.password)
        mqtt_bridge.start()
        self.mqtt_bridge = mqtt_bridge

        threading.Thread.start(self)
Exemplo n.º 16
0
	def on_message(self, client, msg):
		topic_g = match_topic.match(msg.topic)
		if not topic_g:
			return
		topic_g = topic_g.groups()
		if len(topic_g) < 2:
			return

		devid = topic_g[0]
		topic = topic_g[1]

		if topic == 'data':
			data = json.loads(msg.payload.decode('utf-8', 'surrogatepass'))
			if not data:
				logging.warning('Decode DATA JSON Failure: %s/%s\t%s', devid, topic, msg.payload.decode('utf-8', 'surrogatepass'))
				return
			input_match = match_data_path.match(data['input'])
			if not input_match:
				return
			input_match = input_match.groups()
			if input_match and msg.retain == 0:
				input = input_match[0]
				prop = input_match[1]
				dv = data['data']
				value = dv[1]
				if prop == "value":
					t, val = self.get_input_vt(devid, input, value)
					if t:
						prop = t + "_" + prop
					value = val
				else:
					value = str(value)
				try:
					# logging.debug('[GZ]device: %s\tInput: %s\t Value: %s', g[0], g[1], value)
					self.handler.data(device=devid, input=input, property=prop, timestamp=dv[0], value=value, quality=dv[2])
				except Exception as ex:
					logging.exception(ex)
			return

		if topic == 'device':
			data = json.loads(msg.payload.decode('utf-8', 'surrogatepass'))
			if not data:
				logging.warning('Decode Device JSON Failure: %s/%s\t%s', devid, topic, msg.payload.decode('utf-8', 'surrogatepass'))
				return

			logging.debug('%s/%s\t%s', devid, topic, data)
			info = data['info']
			try:
				self.handler.device(device=devid, gate=data['gate'], info=_dict(info))
			except Exception as ex:
				logging.exception(ex)
			self.make_input_map(info)
			return

		if topic == 'status':
			data = json.loads(msg.payload.decode('utf-8', 'surrogatepass'))
			if not data:
				logging.warning('Decode Status JSON Failure: %s/%s\t%s', devid, topic, msg.payload.decode('utf-8', 'surrogatepass'))
				return

			status = data['status']
			if status == "ONLINE" or status == "OFFLINE":
				val = status == "ONLINE"
				try:
					self.handler.status(device=devid, gate=data['gate'], online=val)
				except Exception as ex:
					logging.exception(ex)
			return

		if topic == 'event':
			data = json.loads(msg.payload.decode('utf-8', 'surrogatepass'))
			if not data:
				logging.warning('Decode EVENT JSON Failure: %s/%s\t%s', devid, topic, msg.payload.decode('utf-8', 'surrogatepass'))
				return
			if msg.retain == 0:
				gate = data['gate']
				event = json.loads(data['event'])
				timestamp = event[2] or time.time()
				try:
					self.handler.event(device=devid, gate=gate, timestamp=timestamp, event=_dict(event[1]))
				except Exception as ex:
					logging.exception(ex)
			return
Exemplo n.º 17
0
 def __init__(self):
     self.devices = _dict({})
     self.devices_sub_handle = _dict({})
     self.device_types = _dict({})