| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- import {getToken} from './token'
- import { MyMqttManager } from '@/uni_modules/flight-mqtt';
-
- export class Onenet {
- url = "tcp://mqtts.heclouds.com:1883"
- productId = ""
- deviceName = ""
- secretKey = ""
- userId = "" //用户id
- userSecretKey = "" //用户 访问秘钥
- mqtt = null
- dataId = 1 // 上传数据时的编号
-
- topics = {} //主题及对应的处理方法
-
- /**
- * @param {string} pid 产品Id
- * @param {string} dn 设备名称
- * @param {string} sk 产品秘钥
- * @param {string} usk 用户账号访问秘钥
- * @param {string} url onenet 链接服务器地址, 默认为"tcp://mqtts.heclouds.com:1883"
- */
- constructor(pid, dn, sk, usk, uid, url){
- if(!!url){
- this.url = url
- }
-
- this.productId = pid
- this.deviceName = dn
- this.secretKey = sk
- this.userSecretKey = usk
- this.userId = uid
- }
-
- /**
- * @param {Object} topic 接收到的publish 主题
- * @param {Object} content 接收到的publish 载荷
- */
- receiveProcess(topic, content){
- console.log(topic, content)
- }
-
- connectParam(pid, dn, sk) {
- const res = `products/${pid}/devices/${dn}`;
- const token = getToken(res, "sha1", sk)
-
- return {
- clientId: dn,
- username: pid,
- password: token
- }
- }
-
- connectMqttServer(){
- const mqtt = new MyMqttManager()
- mqtt.on(this.receiveProcess)
-
- let param = this.connectParam(this.productId, this.deviceName, this.secretKey)
- mqtt.connect(this.url, param.clientId, param.username, param.password);
- mqtt.initCallback();
- this.mqtt = mqtt
- }
-
- subscript(type, fn){
- this.mqtt.subscript(this.generateTopic(type))
- this.topics[type] = fn
- }
-
- publishRealData(fn){
- let data = fn()
- let real = {
- "id": (this.dataId++).toString(),
- "version": "1.0",
- "params": {}
- }
-
- if(typeof data === "string") {
- try{
- data = JSON.parse(data)
- }catch(e){
- //TODO handle the exception
- console.log("传入数据格式错误")
- return
- }
- }
- const keys = Object.keys(data);
-
- const timestamp = Date.now();
- keys.forEach((key) => {
- const value = data[key];
-
- real.params[key] = {
- time: timestamp,
- value: value
- }
- });
-
- this.mqtt.publish(this.generateTopic("property"), JSON.stringify(real))
- }
-
- generateTopic(type) {
- switch(type) {
- case "property":
- return `$sys/${this.productId}/${this.deviceName}/thing/property/post`
- case 'propertyRespond':
- return `$sys/${this.productId}/${this.deviceName}/thing/property/post/reply`
- case "setProperty":
- return `$sys/${this.productId}/${this.deviceName}/thing/property/set`
- case 'setPropertyRespond':
- return `$sys/${this.productId}/${this.deviceName}/thing/property/set/reply`
- default:
- return `$sys/${this.productId}/${this.deviceName}/thing/property/get`
- }
- }
-
-
- apiParam(uid, usk) {
- const res = `userid/${uid}`;
- const token = getToken(res, "sha1", usk)
-
- return token
- }
-
- uploadFile(file) {
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: 'https://iot-api.heclouds.com/device/file-upload',
- filePath: file,
- name: "file",
- formData: {
- imei: this.deviceName,
- device_name: this.deviceName,
- product_id: this.productId
- },
- header:{
- authorization: this.apiParam(this.userId, this.userSecretKey)
- },
- success: uploadFileRes => {
- if(uploadFileRes.statusCode == 200) {
- // getApp().$loger.log('上传成功', uploadFileRes, file)
- console.log('上传成功', uploadFileRes, file)
-
- resolve(uploadFileRes)
- }
- else
- {
- // getApp().$loger.log('上传失败', uploadFileRes, file)
- console.log('上传失败', uploadFileRes, file)
-
- reject(Error(uploadFileRes.errMsg))
- }
- },
- fail: err => {
- // getApp().$loger.log('上传失败', err);
- console.log('上传失败', err);
-
- reject(err)
- },
- complete: console.log
- });
- })
- }
- }
|