@cpzxrobot/sdk 1.0.4 → 1.0.6

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/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";
@@ -31,6 +32,8 @@ class shzx {
31
32
  _getSelectedUnitFromMiniApp!: () => any;
32
33
  _jumpToMiniApp!: (url: string) => any;
33
34
  setTitle: Function = () => {};
35
+ saveBase64: Function = (base64: string, filename: string) => {};
36
+ saveBlob: Function = (blob: Blob, filename: string) => {};
34
37
  assistant: AssistantGateway;
35
38
  energy: EnergyGateway;
36
39
  camera: CameraGateway;
@@ -65,7 +68,7 @@ class shzx {
65
68
  return this.mode === "miniapp_in_app";
66
69
  }
67
70
 
68
- initAxios(baseURL:string) {
71
+ initAxios(baseURL: string) {
69
72
  if (this.mode !== "miniapp_in_app") {
70
73
  const instance = axios.create({
71
74
  baseURL,
@@ -87,6 +90,16 @@ class shzx {
87
90
  }
88
91
  }
89
92
 
93
+ _saveBlobAsBase64(blob: Blob, filename: string) {
94
+ const reader = new FileReader();
95
+ reader.onload = function () {
96
+ const base64 = reader.result as string;
97
+ // @ts-ignore
98
+ window.miniapp.saveBase64(base64, filename);
99
+ };
100
+ reader.readAsDataURL(blob);
101
+ }
102
+
90
103
  //获取当前浏览器的域名
91
104
  getDomain() {
92
105
  const domain = window.location.hostname;
@@ -104,6 +117,9 @@ class shzx {
104
117
  this._jumpToMiniApp = window.miniapp.open;
105
118
  // @ts-ignore
106
119
  this.setTitle = window.miniapp.setTitle;
120
+ // @ts-ignore
121
+ this.saveBase64 = window.miniapp.saveBase64;
122
+ this.saveBlob = this._saveBlobAsBase64;
107
123
  } else if (
108
124
  domain == "appassets.androidplatform.net" ||
109
125
  this.isIosMiniApp(window.location)
@@ -125,7 +141,11 @@ class shzx {
125
141
  };
126
142
  this.setTitle = function (title: string) {
127
143
  return platform.callHandler("app.setTitle", title);
128
- }
144
+ };
145
+ this.saveBlob = this._saveBlobAsBase64;
146
+ this.saveBase64 = function (base64: string, filename: string) {
147
+ return platform.callHandler("app.saveBase64", base64, filename);
148
+ };
129
149
  this.axios = {
130
150
  get: function (url, data) {
131
151
  console.log(url, data);
@@ -152,7 +172,29 @@ class shzx {
152
172
  this.mode = "dev";
153
173
  this.setTitle = function (title: string) {
154
174
  document.title = title;
155
- }
175
+ };
176
+ this.saveBase64 = function (base64: string, filename: string) {
177
+ //decode base64 to blob
178
+ const byteCharacters = atob(base64);
179
+ const byteNumbers = new Array(byteCharacters.length);
180
+ for (let i = 0; i < byteCharacters.length; i++) {
181
+ byteNumbers[i] = byteCharacters.charCodeAt(i);
182
+ }
183
+ const byteArray = new Uint8Array(byteNumbers);
184
+ const blob = new Blob([byteArray], {
185
+ type: "application/octet-stream",
186
+ });
187
+ this.saveBlob(blob, filename);
188
+ };
189
+ this.saveBlob = function (blob: Blob, filename: string) {
190
+ const url = URL.createObjectURL(blob);
191
+ const a = document.createElement("a");
192
+ a.href = url;
193
+ a.download = filename;
194
+ a.click();
195
+ URL.revokeObjectURL(url);
196
+ document.body.removeChild(a);
197
+ };
156
198
  console.log(
157
199
  "欢迎使用上海正诚物联网IOT系统,您当前是开发环境,请在平台获取开发用jwt key,并传入devAuth设置"
158
200
  );
@@ -181,7 +223,7 @@ class shzx {
181
223
  this._jumpToMiniApp(url);
182
224
  }
183
225
 
184
- setAuth(auth: string,baseURL:string) {
226
+ setAuth(auth: string, baseURL: string) {
185
227
  if (this.mode === "miniapp_in_app") {
186
228
  this.platformReady.then(() => {
187
229
  this.initAxios(baseURL);
@@ -220,7 +262,7 @@ class shzx {
220
262
  }
221
263
  }
222
264
 
223
- let _instance!: shzx;
265
+ let _instance!: Cpzxrobot;
224
266
 
225
267
  export default function (
226
268
  args: {
@@ -244,10 +286,10 @@ export default function (
244
286
  name: "",
245
287
  },
246
288
  }
247
- ): shzx {
289
+ ): Cpzxrobot {
248
290
  if (!_instance) {
249
- _instance = new shzx(args.appCode);
250
- _instance.setAuth(args.devAuth,args.baseURL);
291
+ _instance = new Cpzxrobot(args.appCode);
292
+ _instance.setAuth(args.devAuth, args.baseURL);
251
293
  if (args.selectedFarm) {
252
294
  _instance.user.selectedFarm = args.selectedFarm;
253
295
  }
@@ -257,29 +299,5 @@ export default function (
257
299
  }
258
300
  return _instance;
259
301
  }
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
- }
302
+
285
303
  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.6",
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
  }
@@ -1,10 +1,12 @@
1
+ import { Cpzxrobot, FodderOrder } from ".";
2
+
1
3
  export class TransportGateway extends Object {
2
- _selectedFarm: string = ''
3
- context: Shzx
4
+ _selectedFarm: string = "";
5
+ context: Cpzxrobot;
4
6
 
5
- constructor(context: Shzx) {
6
- super()
7
- this.context = context
7
+ constructor(context: Cpzxrobot) {
8
+ super();
9
+ this.context = context;
8
10
  }
9
11
 
10
12
  get fodder() {
@@ -13,55 +15,70 @@ export class TransportGateway extends Object {
13
15
  getData: (
14
16
  id: number | undefined = undefined,
15
17
  page: {
16
- current: number
17
- pageSize: number
18
- deviceId: number | undefined
18
+ current: number;
19
+ pageSize: number;
20
+ deviceId: number | undefined;
19
21
  } = {
20
- current: 1,
21
- pageSize: 10,
22
- deviceId: id
23
- }
22
+ current: 1,
23
+ pageSize: 10,
24
+ deviceId: id,
25
+ }
24
26
  ): Promise<FodderOrder[]> => {
25
27
  return this.context.ready.then(() => {
26
28
  return this.context.axios
27
- .get('/api/v1/pigfarm/device/fodder', {
28
- params: page
29
+ .get("/api/v1/pigfarm/device/fodder", {
30
+ params: page,
29
31
  })
30
32
  .then((res) => {
31
33
  if (res.data.code != 200) {
32
- throw res.data.message
34
+ throw res.data.message;
33
35
  }
34
- return res.data.data.list
36
+ return res.data.data.list;
37
+ });
38
+ });
39
+ },
40
+ //获取设备打料数据
41
+ getDetail: (ids: Array<number>, date: string): Promise<any> => {
42
+ return this.context.ready.then(() => {
43
+ return this.context.axios
44
+ .post("/api/v1/pigfarm/device/fodderTower/detailDeviceLoad", {
45
+ ids,
46
+ date,
35
47
  })
36
- })
48
+ .then((res) => {
49
+ return res.data;
50
+ });
51
+ });
37
52
  },
38
53
  create: (data: FodderOrder) => {
39
54
  return this.context.ready.then(() => {
40
- return this.context.axios.post('/api/v1/pigfarm/device/fodder/add', data).then((res) => {
41
- if (res.data.code != 200) {
42
- throw res.data.message
43
- }
44
- return res.data.data
45
- })
46
- })
55
+ return this.context.axios
56
+ .post("/api/v1/pigfarm/device/fodder/add", data)
57
+ .then((res) => {
58
+ if (res.data.code != 200) {
59
+ throw res.data.message;
60
+ }
61
+ return res.data.data;
62
+ });
63
+ });
47
64
  },
48
65
  bind: (data: {
49
- deviceIds: number[]
50
- fodderType: string //料塔类型
51
- fodderAmount: number //料塔容量
52
- fodderId: number //料塔id,类 ZC2023 用于绑定料塔
66
+ deviceIds: number[];
67
+ fodderType: string; //料塔类型
68
+ fodderAmount: number; //料塔容量
69
+ fodderId: number; //料塔id,类 ZC2023 用于绑定料塔
53
70
  }) => {
54
71
  return this.context.ready.then(() => {
55
72
  return this.context.axios
56
- .post('/api/v1/pigfarm/device/fodderTower/addList', data)
73
+ .post("/api/v1/pigfarm/device/fodderTower/addList", data)
57
74
  .then((res) => {
58
75
  if (res.data.code != 200) {
59
- throw res.data.message
76
+ throw res.data.message;
60
77
  }
61
- return res.data.data
62
- })
63
- })
64
- }
65
- }
78
+ return res.data.data;
79
+ });
80
+ });
81
+ },
82
+ };
66
83
  }
67
84
  }
package/types.d.ts CHANGED
@@ -1,102 +1,184 @@
1
-
1
+ import { DeviceGateway } from "@cpzxrobot/sdk/device_gateway";
2
+ import { FactoryGateway } from "@cpzxrobot/sdk/factory_gateway";
3
+ import { TransportGateway } from "@cpzxrobot/sdk/transport_gateway";
4
+ import { UserGateway } from "@cpzxrobot/sdk/user_gateway";
2
5
 
3
6
  type Device = {
4
- id: number
5
- deviceId: string
6
- name: string
7
- type: number
8
- typeCode:string
9
- currentAmount: number
10
- location: string
11
- supplier?: string
12
- data?: number[]
13
- typeCode: string
14
- }
7
+ id: number;
8
+ deviceId: string;
9
+ name: string;
10
+ type: number;
11
+ typeCode: string;
12
+ currentAmount: number;
13
+ location: string;
14
+ supplier?: string;
15
+ data?: number[];
16
+ typeCode: string;
17
+ };
15
18
 
16
- type FeedTowerExtraInfo = {
17
- currentAmount: number
18
- loadAmount: number
19
- feedTowerAmount: number
20
- feedTowerCode: string
21
- id: number
22
- surplus_amount: number
23
- }
19
+ export type FeedTowerExtraInfo = {
20
+ currentAmount: number;
21
+ loadAmount: number;
22
+ feedTowerAmount: number;
23
+ feedTowerCode: string;
24
+ id: number;
25
+ surplus_amount: number;
26
+ };
24
27
 
25
- type FodderOrder = {
26
- id?: number
27
- fodderId?: string
28
- fodderAmount: number
29
- fodderCode: string
30
- createTime: string
31
- status: string
32
- fodderUnit: string
33
- truckCode?: string
34
- date?: string
35
- type?: string
36
- }
37
- //录入
38
- type FodderAdd = {
39
- car: string //车牌号
40
- date: string //日期
41
- source: string //来源
42
- factoryCode: string //当前工厂编号
43
- results: FodderUpdate[]
44
- }
45
- type FodderUpdate = {
46
- farm: string //工厂名称
47
- fodderCode: string //料号
48
- towers: Fodder[]
49
- }
28
+ //FodderOrder 料单信息
29
+ export type FodderOrder = {
30
+ id?: number;
31
+ fodderId?: string;
32
+ fodderAmount: number;
33
+ fodderCode: string;
34
+ createTime: string;
35
+ status: string;
36
+ fodderUnit: string;
37
+ truckCode?: string;
38
+ date?: string;
39
+ type?: string;
40
+ };
41
+ //
42
+ export type FodderOrderList = {
43
+ car: string; //车牌号
44
+ date: string; //日期
45
+ source: string; //来源
46
+ factoryCode: string; //当前工厂编号
47
+ results: FodderOrderInFactory[];
48
+ };
49
+ type FodderOrderInFactory = {
50
+ farm: string; //工厂名称
51
+ fodderCode: string; //料号
52
+ towers: FodderOrderInTower[];
53
+ };
50
54
 
51
- type Fodder = {
52
- name: string //料塔名称
53
- unit: string //单位
54
- weight: number //重量
55
- }
55
+ export type FodderOrderInTower = {
56
+ name: string; //料塔名称
57
+ unit: string; //单位
58
+ weight: number; //重量
59
+ };
56
60
 
57
- type Shzx = {
58
- user: UserGateway
59
- ready: Promise<MyAxiosInstance>
60
- axios: MyAxiosInstance
61
- _getSelectedFarmFromMiniApp: () => Promise<Factory>
62
- _getSelectedUnitFromMiniApp: () => Promise<Unit>
63
- mode: string
64
- }
65
61
  type Assistant = {
66
- info: String
67
- status: String
68
- name: String
69
- id: Number
70
- }
71
- type Factory = {
72
- code: string
73
- name?: string
74
- company_code: string
75
- id?: number
76
- }
62
+ info: String;
63
+ status: String;
64
+ name: String;
65
+ id: Number;
66
+ };
67
+ export type Factory = {
68
+ code: string;
69
+ name?: string;
70
+ company_code: string;
71
+ id?: number;
72
+ };
77
73
  type Unit = {
78
- name?: string
79
- id?: number
80
- type?: string
81
- workshopName?:string
82
- }
74
+ name?: string;
75
+ id?: number;
76
+ type?: string;
77
+ workshopName?: string;
78
+ };
83
79
 
