@cpzxrobot/sdk 1.0.4 → 1.0.5

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 CHANGED
@@ -1,10 +1,9 @@
1
1
  "use strict";
2
- var __importDefault =
3
- (this && this.__importDefault) ||
4
- function (mod) {
5
- return mod && mod.__esModule ? mod : { default: mod };
6
- };
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
7
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Cpzxrobot = void 0;
8
7
  exports.default = default_1;
9
8
  const device_gateway_1 = require("./device_gateway");
10
9
  const user_gateway_1 = require("./user_gateway");
@@ -15,239 +14,205 @@ const assistant_gateway_1 = require("./assistant_gateway");
15
14
  const energy_gateway_1 = require("./energy_gateway");
16
15
  const camera_gateway_1 = require("./camera_gateway");
17
16
  const pigfarm_gateway_1 = require("./pigfarm_gateway");
18
- class shzx {
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();
17
+ class Cpzxrobot {
18
+ constructor(appCode) {
19
+ this.pigfarm = new pigfarm_gateway_1.PigfarmGateway(this);
20
+ this.hash = "shzx";
21
+ this.mode = "dev";
22
+ this.auth = "";
23
+ this.token = "";
24
+ this.setTitle = () => { };
25
+ //获取当前浏览器的域名
26
+ this.ready = new Promise((resolve, reject) => {
27
+ this.resolveReady = resolve;
28
+ this.rejectReady = reject;
29
+ });
30
+ this.getDomain();
31
+ this.appCode = appCode;
32
+ this.user = new user_gateway_1.UserGateway(this);
33
+ this.factory = new factory_gateway_1.FactoryGateway(this);
34
+ this.device = new device_gateway_1.DeviceGateway(this);
35
+ this.transport = new transport_gateway_1.TransportGateway(this);
36
+ this.assistant = new assistant_gateway_1.AssistantGateway(this);
37
+ this.energy = new energy_gateway_1.EnergyGateway(this);
38
+ this.camera = new camera_gateway_1.CameraGateway(this);
47
39
  }
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);
40
+ exit() {
41
+ if (this.mode === "miniapp_in_app") {
42
+ // @ts-ignore
43
+ window.exitMiniapp();
44
+ }
45
+ else {
46
+ window.close();
67
47
  }
68
- );
69
- this.axios = instance;
70
48
  }
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";
49
+ inApp() {
50
+ return this.mode === "miniapp_in_app";
146
51
  }
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;
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((config) => {
58
+ // 在发送请求之前在头部添加token
59
+ config.headers.Authorization = this.token;
60
+ // 解析和验证Token
61
+ return config;
62
+ }, function (error) {
63
+ // 对请求错误做些什么
64
+ return Promise.reject(error);
65
+ });
66
+ this.axios = instance;
67
+ }
161
68
  }
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
69
+ //获取当前浏览器的域名
70
+ getDomain() {
71
+ const domain = window.location.hostname;
72
+ console.log("load domain ", domain);
73
+ if (domain == "miniapp") {
74
+ this.mode = "miniapp_in_app";
75
+ // @ts-ignore
76
+ this.axios = getAxiosFromMiniApp();
77
+ this.platformReady = Promise.resolve(true);
78
+ // @ts-ignore
79
+ this._getSelectedFarmFromMiniApp = window.getSelectedFarmFromMiniApp;
80
+ // @ts-ignore
81
+ this._getSelectedUnitFromMiniApp = window.getSelectedUnitFromMiniApp;
82
+ // @ts-ignore
83
+ this._jumpToMiniApp = window.miniapp.open;
84
+ // @ts-ignore
85
+ this.setTitle = window.miniapp.setTitle;
86
+ }
87
+ else if (domain == "appassets.androidplatform.net" ||
88
+ this.isIosMiniApp(window.location)) {
89
+ this.mode = "miniapp_in_app";
90
+ const platform =
91
+ // @ts-ignore
92
+ window.flutter_inappwebview;
93
+ this._getSelectedFarmFromMiniApp = function () {
94
+ return platform.callHandler("getSelectedFarmFromMiniApp");
95
+ };
96
+ this._getSelectedUnitFromMiniApp = function () {
97
+ return platform.callHandler("getSelectedUnitFromMiniApp");
98
+ };
99
+ this._jumpToMiniApp = function (url) {
100
+ return platform.callHandler("app.openMiniapp", url);
101
+ };
102
+ this.setTitle = function (title) {
103
+ return platform.callHandler("app.setTitle", title);
104
+ };
105
+ this.axios = {
106
+ get: function (url, data) {
107
+ console.log(url, data);
108
+ return platform.callHandler("axios_get", url, data);
109
+ },
110
+ post: function (url, data) {
111
+ console.log(url, data);
112
+ return platform.callHandler("axios_post", url, data);
113
+ },
114
+ };
115
+ this.platformReady = new Promise((resolve, reject) => {
116
+ window.addEventListener("flutterInAppWebViewPlatformReady", function () {
117
+ console.log("flutter android/ios platform is ready");
118
+ resolve(true);
119
+ });
120
+ setTimeout(() => {
121
+ reject("timeout");
122
+ }, 1500);
123
+ });
124
+ }
125
+ else if (this.isLocalDomain(domain)) {
126
+ this.mode = "dev";
127
+ this.setTitle = function (title) {
128
+ document.title = title;
129
+ };
130
+ console.log("欢迎使用上海正诚物联网IOT系统,您当前是开发环境,请在平台获取开发用jwt key,并传入devAuth设置");
131
+ }
132
+ else {
133
+ this.mode = "miniapp_in_web";
134
+ }
186
135
  }
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;
136
+ isIosMiniApp(location) {
137
+ //hostname is 'localhost' and has search params starting with 'mode=ios'
138
+ return (location.hostname === "localhost" &&
139
+ location.search.startsWith("?mode=ios"));
196
140
  }
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;
141
+ on(event, cb) {
142
+ switch (event) {
143
+ case "unitChanged":
144
+ // @ts-ignore
145
+ window._notifyUnitChanged = cb;
146
+ break;
209
147
  }
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
- }
148
+ }
149
+ //打开其他小程序
150
+ openMiniApp(url) {
151
+ this._jumpToMiniApp(url);
152
+ }
153
+ setAuth(auth, baseURL) {
154
+ if (this.mode === "miniapp_in_app") {
155
+ this.platformReady.then(() => {
156
+ this.initAxios(baseURL);
157
+ console.log("shzx is ready");
158
+ this.resolveReady(this.axios);
159
+ });
160
+ return;
161
+ }
162
+ this.initAxios(baseURL);
163
+ this.auth = auth;
164
+ this.axios
165
+ .post("/api/v1/user/auth", {
166
+ appCode: this.appCode,
167
+ auth: auth,
168
+ })
169
+ .then((res) => {
170
+ if (res.data.Error) {
171
+ throw res.data.Error;
172
+ }
173
+ else {
174
+ this.token = "Bearer " + res.data.token;
175
+ }
176
+ this.resolveReady(this.axios);
177
+ })
178
+ .catch((err) => {
179
+ console.log("shzx ready caller reject", err);
180
+ this.rejectReady(err);
181
+ });
182
+ }
183
+ isLocalDomain(domain) {
184
+ // 列出所有的本地开发域名
185
+ const localDomains = ["localhost", "127.0.0.1", "0.0.0.0"];
186
+ // 检查当前域名是否在本地开发域名列表中
187
+ return localDomains.includes(domain);
188
+ }
223
189
  }
