iot.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import axios from "axios";
  2. //获取设备列表
  3. export async function getDevices(){
  4. const body: any = {
  5. case: '',
  6. name: '',
  7. productId: '',
  8. };
  9. const ret: { records: any[]; total: number; type:number } = await axios.post(
  10. '/api/iot/devices',
  11. // '/api/iot/device/list'
  12. {
  13. pageIndex: 1,
  14. pageSize: 1000
  15. }
  16. );
  17. return ret;
  18. }
  19. export async function getMqttUrl () {
  20. const ret:any = await axios.get('/api/iot/mqtt/url');
  21. return ret;
  22. }
  23. //获取设备属性列表
  24. export async function getDeviceProperties(deviceId:string){
  25. // const ret:any = await axios.get(`/api/iot/device/properties?id=${deviceId}`);
  26. const ret:any = await axios.post(`/api/iot/device/properties/${deviceId}`);
  27. return ret;
  28. }
  29. export async function subscribeProperties(devices:{deviceId:string; token:string;properties:string[]}[]){
  30. const ret:any = await axios.post(`/api/iot/subscribe/properties`,{
  31. devices
  32. });
  33. return ret;
  34. }
  35. export async function unsubscribeProperties(token:string){
  36. const ret:any = await axios.post(`/api/iot/unsubscribe/properties`,{
  37. token
  38. });
  39. return ret;
  40. }
  41. //获取sql数据源列表
  42. export async function getSqlSourceList(){
  43. const ret:any = await axios.post(
  44. '/api/iot/data/sql/sources',
  45. {
  46. params: {
  47. current: 1,
  48. pageSize: 100,
  49. },
  50. }
  51. );
  52. return ret;
  53. }
  54. //执行查数据库操作 eg:SELECT id, fullpath FROM "directory" ;
  55. export type SqlType = 'list' | 'get' | 'exec' | 'add' | 'update' | 'delete';
  56. export async function doSqlCode(sql:{method?:SqlType, dbid?:string,sql?:string,pageSize?:number,current?:number}) {
  57. let _sql = sql.sql
  58. if(sql.method==='list'){
  59. _sql+= ` LIMIT ${sql.pageSize||20}`+(sql.current>1?(' OFFSET '+(sql.current-1)*sql.pageSize):'');
  60. }
  61. console.log("doSqlGet",_sql);
  62. const ret = await axios.post(
  63. `/api/iot/data/sql/${sql.method}`,{
  64. dbid:sql.dbid,
  65. sql:_sql,
  66. }
  67. );
  68. return ret;
  69. }
  70. //数据库操作
  71. //增 INSERT INTO tbl_name SET col_name1=value1,col_name2=value2,......;
  72. //删 DELETE FROM users WHERE name = 'John';
  73. //改 UPDATE tbl_name SET col_name1=value1,col_name2=value2,...... WHERE some_column=some_value;