@cpzxrobot/sdk 1.0.0

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.
@@ -0,0 +1,253 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deviceFilter = void 0;
4
+ class deviceFilter {
5
+ // private getDetail!: (info: T, id: number, context: Shzx) => Promise<any> | null
6
+ // axios: MyAxiosInstance;
7
+ // user: UserGateway;
8
+ // ready: Promise<boolean>;
9
+ constructor(context) {
10
+ this.devices = [];
11
+ this.context = context;
12
+ }
13
+ //如果子类有更多其他属性需要附加到属性列表中,需要实现该方法
14
+ getDetail(info, _id) {
15
+ return Promise.resolve(info);
16
+ }
17
+ //如果子类需要对返回的数据进行包装,需要实现该方法
18
+ wrapData(_id, _args, p) {
19
+ return p;
20
+ }
21
+ wrapList(list) {
22
+ return Promise.resolve(list);
23
+ }
24
+ getInfo(id) {
25
+ if (this.devices) {
26
+ const one = this.devices.find((device) => device.id == id);
27
+ if (one) {
28
+ return Promise.resolve(one);
29
+ }
30
+ }
31
+ return this.context.axios
32
+ .get(`/api/v1/device/${id}/detail?latest=true`)
33
+ .then((res) => {
34
+ if (res.data.Error) {
35
+ throw res.data.Error;
36
+ }
37
+ return res.data;
38
+ });
39
+ }
40
+ //获得单个设备的详细信息和状态,包括设备的基本信息和跟业务类型相关的附加状态信息
41
+ //不应单独使用getInfo和getDetail,应使用getStatus
42
+ getStatus(id) {
43
+ return this.context.ready
44
+ .then(() => this.getInfo(id))
45
+ .then((info) => {
46
+ return this.getDetail(info, id);
47
+ });
48
+ }
49
+ // 标记设备状态,部分类型的设备支持标记状态,比如喂料塔可以标记为零点已校准
50
+ markStatus(id, name) {
51
+ let action;
52
+ switch (name) {
53
+ case "zero-calibrated": // 标记为零点已校准状态
54
+ action = "/updateZeroTime";
55
+ break;
56
+ default:
57
+ throw new Error("Method not implemented.");
58
+ }
59
+ return this.context.ready.then(() => this.context.axios
60
+ .post("/api/v1/pigfarm/device/status" + action + "?id=" + id)
61
+ .then((res) => {
62
+ if (res.data.code != 200) {
63
+ throw res.data.message;
64
+ }
65
+ return res.data;
66
+ }));
67
+ }
68
+ update(deviceId, cycle, towerVolume) {
69
+ return this.context.ready.then(() => {
70
+ this.context.axios
71
+ .post("/api/v1/pigfarm/device/status/update", {
72
+ deviceId,
73
+ cycle,
74
+ towerVolume,
75
+ })
76
+ .then((res) => {
77
+ if (res.data.code != 200) {
78
+ throw res.data.message;
79
+ }
80
+ return res.data;
81
+ });
82
+ });
83
+ }
84
+ getData(id, args = {
85
+ start: "",
86
+ stop: "",
87
+ type: "diffPerDay", // diffPerDay, sumPerDay, latest
88
+ }) {
89
+ if (args.stop == "") {
90
+ args.stop = this.formateDateToYYYYMMDD(new Date());
91
+ }
92
+ if (args.start == "") {
93
+ const sevenDaysAgo = new Date();
94
+ sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
95
+ args.start = this.formateDateToYYYYMMDD(sevenDaysAgo);
96
+ }
97
+ return this.context.ready
98
+ .then(() => {
99
+ const p1 = this.context.axios.get(`/api/v1/device/${id}/data/${args.type}`, {
100
+ params: args,
101
+ });
102
+ return this.wrapData(id, args, p1);
103
+ })
104
+ .then((res) => {
105
+ if (res.data.Error) {
106
+ throw res.data.Error;
107
+ }
108
+ // this.devices.push(res.data);
109
+ return res.data;
110
+ });
111
+ }
112
+ //按月获取数据:period = '1mo',按天获取数据:period = '1d'
113
+ //获取数据类型:latestInRangePerDay(按周期获得每天的数据,只用于峰谷平)
114
+ // 不再使用:diffPerDay(每天差值),sumPerDay(每日汇总),last(每个周期最新)
115
+ // difference(每个周期差值,根据period的不同,差值的计算方式不同
116
+ getHistoryDatas(ids, args = {
117
+ start: "",
118
+ stop: "",
119
+ period: null,
120
+ type: "latestInRangePerDay",
121
+ }) {
122
+ // 对period = '1mo'的情况, 进行特殊优化
123
+ if (args.period == "1mo") {
124
+ //按start获得每个月
125
+ let d;
126
+ if (args.start && args.start[0] == "-") {
127
+ //args.start like '-1y'
128
+ const matcher = args.start.match(/-(\d+)(\w+)/);
129
+ if (matcher) {
130
+ d = new Date();
131
+ const num = parseInt(matcher[1]);
132
+ const unit = matcher[2];
133
+ if (unit == "y") {
134
+ d.setFullYear(d.getFullYear() - num);
135
+ }
136
+ else if (unit == "m") {
137
+ d.setMonth(d.getMonth() - num);
138
+ }
139
+ else if (unit == "d") {
140
+ d.setDate(d.getDate() - num);
141
+ }
142
+ }
143
+ }
144
+ else {
145
+ //args.start is a date string
146
+ d = new Date(args.start);
147
+ }
148
+ //获取从d开始,到现在的每个月的数据
149
+ const now = new Date();
150
+ const months = [];
151
+ while (d < now) {
152
+ months.push(this.formateDateToYYYYMM(d));
153
+ d.setMonth(d.getMonth() + 1);
154
+ }
155
+ return this.context.ready.then((axios) => {
156
+ const allPs = ids.map((id) => {
157
+ const promises = months.map((month) => {
158
+ return axios
159
+ .get(`/api/v1/device/history/monthly/${month}/${args.type}/${id}`)
160
+ .then((res) => {
161
+ return {
162
+ month: month,
163
+ id: id,
164
+ data: res.data,
165
+ };
166
+ });
167
+ });
168
+ return Promise.all(promises).then((res) => {
169
+ return res.reduce((acc, cur) => {
170
+ const singleIds = acc[cur.id] || {};
171
+ const values = Object.values(cur.data);
172
+ if (values.length > 0) {
173
+ singleIds[cur.month] = values[0];
174
+ }
175
+ acc[cur.id] = singleIds;
176
+ return acc;
177
+ }, {});
178
+ });
179
+ }, {});
180
+ return Promise.all(allPs).then((res) => {
181
+ const data = res.reduce((acc, cur) => {
182
+ return Object.assign(Object.assign({}, acc), cur);
183
+ }, {});
184
+ return {
185
+ data,
186
+ };
187
+ });
188
+ });
189
+ }
190
+ return this.context.ready.then((axios) => {
191
+ return axios.post("/api/v1/device/history", Object.assign({ device_ids: ids }, args));
192
+ });
193
+ }
194
+ formateDateToYYYYMMDD(time) {
195
+ const year = time.getFullYear();
196
+ const month = time.getMonth() + 1; // getMonth() 返回的月份从0开始
197
+ const date = time.getDate();
198
+ // 使用 `padStart` 方法来确保月份和日期总是两位数
199
+ return `${year}-${String(month).padStart(2, "0")}-${String(date).padStart(2, "0")}`;
200
+ }
201
+ formateDateToYYYYMM(time) {
202
+ const year = time.getFullYear();
203
+ const month = time.getMonth() + 1; // getMonth() 返回的月份从0开始
204
+ // const date = time.getDate()
205
+ // 使用 `padStart` 方法来确保月份和日期总是两位数
206
+ return `${year}-${String(month).padStart(2, "0")}`;
207
+ }
208
+ //获取设备列表,如果options.id为空,则获取用户选中工厂的设备列表,否则获取指定id的工厂的设备
209
+ //last:获取最新数据
210
+ // rangeToday:获取今天的第一个数据和最后一个数据
211
+ list(options = undefined) {
212
+ // if (this.devices.length > 0) {
213
+ // return Promise.resolve(this.devices)
214
+ // } else {
215
+ let p;
216
+ if (options === null || options === void 0 ? void 0 : options.id) {
217
+ p = Promise.resolve({ id: options.id });
218
+ }
219
+ else {
220
+ p = this.context.ready.then(() => {
221
+ return this.context.user.getSelectedFarm();
222
+ });
223
+ }
224
+ const rand = Math.random();
225
+ const type = this.deviceType;
226
+ return p
227
+ .then((farm) => {
228
+ console.log("type", type);
229
+ return this.context.axios.get("/api/v1/device/list?rand=" + rand, {
230
+ params: {
231
+ location: farm,
232
+ type: this.deviceType,
233
+ unit: options === null || options === void 0 ? void 0 : options.unit,
234
+ data: options === null || options === void 0 ? void 0 : options.data,
235
+ status: options === null || options === void 0 ? void 0 : options.status,
236
+ },
237
+ });
238
+ })
239
+ .then((res) => {
240
+ if (res.data.Error) {
241
+ throw res.data.Error;
242
+ }
243
+ else {
244
+ if (options == undefined || options.id == undefined) {
245
+ //当id为空时(获取用户选中工厂的设备),将所有设备保存到devices中
246
+ this.devices = res.data;
247
+ }
248
+ return res.data;
249
+ }
250
+ });
251
+ }
252
+ }
253
+ exports.deviceFilter = deviceFilter;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeviceGateway = void 0;
4
+ const device_type_gateway_1 = require("./device_type_gateway");
5
+ const feedtower_1 = require("./device_types/feedtower");
6
+ const electricmeter_1 = require("./device_types/electricmeter");
7
+ const normal_type_1 = require("./device_types/normal_type");
8
+ //获得设备数据的网关,所有设备类型的数据都通过这个网关获取, 常见的设备直接通过属性访问,特殊的设备通过子属性访问
9
+ //例如
10
+ // shzx.device.feedTower: 料塔设备的数据获取网关
11
+ // shzx.device.electricMeter:电表设备的数据获取网关
12
+ class DeviceGateway extends Object {
13
+ constructor(context) {
14
+ super();
15
+ this.context = context;
16
+ // this.devices = []
17
+ this.type = new device_type_gateway_1.DeviceTypeGateway(context);
18
+ this.normalFilter = new normal_type_1.NormalGateway(context);
19
+ this.filters = new Map();
20
+ }
21
+ //料塔设备的数据获取网关
22
+ get feedTower() {
23
+ return this.getFilter("FeedTower", feedtower_1.FeedTowerGateway);
24
+ }
25
+ //电表设备的数据获取网关
26
+ get electricMeter() {
27
+ return this.getFilter("ElectricMeter", electricmeter_1.ElectricMeterGateway);
28
+ }
29
+ getFilter(type, cls) {
30
+ if (this.filters.has(type)) {
31
+ return this.filters.get(type);
32
+ }
33
+ const filter = new cls(this.context);
34
+ this.filters.set(type, filter);
35
+ return filter;
36
+ }
37
+ // addDevice(device: Device) {
38
+ // this.devices.push(device)
39
+ // }
40
+ // removeDevice(device: Device) {
41
+ // const index = this.devices.indexOf(device)
42
+ // if (index !== -1) {
43
+ // this.devices.splice(index, 1)
44
+ // }
45
+ // }
46
+ // getDevices() {
47
+ // return this.devices
48
+ // }
49
+ //获取设备列表,注意特殊类型,比如electricMeter和feedTower,需要通过属性访问
50
+ get list() {
51
+ return this.normalFilter.list;
52
+ }
53
+ get getHistoryDatas() {
54
+ return this.normalFilter.getHistoryDatas;
55
+ }
56
+ }
57
+ exports.DeviceGateway = DeviceGateway;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeviceTypeGateway = void 0;
4
+ //缓存设备类型数据
5
+ class DeviceTypeGateway {
6
+ constructor(context) {
7
+ this.context = context;
8
+ }
9
+ list() {
10
+ if (this.deviceTypes) {
11
+ return Promise.resolve(this.deviceTypes);
12
+ }
13
+ return this.context.axios.get('/api/device_type/list');
14
+ }
15
+ alarmConfigForUnit(unit) {
16
+ return this.context.ready.then((axios) => {
17
+ return axios.get(`/api/v1/device/type/alarm`, {
18
+ params: {
19
+ unit_type: unit.type
20
+ }
21
+ });
22
+ });
23
+ }
24
+ }
25
+ exports.DeviceTypeGateway = DeviceTypeGateway;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ElectricMeterGateway = void 0;
4
+ const device_filter_1 = require("../device_filter");
5
+ class ElectricMeterGateway extends device_filter_1.deviceFilter {
6
+ // Add properties and methods specific to the FeedTower type here
7
+ get deviceType() {
8
+ return "Electric";
9
+ }
10
+ //获取能耗关系
11
+ getEnergyRelation() {
12
+ return this.context.ready.then(() => {
13
+ return this.context.axios.get("/api/v1/energy/flow ").then((res) => {
14
+ if (res.data.code != 200) {
15
+ throw res.data.message;
16
+ }
17
+ return res.data;
18
+ });
19
+ });
20
+ }
21
+ }
22
+ exports.ElectricMeterGateway = ElectricMeterGateway;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FeedTowerGateway = void 0;
4
+ const device_filter_1 = require("../device_filter");
5
+ class FeedTowerGateway extends device_filter_1.deviceFilter {
6
+ // Add properties and methods specific to the FeedTower type here
7
+ get deviceType() {
8
+ return 'FeedTower';
9
+ }
10
+ getDetail(info, id) {
11
+ return this.context.axios
12
+ .post('/api/v1/pigfarm/device/status/detail?deviceId=' + id)
13
+ .then((res) => {
14
+ if (res.data.code != 200) {
15
+ throw res.data.message;
16
+ }
17
+ res.data.data.unit = info.unit;
18
+ return res.data.data;
19
+ });
20
+ }
21
+ wrapData(id, args, p1) {
22
+ const ps = [p1];
23
+ if (!args.ingoreInput) {
24
+ ps.push(this.context.axios.post(`/api/v1/pigfarm/device/fodder`, {
25
+ device: id,
26
+ times: [args.start, args.stop]
27
+ }));
28
+ }
29
+ return Promise.all(ps).then((res) => {
30
+ if (res[0].data.Error) {
31
+ throw res[0].data.Error;
32
+ }
33
+ else {
34
+ if (!args.ingoreInput) {
35
+ res[1].data.data.list.forEach((item) => {
36
+ res[0].data[item.date] += item.data;
37
+ });
38
+ }
39
+ return res[0].data;
40
+ }
41
+ });
42
+ }
43
+ wrapList(list) {
44
+ const ids = this.devices.map((device) => device.id);
45
+ //TODO 其他设备的详情需要支持,目前只有喂料塔
46
+ return this.context.axios
47
+ .post('/api/v1/pigfarm/device/status/list', ids)
48
+ .then((res) => {
49
+ if (res.data.code != 200) {
50
+ throw res.data.message;
51
+ }
52
+ else {
53
+ res.data.data.forEach((device) => {
54
+ const found = list.find((d) => d.id === device.id);
55
+ if (found) {
56
+ Object.assign(found, device);
57
+ }
58
+ });
59
+ return list;
60
+ }
61
+ });
62
+ }
63
+ async enter(data) {
64
+ const res = await this.context.axios.post('/api/v1/pigfarm/device/fodder/import', data);
65
+ return res;
66
+ }
67
+ async getCar(truck) {
68
+ const res = await this.context.axios.get('/api/v1/pigfarm/car', {
69
+ params: {
70
+ truckCode: truck,
71
+ random: 1
72
+ }
73
+ });
74
+ return res.data;
75
+ }
76
+ async getCarList() {
77
+ const res = await this.context.axios.get('/api/v1/pigfarm/car/list');
78
+ return res.data;
79
+ }
80
+ async updateZeroTime(id) {
81
+ const res = await this.context.axios.post('/api/v1/pigfarm/device/status/updateZeroTime' + '?id=' + id);
82
+ return res;
83
+ }
84
+ }
85
+ exports.FeedTowerGateway = FeedTowerGateway;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NormalGateway = void 0;
4
+ const device_filter_1 = require("../device_filter");
5
+ //适配常见设备类型
6
+ class NormalGateway extends device_filter_1.deviceFilter {
7
+ get deviceType() {
8
+ return null;
9
+ }
10
+ }
11
+ exports.NormalGateway = NormalGateway;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EnergyGateway = void 0;
4
+ const electric_meter_gateway_1 = require("./energy_types/electric_meter_gateway");
5
+ //消息助手网关,获取消息助手的信息
6
+ class EnergyGateway extends Object {
7
+ constructor(context) {
8
+ super();
9
+ this._electric = null;
10
+ this.context = context;
11
+ }
12
+ get electricMeter() {
13
+ if (this._electric) {
14
+ return this._electric;
15
+ }
16
+ return new electric_meter_gateway_1.ElectricMeterGateway(this.context);
17
+ }
18
+ }
19
+ exports.EnergyGateway = EnergyGateway;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ElectricMeterGateway = void 0;
7
+ const __1 = __importDefault(require(".."));
8
+ //消息助手网关,获取消息助手的信息
9
+ class ElectricMeterGateway extends Object {
10
+ constructor(context) {
11
+ super();
12
+ this.context = context;
13
+ }
14
+ //返回峰谷平电表的阶梯区间
15
+ getRates() {
16
+ return (0, __1.default)().ready.then((axios) => {
17
+ return axios.get('/api/v1/energy/electricRange').then((res) => {
18
+ if (res.data.code !== 200) {
19
+ throw res.data.message;
20
+ }
21
+ const list = res.data.data;
22
+ list.forEach((item) => {
23
+ item.endMin || (item.endMin = 0);
24
+ item.startMin || (item.startMin = 0);
25
+ });
26
+ return list;
27
+ });
28
+ });
29
+ }
30
+ }
31
+ exports.ElectricMeterGateway = ElectricMeterGateway;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FactoryGateway = void 0;
4
+ class FactoryGateway extends Object {
5
+ constructor(context) {
6
+ super();
7
+ this._selectedFarm = {
8
+ code: '',
9
+ name: '',
10
+ company_code: '',
11
+ id: 0
12
+ };
13
+ this.context = context;
14
+ }
15
+ async search(data) {
16
+ await this.context.ready;
17
+ const response = await this.context.axios.post('/api/v1/factory/search', data);
18
+ return response.data;
19
+ }
20
+ //获得工厂的单元信息
21
+ async unit(id) {
22
+ var axios = await this.context.ready;
23
+ const response = await axios.get(`/api/v1/workshop/unit/${id}`);
24
+ return response.data;
25
+ }
26
+ //获得工厂的车间信息
27
+ async workshop(id) {
28
+ var axios = await this.context.ready;
29
+ const response = await axios.get(`/api/v1/workshop/${id}`);
30
+ return response.data;
31
+ }
32
+ //获得工厂的所有车间信息
33
+ async workshops(id) {
34
+ var axios = await this.context.ready;
35
+ const response = await axios.get(`/api/v1/factory/${id}/workshops`);
36
+ return response.data;
37
+ }
38
+ //获得工厂的所有单元信息,以车间为单位树状结构
39
+ async units(id) {
40
+ var axios = await this.context.ready;
41
+ const response = await axios.get(`/api/v1/factory/${id}/units`);
42
+ return response.data;
43
+ }
44
+ async company(data) {
45
+ await this.context.ready;
46
+ const response = await this.context.axios.get(`/api/v1/factory/${data.id}/organizaion`, data);
47
+ return response.data;
48
+ }
49
+ }
50
+ exports.FactoryGateway = FactoryGateway;