190
+ exports.Cpzxrobot = Cpzxrobot;
224
191
  let _instance;
225
- function default_1(
226
- args = {
192
+ function default_1(args = {
227
193
  devAuth: "",
228
194
  appCode: "",
229
195
  baseURL: "https://www.cpzxrobot.com/",
230
196
  selectedFarm: {
231
- id: 0,
232
- code: "",
233
- name: "",
234
- company_code: "",
197
+ id: 0,
198
+ code: "",
199
+ name: "",
200
+ company_code: "",
235
201
  },
236
202
  selectedUnit: {
237
- id: 0,
238
- name: "",
203
+ id: 0,
204
+ name: "",
239
205
  },
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;
206
+ }) {
207
+ if (!_instance) {
208
+ _instance = new Cpzxrobot(args.appCode);
209
+ _instance.setAuth(args.devAuth, args.baseURL);
210
+ if (args.selectedFarm) {
211
+ _instance.user.selectedFarm = args.selectedFarm;
212
+ }
213
+ if (args.selectedUnit) {
214
+ _instance.user.selectedUnit = args.selectedUnit;
215
+ }
250
216
  }
251
- }
252
- return _instance;
217
+ return _instance;
253
218
  }
@@ -4,7 +4,7 @@ exports.TransportGateway = void 0;
4
4
  class TransportGateway extends Object {
5
5
  constructor(context) {
6
6
  super();
7
- this._selectedFarm = '';
7
+ this._selectedFarm = "";
8
8
  this.context = context;
9
9
  }
10
10
  get fodder() {
@@ -13,12 +13,12 @@ class TransportGateway extends Object {
13
13
  getData: (id = undefined, page = {
14
14
  current: 1,
15
15
  pageSize: 10,
16
- deviceId: id
16
+ deviceId: id,
17
17
  }) => {
18
18
  return this.context.ready.then(() => {
19
19
  return this.context.axios
20
- .get('/api/v1/pigfarm/device/fodder', {
21
- params: page
20
+ .get("/api/v1/pigfarm/device/fodder", {
21
+ params: page,
22
22
  })
23
23
  .then((res) => {
24
24
  if (res.data.code != 200) {
@@ -28,9 +28,24 @@ class TransportGateway extends Object {
28
28
  });
29
29
  });
30
30
  },
31
+ //获取设备打料数据
32
+ getDetail: (ids, date) => {
33
+ return this.context.ready.then(() => {
34
+ return this.context.axios
35
+ .post("/api/v1/pigfarm/device/fodderTower/detailDeviceLoad", {
36
+ ids,
37
+ date,
38
+ })
39
+ .then((res) => {
40
+ return res.data;
41
+ });
42
+ });
43
+ },
31
44
  create: (data) => {
32
45
  return this.context.ready.then(() => {
33
- return this.context.axios.post('/api/v1/pigfarm/device/fodder/add', data).then((res) => {
46
+ return this.context.axios
47
+ .post("/api/v1/pigfarm/device/fodder/add", data)
48
+ .then((res) => {
34
49
  if (res.data.code != 200) {
35
50
  throw res.data.message;
36
51
  }
@@ -41,7 +56,7 @@ class TransportGateway extends Object {
41
56
  bind: (data) => {
42
57
  return this.context.ready.then(() => {
43
58
  return this.context.axios
44
- .post('/api/v1/pigfarm/device/fodderTower/addList', data)
59
+ .post("/api/v1/pigfarm/device/fodderTower/addList", data)
45
60
  .then((res) => {
46
61
  if (res.data.code != 200) {
47
62
  throw res.data.message;
@@ -49,7 +64,7 @@ class TransportGateway extends Object {
49
64
  return res.data.data;
50
65
  });
51
66
  });
52
- }
67
+ },
53
68
  };
54
69
  }
55
70
  }
package/energy_gateway.ts CHANGED
@@ -1,10 +1,11 @@
1
+ import { Cpzxrobot } from ".";
1
2
  import { ElectricMeterGateway } from "./energy_types/electric_meter_gateway";
2
3
  //消息助手网关,获取消息助手的信息
3
4
  export class EnergyGateway extends Object {
4
- context: Shzx;
5
+ context: Cpzxrobot;
5
6
  _electric: ElectricMeterGateway | null = null;
6
7
 
7
- constructor(context: Shzx) {
8
+ constructor(context: Cpzxrobot) {
8
9
  super();
9
10
  this.context = context;
10
11
  }
@@ -1,4 +1,4 @@
1
- import shzx from ".."
1
+ import { type Cpzxrobot } from "..";
2
2
 
3
3
  export interface ElectricMeterRate {
4
4
  endHour: number;
@@ -12,16 +12,16 @@ export interface ElectricMeterRate {
12
12
 
13
13
  //消息助手网关,获取消息助手的信息
14
14
  export class ElectricMeterGateway extends Object {
15
- context: Shzx
15
+ context: Cpzxrobot
16
16
 
17
- constructor(context: Shzx) {
17
+ constructor(context: Cpzxrobot) {
18
18
  super()
19
19
  this.context = context
20
20
  }
21
21
 
22
22
  //返回峰谷平电表的阶梯区间
23
23
  getRates() {
24
- return shzx().ready.then((axios) => {
24
+ return this.context.ready.then((axios) => {
25
25
  return axios.get('/api/v1/energy/electricRange').then((res) => {
26
26
  if (res.data.code !== 200) {
27
27
  throw res.data.message
@@ -1,3 +1,5 @@
1
+ import { Cpzxrobot, Factory } from "./types"
2
+
1
3
  export class FactoryGateway extends Object {
2
4
  _selectedFarm: Factory = {
3
5
  code: '',
@@ -5,8 +7,8 @@ export class FactoryGateway extends Object {
5
7
  company_code: '',
6
8
  id: 0
7
9
  }
8
- context: Shzx
9
- constructor(context: Shzx) {
10
+ context: Cpzxrobot
11
+ constructor(context: Cpzxrobot) {
10
12
  super()
11
13
  this.context = context
12
14
  }
package/index.ts CHANGED
@@ -8,8 +8,9 @@ import { EnergyGateway } from "./energy_gateway";
8
8
  import { CameraGateway } from "./camera_gateway";
9
9
  import { type ElectricMeterRate } from "./energy_types/electric_meter_gateway";
10
10
  import { PigfarmGateway } from "./pigfarm_gateway";
11
+ import { Device, Factory, MyAxiosInstance, Unit } from "./types";
11
12
 
12
- class shzx {
13
+ export class Cpzxrobot {
13
14
  device: DeviceGateway;
14
15
  pigfarm: PigfarmGateway = new PigfarmGateway(this);
15
16
  public hash = "shzx";
@@ -220,7 +221,7 @@ class shzx {
220
221
  }
221
222
  }
222
223
 
223
- let _instance!: shzx;
224
+ let _instance!: Cpzxrobot;
224
225
 
225
226
  export default function (
226
227
  args: {
@@ -244,9 +245,9 @@ export default function (
244
245
  name: "",
245
246
  },
246
247
  }
247
- ): shzx {
248
+ ): Cpzxrobot {
248
249
  if (!_instance) {
249
- _instance = new shzx(args.appCode);
250
+ _instance = new Cpzxrobot(args.appCode);
250
251
  _instance.setAuth(args.devAuth,args.baseURL);
251
252
  if (args.selectedFarm) {
252
253
  _instance.user.selectedFarm = args.selectedFarm;
@@ -257,29 +258,5 @@ export default function (
257
258
  }
258
259
  return _instance;
259
260
  }
260
- export interface FeedTower {
261
- id: number;
262
- name: string;
263
- currentAmount: number;
264
- lastZeroTime: string;
265
- cycle?: number;
266
- "pg.feedtower.status"?: string;
267
- "pg.feedtower.near-zero"?: string;
268
- deviation: number;
269
- towerVolume: number;
270
- unit: string;
271
- deviceId: number;
272
- fodderUnit: string;
273
- fodderCode: string;
274
- }
275
- export interface Assistant {
276
- info: String;
277
- status: Number;
278
- name: String;
279
- id: Number;
280
- }
281
- export interface ElectricMeter extends Device {
282
- coefficient: number;
283
- status: string;
284
- }
261
+
285
262
  export { type ElectricMeterRate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cpzxrobot/sdk",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -14,5 +14,7 @@
14
14
  },
15
15
  "dependencies": {
16
16
  "axios": "^1.7.2"
17
- }
17
+ },
18
+ "types": "types.d.ts",
19
+ "typings": "types.d.ts"
18
20
  }
@@ -1,6 +1,8 @@
1
+ import { Cpzxrobot, Unit } from ".";
2
+
1
3
  export class PigfarmGateway extends Object {
2
- context: Shzx
3
- constructor(context: Shzx) {
4
+ context: Cpzxrobot
5
+ constructor(context: Cpzxrobot) {
4
6
  super()
5
7
  this.context = context
6
8
  }