84
80
  type Electricity = {
85
- deviceId: string
86
- nextDeviceList: number[] | []
87
- power: number
88
- }
81
+ deviceId: string;
82
+ nextDeviceList: number[] | [];
83
+ power: number;
84
+ };
89
85
 
90
86
  type Camera = {
91
- id: number
92
- name: string
87
+ id: number;
88
+ name: string;
89
+ };
90
+
91
+ export type FeedTower = {
92
+ id: number;
93
+ name: string;
94
+ currentAmount: number;
95
+ lastZeroTime: string;
96
+ cycle?: number;
97
+ "pg.feedtower.status"?: string;
98
+ "pg.feedtower.near-zero"?: string;
99
+ deviation: number;
100
+ towerVolume: number;
101
+ unit: string;
102
+ unitId: number;
103
+ deviceId: number;
104
+ fodderUnit: string;
105
+ fodderCode: string;
106
+ diff: number;
107
+ };
108
+
109
+ export interface MyAxiosInstance {
110
+ get: (url: string, config?: any) => Promise<any>;
111
+ post: (url: string, data?: any, config?: any) => Promise<any>;
93
112
  }
94
113
 
95
- interface MyAxiosInstance {
96
- get: (url: string, config?: any) => Promise<any>
97
- post: (url: string, data?: any, config?: any) => Promise<any>
114
+ export interface Assistant {
115
+ info: String;
116
+ status: Number;
117
+ name: String;
118
+ id: Number;
119
+ }
120
+ export interface ElectricMeter extends Device {
121
+ coefficient: number;
122
+ status: string;
123
+ }
124
+
125
+ export class Cpzxrobot {
126
+ transport: TransportGateway;
127
+ ready: Promise<MyAxiosInstance>;
128
+ factory: FactoryGateway;
129
+ user: UserGateway;
130
+ device: DeviceGateway;
131
+ mode: string;
132
+ axios: MyAxiosInstance;
133
+ _getSelectedFarmFromMiniApp: () => Promise<Factory>;
134
+ _getSelectedUnitFromMiniApp: () => Promise<Unit>;
135
+ saveBase64: Function = (base64: string, filename: string) => {};
136
+ saveBlob: Function = (blob: Blob, filename: string) => {};
98
137
  }
99
138
 
100
139
  declare global {
101
- let getAuth: () => string
140
+ let getAuth: () => string;
141
+ }
142
+
143
+ declare module "@cpzxrobot/sdk" {
144
+ export default function (
145
+ args: {
146
+ devAuth: string;
147
+ baseURL: string;
148
+ appCode: string;
149
+ selectedFarm: Factory;
150
+ selectedUnit: Unit;
151
+ } = {
152
+ devAuth: "",
153
+ appCode: "",
154
+ baseURL: "https://www.cpzxrobot.com/",
155
+ selectedFarm: {
156
+ id: 0,
157
+ code: "",
158
+ name: "",
159
+ company_code: "",
160
+ },
161
+ selectedUnit: {
162
+ id: 0,
163
+ name: "",
164
+ },
165
+ }
166
+ ): Cpzxrobot;
167
+ export {
168
+ Cpzxrobot,
169
+ Factory,
170
+ Unit,
171
+ Device,
172
+ FeedTower,
173
+ FodderOrder,
174
+ FodderOrderList,
175
+ FodderOrderInFactory,
176
+ FodderOrderInTower,
177
+ MyAxiosInstance,
178
+ Assistant,
179
+ ElectricMeter,
180
+ Camera,
181
+ FeedTowerExtraInfo,
182
+ Electricity,
183
+ };
102
184
  }
package/user_gateway.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { Cpzxrobot, Factory, Unit } from "./types";
2
+
1
3
  export class UserGateway extends Object {
2
4
  _selectedFarm: Factory = {
3
5
  code: "",
@@ -5,19 +7,19 @@ export class UserGateway extends Object {
5
7
  company_code: "",
6
8
  id: 0,
7
9
  };
8
- context: Shzx;
10
+ context: Cpzxrobot;
9
11
  _selectedUnit: Unit = {
10
12
  name: "",
11
13
  id: 0,
12
14
  };
13
15
 
14
- constructor(context: Shzx) {
16
+ constructor(context: Cpzxrobot) {
15
17
  super();
16
18
  this.context = context;
17
19
  }
18
20
 
19
21
  //获得当前用户选择的工厂
20
- getSelectedFarm() {
22
+ public getSelectedFarm(): Promise<Factory> {
21
23
  return new Promise<Factory>((resolve, reject) => {
22
24
  switch (this.context.mode) {
23
25
  case "dev":