Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mscreenlocal
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
jenny
mscreenlocal
Commits
ea52f7d0
Commit
ea52f7d0
authored
Jun 15, 2015
by
jenny
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加注册流程
运行命令:node bin/index.js
parent
4ebb9d26
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
159 additions
and
1 deletions
+159
-1
bin/index.js
+154
-0
package.json
+5
-1
No files found.
bin/index.js
0 → 100644
View file @
ea52f7d0
/**
* Created by CJLIU on 2015/6/6.
*/
var
request
=
require
(
'request'
);
var
fs
=
require
(
'fs'
);
var
crypto
=
require
(
'crypto'
);
var
_
=
require
(
'underscore'
);
//请求重试次数
var
retryTimes
=
3
;
//请求重试间隔时间
var
retryTimeout
=
2000
;
//用户信息文件
var
userFile
=
"../user.json"
;
//api请求地址前缀
var
apiURL
=
'http://127.0.0.1:3003/api/'
;
start
();
/**
* 启动
*/
function
start
(){
checkFile
(
function
(
file
){
var
clientId
;
if
(
!
file
||
!
file
.
clientId
){
//设置唯一ID
clientId
=
crypto
.
createHash
(
'md5'
).
update
(
Date
.
now
().
toString
()).
digest
(
"hex"
);
register
(
clientId
);
}
else
if
(
!
file
.
boxId
||
!
file
.
secretKey
){
clientId
=
file
.
clientId
;
register
(
clientId
);
}
else
if
(
!
file
.
accessId
){
activate
(
file
.
boxId
,
file
.
secretKey
);
}
else
{
require
(
'../app'
);
}
});
}
/**
* 注册
* @param clientId 设备唯一ID
*/
function
register
(
clientId
){
//发送注册请求
var
options
=
{
uri
:
apiURL
+
'register'
,
method
:
'POST'
,
json
:{
"clientId"
:
clientId
}
};
goRegister
();
function
goRegister
(){
_request
(
options
,
function
(
err
,
body
){
if
(
!!
err
){
console
.
error
(
retryTimes
,
err
);
if
(
retryTimes
<
1
){
return
;
}
else
{
retryTimes
--
;
setTimeout
(
function
(){
goRegister
();},
retryTimeout
);
}
}
else
{
var
user
=
{
'clientId'
:
clientId
,
'boxId'
:
body
.
msg
.
boxId
,
'secretKey'
:
body
.
msg
.
secretKey
}
fs
.
writeFileSync
(
userFile
,
JSON
.
stringify
(
user
,
null
,
2
),
'utf8'
);
console
.
log
(
"请记录boxID:"
,
body
.
msg
.
boxId
,
"secretKey:"
,
body
.
msg
.
secretKey
);
console
.
log
(
"请去激活,谢谢"
);
}
});
}
}
/**
* 激活
* @param boxId
* @param secretKey
*/
function
activate
(
boxId
,
secretKey
){
var
options
=
{
//是否已注册
uri
:
apiURL
+
'signin'
,
method
:
'POST'
,
json
:{
'boxId'
:
boxId
,
'secretKey'
:
secretKey
}
};
goActivate
();
function
goActivate
(){
_request
(
options
,
function
(
err
,
body
){
if
(
!!
err
){
console
.
error
(
retryTimes
,
err
);
if
(
retryTimes
<
1
){
return
;
}
else
{
retryTimes
--
;
setTimeout
(
function
(){
goActivate
();},
retryTimeout
);
}
}
else
{
var
user
=
require
(
userFile
);
_
.
extend
(
user
,
{
'accessId'
:
body
.
msg
.
accessId
});
fs
.
writeFileSync
(
userFile
,
JSON
.
stringify
(
user
,
null
,
2
),
'utf8'
);
start
();
}
});
}
}
/**
* 检查文件
* @param cb
*/
function
checkFile
(
cb
){
fs
.
exists
(
userFile
,
function
(
exists
)
{
if
(
exists
)
{
//需要验证json格式 参考
try
{
var
user
=
require
(
userFile
);
}
catch
(
e
)
{
console
.
log
(
'is not json'
);
cb
(
null
);
return
;
}
cb
(
user
);
}
else
{
cb
(
null
);
}
});
}
/**
* request
* @param options
* @param cb
* @private
*/
function
_request
(
options
,
cb
){
request
(
options
,
function
(
err
,
res
,
body
)
{
if
(
!
err
&&
res
.
statusCode
==
200
)
{
if
(
body
.
code
==
200
){
cb
(
null
,
body
);
}
else
{
cb
(
body
.
code
,
null
);
}
}
else
{
cb
(
err
,
null
);
}
});
}
\ No newline at end of file
package.json
View file @
ea52f7d0
...
@@ -7,6 +7,10 @@
...
@@ -7,6 +7,10 @@
},
},
"dependencies"
:
{
"dependencies"
:
{
"
express
"
:
"3.9.0"
,
"
express
"
:
"3.9.0"
,
"
pty
"
:
"0.0.0"
"
pty
"
:
"0.0.0"
,
"
request
"
:
"~2.57.0"
,
"
moment
"
:
"~2.10.3"
,
"
underscore
"
:
"~1.8.3"
,
"
mqtt
"
:
"1.1.2"
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment