@cpzxrobot/sdk 1.0.1 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +222 -184
- package/package.json +1 -1
- package/readme.md +42 -0
package/dist/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault =
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __importDefault =
|
|
3
|
+
(this && this.__importDefault) ||
|
|
4
|
+
function (mod) {
|
|
5
|
+
return mod && mod.__esModule ? mod : { default: mod };
|
|
6
|
+
};
|
|
5
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
8
|
exports.default = default_1;
|
|
7
9
|
const device_gateway_1 = require("./device_gateway");
|
|
@@ -14,202 +16,238 @@ const energy_gateway_1 = require("./energy_gateway");
|
|
|
14
16
|
const camera_gateway_1 = require("./camera_gateway");
|
|
15
17
|
const pigfarm_gateway_1 = require("./pigfarm_gateway");
|
|
16
18
|
class shzx {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
window.close();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
inApp() {
|
|
49
|
-
return this.mode === "miniapp_in_app";
|
|
19
|
+
constructor(appCode) {
|
|
20
|
+
this.pigfarm = new pigfarm_gateway_1.PigfarmGateway(this);
|
|
21
|
+
this.hash = "shzx";
|
|
22
|
+
this.mode = "dev";
|
|
23
|
+
this.auth = "";
|
|
24
|
+
this.token = "";
|
|
25
|
+
this.setTitle = () => {};
|
|
26
|
+
//获取当前浏览器的域名
|
|
27
|
+
this.ready = new Promise((resolve, reject) => {
|
|
28
|
+
this.resolveReady = resolve;
|
|
29
|
+
this.rejectReady = reject;
|
|
30
|
+
});
|
|
31
|
+
this.getDomain();
|
|
32
|
+
this.appCode = appCode;
|
|
33
|
+
this.user = new user_gateway_1.UserGateway(this);
|
|
34
|
+
this.factory = new factory_gateway_1.FactoryGateway(this);
|
|
35
|
+
this.device = new device_gateway_1.DeviceGateway(this);
|
|
36
|
+
this.transport = new transport_gateway_1.TransportGateway(this);
|
|
37
|
+
this.assistant = new assistant_gateway_1.AssistantGateway(this);
|
|
38
|
+
this.energy = new energy_gateway_1.EnergyGateway(this);
|
|
39
|
+
this.camera = new camera_gateway_1.CameraGateway(this);
|
|
40
|
+
}
|
|
41
|
+
exit() {
|
|
42
|
+
if (this.mode === "miniapp_in_app") {
|
|
43
|
+
// @ts-ignore
|
|
44
|
+
window.exitMiniapp();
|
|
45
|
+
} else {
|
|
46
|
+
window.close();
|
|
50
47
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
48
|
+
}
|
|
49
|
+
inApp() {
|
|
50
|
+
return this.mode === "miniapp_in_app";
|
|
51
|
+
}
|
|
52
|
+
initAxios(baseURL) {
|
|
53
|
+
if (this.mode !== "miniapp_in_app") {
|
|
54
|
+
const instance = axios_1.default.create({
|
|
55
|
+
baseURL,
|
|
56
|
+
});
|
|
57
|
+
instance.interceptors.request.use(
|
|
58
|
+
(config) => {
|
|
59
|
+
// 在发送请求之前在头部添加token
|
|
60
|
+
config.headers.Authorization = this.token;
|
|
61
|
+
// 解析和验证Token
|
|
62
|
+
return config;
|
|
63
|
+
},
|
|
64
|
+
function (error) {
|
|
65
|
+
// 对请求错误做些什么
|
|
66
|
+
return Promise.reject(error);
|
|
66
67
|
}
|
|
68
|
+
);
|
|
69
|
+
this.axios = instance;
|
|
67
70
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
71
|
+
}
|
|
72
|
+
//获取当前浏览器的域名
|
|
73
|
+
getDomain() {
|
|
74
|
+
const domain = window.location.hostname;
|
|
75
|
+
console.log("load domain ", domain);
|
|
76
|
+
if (domain == "miniapp") {
|
|
77
|
+
this.mode = "miniapp_in_app";
|
|
78
|
+
// @ts-ignore
|
|
79
|
+
this.axios = getAxiosFromMiniApp();
|
|
80
|
+
this.platformReady = Promise.resolve(true);
|
|
81
|
+
// @ts-ignore
|
|
82
|
+
this._getSelectedFarmFromMiniApp = window.getSelectedFarmFromMiniApp;
|
|
83
|
+
// @ts-ignore
|
|
84
|
+
this._getSelectedUnitFromMiniApp = window.getSelectedUnitFromMiniApp;
|
|
85
|
+
// @ts-ignore
|
|
86
|
+
this._jumpToMiniApp = window.miniapp.open;
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
this.setTitle = window.miniapp.setTitle;
|
|
89
|
+
// @ts-ignore
|
|
90
|
+
this.saveBase64 = window.miniapp.saveBase64;
|
|
91
|
+
} else if (
|
|
92
|
+
domain == "appassets.androidplatform.net" ||
|
|
93
|
+
this.isIosMiniApp(window.location)
|
|
94
|
+
) {
|
|
95
|
+
this.mode = "miniapp_in_app";
|
|
96
|
+
const platform =
|
|
97
|
+
// @ts-ignore
|
|
98
|
+
window.flutter_inappwebview;
|
|
99
|
+
this._getSelectedFarmFromMiniApp = function () {
|
|
100
|
+
return platform.callHandler("getSelectedFarmFromMiniApp");
|
|
101
|
+
};
|
|
102
|
+
this._getSelectedUnitFromMiniApp = function () {
|
|
103
|
+
return platform.callHandler("getSelectedUnitFromMiniApp");
|
|
104
|
+
};
|
|
105
|
+
this._jumpToMiniApp = function (url) {
|
|
106
|
+
return platform.callHandler("app.openMiniapp", url);
|
|
107
|
+
};
|
|
108
|
+
this.setTitle = function (title) {
|
|
109
|
+
return platform.callHandler("app.setTitle", title);
|
|
110
|
+
};
|
|
111
|
+
this.saveBase64 = function (base64, filename) {
|
|
112
|
+
return platform.callHandler("app.saveBase64", base64, filename);
|
|
113
|
+
};
|
|
114
|
+
this.axios = {
|
|
115
|
+
get: function (url, data) {
|
|
116
|
+
console.log(url, data);
|
|
117
|
+
return platform.callHandler("axios_get", url, data);
|
|
118
|
+
},
|
|
119
|
+
post: function (url, data) {
|
|
120
|
+
console.log(url, data);
|
|
121
|
+
return platform.callHandler("axios_post", url, data);
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
this.platformReady = new Promise((resolve, reject) => {
|
|
125
|
+
window.addEventListener(
|
|
126
|
+
"flutterInAppWebViewPlatformReady",
|
|
127
|
+
function () {
|
|
128
|
+
console.log("flutter android/ios platform is ready");
|
|
129
|
+
resolve(true);
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
setTimeout(() => {
|
|
133
|
+
reject("timeout");
|
|
134
|
+
}, 1500);
|
|
135
|
+
});
|
|
136
|
+
} else if (this.isLocalDomain(domain)) {
|
|
137
|
+
this.mode = "dev";
|
|
138
|
+
this.setTitle = function (title) {
|
|
139
|
+
document.title = title;
|
|
140
|
+
};
|
|
141
|
+
console.log(
|
|
142
|
+
"欢迎使用上海正诚物联网IOT系统,您当前是开发环境,请在平台获取开发用jwt key,并传入devAuth设置"
|
|
143
|
+
);
|
|
144
|
+
} else {
|
|
145
|
+
this.mode = "miniapp_in_web";
|
|
134
146
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
147
|
+
}
|
|
148
|
+
isIosMiniApp(location) {
|
|
149
|
+
//hostname is 'localhost' and has search params starting with 'mode=ios'
|
|
150
|
+
return (
|
|
151
|
+
location.hostname === "localhost" &&
|
|
152
|
+
location.search.startsWith("?mode=ios")
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
on(event, cb) {
|
|
156
|
+
switch (event) {
|
|
157
|
+
case "unitChanged":
|
|
158
|
+
// @ts-ignore
|
|
159
|
+
window._notifyUnitChanged = cb;
|
|
160
|
+
break;
|
|
139
161
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
162
|
+
}
|
|
163
|
+
//打开其他小程序
|
|
164
|
+
openMiniApp(url) {
|
|
165
|
+
this._jumpToMiniApp(url);
|
|
166
|
+
}
|
|
167
|
+
saveBlob(blob, filename) {
|
|
168
|
+
if (this.mode === "miniapp_in_app") {
|
|
169
|
+
//blob to base64
|
|
170
|
+
const reader = new FileReader();
|
|
171
|
+
reader.readAsDataURL(blob);
|
|
172
|
+
const that = this;
|
|
173
|
+
reader.onload = function () {
|
|
174
|
+
that.saveBase64(reader.result, filename);
|
|
175
|
+
};
|
|
176
|
+
} else {
|
|
177
|
+
// For browsers: Create a link and trigger a download
|
|
178
|
+
const a = document.createElement("a");
|
|
179
|
+
const url = URL.createObjectURL(blob);
|
|
180
|
+
a.href = url;
|
|
181
|
+
a.download = filename; // Set the file name for the download
|
|
182
|
+
document.body.appendChild(a); // Append the link to the document
|
|
183
|
+
a.click(); // Trigger the download
|
|
184
|
+
URL.revokeObjectURL(url); // Clean up by revoking the Blob URL
|
|
185
|
+
a.remove(); // Remove the link from the document
|
|
147
186
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
187
|
+
}
|
|
188
|
+
setAuth(auth, baseURL) {
|
|
189
|
+
if (this.mode === "miniapp_in_app") {
|
|
190
|
+
this.platformReady.then(() => {
|
|
191
|
+
this.initAxios(baseURL);
|
|
192
|
+
console.log("shzx is ready");
|
|
193
|
+
this.resolveReady(this.axios);
|
|
194
|
+
});
|
|
195
|
+
return;
|
|
151
196
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
197
|
+
this.initAxios(baseURL);
|
|
198
|
+
this.auth = auth;
|
|
199
|
+
this.axios
|
|
200
|
+
.post("/api/v1/user/auth", {
|
|
201
|
+
appCode: this.appCode,
|
|
202
|
+
auth: auth,
|
|
203
|
+
})
|
|
204
|
+
.then((res) => {
|
|
205
|
+
if (res.data.Error) {
|
|
206
|
+
throw res.data.Error;
|
|
207
|
+
} else {
|
|
208
|
+
this.token = "Bearer " + res.data.token;
|
|
160
209
|
}
|
|
161
|
-
this.
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
this.resolveReady(this.axios);
|
|
176
|
-
})
|
|
177
|
-
.catch((err) => {
|
|
178
|
-
console.log("shzx ready caller reject", err);
|
|
179
|
-
this.rejectReady(err);
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
isLocalDomain(domain) {
|
|
183
|
-
// 列出所有的本地开发域名
|
|
184
|
-
const localDomains = ["localhost", "127.0.0.1", "0.0.0.0"];
|
|
185
|
-
// 检查当前域名是否在本地开发域名列表中
|
|
186
|
-
return localDomains.includes(domain);
|
|
187
|
-
}
|
|
210
|
+
this.resolveReady(this.axios);
|
|
211
|
+
})
|
|
212
|
+
.catch((err) => {
|
|
213
|
+
console.log("shzx ready caller reject", err);
|
|
214
|
+
this.rejectReady(err);
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
isLocalDomain(domain) {
|
|
218
|
+
// 列出所有的本地开发域名
|
|
219
|
+
const localDomains = ["localhost", "127.0.0.1", "0.0.0.0"];
|
|
220
|
+
// 检查当前域名是否在本地开发域名列表中
|
|
221
|
+
return localDomains.includes(domain);
|
|
222
|
+
}
|
|
188
223
|
}
|
|
189
224
|
let _instance;
|
|
190
|
-
function default_1(
|
|
225
|
+
function default_1(
|
|
226
|
+
args = {
|
|
191
227
|
devAuth: "",
|
|
192
228
|
appCode: "",
|
|
229
|
+
baseURL: "https://www.cpzxrobot.com/",
|
|
193
230
|
selectedFarm: {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
231
|
+
id: 0,
|
|
232
|
+
code: "",
|
|
233
|
+
name: "",
|
|
234
|
+
company_code: "",
|
|
198
235
|
},
|
|
199
236
|
selectedUnit: {
|
|
200
|
-
|
|
201
|
-
|
|
237
|
+
id: 0,
|
|
238
|
+
name: "",
|
|
202
239
|
},
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
240
|
+
}
|
|
241
|
+
) {
|
|
242
|
+
if (!_instance) {
|
|
243
|
+
_instance = new shzx(args.appCode);
|
|
244
|
+
_instance.setAuth(args.devAuth, args.baseURL);
|
|
245
|
+
if (args.selectedFarm) {
|
|
246
|
+
_instance.user.selectedFarm = args.selectedFarm;
|
|
247
|
+
}
|
|
248
|
+
if (args.selectedUnit) {
|
|
249
|
+
_instance.user.selectedUnit = args.selectedUnit;
|
|
213
250
|
}
|
|
214
|
-
|
|
251
|
+
}
|
|
252
|
+
return _instance;
|
|
215
253
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -65,3 +65,45 @@ baseURL: "/"
|
|
|
65
65
|
| cpzxrobot().device.xxx | 获得设备的相关信息 |
|
|
66
66
|
| cpzxrobot().camera.xxx | 获得摄像头的相关信息 |
|
|
67
67
|
|
|
68
|
+
### 工厂信息接口
|
|
69
|
+
|
|
70
|
+
#### cpzxrobot().factory.workshops(factory_id)
|
|
71
|
+
|
|
72
|
+
获得工厂的所有车间
|
|
73
|
+
|
|
74
|
+
入参:
|
|
75
|
+
|
|
76
|
+
factory_id: 工厂id
|
|
77
|
+
|
|
78
|
+
#### cpzxrobot().factory.units(factory_id)
|
|
79
|
+
|
|
80
|
+
获得工厂的所有单元
|
|
81
|
+
|
|
82
|
+
入参:
|
|
83
|
+
|
|
84
|
+
factory_id: 工厂id
|
|
85
|
+
|
|
86
|
+
### 摄像头相关
|
|
87
|
+
|
|
88
|
+
`cpzxrobot().camera.search(args)`
|
|
89
|
+
|
|
90
|
+
搜索摄像头
|
|
91
|
+
|
|
92
|
+
入参:
|
|
93
|
+
|
|
94
|
+
入参为json object,可接受多种参数中的一个
|
|
95
|
+
|
|
96
|
+
如果入参为:{unit_id:xxx}则按单元搜索
|
|
97
|
+
|
|
98
|
+
如果入参为:{factory_id:xxx}则按工厂搜索
|
|
99
|
+
|
|
100
|
+
### app功能相关
|
|
101
|
+
|
|
102
|
+
`cpzxrobot().saveBlob(blob,filname)`
|
|
103
|
+
|
|
104
|
+
保存blob为文件
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
`cpzxrobot().saveBase64(base64,filename)`
|
|
108
|
+
|
|
109
|
+
保存Base64内容为文件
|