1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { Controller, Get, Param } from '@nestjs/common';
17import { AppService } from './app.service';
18import { executeCommand, logger } from './util';
19import { ApiOperation, ApiTags } from '@nestjs/swagger';
20
21@ApiTags('获取设备SN')
22@Controller()
23export class AppController {
24  constructor(private readonly appService: AppService) { }
25
26  @ApiOperation({
27    summary: '获取设备SN'
28  })
29  @Get('sn')
30  async findAll(): Promise<string> {
31    try {
32      let sn = await executeCommand('hdc list targets');
33      sn = sn.slice(1).slice(0, sn.length - 6);
34      if (sn.indexOf('Empty') !== -1) {
35        logger(AppController.name).log(`获取设备号失败,当前终端没有连接设备`);
36        return JSON.stringify({
37          status: 'failed',
38          sn: '',
39          msg: '当前终端没有连接设备'
40        });
41      };
42      logger(AppController.name).log(`获取设备号成功 ` + sn);
43      return JSON.stringify({
44        status: 'success',
45        sn,
46        msg: '获取设备号成功'
47      });
48    } catch (error) {
49      logger(AppController.name).log(`获取设备号失败 ` + JSON.stringify(error));
50      return JSON.stringify({
51        status: 'failed',
52        sn: '',
53        msg: `获取设备号失败 ` + JSON.stringify(error)
54      });
55    };
56  }
57}
58