Last active
February 20, 2020 10:12
-
-
Save onurkagan/873f9a72dba87b8d79f416e4f4b6b76f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/md5" | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"strings" | |
"time" | |
) | |
const seqValue = "hades" | |
var ( | |
user = "shcadmin" | |
baseURL = "http://10.81.102.22" | |
port = "80" | |
) | |
// BasicOperationResponse ... | |
type BasicOperationResponse struct { | |
Seq string `json:"seq"` | |
StatusCode int `json:"status_code"` | |
Result string `json:"result"` | |
} | |
// ListOperationResponse ... | |
type ListOperationResponse struct { | |
Seq string `json:"seq"` | |
StatusCode int `json:"status_code"` | |
Result []Device `json:"result"` | |
} | |
// Device ... | |
type Device struct { | |
Name string `json:"name"` | |
ID string `json:"id"` | |
UDeviceID string `json:"udeviceid"` | |
DeviceType string `json:"devicetype"` | |
RoomID string `json:"roomid"` | |
Status string `json:"status"` | |
Pictrue string `json:"pic"` | |
Detail DeviceDetail `json:"details"` | |
} | |
// DeviceDetail ... | |
type DeviceDetail struct { | |
Profileid string `json:"profileid"` | |
Modelid string `json:"modelid"` | |
EpModeID string `json:"ep_mode_id"` | |
Ieee string `json:"ieee"` | |
Ep string `json:"ep"` | |
Nwkaddr string `json:"nwkaddr"` | |
PowerMode string `json:"power_mode"` | |
Manufacturer string `json:"manufacturer"` | |
CurrentPower int `json:"current_power"` | |
ZclVersion string `json:"zcl_version"` | |
AppVersion string `json:"app_version"` | |
StackVersion string `json:"stack_version"` | |
HwVersion string `json:"hw_version"` | |
VerDate string `json:"ver_date"` | |
ArmStatus string `json:"arm_status"` | |
BatteryLevel int `json:"battery_level"` | |
ZoneType string `json:"zone_type"` | |
HeartbeatPeriod int `json:"heartbeat_period"` | |
Enable int `json:"enable"` | |
Alarm1 int `json:"alarm1"` | |
Alarm2 int `json:"alarm2"` | |
WarnWay string `json:"warn_way"` | |
UpdateTime string `json:"update_time"` | |
Level int `json:"level"` | |
Power int `json:"power"` | |
Energy int `json:"energy"` | |
Voltage int `json:"voltage"` | |
Current int `json:"current"` | |
Temperature float64 `json:"temperature"` | |
Humidity int `json:"humidity"` | |
Lux int `json:"lux"` | |
Pm25 int `json:"pm2_5"` | |
O3 int `json:"o3"` | |
Co string `json:"co"` | |
No int `json:"no"` | |
No2 int `json:"no2"` | |
So2 int `json:"so2"` | |
Noise int `json:"noise"` | |
Sn string `json:"sn"` | |
VerifyCode string `json:"verify_code"` | |
NvrSn string `json:"nvr_sn"` | |
NvrVerifyCode string `json:"nvr_verify_code"` | |
NvrChannel string `json:"nvr_channel"` | |
UID string `json:"uid"` | |
IP string `json:"ip"` | |
Port int `json:"port"` | |
Username string `json:"username"` | |
Password string `json:"password"` | |
Flip int `json:"flip"` | |
Mirror int `json:"mirror"` | |
Windspeed string `json:"windspeed"` | |
Huminity float64 `json:"huminity"` | |
SettingTemperature float64 `json:"setting_temperature"` | |
} | |
func main() { | |
// AddDevice("00137A0000066235") // magnetic | |
// AddDevice("00137A00000660D1") // pendant | |
// AddDevice("00137A0000066460") // coh | |
//AddDevice("00137A0000066376") // smoke | |
//DeleteDevice("00137A00000660D101") // pendant | |
/* DeleteDevice("00137A000006623501") // coh | |
DeleteDevice("00137A00000660D101") | |
DeleteDevice("00137A000006637601") // smoke | |
DeleteDevice("00137A000006623501") // magnetic */ | |
//DeleteDevice("00137A00000660D101") | |
/* devices, getDevicesErr := GetDevices() | |
if getDevicesErr != nil { | |
fmt.Println("get err: ", getDevicesErr) | |
} | |
fmt.Println("device count: ", len(*(devices))) */ | |
devices, getDevicesErr := GetDevicesWithDetail() | |
if getDevicesErr != nil { | |
fmt.Println("get err: ", getDevicesErr) | |
} | |
fmt.Println("Device : ", len((*devices))) | |
} | |
// AddDevice ... | |
func AddDevice(ieee string) error { | |
var operationResponse BasicOperationResponse | |
data := make(map[string]interface{}) | |
data["devicetype"] = "1000001" | |
data["ieee"] = ieee | |
data["op"] = "adddev" | |
dataByte, marshalErr := json.Marshal(data) | |
if marshalErr != nil { | |
fmt.Println("marsh err", marshalErr) | |
} | |
sec := time.Now().UnixNano() / int64(time.Millisecond) | |
req := fmt.Sprintf("data=%s&seq=%s×tamp=%d&user=%s&123456", string(dataByte), seqValue, sec, user) | |
sign := fmt.Sprintf("%x", md5.Sum([]byte(req))) | |
params := fmt.Sprintf("seq=%s&user=%s&data=%s×tamp=%d&sign=%s", seqValue, user, string(dataByte), sec, sign) | |
// Create get request. | |
resp, err := http.Get(fmt.Sprintf("%s:%s/cgi-bin/smarthome/device.cgi?%s", baseURL, port, params)) | |
if err != nil { | |
fmt.Println("get err", err) | |
} | |
defer resp.Body.Close() | |
// Read response body. | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println("body err", err) | |
} | |
strBody := string(body) | |
// Restore body. | |
strBody = strings.ReplaceAll(strBody, "\n", "") | |
strBody = strings.ReplaceAll(strBody, "\t", "") | |
strBody = strings.TrimLeft(strBody, fmt.Sprintf("%s(", seqValue)) | |
strBody = strings.TrimRight(strBody, ")") | |
fmt.Println(strBody) | |
// Unmarshal response body. | |
jsonErr := json.Unmarshal([]byte(strBody), &operationResponse) | |
if jsonErr != nil { | |
fmt.Println("json err : ", jsonErr.Error()) | |
return jsonErr | |
} | |
// Check status. | |
if operationResponse.StatusCode == 1000001 { | |
return fmt.Errorf("add device failed") | |
} | |
if operationResponse.Result == "success" { | |
fmt.Println("woo hoo") | |
} | |
return nil | |
} | |
// GetDevices ... | |
func GetDevices() (*[]Device, error) { | |
var listOperationResponse ListOperationResponse | |
data := make(map[string]interface{}) | |
data["op"] = "list" | |
data["roomid"] = "-1" | |
data["pagenum"] = 1 | |
data["pagesize"] = 10 | |
dataByte, marshalErr := json.Marshal(data) | |
if marshalErr != nil { | |
fmt.Println("marsh err", marshalErr) | |
} | |
sec := time.Now().UnixNano() / int64(time.Millisecond) | |
req := fmt.Sprintf("data=%s&seq=%s×tamp=%d&user=%s&123456", string(dataByte), seqValue, sec, user) | |
sign := fmt.Sprintf("%x", md5.Sum([]byte(req))) | |
params := fmt.Sprintf("seq=%s&user=%s&data=%s×tamp=%d&sign=%s", seqValue, user, string(dataByte), sec, sign) | |
// Create get request. | |
resp, err := http.Get(fmt.Sprintf("%s:%s/cgi-bin/smarthome/device.cgi?%s", baseURL, port, params)) | |
if err != nil { | |
fmt.Println("get err", err) | |
} | |
defer resp.Body.Close() | |
// Read response body. | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println("body err", err) | |
} | |
strBody := string(body) | |
// Restore body. | |
strBody = strings.ReplaceAll(strBody, "\n", "") | |
strBody = strings.ReplaceAll(strBody, "\t", "") | |
strBody = strings.TrimLeft(strBody, fmt.Sprintf("%s(", seqValue)) | |
strBody = strings.TrimRight(strBody, ")") | |
//fmt.Println(strBody) | |
// Unmarshal response body. | |
jsonErr := json.Unmarshal([]byte(strBody), &listOperationResponse) | |
if jsonErr != nil { | |
fmt.Println("json err : ", jsonErr.Error()) | |
} | |
return &listOperationResponse.Result, nil | |
} | |
// DeleteDevice ... | |
func DeleteDevice(deviceID string) (string, error) { | |
var basicOperationResponse BasicOperationResponse | |
data := make(map[string]interface{}) | |
data["op"] = "deldev" | |
data["id"] = deviceID | |
dataByte, marshalErr := json.Marshal(data) | |
if marshalErr != nil { | |
fmt.Println("marsh err", marshalErr) | |
} | |
sec := time.Now().UnixNano() / int64(time.Millisecond) | |
req := fmt.Sprintf("data=%s&seq=%s×tamp=%d&user=%s&123456", string(dataByte), seqValue, sec, user) | |
sign := fmt.Sprintf("%x", md5.Sum([]byte(req))) | |
params := fmt.Sprintf("seq=%s&user=%s&data=%s×tamp=%d&sign=%s", seqValue, user, string(dataByte), sec, sign) | |
// Create get request. | |
resp, err := http.Get(fmt.Sprintf("%s:%s/cgi-bin/smarthome/device.cgi?%s", baseURL, port, params)) | |
if err != nil { | |
fmt.Println("get err", err) | |
} | |
defer resp.Body.Close() | |
// Read response body. | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println("body err", err) | |
} | |
strBody := string(body) | |
// Restore body. | |
strBody = strings.ReplaceAll(strBody, "\n", "") | |
strBody = strings.ReplaceAll(strBody, "\t", "") | |
strBody = strings.TrimLeft(strBody, fmt.Sprintf("%s(", seqValue)) | |
strBody = strings.TrimRight(strBody, ")") | |
fmt.Println("Delete body : ", strBody) | |
// Unmarshal response body. | |
jsonErr := json.Unmarshal([]byte(strBody), &basicOperationResponse) | |
if jsonErr != nil { | |
fmt.Println("json err : ", jsonErr.Error()) | |
} | |
return basicOperationResponse.Result, nil | |
} | |
// GetDevicesWithDetail ... | |
func GetDevicesWithDetail() (*[]Device, error) { | |
var listOperationResponse ListOperationResponse | |
// Get devices. | |
dvcs, getDevicesErr := GetDevices() | |
if getDevicesErr != nil { | |
return nil, getDevicesErr | |
} | |
deviceIDs := []string{} | |
for _, device := range *dvcs { | |
deviceIDs = append(deviceIDs, device.ID) | |
} | |
data := make(map[string]interface{}) | |
data["dev_ids"] = deviceIDs | |
data["op"] = "list_details" | |
data["roomid"] = "-1" | |
data["pagenum"] = 1 | |
data["pagesize"] = 10 | |
dataByte, marshalErr := json.Marshal(data) | |
if marshalErr != nil { | |
fmt.Println("marsh err", marshalErr) | |
} | |
sec := time.Now().UnixNano() / int64(time.Millisecond) | |
req := fmt.Sprintf("data=%s&seq=%s×tamp=%d&user=%s&123456", string(dataByte), seqValue, sec, user) | |
sign := fmt.Sprintf("%x", md5.Sum([]byte(req))) | |
params := fmt.Sprintf("seq=%s&user=%s&data=%s×tamp=%d&sign=%s", seqValue, user, string(dataByte), sec, sign) | |
// Create get request. | |
resp, err := http.Get(fmt.Sprintf("%s:%s/cgi-bin/smarthome/device.cgi?%s", baseURL, port, params)) | |
if err != nil { | |
fmt.Println("get err", err) | |
} | |
defer resp.Body.Close() | |
// Read response body. | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
fmt.Println("body err", err) | |
} | |
strBody := string(body) | |
// Restore body. | |
strBody = strings.ReplaceAll(strBody, "\n", "") | |
strBody = strings.ReplaceAll(strBody, "\t", "") | |
strBody = strings.TrimLeft(strBody, fmt.Sprintf("%s(", seqValue)) | |
strBody = strings.TrimRight(strBody, ")") | |
fmt.Println(strBody) | |
// Unmarshal response body. | |
jsonErr := json.Unmarshal([]byte(strBody), &listOperationResponse) | |
if jsonErr != nil { | |
fmt.Println("json err : ", jsonErr.Error()) | |
} | |
return &listOperationResponse.Result, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment