def iot_device_his_data(key, sn, vsn=None, fields="*", condition=None): vsn = vsn or sn doc = frappe.get_doc('IOT Device', sn) if not doc.has_permission("read"): raise frappe.PermissionError if vsn != sn: if vsn not in iot_device_tree(sn): return 401 inf_server = IOTHDBSettings.get_influxdb_server() if not inf_server: frappe.logger(__name__).error( "InfluxDB Configuration missing in IOTHDBSettings") return 500 query = 'SELECT ' + fields + ' FROM "' + key + '" WHERE device=\'' + vsn + '\'' if condition: query = query + condition else: query = query + " ORDER BY time DESC LIMIT 1000" domain = frappe.get_value("Cloud Company", doc.company, "domain") r = requests.session().get(inf_server + "/query", params={ "q": query, "db": domain }, timeout=10) if r.status_code == 200: return r.json()["results"] or r.json() return r.text
def single_device_event_type_statistics(device): valid_auth_code() companies = list_user_companies(frappe.session.user) if len(companies) == 0: return company = companies[0] inf_server = IOTHDBSettings.get_influxdb_server() if not inf_server: frappe.logger(__name__).error("InfluxDB Configuration missing in IOTHDBSettings") return query = 'SELECT sum("SYS"), sum("DEV"), sum("COMM"), sum("DATA"), sum("APP")' query = query + ' FROM "single_device_event_type_statistics" WHERE time > now() - 7d' query = query + ' AND "owner"=\'' + company + '\'' + ' AND "iot"=\'' + device + '\' GROUP BY time(1d) FILL(0)' domain = frappe.get_value("Cloud Company", company, "domain") r = requests.session().get(inf_server + "/query", params={"q": query, "db": domain + '.statistics'}, timeout=10) if r.status_code == 200: ret = r.json() if not ret: return frappe.logger(__name__).error("Got Single Device Event Type Count {0}".format(json.dumps(ret))) results = ret['results'] if not results or len(results) < 1: return series = results[0].get('series') if not series or len(series) < 1: return res = series[0].get('values') if not res: return taghis = [] for i in range(0, len(res)): hisvalue = {} # print('*********', res[i][0]) try: utc_time = datetime.datetime.strptime(res[i][0], UTC_FORMAT1) except Exception as err: pass try: utc_time = datetime.datetime.strptime(res[i][0], UTC_FORMAT2) except Exception as err: pass local_time = str(convert_utc_to_user_timezone(utc_time).replace(tzinfo=None)) hisvalue = {'name': 'single_device_event_type_statistics', 'time': local_time, 'owner': company, 'device': device} hisvalue['系统'] = res[i][1] or 0 hisvalue['设备'] = res[i][2] or 0 hisvalue['通讯'] = res[i][3] or 0 hisvalue['数据'] = res[i][4] or 0 hisvalue['应用'] = res[i][5] or 0 taghis.append(hisvalue) return taghis else: return r.txt
def device_status_statistics(): valid_auth_code() companies = list_user_companies(frappe.session.user) if len(companies) == 0: return company = companies[0] inf_server = IOTHDBSettings.get_influxdb_server() if not inf_server: frappe.logger(__name__).error("InfluxDB Configuration missing in IOTHDBSettings") return query = 'SELECT "online", "offline" FROM "device_status_statistics" WHERE time > now() - 12h AND "owner"=\'' + company + '\'' domain = frappe.get_value("Cloud Company", company, "domain") r = requests.session().get(inf_server + "/query", params={"q": query, "db": domain + '.statistics'}, timeout=10) if r.status_code == 200: ret = r.json() if not ret: return results = ret['results'] if not results or len(results) < 1: return series = results[0].get('series') if not series or len(series) < 1: return res = series[0].get('values') if not res: return taghis = [] for i in range(0, len(res)): hisvalue = {} # print('*********', res[i][0]) try: utc_time = datetime.datetime.strptime(res[i][0], UTC_FORMAT1) except Exception as err: pass try: utc_time = datetime.datetime.strptime(res[i][0], UTC_FORMAT2) except Exception as err: pass local_time = str(convert_utc_to_user_timezone(utc_time).replace(tzinfo=None)) hisvalue = {'name': 'device_status_statistics', 'online': res[i][1], 'time': local_time, 'offline': res[i][2], 'owner': company} taghis.append(hisvalue) return taghis else: return r.txt
def taghisdata(sn=None, vsn=None, fields=None, condition=None): vsn = vsn or sn fields = fields or "*" doc = frappe.get_doc('IOT Device', sn) doc.has_permission("read") inf_server = IOTHDBSettings.get_influxdb_server() if not inf_server: frappe.logger(__name__).error( "InfluxDB Configuration missing in IOTHDBSettings") return 500 query = 'SELECT ' + fields + ' FROM "' + vsn + '"' if condition: query = query + " WHERE " + condition else: query = query + " LIMIT 1000" domain = frappe.get_value("Cloud Company", doc.company, "domain") r = requests.session().get(inf_server + "/query", params={ "q": query, "db": domain }, timeout=10) if r.status_code == 200: res = r.json()["results"][0]['series'][0]['values'] taghis = [] for i in range(0, len(res)): hisvalue = {} if len(res[i]) == 5: hisvalue = { 'name': res[i][1], 'value': res[i][3], 'time': res[i][0], 'quality': 0 } taghis.append(hisvalue) elif len(res[i]) == 6: hisvalue = { 'name': res[i][1], 'value': res[i][4], 'time': res[i][0], 'quality': 0 } taghis.append(hisvalue) #print(taghis) return taghis or r.json()
def create_influxdb(db_name, max_retry=10, sleep=None): if sleep: time.sleep(sleep) max_retry = max_retry - 1 inf_server = IOTHDBSettings.get_influxdb_server() if not inf_server: frappe.logger(__name__).error( "InfluxDB Configuration missing in IOTHDBSettings") return try: r = requests.session().get( inf_server + "/query", params={"q": ('''CREATE DATABASE "{0}"''').format(db_name)}, timeout=1) if r.status_code != 200: frappe.logger(__name__).error(r.text) if max_retry > 0: frappe.enqueue( 'iot.controllers.cloud_company_hooks.create_influxdb', db_name=db_name, max_retry=max_retry, sleep=60) throw(r.text) else: frappe.logger(__name__).debug(r.text) except Exception as ex: frappe.logger(__name__).error(ex.message) if max_retry > 0: frappe.enqueue( 'iot.controllers.cloud_company_hooks.create_influxdb', db_name=db_name, max_retry=max_retry, sleep=60) throw(ex.message)
def history_data(gateway, name, input, vt='float', time_condition='time > now() - 10m', value_method='raw', group_time_span='1m', fill_method="none", count_limit=1000, time_zone='Asia/Shanghai'): try: valid_auth_code() doc = frappe.get_doc('IOT Device', gateway) if not doc.has_permission("read"): raise frappe.PermissionError inf_server = IOTHDBSettings.get_influxdb_server() if not inf_server: frappe.logger(__name__).error( "InfluxDB Configuration missing in IOTHDBSettings") return # ------------------------------------------------------------------------------------------------------------- vtdict = { "float": "value", "int": "int_value", "string": "string_value" } field = '"' + vtdict.get(vt) + '"' fields = '"' + vtdict.get(vt) + '"' + ' , "quality"' method = dict(raw=fields, mean='mean(' + field + ')', max='max(' + field + ')', min='min(' + field + ')', first='first(' + field + ')', last='last(' + field + ')', sum='sum(' + field + ')', count='count(' + field + ')') if value_method not in [ "raw", "mean", "max", "min", "first", "last", "sum", "count" ]: value_method = "raw" filter = ' "iot"=\'' + gateway + '\' AND "device"=\'' + name + '\'' if value_method != "raw": filter = ' "iot"=\'' + gateway + '\' AND "device"=\'' + name + '\'' + ' AND "quality"=0 ' time_condition = html.unescape(time_condition) # fill_method = "null/previous/none/linear" group_method = ' GROUP BY time(' + group_time_span + ') FILL(' + fill_method + ')' query = 'SELECT' get_method = method["raw"] if value_method: get_method = method[value_method] query = query + ' ' + get_method + ' FROM "' + input + '"' + ' WHERE ' + filter + ' AND ' + time_condition if value_method != "raw": query = query + group_method query = query + ' limit ' + str( count_limit) + " tz('" + time_zone + "')" domain = frappe.get_value("Cloud Company", doc.company, "domain") r = requests.session().get(inf_server + "/query", params={ "q": query, "db": domain }, timeout=10) if r.status_code == 200: ret = r.json() # print(ret) if not ret: return results = ret['results'] if not results or len(results) < 1: return series = results[0].get('series') if not series or len(series) < 1: return res = series[0].get('values') if not res: return taghis = [] if value_method == "raw": for i in range(0, len(res)): hisvalue = { 'name': input, 'value': res[i][1], 'time': res[i][0], 'quality': res[i][2], 'device': name } taghis.append(hisvalue) else: for i in range(0, len(res)): hisvalue = { 'name': input, 'value': res[i][1], 'time': res[i][0], 'quality': 0, 'device': name } taghis.append(hisvalue) frappe.response.update({"ok": True, "data": taghis}) else: throw("failed_to_read_history") except Exception as ex: frappe.response.update({"ok": False, "error": str(ex)})
def taghisdata(sn=None, vsn=None, fields=None, condition=None): vsn = vsn or sn fields = fields or "*" doc = frappe.get_doc('IOT Device', sn) doc.has_permission("read") inf_server = IOTHDBSettings.get_influxdb_server() if not inf_server: frappe.logger(__name__).error( "InfluxDB Configuration missing in IOTHDBSettings") return 500 query = 'SELECT ' + fields + ' FROM "' + vsn + '"' if condition: query = query + " WHERE " + condition else: query = query + " LIMIT 1000" domain = frappe.get_value("Cloud Company", doc.company, "domain") r = requests.session().get(inf_server + "/query", params={ "q": query, "db": domain }, timeout=10) if r.status_code == 200: try: res = r.json()["results"][0]['series'][0]['values'] taghis = [] for i in range(0, len(res)): hisvalue = {} #print('*********', res[i][0]) try: utc_time = datetime.datetime.strptime( res[i][0], UTC_FORMAT1) except Exception as err: pass try: utc_time = datetime.datetime.strptime( res[i][0], UTC_FORMAT2) except Exception as err: pass #local_time = utc2local(utc_time).strftime("%Y-%m-%d %H:%M:%S") local_time = str( convert_utc_to_user_timezone(utc_time).replace( tzinfo=None)) #print('#######', local_time) if res[i][2] == '1': hisvalue = { 'name': res[i][1], 'value': res[i][4], 'time': local_time, 'quality': 0 } elif res[i][2] == '2': hisvalue = { 'name': res[i][1], 'value': res[i][3], 'time': local_time, 'quality': 0 } taghis.append(hisvalue) #print(taghis) return taghis except Exception as err: return r.json()